DownloaderWebClient extends DataBridgeWebClient { public StorageUnitDownloadCredential getStorageUnitDownloadCredential(DownloaderInputManifestDto manifest, String storageName) throws URISyntaxException, IOException, JAXBException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { URIBuilder uriBuilder = new URIBuilder().setScheme(getUriScheme()).setHost(regServerAccessParamsDto.getRegServerHost()).setPort(regServerAccessParamsDto.getRegServerPort()) .setPath(String.join("/", HERD_APP_REST_URI_PREFIX, "storageUnits", "download", "credential", "namespaces", manifest.getNamespace(), "businessObjectDefinitionNames", manifest.getBusinessObjectDefinitionName(), "businessObjectFormatUsages", manifest.getBusinessObjectFormatUsage(), "businessObjectFormatFileTypes", manifest.getBusinessObjectFormatFileType(), "businessObjectFormatVersions", manifest.getBusinessObjectFormatVersion(), "partitionValues", manifest.getPartitionValue(), "businessObjectDataVersions", manifest.getBusinessObjectDataVersion(), "storageNames", storageName)); if (manifest.getSubPartitionValues() != null) { uriBuilder.addParameter("subPartitionValues", herdStringHelper.join(manifest.getSubPartitionValues(), "|", "\\")); } HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.addHeader("Accept", DEFAULT_ACCEPT); if (regServerAccessParamsDto.isUseSsl()) { httpGet.addHeader(getAuthorizationHeader()); } try (CloseableHttpClient httpClient = httpClientHelper .createHttpClient(regServerAccessParamsDto.isTrustSelfSignedCertificate(), regServerAccessParamsDto.isDisableHostnameVerification())) { LOGGER.info("Retrieving download credentials from registration server..."); return getStorageUnitDownloadCredential(httpClientOperations.execute(httpClient, httpGet)); } }  BusinessObjectData getBusinessObjectData(DownloaderInputManifestDto manifest); S3KeyPrefixInformation getS3KeyPrefix(BusinessObjectData businessObjectData); StorageUnitDownloadCredential getStorageUnitDownloadCredential(DownloaderInputManifestDto manifest, String storageName);  }
@Test public void testGetBusinessObjectDataDownloadCredential1() throws Exception { DownloaderInputManifestDto manifest = new DownloaderInputManifestDto(); manifest.setNamespace("test1"); manifest.setBusinessObjectDefinitionName("test2"); manifest.setBusinessObjectFormatUsage("test3"); manifest.setBusinessObjectFormatFileType("test4"); manifest.setBusinessObjectFormatVersion("test5"); manifest.setPartitionValue("test6"); manifest.setSubPartitionValues(Arrays.asList("test7", "test8")); manifest.setBusinessObjectDataVersion("test9"); String storageName = "test10"; StorageUnitDownloadCredential storageUnitDownloadCredential = downloaderWebClient.getStorageUnitDownloadCredential(manifest, storageName); Assert.assertNotNull(storageUnitDownloadCredential); AwsCredential awsCredential = storageUnitDownloadCredential.getAwsCredential(); Assert.assertNotNull(awsCredential); Assert.assertEquals("https: "/businessObjectDefinitionNames/test2/businessObjectFormatUsages/test3/businessObjectFormatFileTypes/test4/businessObjectFormatVersions/test5" + "/partitionValues/test6/businessObjectDataVersions/test9/storageNames/test10?subPartitionValues=test7%7Ctest8", awsCredential.getAwsAccessKey()); }
@Test public void testGetBusinessObjectDataDownloadCredential2() throws Exception { DownloaderInputManifestDto manifest = new DownloaderInputManifestDto(); manifest.setNamespace("test1"); manifest.setBusinessObjectDefinitionName("test2"); manifest.setBusinessObjectFormatUsage("test3"); manifest.setBusinessObjectFormatFileType("test4"); manifest.setBusinessObjectFormatVersion("test5"); manifest.setPartitionValue("test6"); manifest.setBusinessObjectDataVersion("test9"); String storageName = "test10"; downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(true); StorageUnitDownloadCredential storageUnitDownloadCredential = downloaderWebClient.getStorageUnitDownloadCredential(manifest, storageName); Assert.assertNotNull(storageUnitDownloadCredential); AwsCredential awsCredential = storageUnitDownloadCredential.getAwsCredential(); Assert.assertNotNull(awsCredential); Assert.assertEquals("https: "/businessObjectDefinitionNames/test2/businessObjectFormatUsages/test3/businessObjectFormatFileTypes/test4/businessObjectFormatVersions/test5" + "/partitionValues/test6/businessObjectDataVersions/test9/storageNames/test10", awsCredential.getAwsAccessKey()); }
@Test public void testGetBusinessObjectDataDownloadCredentialAssertHttpClientClosedWhenIOException() throws Exception { HttpClientHelper mockHttpClientHelper = mock(HttpClientHelper.class); HttpClientHelper originalHttpClientHelper = (HttpClientHelper) ReflectionTestUtils.getField(downloaderWebClient, "httpClientHelper"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientHelper", mockHttpClientHelper); HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { IOException expectedException = new IOException(); CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); when(mockHttpClientHelper.createHttpClient(any(), any())).thenReturn(closeableHttpClient); when(mockHttpClientOperations.execute(any(), any())).thenThrow(expectedException); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; try { downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(closeableHttpClient).close(); fail(); } catch (Exception e) { assertEquals(expectedException, e); } } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientHelper", originalHttpClientHelper); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }
@Test public void testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl() throws Exception { HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS")); when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential()))); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; boolean useSsl = false; downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(useSsl); downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null)); } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }
@Test public void testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException() throws Exception { HttpClientHelper mockHttpClientHelper = mock(HttpClientHelper.class); HttpClientHelper originalHttpClientHelper = (HttpClientHelper) ReflectionTestUtils.getField(downloaderWebClient, "httpClientHelper"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientHelper", mockHttpClientHelper); HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class); HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations"); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations); try { CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class); when(mockHttpClientHelper.createHttpClient(any(), any())).thenReturn(closeableHttpClient); doThrow(IOException.class).when(closeableHttpClient).close(); CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class); when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse); when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS")); when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential()))); DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto(); String storageName = "storageName"; try { downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName); verify(closeableHttpClient).close(); fail(); } catch (Exception e) { assertEquals(IOException.class, e.getClass()); } } finally { ReflectionTestUtils.setField(downloaderWebClient, "httpClientHelper", originalHttpClientHelper); ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations); } }