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