diff --git a/.gitignore b/.gitignore index 8e171704c09..b71ef365088 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,9 @@ release.properties dependency-reduced-pom.xml buildNumber.properties +# Secrets service-account.json +secrets.env # intellij .idea/ diff --git a/.travis.yml b/.travis.yml index 924cec2e7fe..a91a22cff3e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,9 @@ before_install: - openssl aes-256-cbc -K $encrypted_37a4f399de75_key -iv $encrypted_37a4f399de75_iv -in service-account.json.enc -out service-account.json -d && export GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/service-account.json GCLOUD_PROJECT=cloud-samples-tests || true +- openssl aes-256-cbc -K $encrypted_eb858daba67b_key -iv $encrypted_eb858daba67b_iv -in secrets.env.enc -out secrets.env -d + && set +x && source secrets.env && set -x + || true # Skip the install step, since Maven will download the dependencies we need # when the test build runs. # http://stackoverflow.com/q/31945809/101923 diff --git a/secrets.env.enc b/secrets.env.enc new file mode 100644 index 00000000000..3396fd83461 Binary files /dev/null and b/secrets.env.enc differ diff --git a/storage/storage-transfer/README.md b/storage/storage-transfer/README.md index 7d15bf4eafc..d2550388e07 100644 --- a/storage/storage-transfer/README.md +++ b/storage/storage-transfer/README.md @@ -2,20 +2,45 @@ This app creates two types of transfers using the Transfer Service tool. + +These samples are used on the following documentation pages: + +> +* https://cloud.google.com/storage/transfer/create-client +* https://cloud.google.com/storage/transfer/create-manage-transfer-program + + + ## Prerequisites -1. Set up a project on Google Developers Console. - 1. Go to the [Developers Console](https://cloud.google.com/console) and create or select your project. - You will need the project ID later. +1. Set up a project on Google Cloud Console. + 1. Go to the [Google Cloud Console](https://console.cloud.google.com) and + create or select your project. You will need the project ID later. +1. Enable the [Google Storage Transfer API in the Google Cloud + Console](https://console.cloud.google.com/apis/api/storagetransfer/overview). 1. Within Developers Console, select APIs & auth > Credentials. 1. Select Add credentials > Service account > JSON key. - 1. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to your JSON key. -1. Add the Storage Transfer service account as an editor of your project - storage-transfer-5031963314028297433@partnercontent.gserviceaccount.com + 1. Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to point to + your JSON key. +1. Add the Storage Transfer service account as an editor of your project. + 1. To get the email address used for the service account, execute the + [googleServiceAccounts.get REST + method](https://cloud.google.com/storage/transfer/reference/rest/v1/googleServiceAccounts/get#try-it). + There should be a "Try It" section on that page, otherwise execute it in + the [APIs + Explorer](https://developers.google.com/apis-explorer/#p/storagetransfer/v1/storagetransfer.googleServiceAccounts.get). + + It should output an email address like: + + ``` + storage-transfer-1234567890@partnercontent.gserviceaccount.com + ``` + 1. Add this as a member and select the Project -> Editor permission on the + [Google Cloud Console IAM and Admin + page](https://console.cloud.google.com/iam-admin/iam/project). 1. Set up gcloud for application default credentials. 1. `gcloud components update` - 1. `gcloud auth login` - 1. `gcloud config set project PROJECT_ID` + 1. `gcloud init` ## Transfer from Amazon S3 to Google Cloud Storage @@ -26,9 +51,21 @@ Creating a one-time transfer from Amazon S3 to Google Cloud Storage. 1. Go to AWS Management Console and create a bucket. 1. Under Security Credentials, create an IAM User with access to the bucket. 1. Create an Access Key for the user. Note the Access Key ID and Secret Access Key. + 1. Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 1. In AwsRequester.java, fill in the user-provided constants. -1. Run with `mvn compile` and - `mvn exec:java -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.AwsRequester"` +1. Compile the package with + ``` + mvn compile + ``` +1. Run the transfer job with + ``` + mvn exec:java \ + -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.AwsRequester" \ + -DprojectId=your-google-cloud-project-id \ + -DjobDescription="Sample transfer job from S3 to GCS." \ + -DawsSourceBucket=your-s3-bucket-name \ + -DgcsSinkBucket=your-gcs-bucket-name + ``` 1. Note the job ID in the returned Transfer Job. ## Transfer data from a standard Cloud Storage bucket to a Cloud Storage Nearline bucket @@ -50,3 +87,8 @@ bucket for files untouched for 30 days. 1. In RequestChecker.java, fill in the user-provided constants. Use the Job Name you recorded earlier. 1. Run with `mvn compile` and `mvn exec:java -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.RequestChecker"` + +## References + +- [Google Storage Transfer API Client + Library](https://developers.google.com/api-client-library/java/apis/storagetransfer/v1) \ No newline at end of file diff --git a/storage/storage-transfer/pom.xml b/storage/storage-transfer/pom.xml index 8b2f46ee990..c7b643697ea 100644 --- a/storage/storage-transfer/pom.xml +++ b/storage/storage-transfer/pom.xml @@ -35,7 +35,6 @@ UTF-8 - 1.6.2 @@ -47,15 +46,21 @@ - org.powermock - powermock-module-junit4 - ${powermock.version} + com.google.truth + truth + 0.29 test - org.powermock - powermock-api-mockito - ${powermock.version} + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 test diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java index 5f4f3577d52..be3e09449dc 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java @@ -27,31 +27,19 @@ import com.google.api.services.storagetransfer.model.TransferSpec; import java.io.IOException; -import java.util.logging.Logger; +import java.io.PrintStream; /** * Creates a one-off transfer job from Amazon S3 to Google Cloud Storage. */ public final class AwsRequester { - - private static final String JOB_DESC = "YOUR DESCRIPTION"; - private static final String PROJECT_ID = "YOUR_PROJECT_ID"; - private static final String AWS_SOURCE_NAME = "YOUR SOURCE BUCKET"; - private static final String AWS_ACCESS_KEY_ID = "YOUR_ACCESS_KEY_ID"; - private static final String AWS_SECRET_ACCESS_KEY = "YOUR_SECRET_ACCESS_KEY"; - private static final String GCS_SINK_NAME = "YOUR_SINK_BUCKET"; - - /** - * Specify times below using US Pacific Time Zone. - */ - private static final String START_DATE = "YYYY-MM-DD"; - private static final String START_TIME = "HH:MM:SS"; - - private static final Logger LOG = Logger.getLogger(AwsRequester.class.getName()); - /** * Creates and executes a request for a TransferJob from Amazon S3 to Cloud Storage. * + *

The {@code startDate} and {@code startTime} parameters should be set according to the UTC + * Time Zone. See: + * https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay() + * * @return the response TransferJob if the request is successful * @throws InstantiationException * if instantiation fails when building the TransferJob @@ -60,43 +48,73 @@ public final class AwsRequester { * @throws IOException * if the client failed to complete the request */ - public static TransferJob createAwsTransferJob() throws InstantiationException, - IllegalAccessException, IOException { - Date date = TransferJobUtils.createDate(START_DATE); - TimeOfDay time = TransferJobUtils.createTimeOfDay(START_TIME); - TransferJob transferJob = TransferJob.class - .newInstance() - .setDescription(JOB_DESC) - .setProjectId(PROJECT_ID) - .setTransferSpec( - TransferSpec.class - .newInstance() - .setAwsS3DataSource( - AwsS3Data.class - .newInstance() - .setBucketName(AWS_SOURCE_NAME) - .setAwsAccessKey( - AwsAccessKey.class.newInstance().setAccessKeyId(AWS_ACCESS_KEY_ID) - .setSecretAccessKey(AWS_SECRET_ACCESS_KEY))) - .setGcsDataSink(GcsData.class.newInstance().setBucketName(GCS_SINK_NAME))) - .setSchedule( - Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) - .setStartTimeOfDay(time)).setStatus("ENABLED"); + public static TransferJob createAwsTransferJob( + String projectId, + String jobDescription, + String awsSourceBucket, + String gcsSinkBucket, + String startDate, + String startTime, + String awsAccessKeyId, + String awsSecretAccessKey) + throws InstantiationException, IllegalAccessException, IOException { + Date date = TransferJobUtils.createDate(startDate); + TimeOfDay time = TransferJobUtils.createTimeOfDay(startTime); + TransferJob transferJob = + new TransferJob() + .setDescription(jobDescription) + .setProjectId(projectId) + .setTransferSpec( + new TransferSpec() + .setAwsS3DataSource( + new AwsS3Data() + .setBucketName(awsSourceBucket) + .setAwsAccessKey( + new AwsAccessKey() + .setAccessKeyId(awsAccessKeyId) + .setSecretAccessKey(awsSecretAccessKey))) + .setGcsDataSink(new GcsData().setBucketName(gcsSinkBucket))) + .setSchedule( + new Schedule() + .setScheduleStartDate(date) + .setScheduleEndDate(date) + .setStartTimeOfDay(time)) + .setStatus("ENABLED"); Storagetransfer client = TransferClientCreator.createStorageTransferClient(); return client.transferJobs().create(transferJob).execute(); } + public static void run(PrintStream out) + throws InstantiationException, IllegalAccessException, IOException { + String projectId = TransferJobUtils.getPropertyOrFail("projectId"); + String jobDescription = TransferJobUtils.getPropertyOrFail("jobDescription"); + String awsSourceBucket = TransferJobUtils.getPropertyOrFail("awsSourceBucket"); + String gcsSinkBucket = TransferJobUtils.getPropertyOrFail("gcsSinkBucket"); + String startDate = TransferJobUtils.getPropertyOrFail("startDate"); + String startTime = TransferJobUtils.getPropertyOrFail("startTime"); + String awsAccessKeyId = TransferJobUtils.getEnvOrFail("AWS_ACCESS_KEY_ID"); + String awsSecretAccessKey = TransferJobUtils.getEnvOrFail("AWS_SECRET_ACCESS_KEY"); + + TransferJob responseT = + createAwsTransferJob( + projectId, + jobDescription, + awsSourceBucket, + gcsSinkBucket, + startDate, + startTime, + awsAccessKeyId, + awsSecretAccessKey); + out.println("Return transferJob: " + responseT.toPrettyString()); + } + /** * Output the contents of a successfully created TransferJob. - * - * @param args - * arguments from the command line */ public static void main(String[] args) { try { - TransferJob responseT = createAwsTransferJob(); - LOG.info("Return transferJob: " + responseT.toPrettyString()); + run(System.out); } catch (Exception e) { e.printStackTrace(); } diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java index f555ef5299d..39ff2dc73d4 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java @@ -27,30 +27,21 @@ import com.google.api.services.storagetransfer.model.TransferSpec; import java.io.IOException; -import java.util.logging.Logger; +import java.io.PrintStream; /** * Creates a daily transfer from a standard Cloud Storage bucket to a Cloud Storage Nearline - * bucket for files untouched for 30 days. + * bucket for files untouched for 30 days. */ public final class NearlineRequester { - private static final String JOB_DESC = "YOUR DESCRIPTION"; - private static final String PROJECT_ID = "YOUR_PROJECT_ID"; - private static final String GCS_SOURCE_NAME = "YOUR_SOURCE_BUCKET"; - private static final String NEARLINE_SINK_NAME = "YOUR_SINK_BUCKET"; - - /** - * Specify times below using US Pacific Time Zone. - */ - private static final String START_DATE = "YYYY-MM-DD"; - private static final String START_TIME = "HH:MM:SS"; - - private static final Logger LOG = Logger.getLogger(AwsRequester.class.getName()); - /** * Creates and executes a request for a TransferJob to Cloud Storage Nearline. * + *

The {@code startDate} and {@code startTime} parameters should be set according to the UTC + * Time Zone. See: + * https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay() + * * @return the response TransferJob if the request is successful * @throws InstantiationException * if instantiation fails when building the TransferJob @@ -59,31 +50,58 @@ public final class NearlineRequester { * @throws IOException * if the client failed to complete the request */ - public static TransferJob createNearlineTransferJob() throws InstantiationException, - IllegalAccessException, IOException { - Date date = TransferJobUtils.createDate(START_DATE); - TimeOfDay time = TransferJobUtils.createTimeOfDay(START_TIME); - TransferJob transferJob = TransferJob.class - .newInstance() - .setDescription(JOB_DESC) - .setProjectId(PROJECT_ID) - .setTransferSpec( - TransferSpec.class - .newInstance() - .setGcsDataSource(GcsData.class.newInstance().setBucketName(GCS_SOURCE_NAME)) - .setGcsDataSink(GcsData.class.newInstance().setBucketName(NEARLINE_SINK_NAME)) - .setObjectConditions( - ObjectConditions.class.newInstance().setMinTimeElapsedSinceLastModification("2592000s")) - .setTransferOptions( - TransferOptions.class.newInstance().setDeleteObjectsFromSourceAfterTransfer(true))) - .setSchedule(Schedule.class.newInstance().setScheduleStartDate(date) - .setStartTimeOfDay(time)) - .setStatus("ENABLED"); + public static TransferJob createNearlineTransferJob( + String projectId, + String jobDescription, + String gcsSourceBucket, + String gcsNearlineSinkBucket, + String startDate, + String startTime) + throws InstantiationException, IllegalAccessException, IOException { + Date date = TransferJobUtils.createDate(startDate); + TimeOfDay time = TransferJobUtils.createTimeOfDay(startTime); + TransferJob transferJob = + new TransferJob() + .setDescription(jobDescription) + .setProjectId(projectId) + .setTransferSpec( + new TransferSpec() + .setGcsDataSource(new GcsData().setBucketName(gcsSourceBucket)) + .setGcsDataSink(new GcsData().setBucketName(gcsNearlineSinkBucket)) + .setObjectConditions( + new ObjectConditions() + .setMinTimeElapsedSinceLastModification("2592000s" /* 30 days */)) + .setTransferOptions( + new TransferOptions() + .setDeleteObjectsFromSourceAfterTransfer(true))) + .setSchedule( + new Schedule().setScheduleStartDate(date).setStartTimeOfDay(time)) + .setStatus("ENABLED"); Storagetransfer client = TransferClientCreator.createStorageTransferClient(); return client.transferJobs().create(transferJob).execute(); } + public static void run(PrintStream out) + throws InstantiationException, IllegalAccessException, IOException { + String projectId = TransferJobUtils.getPropertyOrFail("projectId"); + String jobDescription = TransferJobUtils.getPropertyOrFail("jobDescription"); + String gcsSourceBucket = TransferJobUtils.getPropertyOrFail("gcsSourceBucket"); + String gcsNearlineSinkBucket = TransferJobUtils.getPropertyOrFail("gcsNearlineSinkBucket"); + String startDate = TransferJobUtils.getPropertyOrFail("startDate"); + String startTime = TransferJobUtils.getPropertyOrFail("startTime"); + + TransferJob responseT = + createNearlineTransferJob( + projectId, + jobDescription, + gcsSourceBucket, + gcsNearlineSinkBucket, + startDate, + startTime); + out.println("Return transferJob: " + responseT.toPrettyString()); + } + /** * Output the contents of a successfully created TransferJob. * @@ -92,8 +110,7 @@ public static TransferJob createNearlineTransferJob() throws InstantiationExcept */ public static void main(String[] args) { try { - TransferJob responseT = createNearlineTransferJob(); - LOG.info("Return transferJob: " + responseT.toPrettyString()); + run(System.out); } catch (Exception e) { e.printStackTrace(); } diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java index 057c190cbc2..64a2bf5e208 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RequestChecker.java @@ -47,9 +47,11 @@ public final class RequestChecker { * @throws IOException * if the client failed to complete the request */ - public static ListOperationsResponse checkTransfer(Storagetransfer client, - String projectId, String jobName) throws IOException { - return client.transferOperations().list("transferOperations") + public static ListOperationsResponse checkTransfer( + Storagetransfer client, String projectId, String jobName) throws IOException { + return client + .transferOperations() + .list("transferOperations") .setFilter("{\"project_id\": \"" + projectId + "\", \"job_names\": [\"" + jobName + "\"] }") .execute(); } @@ -63,8 +65,7 @@ public static ListOperationsResponse checkTransfer(Storagetransfer client, public static void main(String[] args) { try { ListOperationsResponse resp = - checkTransfer(TransferClientCreator.createStorageTransferClient(), - PROJECT_ID, JOB_NAME); + checkTransfer(TransferClientCreator.createStorageTransferClient(), PROJECT_ID, JOB_NAME); LOG.info("Result of transferOperations/list: " + resp.toPrettyString()); } catch (Exception e) { e.printStackTrace(); diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java index 74771a41d31..c035e4a808e 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/RetryHttpInitializerWrapper.java @@ -73,27 +73,28 @@ public RetryHttpInitializerWrapper(Credential wrappedCredential) { public void initialize(HttpRequest request) { request.setReadTimeout(2 * MILLIS_PER_MINUTE); // 2 minutes read timeout final HttpUnsuccessfulResponseHandler backoffHandler = - new HttpBackOffUnsuccessfulResponseHandler( - new ExponentialBackOff()).setSleeper(sleeper); + new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper); request.setInterceptor(wrappedCredential); - request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() { - public boolean handleResponse(final HttpRequest request, final HttpResponse response, - final boolean supportsRetry) throws IOException { - if (wrappedCredential.handleResponse(request, response, supportsRetry)) { - // If credential decides it can handle it, the return code or message indicated - // something specific to authentication, and no backoff is desired. - return true; - } else if (backoffHandler.handleResponse(request, response, supportsRetry)) { - // Otherwise, we defer to the judgement of our internal backoff handler. - LOG.info("Retrying " + request.getUrl().toString()); - return true; - } else { - return false; - } - } - }); - request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()) - .setSleeper(sleeper)); + request.setUnsuccessfulResponseHandler( + new HttpUnsuccessfulResponseHandler() { + public boolean handleResponse( + final HttpRequest request, final HttpResponse response, final boolean supportsRetry) + throws IOException { + if (wrappedCredential.handleResponse(request, response, supportsRetry)) { + // If credential decides it can handle it, the return code or message indicated + // something specific to authentication, and no backoff is desired. + return true; + } else if (backoffHandler.handleResponse(request, response, supportsRetry)) { + // Otherwise, we defer to the judgement of our internal backoff handler. + LOG.info("Retrying " + request.getUrl().toString()); + return true; + } else { + return false; + } + } + }); + request.setIOExceptionHandler( + new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper)); } } //[END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java index 3199593f3ac..2dea7d602a5 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferClientCreator.java @@ -43,8 +43,8 @@ public final class TransferClientCreator { public static Storagetransfer createStorageTransferClient() throws IOException { HttpTransport httpTransport = Utils.getDefaultTransport(); JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); - GoogleCredential credential = GoogleCredential - .getApplicationDefault(httpTransport, jsonFactory); + GoogleCredential credential = + GoogleCredential.getApplicationDefault(httpTransport, jsonFactory); return createStorageTransferClient(httpTransport, jsonFactory, credential); } @@ -59,8 +59,8 @@ public static Storagetransfer createStorageTransferClient() throws IOException { * a user-supplied Google credential * @return a Storage Transfer client */ - public static Storagetransfer createStorageTransferClient(HttpTransport httpTransport, - JsonFactory jsonFactory, GoogleCredential credential) { + public static Storagetransfer createStorageTransferClient( + HttpTransport httpTransport, JsonFactory jsonFactory, GoogleCredential credential) { Preconditions.checkNotNull(httpTransport); Preconditions.checkNotNull(jsonFactory); Preconditions.checkNotNull(credential); @@ -74,7 +74,8 @@ public static Storagetransfer createStorageTransferClient(HttpTransport httpTran // implementation in the "Retry Handling" section. HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Storagetransfer.Builder(httpTransport, jsonFactory, initializer) - .setApplicationName("storagetransfer-sample").build(); + .setApplicationName("storagetransfer-sample") + .build(); } } //[END all] diff --git a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java index 2717bd81222..ff99e2bfeb4 100644 --- a/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java +++ b/storage/storage-transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/TransferJobUtils.java @@ -24,13 +24,12 @@ * */ public final class TransferJobUtils { + private static final int BASE_10 = 10; /** * A private constructor. */ - private TransferJobUtils() { - - } + private TransferJobUtils() {} /** * Parses a Date from a string of the form "YYYY-MM-DD". @@ -45,11 +44,14 @@ private TransferJobUtils() { * @throws IllegalAccessException * if Date object cannot be accessed */ - public static Date createDate(String dateString) throws NumberFormatException, - InstantiationException, IllegalAccessException { - Date date = Date.class.newInstance().setYear(Integer.decode(dateString.split("-")[0])) - .setMonth(Integer.decode(dateString.split("-")[1])) - .setDay(Integer.decode(dateString.split("-")[2])); + public static Date createDate(String dateString) + throws NumberFormatException, InstantiationException, IllegalAccessException { + Date date = + Date.class + .newInstance() + .setYear(Integer.parseInt(dateString.split("-")[0], BASE_10)) + .setMonth(Integer.parseInt(dateString.split("-")[1], BASE_10)) + .setDay(Integer.parseInt(dateString.split("-")[2], BASE_10)); return date; } @@ -66,13 +68,33 @@ public static Date createDate(String dateString) throws NumberFormatException, * @throws IllegalAccessException * if Date object cannot be accessed */ - public static TimeOfDay createTimeOfDay(String timeString) throws NumberFormatException, - InstantiationException, IllegalAccessException { - TimeOfDay time = TimeOfDay.class.newInstance() - .setHours(Integer.decode(timeString.split(":")[0])) - .setMinutes(Integer.decode(timeString.split(":")[1])) - .setSeconds(Integer.decode(timeString.split(":")[2])); + public static TimeOfDay createTimeOfDay(String timeString) + throws NumberFormatException, InstantiationException, IllegalAccessException { + TimeOfDay time = + TimeOfDay.class + .newInstance() + .setHours(Integer.parseInt(timeString.split(":")[0], BASE_10)) + .setMinutes(Integer.parseInt(timeString.split(":")[1], BASE_10)) + .setSeconds(Integer.parseInt(timeString.split(":")[2], BASE_10)); return time; } + + public static String getEnvOrFail(String envName) { + String envValue = System.getenv(envName); + if (envValue == null || envValue.isEmpty()) { + System.err.printf("The %s environment variable must be set.", envName); + System.exit(1); + } + return envValue; + } + + public static String getPropertyOrFail(String propertyName) { + String propertyValue = System.getProperty(propertyName); + if (propertyValue == null || propertyValue.isEmpty()) { + System.err.printf("The %s property must be set.", propertyName); + System.exit(1); + } + return propertyValue; + } } //[END all] diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java index cac2eaa8918..ee1e46c2373 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/AwsRequesterTest.java @@ -16,62 +16,37 @@ package com.google.cloud.storage.storagetransfer.samples.test; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; +import static com.google.common.truth.Truth.assertThat; -import com.google.api.services.storagetransfer.Storagetransfer; -import com.google.api.services.storagetransfer.Storagetransfer.TransferJobs; -import com.google.api.services.storagetransfer.Storagetransfer.TransferJobs.Create; -import com.google.api.services.storagetransfer.model.Date; -import com.google.api.services.storagetransfer.model.Schedule; -import com.google.api.services.storagetransfer.model.TimeOfDay; -import com.google.api.services.storagetransfer.model.TransferJob; -import com.google.api.services.storagetransfer.model.TransferSpec; import com.google.cloud.storage.storagetransfer.samples.AwsRequester; -import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; -import com.google.cloud.storage.storagetransfer.samples.TransferJobUtils; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.junit.runners.JUnit4; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ TransferJobUtils.class, TransferClientCreator.class }) +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +@RunWith(JUnit4.class) public class AwsRequesterTest { /** * Tests whether AwsRequester executes a request to create a TransferJob. */ @Test - public void testTest() throws Exception { - Date date = TransferJobUtils.createDate("2000-1-1"); - TimeOfDay time = TransferJobUtils.createTimeOfDay("1:1:1"); - TransferJob dummyJob = TransferJob.class - .newInstance() - .setDescription("DUMMY DESCRIPTION") - .setProjectId("DUMMY_PROJECT_ID") - .setTransferSpec(TransferSpec.class.newInstance()) - .setSchedule( - Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) - .setStartTimeOfDay(time)).setStatus("ENABLED"); - - PowerMockito.mockStatic(TransferClientCreator.class); - PowerMockito.mockStatic(TransferJobUtils.class); - Storagetransfer client = Mockito.mock(Storagetransfer.class); - TransferJobs jobs = Mockito.mock(TransferJobs.class); - Create create = Mockito.mock(Create.class); - - PowerMockito.when(TransferClientCreator.createStorageTransferClient()).thenReturn(client); - when(client.transferJobs()).thenReturn(jobs); - when(jobs.create(Matchers.any(TransferJob.class))).thenReturn(create); - when(create.execute()).thenReturn(dummyJob); - - TransferJob returnedJob = AwsRequester.createAwsTransferJob(); - - assertEquals(returnedJob, dummyJob); + public void testRun() throws Exception { + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); + PrintStream outStream = new PrintStream(outBytes); + System.setProperty("projectId", "cloud-samples-tests"); + System.setProperty("jobDescription", "Sample transfer job from S3 to GCS."); + System.setProperty("awsSourceBucket", "cloud-samples-tests"); + System.setProperty("gcsSinkBucket", "cloud-samples-tests-storagetransfer"); + System.setProperty("startDate", "2000-01-01"); + System.setProperty("startTime", "00:00:00"); + + AwsRequester.run(outStream); + String out = outBytes.toString(); + + assertThat(out).contains("\"description\" : \"Sample transfer job from S3 to GCS.\""); } -} +} \ No newline at end of file diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java index 4816ac1cd3c..4f415ffcc75 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/NearlineRequesterTest.java @@ -16,62 +16,38 @@ package com.google.cloud.storage.storagetransfer.samples.test; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.when; +import static com.google.common.truth.Truth.assertThat; -import com.google.api.services.storagetransfer.Storagetransfer; -import com.google.api.services.storagetransfer.Storagetransfer.TransferJobs; -import com.google.api.services.storagetransfer.Storagetransfer.TransferJobs.Create; -import com.google.api.services.storagetransfer.model.Date; -import com.google.api.services.storagetransfer.model.Schedule; -import com.google.api.services.storagetransfer.model.TimeOfDay; -import com.google.api.services.storagetransfer.model.TransferJob; -import com.google.api.services.storagetransfer.model.TransferSpec; import com.google.cloud.storage.storagetransfer.samples.NearlineRequester; -import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; -import com.google.cloud.storage.storagetransfer.samples.TransferJobUtils; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.junit.runners.JUnit4; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ TransferJobUtils.class, TransferClientCreator.class }) +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +@RunWith(JUnit4.class) public class NearlineRequesterTest { /** * Tests whether NearlineRequester executes a request to create a TransferJob. */ @Test - public void testTest() throws Exception { - Date date = TransferJobUtils.createDate("2000-1-1"); - TimeOfDay time = TransferJobUtils.createTimeOfDay("1:1:1"); - TransferJob dummyJob = TransferJob.class - .newInstance() - .setDescription("DUMMY DESCRIPTION") - .setProjectId("DUMMY_PROJECT_ID") - .setTransferSpec(TransferSpec.class.newInstance()) - .setSchedule( - Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) - .setStartTimeOfDay(time)).setStatus("ENABLED"); - - PowerMockito.mockStatic(TransferClientCreator.class); - PowerMockito.mockStatic(TransferJobUtils.class); - Storagetransfer client = Mockito.mock(Storagetransfer.class); - TransferJobs jobs = Mockito.mock(TransferJobs.class); - Create create = Mockito.mock(Create.class); + public void testRun() throws Exception { - PowerMockito.when(TransferClientCreator.createStorageTransferClient()).thenReturn(client); - when(client.transferJobs()).thenReturn(jobs); - when(jobs.create(Matchers.any(TransferJob.class))).thenReturn(create); - when(create.execute()).thenReturn(dummyJob); + ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); + PrintStream outStream = new PrintStream(outBytes); + System.setProperty("projectId", "cloud-samples-tests"); + System.setProperty("jobDescription", "Sample transfer job from GCS to GCS Nearline."); + System.setProperty("gcsSourceBucket", "cloud-samples-tests-storagetransfer-source"); + System.setProperty("gcsNearlineSinkBucket", "cloud-storage-samples-tests-storagetransfer-sink"); + System.setProperty("startDate", "2000-01-01"); + System.setProperty("startTime", "00:00:00"); - TransferJob returnedJob = NearlineRequester.createNearlineTransferJob(); + NearlineRequester.run(outStream); + String out = outBytes.toString(); - assertEquals(returnedJob, dummyJob); + assertThat(out).contains("\"description\" : \"Sample transfer job from GCS to GCS Nearline.\""); } } diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java index d82eb2535f8..ea21c5b159e 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/RequestCheckerTest.java @@ -25,9 +25,12 @@ import com.google.cloud.storage.storagetransfer.samples.RequestChecker; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; import org.mockito.Matchers; import org.mockito.Mockito; +@RunWith(JUnit4.class) public class RequestCheckerTest { private Storagetransfer mockClient = Mockito.mock(Storagetransfer.class); private List mockList = Mockito.mock(List.class); diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java deleted file mode 100644 index d36d342082a..00000000000 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferClientCreatorTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud.storage.storagetransfer.samples.test; - -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.util.Utils; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.services.storagetransfer.Storagetransfer.Builder; -import com.google.cloud.storage.storagetransfer.samples.RetryHttpInitializerWrapper; -import com.google.cloud.storage.storagetransfer.samples.TransferClientCreator; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ TransferClientCreator.class, Builder.class }) -public class TransferClientCreatorTest { - - private Builder mockBuilder = PowerMockito.mock(Builder.class); - private GoogleCredential mockCredential = Mockito.mock(GoogleCredential.class); - private RetryHttpInitializerWrapper mockInitializer = - Mockito.mock(RetryHttpInitializerWrapper.class); - private HttpTransport httpTransport = Utils.getDefaultTransport(); - private JsonFactory jsonFactory = Utils.getDefaultJsonFactory(); - - @Before - public void setUp() throws Exception { - PowerMockito.whenNew(RetryHttpInitializerWrapper.class).withArguments(mockCredential) - .thenReturn(mockInitializer); - PowerMockito.mockStatic(Builder.class); - PowerMockito.whenNew(Builder.class).withArguments(httpTransport, jsonFactory, mockInitializer) - .thenReturn(mockBuilder); - when(mockBuilder.setApplicationName(Matchers.anyString())).thenReturn(mockBuilder); - PowerMockito.mockStatic(GoogleCredential.class); - } - - /** - * Tests whether createStorageTransferClient() makes the API call for building clients when the - * credential does not require a Scope. - */ - @Test - public void testCreateStorageTransferClientScopedRequiredFalse() throws Exception { - when(mockCredential.createScopedRequired()).thenReturn(false); - - TransferClientCreator.createStorageTransferClient( - Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), mockCredential); - - verify(mockBuilder).build(); - } - - /** - * Tests whether createStorageTransferClient() makes the API call for building clients when the - * credential requires a Scope. - */ - @Test - public void testCreateStorageTransferClientScopedRequiredTrue() throws Exception { - when(mockCredential.createScopedRequired()).thenReturn(true); - when(mockCredential.createScoped(Matchers.anyCollectionOf(String.class))) - .thenReturn(mockCredential); - - TransferClientCreator.createStorageTransferClient( - Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), mockCredential); - - verify(mockBuilder).build(); - } -} diff --git a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java index 631132436d8..bbdfd0a30c9 100644 --- a/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java +++ b/storage/storage-transfer/src/test/java/com/google/cloud/storage/storagetransfer/samples/test/TransferJobUtilsTest.java @@ -16,7 +16,7 @@ package com.google.cloud.storage.storagetransfer.samples.test; -import static org.junit.Assert.assertEquals; +import static com.google.common.truth.Truth.assertThat; import com.google.api.services.storagetransfer.model.Date; import com.google.api.services.storagetransfer.model.TimeOfDay; @@ -34,15 +34,11 @@ public class TransferJobUtilsTest { */ @Test public void testCreateDate() throws Exception { - int year = rand.nextInt(2000) + 1; - int month = rand.nextInt(12) + 1; - int day = rand.nextInt(30) + 1; - String dateString = - Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day); + Date date = TransferJobUtils.createDate("2000-12-30"); + assertThat(date).isEqualTo(new Date().setYear(2000).setMonth(12).setDay(30)); - Date date = TransferJobUtils.createDate(dateString); - - assertEquals(date, Date.class.newInstance().setYear(year).setMonth(month).setDay(day)); + date = TransferJobUtils.createDate("2016-09-08"); + assertThat(date).isEqualTo(new Date().setYear(2016).setMonth(9).setDay(8)); } /** @@ -50,15 +46,10 @@ public void testCreateDate() throws Exception { */ @Test public void testCreateTimeOfDay() throws Exception { - int hour = rand.nextInt(24); - int minute = rand.nextInt(60); - int second = rand.nextInt(60); - String timeString = - Integer.toString(hour) + ":" + Integer.toString(minute) + ":" + Integer.toString(second); - - TimeOfDay time = TransferJobUtils.createTimeOfDay(timeString); + TimeOfDay time = TransferJobUtils.createTimeOfDay("17:00:42"); + assertThat(time).isEqualTo(new TimeOfDay().setHours(17).setMinutes(0).setSeconds(42)); - assertEquals(time, - TimeOfDay.class.newInstance().setHours(hour).setMinutes(minute).setSeconds(second)); + time = TransferJobUtils.createTimeOfDay("08:09:08"); + assertThat(time).isEqualTo(new TimeOfDay().setHours(8).setMinutes(9).setSeconds(8)); } }