Search in sources :

Example 1 with BlackDuckResponseTransformer

use of com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer 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 BlackDuckResponseTransformer

use of com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer in project blackduck-common by blackducksoftware.

the class BlackDuckApiClientTestIT method testMediaType.

@Test
public void testMediaType() throws IntegrationException {
    IntLogger logger = new PrintStreamIntLogger(System.out, LogLevel.DEBUG);
    BlackDuckMediaTypeDiscoveryVerifier blackDuckMediaTypeDiscoveryVerifier = new BlackDuckMediaTypeDiscoveryVerifier();
    BlackDuckServerConfigBuilder blackDuckServerConfigBuilder = BlackDuckServerConfig.newCredentialsBuilder();
    blackDuckServerConfigBuilder.setUrl(testHelper.getProperty(TestingPropertyKey.TEST_BLACK_DUCK_SERVER_URL));
    blackDuckServerConfigBuilder.setUsername(testHelper.getProperty(TestingPropertyKey.TEST_USERNAME));
    blackDuckServerConfigBuilder.setPassword(testHelper.getProperty(TestingPropertyKey.TEST_PASSWORD));
    blackDuckServerConfigBuilder.setTrustCert(Boolean.parseBoolean(testHelper.getProperty(TestingPropertyKey.TEST_TRUST_HTTPS_CERT)));
    BlackDuckHttpClient blackDuckHttpClient = blackDuckServerConfigBuilder.build().createBlackDuckHttpClient(logger);
    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);
    BlackDuckApiClient blackDuckApiClient = new BlackDuckApiClient(blackDuckHttpClient, blackDuckJsonTransformer, blackDuckResponseTransformer, blackDuckResponsesTransformer);
    ApiDiscovery apiDiscovery = new ApiDiscovery(blackDuckServerConfigBuilder.build().getBlackDuckUrl());
    assertNull(blackDuckMediaTypeDiscoveryVerifier.originalMediaType);
    assertNull(blackDuckMediaTypeDiscoveryVerifier.discoveredMediaType);
    BlackDuckRequestBuilder blackDuckRequestBuilder = new BlackDuckRequestBuilder().commonGet();
    BlackDuckMultipleRequest<ProjectView> requestMultiple = new BlackDuckMultipleRequest<>(blackDuckRequestBuilder, apiDiscovery.metaProjectsLink(), new PagingDefaultsEditor(), new AcceptHeaderEditor(blackDuckMediaTypeDiscoveryVerifier));
    List<ProjectView> projects = blackDuckApiClient.getSomeResponses(requestMultiple, 5);
    assertTrue(projects.size() > 0);
    assertEquals(null, blackDuckMediaTypeDiscoveryVerifier.originalMediaType);
    assertEquals("application/json", blackDuckMediaTypeDiscoveryVerifier.discoveredMediaType);
    blackDuckRequestBuilder = new BlackDuckRequestBuilder().commonGet();
    ProjectView firstProject = projects.get(0);
    UrlSingleResponse<ProjectView> projectViewUrlSingleResponse = new UrlSingleResponse<>(firstProject.getHref(), ProjectView.class);
    BlackDuckSingleRequest<ProjectView> requestSingle = new BlackDuckSingleRequest<>(blackDuckRequestBuilder, projectViewUrlSingleResponse, new PagingDefaultsEditor(), new AcceptHeaderEditor(blackDuckMediaTypeDiscoveryVerifier));
    ProjectView retrievedById = blackDuckApiClient.getResponse(requestSingle);
    assertEquals(null, blackDuckMediaTypeDiscoveryVerifier.originalMediaType);
    assertEquals("application/vnd.blackducksoftware.project-detail-4+json", blackDuckMediaTypeDiscoveryVerifier.discoveredMediaType);
}
Also used : UrlSingleResponse(com.synopsys.integration.blackduck.api.core.response.UrlSingleResponse) BlackDuckSingleRequest(com.synopsys.integration.blackduck.service.request.BlackDuckSingleRequest) BlackDuckServerConfigBuilder(com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder) PrintStreamIntLogger(com.synopsys.integration.log.PrintStreamIntLogger) BlackDuckResponseResolver(com.synopsys.integration.blackduck.http.transform.subclass.BlackDuckResponseResolver) BlackDuckHttpClient(com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient) BlackDuckMultipleRequest(com.synopsys.integration.blackduck.service.request.BlackDuckMultipleRequest) AcceptHeaderEditor(com.synopsys.integration.blackduck.service.request.AcceptHeaderEditor) Gson(com.google.gson.Gson) IntLogger(com.synopsys.integration.log.IntLogger) PrintStreamIntLogger(com.synopsys.integration.log.PrintStreamIntLogger) BlackDuckJsonTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckJsonTransformer) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) BlackDuckResponseTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer) BlackDuckRequestBuilder(com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PagingDefaultsEditor(com.synopsys.integration.blackduck.service.request.PagingDefaultsEditor) BlackDuckResponsesTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer) Test(org.junit.jupiter.api.Test)

Example 3 with BlackDuckResponseTransformer

use of com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer in project blackduck-common by blackducksoftware.

the class BlackDuckApiClientTest method testGettingResponseWhenLinkPresent.

@Test
public void testGettingResponseWhenLinkPresent() 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_complete.json");
    String completeJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    ProjectVersionView projectVersionView = blackDuckJsonTransformer.getResponseAs(completeJson, ProjectVersionView.class);
    InputStream responseInputStream = getClass().getResourceAsStream("/json/VersionBomPolicyStatusView.json");
    String responseContentString = IOUtils.toString(responseInputStream, StandardCharsets.UTF_8);
    Response mockedResponse = Mockito.mock(Response.class);
    Mockito.when(mockedResponse.getContentString()).thenReturn(responseContentString);
    Mockito.when(blackDuckHttpClient.execute(Mockito.any(BlackDuckRequest.class))).thenReturn(mockedResponse);
    BlackDuckApiClient blackDuckApiClient = new BlackDuckApiClient(blackDuckHttpClient, blackDuckJsonTransformer, blackDuckResponseTransformer, blackDuckResponsesTransformer);
    ProjectVersionPolicyStatusView projectVersionPolicyStatusView = blackDuckApiClient.getResponse(projectVersionView.metaPolicyStatusLink());
    assertEquals(ProjectVersionComponentPolicyStatusType.IN_VIOLATION, projectVersionPolicyStatusView.getOverallStatus());
}
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) ProjectVersionPolicyStatusView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionPolicyStatusView) Response(com.synopsys.integration.rest.response.Response) 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) BlackDuckRequest(com.synopsys.integration.blackduck.service.request.BlackDuckRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BlackDuckResponsesTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Gson (com.google.gson.Gson)3 BlackDuckHttpClient (com.synopsys.integration.blackduck.http.client.BlackDuckHttpClient)3 BlackDuckJsonTransformer (com.synopsys.integration.blackduck.http.transform.BlackDuckJsonTransformer)3 BlackDuckResponseTransformer (com.synopsys.integration.blackduck.http.transform.BlackDuckResponseTransformer)3 BlackDuckResponsesTransformer (com.synopsys.integration.blackduck.http.transform.BlackDuckResponsesTransformer)3 BlackDuckResponseResolver (com.synopsys.integration.blackduck.http.transform.subclass.BlackDuckResponseResolver)3 IntLogger (com.synopsys.integration.log.IntLogger)3 Test (org.junit.jupiter.api.Test)3 ProjectVersionView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView)2 BufferedIntLogger (com.synopsys.integration.log.BufferedIntLogger)2 InputStream (java.io.InputStream)2 UrlSingleResponse (com.synopsys.integration.blackduck.api.core.response.UrlSingleResponse)1 ApiDiscovery (com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery)1 ProjectVersionPolicyStatusView (com.synopsys.integration.blackduck.api.generated.view.ProjectVersionPolicyStatusView)1 ProjectView (com.synopsys.integration.blackduck.api.generated.view.ProjectView)1 BlackDuckServerConfigBuilder (com.synopsys.integration.blackduck.configuration.BlackDuckServerConfigBuilder)1 BlackDuckRequestBuilder (com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder)1 AcceptHeaderEditor (com.synopsys.integration.blackduck.service.request.AcceptHeaderEditor)1 BlackDuckMultipleRequest (com.synopsys.integration.blackduck.service.request.BlackDuckMultipleRequest)1