@@ -896,6 +896,100 @@ void generateSbom_should_contain_metadata_component() throws IOException {
896896 Files .deleteIfExists (tmpFile );
897897 }
898898
899+ @ Test
900+ @ SetSystemProperty (key = "TRUSTIFY_DA_RECOMMEND" , value = "false" )
901+ @ SetSystemProperty (key = "TRUST_DA_TOKEN" , value = "trust-da-token" )
902+ @ SetSystemProperty (key = "TRUST_DA_SOURCE" , value = "trust-da-source" )
903+ void stackAnalysis_when_recommend_disabled_should_append_query_param ()
904+ throws IOException , ExecutionException , InterruptedException {
905+ var tmpFile = Files .createTempFile ("TRUSTIFY_DA_test_pom_" , ".xml" );
906+ try (var is =
907+ getResourceAsStreamDecision (this .getClass (), "tst_manifests/maven/empty/pom.xml" )) {
908+ Files .write (tmpFile , is .readAllBytes ());
909+ }
910+
911+ given (mockProvider .provideStack ())
912+ .willReturn (new Provider .Content ("fake-body-content" .getBytes (), "fake-content-type" ));
913+
914+ ArgumentMatcher <HttpRequest > matchesRequest =
915+ r ->
916+ r .uri ().toString ().contains ("?recommend=false" )
917+ && r .uri ().toString ().startsWith (exhortApiSut .getEndpoint () + "/api/v5/analysis" )
918+ && r .method ().equals ("POST" );
919+
920+ var mapper = new ObjectMapper ().disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
921+ AnalysisReport expectedAnalysis ;
922+ try (var is =
923+ getResourceAsStreamDecision (
924+ this .getClass (), "dummy_responses/maven/analysis-report.json" )) {
925+ expectedAnalysis = mapper .readValue (is , AnalysisReport .class );
926+ }
927+
928+ var mockHttpResponse = mock (HttpResponse .class );
929+ given (mockHttpResponse .body ()).willReturn (mapper .writeValueAsString (expectedAnalysis ));
930+ given (mockHttpResponse .statusCode ()).willReturn (200 );
931+
932+ try (var ecosystemTool = mockStatic (Ecosystem .class )) {
933+ ecosystemTool .when (() -> Ecosystem .getProvider (tmpFile )).thenReturn (mockProvider );
934+
935+ given (mockHttpClient .sendAsync (argThat (matchesRequest ), any ()))
936+ .willReturn (CompletableFuture .completedFuture (mockHttpResponse ));
937+
938+ var responseAnalysis = exhortApiSut .stackAnalysis (tmpFile .toString ());
939+ then (responseAnalysis .get ()).isEqualTo (expectedAnalysis );
940+ }
941+ Files .deleteIfExists (tmpFile );
942+ }
943+
944+ @ Test
945+ @ ClearSystemProperty (key = "TRUSTIFY_DA_RECOMMEND" )
946+ @ SetSystemProperty (key = "TRUST_DA_TOKEN" , value = "trust-da-token" )
947+ @ SetSystemProperty (key = "TRUST_DA_SOURCE" , value = "trust-da-source" )
948+ void stackAnalysis_when_recommend_default_should_not_append_query_param ()
949+ throws IOException , ExecutionException , InterruptedException {
950+ var tmpFile = Files .createTempFile ("TRUSTIFY_DA_test_pom_" , ".xml" );
951+ try (var is =
952+ getResourceAsStreamDecision (this .getClass (), "tst_manifests/maven/empty/pom.xml" )) {
953+ Files .write (tmpFile , is .readAllBytes ());
954+ }
955+
956+ given (mockProvider .provideStack ())
957+ .willReturn (new Provider .Content ("fake-body-content" .getBytes (), "fake-content-type" ));
958+
959+ ArgumentMatcher <HttpRequest > matchesRequest =
960+ r ->
961+ !r .uri ().toString ().contains ("recommend" )
962+ && r .uri ()
963+ .equals (
964+ URI .create (
965+ String .format (
966+ ExhortApi .S_API_V_5_ANALYSIS , exhortApiSut .getEndpoint ())))
967+ && r .method ().equals ("POST" );
968+
969+ var mapper = new ObjectMapper ().disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
970+ AnalysisReport expectedAnalysis ;
971+ try (var is =
972+ getResourceAsStreamDecision (
973+ this .getClass (), "dummy_responses/maven/analysis-report.json" )) {
974+ expectedAnalysis = mapper .readValue (is , AnalysisReport .class );
975+ }
976+
977+ var mockHttpResponse = mock (HttpResponse .class );
978+ given (mockHttpResponse .body ()).willReturn (mapper .writeValueAsString (expectedAnalysis ));
979+ given (mockHttpResponse .statusCode ()).willReturn (200 );
980+
981+ try (var ecosystemTool = mockStatic (Ecosystem .class )) {
982+ ecosystemTool .when (() -> Ecosystem .getProvider (tmpFile )).thenReturn (mockProvider );
983+
984+ given (mockHttpClient .sendAsync (argThat (matchesRequest ), any ()))
985+ .willReturn (CompletableFuture .completedFuture (mockHttpResponse ));
986+
987+ var responseAnalysis = exhortApiSut .stackAnalysis (tmpFile .toString ());
988+ then (responseAnalysis .get ()).isEqualTo (expectedAnalysis );
989+ }
990+ Files .deleteIfExists (tmpFile );
991+ }
992+
899993 @ Test
900994 void generateSbom_should_not_make_http_calls () throws IOException {
901995 var tmpFile = Files .createTempFile ("TRUSTIFY_DA_test_pom_" , ".xml" );
0 commit comments