src/test/java/edu/ucla/library/bucketeer/ImageUploadKakaduIT.java
@Test
    public final void testImageUpload(final TestContext aContext)
            throws UnsupportedEncodingException, SdkClientException, AmazonServiceException {
        Assume.assumeNotNull(System.getProperty(TestConstants.BUCKETEER_VERSION));

        final String filePath = URLEncoder.encode(TEST_FILE_PATH, StandardCharsets.UTF_8);
        final String request = StringUtils.format("/images/{}/{}", myUUID, filePath);
        final WebClient client = WebClient.create(myVertx);
        final Async asyncTask = aContext.async();

        myJP2 = myUUID + ".jpx";
        LOGGER.debug(MessageCodes.BUCKETEER_036, request);

        client.get(PORT, Constants.UNSPECIFIED_HOST, request).expect(SC_SUCCESS).as(JSON_CODEC).send(handler -> {
            if (handler.succeeded()) {
                final JsonObject body = handler.result().body();

                // Check that we received an S3 key back from our request
                aContext.assertEquals(body.getString(Constants.IMAGE_ID), myUUID);
                aContext.assertEquals(body.getString(Constants.FILE_PATH), TEST_FILE_PATH);

                // We keep checking S3 to see if our JP2 has been added to the S3 bucket, up to one minute
                for (int counter = 0; !asyncTask.isCompleted() && counter < 12; counter++) {
                    LOGGER.debug(MessageCodes.BUCKETEER_037, counter + 1);

                    if (myS3Client.doesBucketExistV2(myS3Bucket) && myS3Client.doesObjectExist(myS3Bucket, myJP2)) {
                        final String srcDir = TestConstants.JP2_SRC_DIR;
                        final File tmpDestDir = new File(TestConstants.TMP_DEST_DIR);
                        final File testFile = new File(new File(tmpDestDir, KakaduConverter.WORKING_DIR_NAME), myJP2);

                        // Confirm we can create our temporary test directory (or that it already exists)
                        aContext.assertTrue(tmpDestDir.exists() || tmpDestDir.mkdirs());

                        // Confirm we can copy the test container's files to the temporary test directory
                        aContext.assertTrue(DockerUtils.copy(TestConstants.BUCKETEER, srcDir, tmpDestDir.toString()));

                        // Check that our converted image file was cleaned up after S3 upload
                        if (testFile.exists()) {
                            aContext.fail(LOGGER.getMessage(MessageCodes.BUCKETEER_166, testFile));
                            FileUtils.delete(testFile);
                        }

                        break; // If our JP2 exists, we can quit checking
                    }

                    waitFiveSeconds(aContext);
                }

                if (!asyncTask.isCompleted()) {
                    if (myS3Client.doesObjectExist(myS3Bucket, myJP2)) {
                        final ObjectMetadata metadata = myS3Client.getObjectMetadata(myS3Bucket, myJP2);
                        final long contentLength = metadata.getContentLength();

                        aContext.assertTrue(contentLength > 0, LOGGER.getMessage(MessageCodes.BUCKETEER_042, myJP2));
                        TestUtils.complete(asyncTask);
                    } else {
                        aContext.fail(LOGGER.getMessage(MessageCodes.BUCKETEER_038, myJP2));
                    }
                } // else, we've already failed an assertion
            } else {
                aContext.fail(handler.cause());
            }
        });
    }
/**
     * Check that we can load an image.
     *
     * @param aContext A test context
     * @throws UnsupportedEncodingException If the encoding isn't supported by the JVM
     * @throws SdkClientException If there is trouble using the AWS SDK
     * @throws AmazonServiceException If there is trouble interacting with the AWS service
     */
Check that we can load an image.
@param aContext A test context
@throws UnsupportedEncodingException If the encoding isn't supported by the JVM
@throws SdkClientException If there is trouble using the AWS SDK
@throws AmazonServiceException If there is trouble interacting with the AWS service
Check that we can load an image . @param aContext A test context @throws UnsupportedEncodingException If the encoding isn ' t supported by the JVM @throws SdkClientException If there is trouble using the AWS SDK @throws AmazonServiceException If there is trouble interacting with the AWS service
@Test
    public final void testImageUpload(final TestContext aContext)
            throws UnsupportedEncodingException, SdkClientException, AmazonServiceException {
        Assume.assumeNotNull(System.getProperty(TestConstants.BUCKETEER_VERSION));
        final String filePath = URLEncoder.encode(TEST_FILE_PATH, StandardCharsets.UTF_8);
        final String request = StringUtils.format("/images/{}/{}", myUUID, filePath);
        final WebClient client = WebClient.create(myVertx);
        final Async asyncTask = aContext.async();
        myJP2 = myUUID + ".jpx";
        LOGGER.debug(MessageCodes.BUCKETEER_036, request);
        client.get(PORT, Constants.UNSPECIFIED_HOST, request).expect(SC_SUCCESS).as(JSON_CODEC).send(handler -> {
            if (handler.succeeded()) {
                final JsonObject body = handler.result().body();
                aContext.assertEquals(body.getString(Constants.IMAGE_ID), myUUID);
                aContext.assertEquals(body.getString(Constants.FILE_PATH), TEST_FILE_PATH);
                for (int counter = 0; !asyncTask.isCompleted() && counter < 12; counter++) {
                    LOGGER.debug(MessageCodes.BUCKETEER_037, counter + 1);
                    if (myS3Client.doesBucketExistV2(myS3Bucket) && myS3Client.doesObjectExist(myS3Bucket, myJP2)) {
                        final String srcDir = TestConstants.JP2_SRC_DIR;
                        final File tmpDestDir = new File(TestConstants.TMP_DEST_DIR);
                        final File testFile = new File(new File(tmpDestDir, KakaduConverter.WORKING_DIR_NAME), myJP2);
                        aContext.assertTrue(tmpDestDir.exists() || tmpDestDir.mkdirs());
                        aContext.assertTrue(DockerUtils.copy(TestConstants.BUCKETEER, srcDir, tmpDestDir.toString()));
                        if (testFile.exists()) {
                            aContext.fail(LOGGER.getMessage(MessageCodes.BUCKETEER_166, testFile));
                            FileUtils.delete(testFile);
                        }
                        break; 
                    }
                    waitFiveSeconds(aContext);
                }
                if (!asyncTask.isCompleted()) {
                    if (myS3Client.doesObjectExist(myS3Bucket, myJP2)) {
                        final ObjectMetadata metadata = myS3Client.getObjectMetadata(myS3Bucket, myJP2);
                        final long contentLength = metadata.getContentLength();
                        aContext.assertTrue(contentLength > 0, LOGGER.getMessage(MessageCodes.BUCKETEER_042, myJP2));
                        TestUtils.complete(asyncTask);
                    } else {
                        aContext.fail(LOGGER.getMessage(MessageCodes.BUCKETEER_038, myJP2));
                    }
                } 
            } else {
                aContext.fail(handler.cause());
            }
        });
    }
@ Test public final void testImageUpload ( final TestContext aContext ) throws UnsupportedEncodingException , SdkClientException , AmazonServiceException { Assume . assumeNotNull ( System . getProperty ( TestConstants . BUCKETEER_VERSION ) ) ; final String filePath = URLEncoder . encode ( TEST_FILE_PATH , StandardCharsets . UTF_8 ) ; final String request = StringUtils . format ( "/images/{}/{}" , myUUID , filePath ) ; final WebClient client = WebClient . create ( myVertx ) ; final Async asyncTask = aContext . async ( ) ; myJP2 = myUUID + ".jpx" ; LOGGER . debug ( MessageCodes . BUCKETEER_036 , request ) ; client . get ( PORT , Constants . UNSPECIFIED_HOST , request ) . expect ( SC_SUCCESS ) . as ( JSON_CODEC ) . send ( handler -> { if ( handler . succeeded ( ) ) { final JsonObject body = handler . result ( ) . body ( ) ; aContext . assertEquals ( body . getString ( Constants . IMAGE_ID ) , myUUID ) ; aContext . assertEquals ( body . getString ( Constants . FILE_PATH ) , TEST_FILE_PATH ) ; for ( int counter = 0 ; ! asyncTask . isCompleted ( ) && counter < 12 ; counter ++ ) { LOGGER . debug ( MessageCodes . BUCKETEER_037 , counter + 1 ) ; if ( myS3Client . doesBucketExistV2 ( myS3Bucket ) && myS3Client . doesObjectExist ( myS3Bucket , myJP2 ) ) { final String srcDir = TestConstants . JP2_SRC_DIR ; final File tmpDestDir = new File ( TestConstants . TMP_DEST_DIR ) ; final File testFile = new File ( new File ( tmpDestDir , KakaduConverter . WORKING_DIR_NAME ) , myJP2 ) ; aContext . assertTrue ( tmpDestDir . exists ( ) || tmpDestDir . mkdirs ( ) ) ; aContext . assertTrue ( DockerUtils . copy ( TestConstants . BUCKETEER , srcDir , tmpDestDir . toString ( ) ) ) ; if ( testFile . exists ( ) ) { aContext . fail ( LOGGER . getMessage ( MessageCodes . BUCKETEER_166 , testFile ) ) ; FileUtils . delete ( testFile ) ; } break ; } waitFiveSeconds ( aContext ) ; } if ( ! asyncTask . isCompleted ( ) ) { if ( myS3Client . doesObjectExist ( myS3Bucket , myJP2 ) ) { final ObjectMetadata metadata = myS3Client . getObjectMetadata ( myS3Bucket , myJP2 ) ; final long contentLength = metadata . getContentLength ( ) ; aContext . assertTrue ( contentLength > 0 , LOGGER . getMessage ( MessageCodes . BUCKETEER_042 , myJP2 ) ) ; TestUtils . complete ( asyncTask ) ; } else { aContext . fail ( LOGGER . getMessage ( MessageCodes . BUCKETEER_038 , myJP2 ) ) ; } } } else { aContext . fail ( handler . cause ( ) ) ; } } ) ; }
Check that we can load an image.
Check that we can load an image .
// Check that we received an S3 key back from our request // We keep checking S3 to see if our JP2 has been added to the S3 bucket, up to one minute // Confirm we can create our temporary test directory (or that it already exists) // Confirm we can copy the test container's files to the temporary test directory // Check that our converted image file was cleaned up after S3 upload // If our JP2 exists, we can quit checking // else, we've already failed an assertion