Search in sources :

Example 1 with BlackDuckJsonTransformer

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

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

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

Example 4 with BlackDuckJsonTransformer

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

the class ProjectServiceTest method testGettingLatestProjectVersion.

@Disabled
@Test
public void testGettingLatestProjectVersion() throws IOException, IntegrationException {
    IntLogger logger = new BufferedIntLogger();
    Gson gson = BlackDuckServicesFactory.createDefaultGson();
    ObjectMapper objectMapper = BlackDuckServicesFactory.createDefaultObjectMapper();
    BlackDuckResponseResolver blackDuckResponseResolver = new BlackDuckResponseResolver(gson);
    BlackDuckJsonTransformer blackDuckJsonTransformer = new BlackDuckJsonTransformer(gson, objectMapper, blackDuckResponseResolver, logger);
    InputStream inputStream = getClass().getResourceAsStream("/json/pageOfProjectVersionViews.json");
    String pageJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
    BlackDuckPageResponse<ProjectVersionView> pageOfProjectVersionViews = blackDuckJsonTransformer.getResponses(pageJson, ProjectVersionView.class);
    List<ProjectVersionView> projectVersionViews = pageOfProjectVersionViews.getItems();
    BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    ProjectGetService projectGetService = Mockito.mock(ProjectGetService.class);
    ApiDiscovery apiDiscovery = new ApiDiscovery(new HttpUrl("https://synopsys.com"));
    ProjectService projectService = new ProjectService(blackDuckApiClient, apiDiscovery, logger, projectGetService);
    ProjectView projectView = new ProjectView();
    projectView.setName("unit test");
    Mockito.when(blackDuckApiClient.getAllResponses(Mockito.eq(projectView.metaVersionsLink()))).thenReturn(projectVersionViews);
    Optional<ProjectVersionView> projectVersionView = projectService.getNewestProjectVersion(projectView);
    assertTrue(projectVersionView.isPresent());
    assertEquals("dockertar", projectVersionView.get().getVersionName());
}
Also used : BlackDuckResponseResolver(com.synopsys.integration.blackduck.http.transform.subclass.BlackDuckResponseResolver) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) IntLogger(com.synopsys.integration.log.IntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) BufferedIntLogger(com.synopsys.integration.log.BufferedIntLogger) HttpUrl(com.synopsys.integration.rest.HttpUrl) BlackDuckJsonTransformer(com.synopsys.integration.blackduck.http.transform.BlackDuckJsonTransformer) ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) ProjectVersionView(com.synopsys.integration.blackduck.api.generated.view.ProjectVersionView) ProjectView(com.synopsys.integration.blackduck.api.generated.view.ProjectView) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

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