github.com/jenkins-x/jx-api@v0.0.24/pkg/util/json/patch_test.go (about) 1 package json_test 2 3 import ( 4 "testing" 5 6 "github.com/jenkins-x/jx-api/pkg/util/json" 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() 16 17 clone.Spec.Name = "foo" 18 patch, err := json.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() 28 29 _, err := json.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 = json.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() 41 42 patch, err := json.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() (*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 }