github.com/jenkins-x/jx/v2@v2.1.155/pkg/util/json/patch_test.go (about) 1 // +build unit 2 3 package json 4 5 import ( 6 "testing" 7 8 jenkinsv1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1" 9 "github.com/stretchr/testify/assert" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 ) 12 13 func TestCreatePatch(t *testing.T) { 14 t.Parallel() 15 orig, clone := setUp(t) 16 17 clone.Spec.Name = "foo" 18 patch, err := CreatePatch(orig, clone) 19 20 assert.NoError(t, err, "patch creation should be successful ") 21 assert.Equal(t, `[{"op":"add","path":"/spec/name","value":"foo"}]`, string(patch), 22 "the patch should have been empty") 23 } 24 25 func TestCreatePatchNil(t *testing.T) { 26 t.Parallel() 27 orig, clone := setUp(t) 28 29 _, err := CreatePatch(nil, clone) 30 assert.Error(t, err, "nil should not be allowed") 31 assert.Equal(t, "'before' cannot be nil when creating a JSON patch", err.Error(), "wrong error message") 32 33 _, err = CreatePatch(orig, nil) 34 assert.Error(t, err, "nil should not be allowed") 35 assert.Equal(t, "'after' cannot be nil when creating a JSON patch", err.Error(), "wrong error message") 36 } 37 38 func TestCreatePatchNoDiff(t *testing.T) { 39 t.Parallel() 40 orig, clone := setUp(t) 41 42 patch, err := CreatePatch(orig, clone) 43 44 assert.NoError(t, err, "patch creation should be successful ") 45 assert.Equal(t, "[]", string(patch), "the patch should have been empty") 46 } 47 48 func setUp(t *testing.T) (*jenkinsv1.Plugin, *jenkinsv1.Plugin) { 49 orig := &jenkinsv1.Plugin{ 50 ObjectMeta: metav1.ObjectMeta{ 51 Name: "test-plugin", 52 }, 53 Spec: jenkinsv1.PluginSpec{}, 54 } 55 56 return orig, orig.DeepCopy() 57 }