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"));
}
}
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);
}
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());
}
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());
}
Aggregations