Search in sources :

Example 1 with BufferedIntLogger

use of com.synopsys.integration.log.BufferedIntLogger in project blackduck-common by blackducksoftware.

the class BlackDuckApiClientTest method testGettingResponseWhenLinkNotPresent.

@Test
public void testGettingResponseWhenLinkNotPresent() throws IOException, IntegrationException {
    IntLogger logger = new BufferedIntLogger();
    BlackDuckHttpClient blackDuckHttpClient = Mockito.mock(BlackDuckHttpClient.class);
    Gson gson = BlackDuckServicesFactory.createDefaultGson();
    ObjectMapper objectMapper = BlackDuckServicesFactory.createDefaultObjectMapper();
    BlackDuckResponseResolver blackDuckResponseResolver = new BlackDuckResponseResolver(gson);
    BlackDuckJsonTransformer blackDuckJsonTransformer = new BlackDuckJsonTransformer(gson, objectMapper, blackDuckResponseResolver, logger);
    BlackDuckResponseTransformer blackDuckResponseTransformer = new BlackDuckResponseTransformer(blackDuckHttpClient, blackDuckJsonTransformer);
    BlackDuckResponsesTransformer blackDuckResponsesTransformer = new BlackDuckResponsesTransformer(blackDuckHttpClient, blackDuckJsonTransformer);
    InputStream inputStream = getClass().getResourceAsStream("/json/ProjectVersionView_not_complete.json");
    String incompleteJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    ProjectVersionView projectVersionViewWithMissingLink = blackDuckJsonTransformer.getResponseAs(incompleteJson, ProjectVersionView.class);
    BlackDuckApiClient blackDuckApiClient = new BlackDuckApiClient(blackDuckHttpClient, blackDuckJsonTransformer, blackDuckResponseTransformer, blackDuckResponsesTransformer);
    try {
        blackDuckApiClient.getResponse(projectVersionViewWithMissingLink.metaPolicyStatusLink());
        fail();
    } catch (NoSuchElementException e) {
        assertTrue(e.getMessage().contains(ProjectVersionView.POLICY_STATUS_LINK));
        assertTrue(e.getMessage().contains("not found"));
    }
}
Also used : BlackDuckResponseResolver(com.synopsys.integration.blackduck.http.transform.subclass.BlackDuckResponseResolver) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) IntLogger(com.synopsys.integration.log.IntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) BlackDuckJsonTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckJsonTransformer) BlackDuckResponseTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NoSuchElementException(java.util.NoSuchElementException) BlackDuckResponsesTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer) Test(org.junit.jupiter.api.Test)

Example 2 with BufferedIntLogger

use of com.synopsys.integration.log.BufferedIntLogger in project blackduck-common by blackducksoftware.

the class InstallAndRunSignatureScannerTestIT method testInstallingAndRunningSignatureScanner.

@Test
void testInstallingAndRunningSignatureScanner() throws IOException, InterruptedException, IntegrationException {
    // here, we do not want to automatically trust the server's certificate
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = intHttpClientTestHelper.getBlackDuckServerConfigBuilder();
    blackDuckServerConfigBuilder.setTrustCert(false);
    BlackDuckServerConfig blackDuckServerConfig = blackDuckServerConfigBuilder.build();
    File installDirectory = new File(scannerDirectoryPath, "scanner_install");
    File outputDirectory = new File(scannerDirectoryPath, "scanner_output");
    ScanBatch scanBatch = createScanBatch(blackDuckServerConfig, outputDirectory);
    BufferedIntLogger logger = new BufferedIntLogger();
    BlackDuckServicesFactory blackDuckServicesFactory = blackDuckServerConfig.createBlackDuckServicesFactory(logger);
    IntEnvironmentVariables environmentVariables = blackDuckServicesFactory.getEnvironmentVariables();
    OperatingSystemType operatingSystemType = OperatingSystemType.determineFromSystem();
    ExecutorService executorService = BlackDuckServicesFactory.NO_THREAD_EXECUTOR_SERVICE;
    BlackDuckHttpClient blackDuckHttpClient = blackDuckServicesFactory.getBlackDuckHttpClient();
    BlackDuckRegistrationService blackDuckRegistrationService = blackDuckServicesFactory.createBlackDuckRegistrationService();
    CleanupZipExpander cleanupZipExpander = new CleanupZipExpander(logger);
    HttpUrl blackDuckServerUrl = blackDuckHttpClient.getBlackDuckUrl();
    ScanPathsUtility scanPathsUtility = new ScanPathsUtility(logger, environmentVariables, operatingSystemType);
    ScanCommandRunner scanCommandRunner = new ScanCommandRunner(logger, environmentVariables, scanPathsUtility, executorService);
    // first, run a scan with an install that will NOT update the embedded keystore, which should fail
    KeyStoreHelper noOpKeyStoreHelper = new NoOpKeyStoreHelper();
    ScannerZipInstaller installerWithoutKeyStoreManagement = new ScannerZipInstaller(logger, new SignatureScannerClient(blackDuckHttpClient), blackDuckRegistrationService, cleanupZipExpander, scanPathsUtility, noOpKeyStoreHelper, blackDuckServerUrl, operatingSystemType, installDirectory);
    ScanBatchRunner scanBatchRunnerWithout = ScanBatchRunner.createComplete(environmentVariables, scanPathsUtility, scanCommandRunner, installerWithoutKeyStoreManagement);
    SignatureScannerService signatureScannerServiceWithout = blackDuckServicesFactory.createSignatureScannerService(scanBatchRunnerWithout);
    assertScanFailure(logger, blackDuckRegistrationService, signatureScannerServiceWithout, scanBatch);
    // now, delete the failed installation
    FileUtils.deleteDirectory(installDirectory);
    // second, run a scan with an install that DOES update the embedded keystore, which should succeed
    logger.resetAllLogs();
    KeyStoreHelper keyStoreHelper = new KeyStoreHelper(logger);
    ScannerZipInstaller installerWithKeyStoreManagement = new ScannerZipInstaller(logger, new SignatureScannerClient(blackDuckHttpClient), blackDuckRegistrationService, cleanupZipExpander, scanPathsUtility, keyStoreHelper, blackDuckServerUrl, operatingSystemType, installDirectory);
    ScanBatchRunner scanBatchRunnerWith = ScanBatchRunner.createComplete(environmentVariables, scanPathsUtility, scanCommandRunner, installerWithKeyStoreManagement);
    SignatureScannerService signatureScannerServiceWith = blackDuckServicesFactory.createSignatureScannerService(scanBatchRunnerWith);
    assertScanSuccess(logger, signatureScannerServiceWith, scanBatch);
    // finally, verify the code location exists and then delete it to clean up
    CodeLocationService codeLocationService = blackDuckServicesFactory.createCodeLocationService();
    BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
    ResilientJobConfig jobConfig = new ResilientJobConfig(logger, 120, System.currentTimeMillis(), 10);
    WaitJob.waitFor(jobConfig, () -> codeLocationService.getCodeLocationByName(CODE_LOCATION_NAME).isPresent(), "codeLocationTest");
    Optional<CodeLocationView> codeLocationViewOptional = codeLocationService.getCodeLocationByName(CODE_LOCATION_NAME);
    assertTrue(codeLocationViewOptional.isPresent());
    blackDuckApiClient.delete(codeLocationViewOptional.get());
}
Also used : CodeLocationService(com.synopsys.integration.blackduck.service.dataservice.CodeLocationService) BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) SignatureScannerService(com.synopsys.integration.blackduck.codelocation.signaturescanner.SignatureScannerService) KeyStoreHelper(com.synopsys.integration.blackduck.keystore.KeyStoreHelper) ScannerZipInstaller(com.synopsys.integration.blackduck.codelocation.signaturescanner.command.ScannerZipInstaller) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) CleanupZipExpander(com.synopsys.integration.util.CleanupZipExpander) CodeLocationView(com.synopsys.integration.blackduck.api.generated.view.CodeLocationView) IntEnvironmentVariables(com.synopsys.integration.util.IntEnvironmentVariables) ScanBatch(com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatch) ScanPathsUtility(com.synopsys.integration.blackduck.codelocation.signaturescanner.command.ScanPathsUtility) SignatureScannerClient(com.synopsys.integration.blackduck.http.client.SignatureScannerClient) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) ScanCommandRunner(com.synopsys.integration.blackduck.codelocation.signaturescanner.command.ScanCommandRunner) HttpUrl(com.synopsys.integration.rest.HttpUrl) BlackDuckServerConfig(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfig) BlackDuckRegistrationService(com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService) ScanBatchRunner(com.synopsys.integration.blackduck.codelocation.signaturescanner.ScanBatchRunner) OperatingSystemType(com.synopsys.integration.util.OperatingSystemType) ExecutorService(java.util.concurrent.ExecutorService) ResilientJobConfig(com.synopsys.integration.wait.ResilientJobConfig) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with BufferedIntLogger

use of com.synopsys.integration.log.BufferedIntLogger in project blackduck-common by blackducksoftware.

the class BinaryScanUploadServiceTestIT method testCodeLocationFromBinaryScanUploadWhenNotConfigured.

@Test
public void testCodeLocationFromBinaryScanUploadWhenNotConfigured() throws Exception {
    BlackDuckServices blackDuckServices = new BlackDuckServices(intHttpClientTestHelper);
    BinaryScanData binaryScanData = createBinaryScanData(blackDuckServices, createTestBinaryScan());
    BinaryScanUploadService binaryScanUploadService = blackDuckServices.blackDuckServicesFactory.createBinaryScanUploadService();
    BinaryScanBatchOutput binaryScanBatchOutput = binaryScanUploadService.uploadBinaryScanAndWait(binaryScanData.binaryScan, 15 * 60);
    BufferedIntLogger logger = new BufferedIntLogger();
    try {
        binaryScanBatchOutput.throwExceptionForError(logger);
    } catch (Exception e) {
        assertTrue(e instanceof BlackDuckIntegrationException);
        assertTrue(e.getMessage().startsWith("Error when uploading binary scan"));
        assertTrue(logger.getOutputString(LogLevel.ERROR).contains(e.getMessage()));
    }
}
Also used : BlackDuckServices(com.synopsys.integration.blackduck.comprehensive.BlackDuckServices) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) IntegrationException(com.synopsys.integration.exception.IntegrationException) BlackDuckIntegrationException(com.synopsys.integration.blackduck.exception.BlackDuckIntegrationException) Test(org.junit.jupiter.api.Test)

Example 4 with BufferedIntLogger

use of com.synopsys.integration.log.BufferedIntLogger in project blackduck-common by blackducksoftware.

the class CodeLocationWaitJobConditionTest method testMultipleNotificationsExpected.

@Test
public void testMultipleNotificationsExpected() throws ParseException, IntegrationException {
    BlackDuckApiClient mockBlackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    ProjectService mockProjectService = Mockito.mock(ProjectService.class);
    NotificationService mockNotificationService = Mockito.mock(NotificationService.class);
    UserView userView = new UserView();
    userView.setUserName("squiggles");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date alanTuringBirth = sdf.parse("1912-06-23");
    Date alanTuringUntimelyDeath = sdf.parse("1954-06-07");
    NotificationTaskRange notificationTaskRange = new NotificationTaskRange(System.currentTimeMillis(), alanTuringBirth, alanTuringUntimelyDeath);
    IntLogger logger = new BufferedIntLogger();
    NameVersion projectAndVersion = new NameVersion("BigSpoon", "LittleSpoon");
    String codeLocationName = "GraceIsButGloryBegunAndGloryIsButGracePerfected";
    Set<String> codeLocationNames = new HashSet<>(Arrays.asList(codeLocationName));
    CodeLocationWaitJobCondition codeLocationWaitJobCondition = new CodeLocationWaitJobCondition(logger, mockBlackDuckApiClient, mockProjectService, mockNotificationService, userView, notificationTaskRange, projectAndVersion, codeLocationNames, 2);
    ProjectView projectView = new ProjectView();
    ResourceLink resourceLink = new ResourceLink();
    resourceLink.setRel(ProjectVersionView.CODELOCATIONS_LINK);
    resourceLink.setHref(codeLocationsUrl);
    ResourceMetadata projectVersionViewMeta = new ResourceMetadata();
    projectVersionViewMeta.setLinks(Arrays.asList(resourceLink));
    ProjectVersionView projectVersionView = new ProjectVersionView();
    projectVersionView.setMeta(projectVersionViewMeta);
    ProjectVersionWrapper projectVersionWrapper = new ProjectVersionWrapper(projectView, projectVersionView);
    Mockito.when(mockProjectService.getProjectVersion(projectAndVersion)).thenReturn(Optional.of(projectVersionWrapper));
    ResourceMetadata resourceMetadata = new ResourceMetadata();
    resourceMetadata.setHref(codeLocationUrl);
    CodeLocationView foundCodeLocationView = new CodeLocationView();
    foundCodeLocationView.setName(codeLocationName);
    foundCodeLocationView.setMeta(resourceMetadata);
    Mockito.when(mockBlackDuckApiClient.getAllResponses(Mockito.eq(codeLocationResponses))).thenReturn(Arrays.asList(foundCodeLocationView));
    NotificationEditor notificationEditor = new NotificationEditor(notificationTaskRange.getStartDate(), notificationTaskRange.getEndDate(), Arrays.asList(NotificationType.VERSION_BOM_CODE_LOCATION_BOM_COMPUTED.name()));
    Mockito.when(mockNotificationService.getAllUserNotifications(userView, notificationEditor)).thenReturn(getExpectedNotifications());
    assertTrue(codeLocationWaitJobCondition.isComplete());
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) CodeLocationWaitJobCondition(com.synopsys.integration.blackduck.codelocation.CodeLocationWaitJobCondition) NotificationTaskRange(com.synopsys.integration.blackduck.service.model.NotificationTaskRange) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) ProjectService(com.synopsys.integration.blackduck.service.dataservice.ProjectService) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView) VersionBomCodeLocationBomComputedNotificationUserView(com.synopsys.integration.blackduck.api.manual.view.VersionBomCodeLocationBomComputedNotificationUserView) NotificationUserView(com.synopsys.integration.blackduck.api.manual.view.NotificationUserView) NotificationService(com.synopsys.integration.blackduck.service.dataservice.NotificationService) IntLogger(com.synopsys.integration.log.IntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) ResourceMetadata(com.synopsys.integration.blackduck.api.core.ResourceMetadata) Date(java.util.Date) CodeLocationView(com.synopsys.integration.blackduck.api.generated.view.CodeLocationView) ResourceLink(com.synopsys.integration.blackduck.api.core.ResourceLink) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) NotificationEditor(com.synopsys.integration.blackduck.service.request.NotificationEditor) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) SimpleDateFormat(java.text.SimpleDateFormat) ProjectVersionWrapper(com.synopsys.integration.blackduck.service.model.ProjectVersionWrapper) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with BufferedIntLogger

use of com.synopsys.integration.log.BufferedIntLogger in project blackduck-common by blackducksoftware.

the class BlackDuckRedirectStrategyTest method testSimpleRedirect.

@Test
public void testSimpleRedirect() throws IOException, IntegrationException, InterruptedException {
    // we need two servers - one to redirect to the other
    MockWebServer redirectingServer = new MockWebServer();
    MockWebServer destinationServer = new MockWebServer();
    String destinationUrl = startDestinationServer(destinationServer);
    String redirectingServerUrl = startRedirectingServer(redirectingServer, destinationUrl);
    IntLogger testLogger = new BufferedIntLogger();
    IntHttpClient redirectingClient = new IntHttpClient(testLogger, gson, 120, false, ProxyInfo.NO_PROXY_INFO) {

        @Override
        public void addToHttpClientBuilder(HttpClientBuilder httpClientBuilder, RequestConfig.Builder defaultRequestConfigBuilder) {
            super.addToHttpClientBuilder(httpClientBuilder, defaultRequestConfigBuilder);
            httpClientBuilder.setRedirectStrategy(new BlackDuckRedirectStrategy());
        }
    };
    Request requestToRedirectingServer = new Request.Builder(new HttpUrl(redirectingServerUrl)).method(HttpMethod.POST).bodyContent(new StringBodyContent("the initial request payload", BodyContentConverter.DEFAULT)).build();
    Response response = redirectingClient.execute(requestToRedirectingServer);
    assertEquals("the final response body", response.getContentString());
    RecordedRequest initialRequest = redirectingServer.takeRequest();
    RecordedRequest redirectedRequest = destinationServer.takeRequest();
    assertEquals(initialRequest.getPath(), redirectedRequest.getPath());
    assertEquals("/donkeynoodle", redirectedRequest.getPath());
    assertEquals(initialRequest.getBody(), redirectedRequest.getBody());
    assertEquals("the initial request payload", redirectedRequest.getBody().readString(StandardCharsets.UTF_8));
    shutdownServers(redirectingServer, destinationServer);
}
Also used : StringBodyContent(com.synopsys.integration.rest.body.StringBodyContent) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Request(com.synopsys.integration.rest.request.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IntLogger(com.synopsys.integration.log.IntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) Response(com.synopsys.integration.rest.response.Response) MockResponse(okhttp3.mockwebserver.MockResponse) IntHttpClient(com.synopsys.integration.rest.client.IntHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.junit.jupiter.api.Test)

Aggregations

BufferedIntLogger (com.synopsys.integration.log.BufferedIntLogger)26 Test (org.junit.jupiter.api.Test)20 IntLogger (com.synopsys.integration.log.IntLogger)8 File (java.io.File)7 UserView (com.synopsys.integration.blackduck.api.generated.view.UserView)6 NotificationTaskRange (com.synopsys.integration.blackduck.service.model.NotificationTaskRange)6 HttpUrl (com.synopsys.integration.rest.HttpUrl)6 CodeLocationView (com.synopsys.integration.blackduck.api.generated.view.CodeLocationView)5 NotificationUserView (com.synopsys.integration.blackduck.api.manual.view.NotificationUserView)5 VersionBomCodeLocationBomComputedNotificationUserView (com.synopsys.integration.blackduck.api.manual.view.VersionBomCodeLocationBomComputedNotificationUserView)5 BlackDuckHttpClient (com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient)5 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)5 NotificationService (com.synopsys.integration.blackduck.service.dataservice.NotificationService)5 HashSet (java.util.HashSet)5 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)4 BlackDuckServerConfigBuilder (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)4 BlackDuckResponseResolver (com.synopsys.integration.blackduck.http.transform.subclass.BlackDuckResponseResolver)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Gson (com.google.gson.Gson)3 UploadBatchRunner (com.synopsys.integration.blackduck.codelocation.bdiolegacy.UploadBatchRunner)3