Untitled
user_4688862
plain_text
7 months ago
2.7 kB
6
Indexable
@Override public byte[] downloadFile(String bucket, String fileName) throws FileDownloadException, IOException { JsonNode keys = getSecrets(bucket); String bucketId = keys.get("bucket").asText(); final AmazonS3 s3Client = awsS3ClientBuilderService.createAwsS3Client(keys.get("api_key").asText(), keys.get("secret_key").asText(), Regions.US_EAST_1); if (bucketIsEmpty(s3Client, bucketId)) { this.awsS3ClientBuilderService.shutdown(s3Client); throw new FileDownloadException("Requested bucket does not exist or is empty"); } GetObjectRequest getObjectRequest = new GetObjectRequest(bucketId, fileName); S3Object s3Object = s3Client.getObject(getObjectRequest); S3ObjectInputStream objectInputStream = s3Object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(objectInputStream); this.awsS3ClientBuilderService.shutdown(s3Client); if (bytes != null && bytes.length != 0) { return bytes; } else { throw new FileDownloadException("Could not find the file!"); } } private boolean bucketIsEmpty(AmazonS3 s3Client, String bucketName) { ListObjectsV2Result result = s3Client.listObjectsV2(bucketName); if (result == null) { return false; } List<S3ObjectSummary> objects = result.getObjectSummaries(); return objects.isEmpty(); } write a unit testcase for the above downloadFile() code if (bucketIsEmpty(s3Client, bucketId)) { this.awsS3ClientBuilderService.shutdown(s3Client); throw new FileDownloadException("Requested bucket does not exist or is empty"); } GetObjectRequest getObjectRequest = new GetObjectRequest(bucketId, fileName); S3Object s3Object = s3Client.getObject(getObjectRequest); S3ObjectInputStream objectInputStream = s3Object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(objectInputStream); this.awsS3ClientBuilderService.shutdown(s3Client); if (bytes != null && bytes.length != 0) { return bytes; } else { throw new FileDownloadException("Could not find the file!"); } } it was not covered for the above lines @Test void testDownloadFile_Error() throws Exception { try { byte[] result = fileMgmtService.downloadFile(AppTestConstants.BUCKET, AppTestConstants.FILENAME); } catch (Exception e) { Assertions.assertEquals(AppTestConstants.DOWNLOAD_FILE_ERROR_STR, e.getMessage()); } } this is the previous code
Editor is loading...
Leave a Comment