use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassRepositoryDownloaderTest method GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed.
@Test
void GIVEN_artifact_url_WHEN_attempt_download_THEN_task_succeed() throws Exception {
// build downloader
Path mockArtifactPath = ComponentTestResourceHelper.getPathForTestPackage(ComponentTestResourceHelper.MONITORING_SERVICE_PACKAGE_NAME, "1.0.0").resolve("monitor_artifact_100.txt");
String checksum = Base64.getEncoder().encodeToString(MessageDigest.getInstance(SHA256).digest(Files.readAllBytes(mockArtifactPath)));
ComponentArtifact artifact = ComponentArtifact.builder().algorithm(SHA256).checksum(checksum).artifactUri(new URI("greengrass:774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt")).build();
ComponentIdentifier pkgId = new ComponentIdentifier("CoolService", new Semver("1.0.0"));
lenient().when(componentStore.getRecipeMetadata(pkgId)).thenReturn(new RecipeMetadata(TEST_ARN));
Path testCache = ComponentTestResourceHelper.getPathForLocalTestCache();
Path saveToPath = testCache.resolve("CoolService").resolve("1.0.0");
Files.createDirectories(saveToPath);
GreengrassRepositoryDownloader downloader = spy(new GreengrassRepositoryDownloader(clientFactory, pkgId, artifact, saveToPath, componentStore));
assertThat(downloader.getArtifactFilename(), is("artifact.txt"));
// mock requests to get downloadSize and local file name
GetComponentVersionArtifactResponse result = GetComponentVersionArtifactResponse.builder().preSignedUrl("https://www.amazon.com/artifact.txt").build();
when(client.getComponentVersionArtifact(getComponentVersionArtifactRequestArgumentCaptor.capture())).thenReturn(result);
// mock requests to return partial stream
doReturn(httpClient).when(downloader).getSdkHttpClient();
doReturn(request).when(httpClient).prepareRequest(any());
when(request.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_OK).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(HTTP_PARTIAL).putHeader(CONTENT_LENGTH_HEADER, String.valueOf(Files.size(mockArtifactPath))).build()).responseBody(AbortableInputStream.create(Files.newInputStream(mockArtifactPath))).build());
downloader.download();
GetComponentVersionArtifactRequest generatedRequest = getComponentVersionArtifactRequestArgumentCaptor.getValue();
assertEquals(TEST_ARN, generatedRequest.arn());
assertEquals("774pP05xtua0RCcwj9uALSdAqGr_vC631EdOBkJxnec=/artifact.txt", generatedRequest.artifactName());
byte[] originalFile = Files.readAllBytes(mockArtifactPath);
Path artifactFilePath = saveToPath.resolve("artifact.txt");
byte[] downloadFile = Files.readAllBytes(artifactFilePath);
assertThat(Arrays.equals(originalFile, downloadFile), is(true));
ComponentTestResourceHelper.cleanDirectory(testCache);
}
use of software.amazon.awssdk.services.greengrassv2data.model.GetComponentVersionArtifactRequest in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassRepositoryDownloader method getArtifactDownloadURL.
@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.AvoidRethrowingException" })
private String getArtifactDownloadURL(ComponentIdentifier componentIdentifier, String artifactName) throws InterruptedException, PackageDownloadException {
String arn;
try {
arn = componentStore.getRecipeMetadata(componentIdentifier).getComponentVersionArn();
} catch (PackageLoadingException e) {
throw new PackageDownloadException(getErrorString("Failed to get component version arn from component store"), e);
}
try {
return RetryUtils.runWithRetry(clientExceptionRetryConfig, () -> {
GetComponentVersionArtifactRequest getComponentArtifactRequest = GetComponentVersionArtifactRequest.builder().artifactName(artifactName).arn(arn).build();
GetComponentVersionArtifactResponse getComponentArtifactResult = clientFactory.getGreengrassV2DataClient().getComponentVersionArtifact(getComponentArtifactRequest);
return getComponentArtifactResult.preSignedUrl();
}, "get-artifact-size", logger);
} catch (InterruptedException e) {
throw e;
} catch (Exception e) {
throw new PackageDownloadException(getErrorString("Failed to get download size"), e);
}
}
Aggregations