@@ -22,6 +22,7 @@ import (
22
22
"encoding/json"
23
23
"fmt"
24
24
"os"
25
+ "strings"
25
26
"testing"
26
27
27
28
"github.com/cdevents/sdk-go/pkg/api"
43
44
testSubjectId = "mySubject123"
44
45
testValue = "testValue"
45
46
testArtifactId = "pkg:oci/myapp@sha256%3A0b31b1c02ff458ad9b7b81cbdf8f028bd54699fa151f221d1e8de6817db93427"
47
+ testInvalidArtifactId = "not-in-purl-format"
46
48
testDataJson = testData {TestValues : []map [string ]string {{"k1" : "v1" }, {"k2" : "v2" }}}
47
49
testDataJsonUnmarshalled = map [string ]any {
48
50
"testValues" : []any {map [string ]any {"k1" : string ("v1" )}, map [string ]any {"k2" : string ("v2" )}},
@@ -61,14 +63,39 @@ var (
61
63
"additionalProperties": true,
62
64
"type": "object"
63
65
}`
64
- testCustomSchemaJson = fmt .Sprintf (testCustomSchemaJsonTemplate , testSchemaUri )
65
- testCustomSchemas = map [string ][]byte {
66
- testSchemaUri : []byte (testCustomSchemaJson ),
66
+ testCustomSchemaJson = fmt .Sprintf (testCustomSchemaJsonTemplate , testSchemaUri )
67
+ testSchemaUriStricter = "https://myorg.com/schema/stricter"
68
+ testCustomSchemaJsonStricterTemplate = `{
69
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
70
+ "$id": "%s",
71
+ "additionalProperties": true,
72
+ "type": "object",
73
+ "properties": {
74
+ "customData": {
75
+ "type": "object",
76
+ "additionalProperties": false,
77
+ "properties": {
78
+ "important": {
79
+ "type": "string"
80
+ }
81
+ },
82
+ "required": [
83
+ "important"
84
+ ]
85
+ }
86
+ }
87
+ }`
88
+ testCustomSchemaJsonStricterJson = fmt .Sprintf (testCustomSchemaJsonStricterTemplate , testSchemaUriStricter )
89
+ testCustomSchemas = map [string ][]byte {
90
+ testSchemaUri : []byte (testCustomSchemaJson ),
91
+ testSchemaUriStricter : []byte (testCustomSchemaJsonStricterJson ),
67
92
}
68
93
69
94
eventJsonCustomData * testapi.FooSubjectBarPredicateEvent
70
95
eventNonJsonCustomData * testapi.FooSubjectBarPredicateEvent
71
96
eventJsonCustomDataUnmarshalled * testapi.FooSubjectBarPredicateEvent
97
+ eventJsonCustomDataCustomSchema * testapi.FooSubjectBarPredicateEvent
98
+ eventInvalidArtifactIdFormat * testapi.FooSubjectBarPredicateEvent
72
99
73
100
eventJsonCustomDataFile = "json_custom_data"
74
101
eventImplicitJsonCustomDataFile = "implicit_json_custom_data"
@@ -80,6 +107,21 @@ var (
80
107
Type : api.CDEventType {
81
108
Subject : "invalid" ,
82
109
Predicate : "invalid" ,
110
+ Version : "#not@semver" , // Invalid version format
111
+ },
112
+ Version : "9.9.9" ,
113
+ },
114
+ api.ContextLinks {},
115
+ api.ContextCustom {},
116
+ },
117
+ }
118
+
119
+ eventUnknownType = & testapi.FooSubjectBarPredicateEvent {
120
+ Context : api.ContextV04 {
121
+ api.Context {
122
+ Type : api.CDEventType {
123
+ Subject : "invalid" , // Unknown subject
124
+ Predicate : "invalid" , // Unknown predicate
83
125
Version : "1.2.3" ,
84
126
},
85
127
Version : "9.9.9" ,
@@ -134,6 +176,19 @@ func init() {
134
176
elr , elp , ele ,
135
177
}
136
178
179
+ setContext (eventInvalidType , testSubjectId )
180
+ setContextV04 (eventInvalidType , true , true )
181
+ eventInvalidType .SetSubjectArtifactId (testArtifactId )
182
+
183
+ setContext (eventUnknownType , testSubjectId )
184
+ setContextV04 (eventUnknownType , true , true )
185
+ eventUnknownType .SetSubjectArtifactId (testArtifactId )
186
+
187
+ eventInvalidArtifactIdFormat , _ = testapi .NewFooSubjectBarPredicateEvent ()
188
+ setContext (eventInvalidArtifactIdFormat , testSubjectId )
189
+ setContextV04 (eventInvalidArtifactIdFormat , true , true )
190
+ eventInvalidArtifactIdFormat .SetSubjectArtifactId (testInvalidArtifactId )
191
+
137
192
eventJsonCustomData , _ = testapi .NewFooSubjectBarPredicateEvent ()
138
193
setContext (eventJsonCustomData , testSubjectId )
139
194
setContextV04 (eventJsonCustomData , true , true )
@@ -164,6 +219,17 @@ func init() {
164
219
err = eventNonJsonCustomData .SetCustomData ("application/xml" , testDataXml )
165
220
panicOnError (err )
166
221
222
+ eventJsonCustomDataCustomSchema , _ = testapi .NewFooSubjectBarPredicateEvent ()
223
+ setContext (eventJsonCustomDataCustomSchema , testSubjectId )
224
+ setContextV04 (eventJsonCustomDataCustomSchema , true , true )
225
+ eventJsonCustomDataCustomSchema .SetSchemaUri (testSchemaUriStricter )
226
+ eventJsonCustomDataCustomSchema .SetSubjectReferenceField (& api.Reference {Id : testChangeId })
227
+ eventJsonCustomDataCustomSchema .SetSubjectPlainField (testValue )
228
+ eventJsonCustomDataCustomSchema .SetSubjectArtifactId (testArtifactId )
229
+ eventJsonCustomDataCustomSchema .SetSubjectObjectField (& testapi.FooSubjectBarPredicateSubjectContentObjectField {Required : testChangeId , Optional : testSource })
230
+ err = eventJsonCustomDataCustomSchema .SetCustomData ("application/json" , testDataJson )
231
+ panicOnError (err )
232
+
167
233
for id , jsonBytes := range testCustomSchemas {
168
234
err = api .LoadJsonSchema (id , jsonBytes )
169
235
panicOnError (err )
@@ -247,19 +313,37 @@ func TestAsCloudEventInvalid(t *testing.T) {
247
313
tests := []struct {
248
314
name string
249
315
event api.CDEventReader
316
+ error string
250
317
}{{
251
318
name : "nil event" ,
252
319
event : nil ,
320
+ error : "nil CDEvent cannot be rendered as CloudEvent" ,
253
321
}, {
254
322
name : "event with invalid type" ,
255
323
event : eventInvalidType ,
324
+ error : "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Context.Context.Type." ,
325
+ }, {
326
+ name : "event with unknown type" ,
327
+ event : eventUnknownType ,
328
+ error : "cannot validate CDEvent jsonschema validation failed with 'https://cdevents.dev/99.1.0/schema/foosubject-barpredicate-event#'" ,
329
+ }, {
330
+ name : "event with invalid artifact id format" ,
331
+ event : eventInvalidArtifactIdFormat ,
332
+ error : "cannot validate CDEvent Key: 'FooSubjectBarPredicateEventV2_2_3.Subject.Content.ArtifactId'" ,
333
+ }, {
334
+ name : "does not match the custom schema" ,
335
+ event : eventJsonCustomDataCustomSchema ,
336
+ error : "cannot validate CDEvent jsonschema validation failed with 'https://myorg.com/schema/stricter#" ,
256
337
}}
257
338
for _ , tc := range tests {
258
339
t .Run (tc .name , func (t * testing.T ) {
259
340
_ , err := api .AsCloudEvent (tc .event )
260
341
if err == nil {
261
342
t .Fatalf ("expected it to fail, but it didn't" )
262
343
}
344
+ if ! strings .HasPrefix (err .Error (), tc .error ) {
345
+ t .Errorf ("error %s does not start with the expected prefix %s" , err .Error (), tc .error )
346
+ }
263
347
})
264
348
}
265
349
}
0 commit comments