github.com/jaylevin/jenkins-library@v1.230.4/cmd/jsonApplyPatch_test.go (about) 1 package cmd 2 3 import ( 4 "github.com/SAP/jenkins-library/pkg/mock" 5 "github.com/stretchr/testify/assert" 6 "testing" 7 ) 8 9 var schema = []byte(` 10 { 11 "$schema": "http://json-schema.org/draft-07/schema#", 12 "title": "SAP Cloud SDK pipeline_config JSON schema", 13 "type": "object", 14 "properties": { 15 "general": { 16 "type": [ 17 "object", 18 "null" 19 ], 20 "properties": { 21 "productiveBranch": { 22 "type": "string", 23 "default": "master" 24 } 25 } 26 } 27 } 28 } 29 `) 30 31 var patch = []byte(` 32 [ 33 { 34 "op": "add", 35 "path": "/properties/general/properties/gitCredentialsId", 36 "value": { 37 "type": "string" 38 } 39 } 40 ] 41 `) 42 43 var patchedSchema = []byte(`{ 44 "$schema": "http://json-schema.org/draft-07/schema#", 45 "title": "SAP Cloud SDK pipeline_config JSON schema", 46 "type": "object", 47 "properties": { 48 "general": { 49 "type": [ 50 "object", 51 "null" 52 ], 53 "properties": { 54 "productiveBranch": { 55 "type": "string", 56 "default": "master" 57 }, 58 "gitCredentialsId": { 59 "type": "string" 60 } 61 } 62 } 63 } 64 }`) 65 66 func TestSchemaPatch(t *testing.T) { 67 t.Run("default", func(t *testing.T) { 68 options := jsonApplyPatchOptions{ 69 Input: "schema.json", 70 Patch: "patch.json", 71 Output: "output.json", 72 } 73 filesMock := mock.FilesMock{} 74 filesMock.AddFile("schema.json", schema) 75 filesMock.AddFile("patch.json", patch) 76 err := runJsonApplyPatch(&options, &filesMock) 77 assert.NoError(t, err) 78 patchedSchemaResult, err := filesMock.FileRead("output.json") 79 assert.NoError(t, err) 80 assert.JSONEq(t, string(patchedSchema), string(patchedSchemaResult)) 81 }) 82 83 t.Run("file does not exist", func(t *testing.T) { 84 options := jsonApplyPatchOptions{ 85 Input: "schema.json", 86 Patch: "patch.json", 87 Output: "output.json", 88 } 89 filesMock := mock.FilesMock{} 90 err := runJsonApplyPatch(&options, &filesMock) 91 assert.Error(t, err) 92 }) 93 }