11/**
2- * Copyright (c) 2022-2024 , Samsung Electronics Co., Ltd. All rights reserved.
2+ * Copyright (c) 2022-2025 , Samsung Electronics Co., Ltd. All rights reserved.
33 *
44 * Use of this source code is governed by a MIT license that can be
55 * found in the LICENSE file.
@@ -520,6 +520,10 @@ public void getLicenseBySpdxIdAndNameTest_connectToOSORI()
520520 osoriDbUrl .setAccessible (true );
521521 osoriDbUrl .set (licenseService , "http://127.0.0.1:8080" );
522522
523+ Field internalMode = licenseService .getClass ().getDeclaredField ("internalMode" );
524+ internalMode .setAccessible (true );
525+ internalMode .set (licenseService , false ); // external mode
526+
523527 HttpURLConnection mockConnection = Mockito .mock (HttpURLConnection .class );
524528 when (mockConnection .getResponseCode ()).thenReturn (200 );
525529
@@ -529,7 +533,7 @@ public void getLicenseBySpdxIdAndNameTest_connectToOSORI()
529533 osoriConnection .setAccessible (true );
530534 osoriConnection .set (licenseService , mockOsoriConnection );
531535
532- when (mockOsoriConnection .createConnection (anyString (), anyString ()))
536+ when (mockOsoriConnection .createConnection (anyString (), anyString (), anyBoolean () ))
533537 .thenReturn (mockConnection );
534538
535539 Path path =
@@ -551,6 +555,51 @@ public void getLicenseBySpdxIdAndNameTest_connectToOSORI()
551555 assertEquals ("UNREVIEWED" , result .getAccess ());
552556 }
553557
558+ @ Test
559+ public void getLicenseBySpdxIdAndNameTest_connectToOSORI_Internal ()
560+ throws NoSuchFieldException ,
561+ IllegalAccessException ,
562+ IOException ,
563+ URISyntaxException {
564+ Field osoriDbUrl = licenseService .getClass ().getDeclaredField ("osoriDbUrl" );
565+ osoriDbUrl .setAccessible (true );
566+ osoriDbUrl .set (licenseService , "http://127.0.0.1:8080" );
567+
568+ Field internalMode = licenseService .getClass ().getDeclaredField ("internalMode" );
569+ internalMode .setAccessible (true );
570+ internalMode .set (licenseService , true ); // internal mode
571+
572+ HttpURLConnection mockConnection = Mockito .mock (HttpURLConnection .class );
573+ when (mockConnection .getResponseCode ()).thenReturn (200 );
574+
575+ LPVSLicenseService .OsoriConnection mockOsoriConnection =
576+ Mockito .mock (LPVSLicenseService .OsoriConnection .class );
577+ Field osoriConnection = licenseService .getClass ().getDeclaredField ("osoriConnection" );
578+ osoriConnection .setAccessible (true );
579+ osoriConnection .set (licenseService , mockOsoriConnection );
580+
581+ when (mockOsoriConnection .createConnection (anyString (), anyString (), anyBoolean ()))
582+ .thenReturn (mockConnection );
583+
584+ Path path =
585+ Paths .get (
586+ Objects .requireNonNull (
587+ getClass ()
588+ .getClassLoader ()
589+ .getResource ("osori_db_response_internal.json" ))
590+ .toURI ());
591+
592+ when (mockConnection .getInputStream ()).thenReturn (Files .newInputStream (path ));
593+ when (licenseRepository .saveAndFlush (Mockito .any (LPVSLicense .class )))
594+ .thenAnswer (i -> i .getArguments ()[0 ]);
595+
596+ LPVSLicense result =
597+ licenseService .getLicenseBySpdxIdAndName ("Apache-2.0" , Optional .empty ());
598+ assertEquals ("Apache-2.0" , result .getSpdxId ());
599+ assertEquals ("Apache License 2.0" , result .getLicenseName ());
600+ assertEquals ("PERMITTED" , result .getAccess ());
601+ }
602+
554603 @ Test
555604 public void getLicenseBySpdxIdAndNameTest_createNewLicense ()
556605 throws NoSuchFieldException ,
@@ -570,7 +619,7 @@ public void getLicenseBySpdxIdAndNameTest_createNewLicense()
570619 osoriConnection .setAccessible (true );
571620 osoriConnection .set (licenseService , mockOsoriConnection );
572621
573- when (mockOsoriConnection .createConnection (anyString (), anyString ()))
622+ when (mockOsoriConnection .createConnection (anyString (), anyString (), anyBoolean () ))
574623 .thenReturn (mockConnection );
575624
576625 Path path =
@@ -624,7 +673,7 @@ public void getLicenseBySpdxIdAndNameTest_errorCodeInOSORI_N()
624673 osoriConnection .setAccessible (true );
625674 osoriConnection .set (licenseService , mockOsoriConnection );
626675
627- when (mockOsoriConnection .createConnection (anyString (), anyString ()))
676+ when (mockOsoriConnection .createConnection (anyString (), anyString (), anyBoolean () ))
628677 .thenReturn (mockConnection );
629678 when (licenseRepository .saveAndFlush (Mockito .any (LPVSLicense .class )))
630679 .thenAnswer (i -> i .getArguments ()[0 ]);
@@ -656,7 +705,7 @@ public void findLicenseInOsoriDBTest()
656705 osoriConnection .setAccessible (true );
657706 osoriConnection .set (licenseService , mockOsoriConnection );
658707
659- when (mockOsoriConnection .createConnection (anyString (), anyString ()))
708+ when (mockOsoriConnection .createConnection (anyString (), anyString (), anyBoolean () ))
660709 .thenReturn (mockConnection );
661710
662711 Path path =
@@ -678,11 +727,13 @@ public void findLicenseInOsoriDBTest()
678727 }
679728
680729 @ Test
681- public void findLicenseTest () {
682- LPVSLicense result = licenseService .findLicense ("GPL-3.0-only" , "GPL-3.0-only" );
683- assertEquals ("GPL-3.0-only" , result .getSpdxId ());
684- assertEquals ("GNU General Public License v3.0 only" , result .getLicenseName ());
685- assertEquals ("PROHIBITED" , result .getAccess ());
730+ public void testCreateConnectionThrowsIOException_N () {
731+ String licenseSpdxId = "Apache-2.0" ;
732+ LPVSLicenseService .OsoriConnection connection =
733+ new LPVSLicenseService .OsoriConnection ();
734+ assertThrows (
735+ IOException .class ,
736+ () -> connection .createConnection (null , licenseSpdxId , false ));
686737 }
687738 }
688739
@@ -696,18 +747,33 @@ public void testCreateConnection() throws IOException {
696747 LPVSLicenseService .OsoriConnection connection =
697748 new LPVSLicenseService .OsoriConnection ();
698749 HttpURLConnection httpURLConnection =
699- connection .createConnection (osoriDbUrl , licenseSpdxId );
750+ connection .createConnection (osoriDbUrl , licenseSpdxId , false ); // external mode
700751 assertEquals (
701752 osoriDbUrl + "/api/v1/user/licenses/spdx_identifier?searchWord=Apache-2.0" ,
702753 httpURLConnection .getURL ().toString ());
703754 }
704755
756+ @ Test
757+ public void testCreateConnectionInternal () throws IOException {
758+ String osoriDbUrl = "https://ossori.com" ;
759+ String licenseSpdxId = "Apache-2.0" ;
760+ LPVSLicenseService .OsoriConnection connection =
761+ new LPVSLicenseService .OsoriConnection ();
762+ HttpURLConnection httpURLConnection =
763+ connection .createConnection (osoriDbUrl , licenseSpdxId , true ); // internal mode
764+ assertEquals (
765+ osoriDbUrl + "/api/v1/licenses/Apache-2.0" ,
766+ httpURLConnection .getURL ().toString ());
767+ }
768+
705769 @ Test
706770 public void testCreateConnectionThrowsIOException_N () {
707771 String licenseSpdxId = "Apache-2.0" ;
708772 LPVSLicenseService .OsoriConnection connection =
709773 new LPVSLicenseService .OsoriConnection ();
710- assertThrows (IOException .class , () -> connection .createConnection (null , licenseSpdxId ));
774+ assertThrows (
775+ IOException .class ,
776+ () -> connection .createConnection (null , licenseSpdxId , false ));
711777 }
712778 }
713779}
0 commit comments