@@ -156,63 +156,73 @@ func assertEvidenceMaterial(t *testing.T, got *attestationApi.Attestation_Materi
156156}
157157
158158func TestEvidenceCraftWithJSONAnnotations (t * testing.T ) {
159- assert := assert .New (t )
160- schema := & contractAPI.CraftingSchema_Material {
161- Name : "test" ,
162- Type : contractAPI .CraftingSchema_Material_EVIDENCE ,
163- }
164- l := zerolog .Nop ()
165- backend := & casclient.CASBackend {}
166-
167- t .Run ("JSON with id, data and schema fields extracts annotations" , func (t * testing.T ) {
168- crafter , err := materials .NewEvidenceCrafter (schema , backend , & l )
169- require .NoError (t , err )
170-
171- got , err := crafter .Craft (context .TODO (), "./testdata/evidence-with-id-data-schema.json" )
172- assert .NoError (err )
173- assert .Equal (contractAPI .CraftingSchema_Material_EVIDENCE .String (), got .MaterialType .String ())
174-
175- // Check annotations were extracted
176- assert .NotNil (got .Annotations )
177- assert .Equal ("custom-evidence-123" , got .Annotations ["chainloop.evidence.id" ])
178- assert .Equal ("https://example.com/schema/v1" , got .Annotations ["chainloop.evidence.schema" ])
179- })
159+ schema := & contractAPI.CraftingSchema_Material {Name : "test" , Type : contractAPI .CraftingSchema_Material_EVIDENCE }
180160
181- t .Run ("JSON with id and data but no schema field extracts only id" , func (t * testing.T ) {
182- crafter , err := materials .NewEvidenceCrafter (schema , backend , & l )
183- require .NoError (t , err )
184-
185- got , err := crafter .Craft (context .TODO (), "./testdata/evidence-with-id-data-no-schema.json" )
186- assert .NoError (err )
187- assert .Equal (contractAPI .CraftingSchema_Material_EVIDENCE .String (), got .MaterialType .String ())
188-
189- // Check annotations were extracted
190- assert .NotNil (got .Annotations )
191- assert .Equal ("custom-evidence-456" , got .Annotations ["chainloop.evidence.id" ])
192- assert .NotContains (got .Annotations , "chainloop.evidence.schema" )
193- })
194-
195- t .Run ("JSON without required structure does not extract annotations" , func (t * testing.T ) {
196- crafter , err := materials .NewEvidenceCrafter (schema , backend , & l )
197- require .NoError (t , err )
198-
199- got , err := crafter .Craft (context .TODO (), "./testdata/evidence-invalid-structure.json" )
200- assert .NoError (err )
201- assert .Equal (contractAPI .CraftingSchema_Material_EVIDENCE .String (), got .MaterialType .String ())
202-
203- // Check no annotations were extracted
204- assert .Empty (got .Annotations )
205- })
206-
207- t .Run ("Non-JSON file does not extract annotations" , func (t * testing.T ) {
208- crafter , err := materials .NewEvidenceCrafter (schema , backend , & l )
209- require .NoError (t , err )
161+ l := zerolog .Nop ()
210162
211- got , err := crafter .Craft (context .TODO (), "./testdata/simple.txt" )
212- assert .NoError (err )
213- assert .Equal (contractAPI .CraftingSchema_Material_EVIDENCE .String (), got .MaterialType .String ())
163+ testCases := []struct {
164+ name string
165+ filePath string
166+ expectedAnnotations map [string ]string
167+ }{
168+ {
169+ name : "JSON with id, data and schema fields extracts annotations" ,
170+ filePath : "./testdata/evidence-with-id-data-schema.json" ,
171+ expectedAnnotations : map [string ]string {
172+ "chainloop.material.evidence.id" : "custom-evidence-123" ,
173+ "chainloop.material.evidence.schema" : "https://example.com/schema/v1" ,
174+ },
175+ },
176+ {
177+ name : "JSON with id and data but no schema field extracts only id" ,
178+ filePath : "./testdata/evidence-with-id-data-no-schema.json" ,
179+ expectedAnnotations : map [string ]string {
180+ "chainloop.material.evidence.id" : "custom-evidence-456" ,
181+ },
182+ },
183+ {
184+ name : "JSON without required structure does not extract annotations" ,
185+ filePath : "./testdata/evidence-invalid-structure.json" ,
186+ expectedAnnotations : nil ,
187+ },
188+ {
189+ name : "Non-JSON file does not extract annotations" ,
190+ filePath : "./testdata/simple.txt" ,
191+ expectedAnnotations : nil ,
192+ },
193+ }
214194
215- // Check no annotations were extracted (non-JSON)
216- assert .Empty (got .Annotations )
217- })
195+ for _ , tc := range testCases {
196+ t .Run (tc .name , func (t * testing.T ) {
197+ assert := assert .New (t )
198+
199+ // Create a new mock uploader for each test case
200+ uploader := mUploader .NewUploader (t )
201+ uploader .On ("UploadFile" , context .TODO (), tc .filePath ).
202+ Return (& casclient.UpDownStatus {
203+ Digest : "deadbeef" ,
204+ Filename : tc .filePath ,
205+ }, nil )
206+
207+ backend := & casclient.CASBackend {Uploader : uploader }
208+
209+ crafter , err := materials .NewEvidenceCrafter (schema , backend , & l )
210+ require .NoError (t , err )
211+
212+ got , err := crafter .Craft (context .TODO (), tc .filePath )
213+ assert .NoError (err )
214+ assert .Equal (contractAPI .CraftingSchema_Material_EVIDENCE .String (), got .MaterialType .String ())
215+
216+ if tc .expectedAnnotations == nil {
217+ assert .Empty (got .Annotations )
218+ } else {
219+ assert .NotNil (got .Annotations )
220+ for key , value := range tc .expectedAnnotations {
221+ assert .Equal (value , got .Annotations [key ])
222+ }
223+ // Ensure no extra keys are present beyond expected
224+ assert .Len (got .Annotations , len (tc .expectedAnnotations ))
225+ }
226+ })
227+ }
218228}
0 commit comments