@@ -107,6 +107,7 @@ def test_project_not_found(self) -> None:
107107 args .verbose = True
108108 args .id = "34ef5c5452014c52aa9ce4bc180624d8"
109109 args .outputfile = self .OUTPUTFILE
110+ args .controlfile = None
110111
111112 self .add_login_response ()
112113
@@ -126,19 +127,12 @@ def test_project_not_found(self) -> None:
126127 except SystemExit as ex :
127128 self .assertEqual (ResultCode .RESULT_ERROR_ACCESSING_SW360 , ex .code )
128129
129- @responses .activate
130- def test_project_by_id (self ):
131- sut = CreateBom ()
132-
133- self .add_login_response ()
134- sut .login (token = TestBase .MYTOKEN , url = TestBase .MYURL )
135-
130+ def add_project_releases_responses (self ):
136131 # the project
137- project = self .get_project_for_test ()
138132 responses .add (
139133 responses .GET ,
140134 url = self .MYURL + "resource/api/projects/p001" ,
141- json = project ,
135+ json = self . get_project_for_test () ,
142136 status = 200 ,
143137 content_type = "application/json" ,
144138 adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
@@ -165,7 +159,7 @@ def test_project_by_id(self):
165159 "attachmentType" : "SOURCE_SELF" ,
166160 "_links" : {
167161 "self" : {
168- "href" : "https://my.server.com/resource/api/attachments/r002a002 "
162+ "href" : "https://my.server.com/resource/api/attachments/r002a003 "
169163 }
170164 }
171165 })
@@ -175,7 +169,7 @@ def test_project_by_id(self):
175169 "attachmentType" : "CLEARING_REPORT" ,
176170 "_links" : {
177171 "self" : {
178- "href" : "https://my.server.com/resource/api/attachments/r002a003 "
172+ "href" : "https://my.server.com/resource/api/attachments/r002a004 "
179173 }
180174 }
181175 })
@@ -188,8 +182,19 @@ def test_project_by_id(self):
188182 content_type = "application/json" ,
189183 adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
190184 )
185+ return release
191186
192- cdx_bom = sut .create_project_cdx_bom ("p001" )
187+ @responses .activate
188+ def test_project_by_id (self ):
189+ sut = CreateBom ()
190+
191+ self .add_login_response ()
192+ sut .login (token = TestBase .MYTOKEN , url = TestBase .MYURL )
193+
194+ release = self .add_project_releases_responses ()
195+ project = self .get_project_for_test ()
196+
197+ cdx_bom , _ = sut .create_project_cdx_bom ("p001" , create_controlfile = False )
193198 cx_comp = cdx_bom .components [0 ]
194199 self .assertEqual (cx_comp .purl , release ["externalIds" ]["package-url" ])
195200
@@ -210,15 +215,15 @@ def test_project_by_id(self):
210215 self .assertEqual (len (ext_refs ), 1 )
211216 self .assertEqual (ext_refs [0 ].url , release ["_embedded" ]["sw360:attachments" ][1 ]["filename" ])
212217 self .assertEqual (ext_refs [0 ].type , ExternalReferenceType .OTHER )
213- self .assertEqual (ext_refs [0 ].comment , CaPyCliBom .CLI_FILE_COMMENT + ", sw360Id: r002a002" )
218+ self .assertEqual (ext_refs [0 ].comment , CaPyCliBom .CLI_FILE_COMMENT )
214219 self .assertEqual (ext_refs [0 ].hashes [0 ].alg , "SHA-1" )
215220 self .assertEqual (ext_refs [0 ].hashes [0 ].content , release ["_embedded" ]["sw360:attachments" ][1 ]["sha1" ])
216221
217222 ext_refs = [e for e in cx_comp .external_references
218223 if e .comment and e .comment .startswith (CaPyCliBom .CRT_FILE_COMMENT )]
219224 self .assertEqual (len (ext_refs ), 1 )
220225 self .assertEqual (ext_refs [0 ].url , release ["_embedded" ]["sw360:attachments" ][3 ]["filename" ])
221- self .assertEqual (ext_refs [0 ].comment , CaPyCliBom .CRT_FILE_COMMENT + ", sw360Id: r002a003" )
226+ self .assertEqual (ext_refs [0 ].comment , CaPyCliBom .CRT_FILE_COMMENT )
222227 self .assertEqual (ext_refs [0 ].type , ExternalReferenceType .OTHER )
223228 self .assertEqual (ext_refs [0 ].hashes [0 ].alg , "SHA-1" )
224229 self .assertEqual (ext_refs [0 ].hashes [0 ].content , release ["_embedded" ]["sw360:attachments" ][3 ]["sha1" ])
@@ -231,6 +236,71 @@ def test_project_by_id(self):
231236 self .assertEqual (cdx_bom .metadata .component .version , project ["version" ])
232237 self .assertEqual (cdx_bom .metadata .component .description , project ["description" ])
233238
239+ @responses .activate
240+ def test_project_by_id_controlfile (self ):
241+ sut = CreateBom ()
242+ self .add_login_response ()
243+ sut .login (token = TestBase .MYTOKEN , url = TestBase .MYURL )
244+
245+ self .add_project_releases ()
246+
247+ # attachment info
248+ responses .add (
249+ method = responses .GET ,
250+ url = self .MYURL + "resource/api/attachments/r001a001" ,
251+ body = """
252+ {
253+ "filename": "CLIXML_wheel-0.38.4.xml",
254+ "sha1": "ccd9f1ed2f59c46ff3f0139c05bfd76f83fd9851",
255+ "attachmentType": "COMPONENT_LICENSE_INFO_XML"
256+ }""" ,
257+ status = 200 ,
258+ content_type = "application/json" ,
259+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
260+ )
261+ responses .add (
262+ method = responses .GET ,
263+ url = self .MYURL + "resource/api/attachments/r002a002" ,
264+ body = """
265+ {
266+ "filename": "CLIXML_clipython-1.3.0.xml",
267+ "sha1": "dd4c38387c6811dba67d837af7742d84e61e20de",
268+ "attachmentType": "COMPONENT_LICENSE_INFO_XML",
269+ "checkedBy": "user2@siemens.com",
270+ "checkStatus": "ACCEPTED",
271+ "createdBy": "user1@siemens.com"
272+ }""" ,
273+ status = 200 ,
274+ content_type = "application/json" ,
275+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
276+ )
277+ responses .add (
278+ method = responses .GET ,
279+ url = self .MYURL + "resource/api/attachments/r002a004" ,
280+ body = """
281+ {
282+ "filename": "clipython-1.3.0.docx",
283+ "sha1": "f0d8f2ddd017bdeaecbaec72ff76a6c0a045ec66",
284+ "attachmentType": "CLEARING_REPORT"
285+
286+ }""" ,
287+ status = 200 ,
288+ content_type = "application/json" ,
289+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
290+ )
291+
292+ _ , controlfile = sut .create_project_cdx_bom ("p001" , create_controlfile = True )
293+ self .assertEqual (controlfile ['ProjectName' ], 'CaPyCLI, 1.9.0' )
294+ self .assertEqual (controlfile ['Components' ][0 ]['ComponentName' ], 'cli-support 1.3' )
295+ self .assertEqual (controlfile ['Components' ][0 ]['Sw360Id' ], 'r002' )
296+ self .assertEqual (controlfile ['Components' ][0 ]['Sw360AttachmentId' ], 'r002a002' )
297+ self .assertEqual (controlfile ['Components' ][0 ]['CliFile' ], 'CLIXML_clipython-1.3.0.xml' )
298+ self .assertEqual (controlfile ['Components' ][0 ]['CheckedBy' ], 'user2@siemens.com' )
299+ self .assertEqual (controlfile ['Components' ][0 ]['CheckStatus' ], 'ACCEPTED' )
300+ self .assertEqual (controlfile ['Components' ][0 ]['CreatedBy' ], 'user1@siemens.com' )
301+
302+ self .assertEqual (controlfile ['Components' ][1 ]['ReportFile' ], 'clipython-1.3.0.docx' )
303+
234304 @responses .activate
235305 def test_project_show_by_name (self ):
236306 sut = CreateBom ()
@@ -246,6 +316,7 @@ def test_project_show_by_name(self):
246316 args .name = "CaPyCLI"
247317 args .version = "1.9.0"
248318 args .outputfile = self .OUTPUTFILE
319+ args .controlfile = None
249320
250321 self .add_login_response ()
251322
@@ -329,6 +400,65 @@ def test_project_show_by_name(self):
329400
330401 self .delete_file (self .OUTPUTFILE )
331402
403+ @responses .activate
404+ def test_create_project_bom_release_error (self ):
405+ sut = CreateBom ()
406+
407+ self .add_login_response ()
408+ sut .login (token = TestBase .MYTOKEN , url = TestBase .MYURL )
409+
410+ responses .add (
411+ responses .GET ,
412+ url = self .MYURL + "resource/api/releases/r001" ,
413+ status = 404 ,
414+ content_type = "application/json" ,
415+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
416+ )
417+ responses .add (
418+ responses .GET ,
419+ url = self .MYURL + "resource/api/releases/r002" ,
420+ json = self .get_release_cli_for_test (),
421+ status = 200 ,
422+ content_type = "application/json" ,
423+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
424+ )
425+ with self .assertRaises (SystemExit ):
426+ bom , _ = sut .create_project_bom (self .get_project_for_test (), create_controlfile = False )
427+
428+ @responses .activate
429+ def test_create_project_bom_controlfile_attachment_error (self ):
430+ sut = CreateBom ()
431+
432+ self .add_login_response ()
433+ sut .login (token = TestBase .MYTOKEN , url = TestBase .MYURL )
434+
435+ responses .add (
436+ responses .GET ,
437+ url = self .MYURL + "resource/api/releases/r001" ,
438+ json = self .get_release_wheel_for_test (),
439+ status = 200 ,
440+ content_type = "application/json" ,
441+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
442+ )
443+ responses .add (
444+ responses .GET ,
445+ url = self .MYURL + "resource/api/releases/r002" ,
446+ json = self .get_release_cli_for_test (),
447+ status = 200 ,
448+ content_type = "application/json" ,
449+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
450+ )
451+ responses .add (
452+ method = responses .GET ,
453+ url = self .MYURL + "resource/api/attachments/r002a002" ,
454+ status = 404 ,
455+ content_type = "application/json" ,
456+ adding_headers = {"Authorization" : "Token " + self .MYTOKEN },
457+ )
458+
459+ with self .assertRaises (SystemExit ):
460+ bom , _ = sut .create_project_bom (self .get_project_for_test (), create_controlfile = True )
461+
332462
333463if __name__ == "__main__" :
334464 APP = TestCreateBom ()
0 commit comments