From c50efe772e5ce4e051bbaae02d04f8732e454f6e Mon Sep 17 00:00:00 2001 From: tetiana-karasova Date: Tue, 22 Mar 2022 01:34:29 +0100 Subject: [PATCH 01/22] Cherry-picked "Update README" --- samples/interactive-tutorials/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index c2957c23..45264ca3 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -36,6 +36,7 @@ The Retail API provides you with the following possibilities to: You can find the information about the Retail services in the [documentation](https://cloud.google.com/retail/docs) + If you would like to have a closer look at the Retail API features and try them yourself, the best option is to use the [Interactive Tutorials](https://cloud.google.com/retail/docs/overview). In the documentation chapters find the "Guide me" button, the tutorials will be launched in the CloudShell environment, and you will be able to request the Retail services and check the response with minimum time and effort. From f643f83bace147307fe2a682ea3094e87aa05ffd Mon Sep 17 00:00:00 2001 From: t-karasova Date: Thu, 31 Mar 2022 17:01:41 +0200 Subject: [PATCH 02/22] Cherry-picked "the bash scripts are added" --- .../user_environment_setup.sh | 32 +++++++++---------- .../user_import_data_to_catalog.sh | 8 ++--- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/samples/interactive-tutorials/user_environment_setup.sh b/samples/interactive-tutorials/user_environment_setup.sh index 46915b6f..7cee8cd3 100644 --- a/samples/interactive-tutorials/user_environment_setup.sh +++ b/samples/interactive-tutorials/user_environment_setup.sh @@ -14,40 +14,40 @@ # See the License for the specific language governing permissions and # limitations under the License. -# set the Google Cloud Project ID +# set the Google Cloud project Id project_id=$1 -echo "Project ID: $project_id" -gcloud config set project "$project_id" +echo Project ID: $project_id +gcloud config set project project_id timestamp=$(date +%s) -service_account_id="service-acc-$timestamp" -echo "Service Account: $service_account_id" +service_account_id="service-acc-"$timestamp +echo Service Account: $service_account_id -# create service account (your service-acc-$timestamp) -gcloud iam service-accounts create "$service_account_id" +# create service account (your project_id+timestamp) +gcloud iam service-accounts create $service_account_id -# assign necessary roles to your new service account +# assign needed roles to your new service account for role in {retail.admin,editor,bigquery.admin} do - gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}" + gcloud projects add-iam-policy-binding $project_id --member="serviceAccount:"$service_account_id"@"$project_id".iam.gserviceaccount.com" --role="roles/${role}" done -echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account" -sleep 60 +echo Wait 70 seconds to be sure the appropriate roles have been assigned to your service account +sleep 70 # upload your service account key file -service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com" -gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email" +service_acc_email=$service_account_id"@"$project_id".iam.gserviceaccount.com" +gcloud iam service-accounts keys create ~/key.json --iam-account $service_acc_email # activate the service account using the key gcloud auth activate-service-account --key-file ~/key.json # install needed Google client libraries -cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit +cd ~/cloudshell_open/java-retail/samples/interactive-tutorials mvn clean install -DskipTests -echo "========================================" +echo ======================================== echo "The Google Cloud setup is completed." echo "Please proceed with the Tutorial steps" -echo "========================================" +echo ======================================== diff --git a/samples/interactive-tutorials/user_import_data_to_catalog.sh b/samples/interactive-tutorials/user_import_data_to_catalog.sh index 05c2747d..ac8a1a08 100644 --- a/samples/interactive-tutorials/user_import_data_to_catalog.sh +++ b/samples/interactive-tutorials/user_import_data_to_catalog.sh @@ -18,9 +18,9 @@ export GOOGLE_APPLICATION_CREDENTIALS=~/key.json # Change the working directory -cd ~/cloudshell_open/java-retail/samples/interactive-tutorials/ || exit +cd ~/cloudshell_open/java-retail/samples/interactive-tutorials/ -# Run the sample for creating the GCS bucket and extract the output of that execution +# Create a GCS bucket and upload the product data to the bucket output=$(mvn compile exec:java -Dexec.mainClass="product.setup.ProductsCreateGcsBucket") # Get the bucket name and store it in the env variable BUCKET_NAME @@ -31,6 +31,6 @@ export BUCKET_NAME=$bucket_name # Import products to the Retail catalog mvn compile exec:java -Dexec.mainClass="product.ImportProductsGcs" -echo "=====================================" +echo ===================================== echo "Your Retail catalog is ready to use!" -echo "=====================================" +echo ===================================== From 62f947db51f4e70913436b21d433735a7762d15d Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Fri, 1 Apr 2022 15:44:09 +0200 Subject: [PATCH 03/22] Cherry-picked "Updated README" --- samples/interactive-tutorials/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index 45264ca3..f1e79247 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -55,6 +55,8 @@ To prepare the work environment you should perform the following steps: - If you want to **speed up the process** of setting up the working environment, run the script java-retail/samples/interactive-tutorials/samples/interactive-tutorials/src/main/java/user_environment_setup.sh and skip the next **set up the work environment step-by-step** tutorial step: +1. Run the following command in Terminal: + ```bash bash java-retail/samples/interactive-tutorials/user_environment_setup.sh ``` From aa1ce1da8547d10b273e98b9ea5b785fc83dbe9c Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Fri, 1 Apr 2022 20:31:08 +0200 Subject: [PATCH 04/22] Update user_environment_setup.sh --- .../user_environment_setup.sh | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/samples/interactive-tutorials/user_environment_setup.sh b/samples/interactive-tutorials/user_environment_setup.sh index 7cee8cd3..46915b6f 100644 --- a/samples/interactive-tutorials/user_environment_setup.sh +++ b/samples/interactive-tutorials/user_environment_setup.sh @@ -14,40 +14,40 @@ # See the License for the specific language governing permissions and # limitations under the License. -# set the Google Cloud project Id +# set the Google Cloud Project ID project_id=$1 -echo Project ID: $project_id -gcloud config set project project_id +echo "Project ID: $project_id" +gcloud config set project "$project_id" timestamp=$(date +%s) -service_account_id="service-acc-"$timestamp -echo Service Account: $service_account_id +service_account_id="service-acc-$timestamp" +echo "Service Account: $service_account_id" -# create service account (your project_id+timestamp) -gcloud iam service-accounts create $service_account_id +# create service account (your service-acc-$timestamp) +gcloud iam service-accounts create "$service_account_id" -# assign needed roles to your new service account +# assign necessary roles to your new service account for role in {retail.admin,editor,bigquery.admin} do - gcloud projects add-iam-policy-binding $project_id --member="serviceAccount:"$service_account_id"@"$project_id".iam.gserviceaccount.com" --role="roles/${role}" + gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}" done -echo Wait 70 seconds to be sure the appropriate roles have been assigned to your service account -sleep 70 +echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account" +sleep 60 # upload your service account key file -service_acc_email=$service_account_id"@"$project_id".iam.gserviceaccount.com" -gcloud iam service-accounts keys create ~/key.json --iam-account $service_acc_email +service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com" +gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email" # activate the service account using the key gcloud auth activate-service-account --key-file ~/key.json # install needed Google client libraries -cd ~/cloudshell_open/java-retail/samples/interactive-tutorials +cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit mvn clean install -DskipTests -echo ======================================== +echo "========================================" echo "The Google Cloud setup is completed." echo "Please proceed with the Tutorial steps" -echo ======================================== +echo "========================================" From 5ec159a030d8412ce590cfd6a0b23190c2465456 Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Fri, 1 Apr 2022 20:33:58 +0200 Subject: [PATCH 05/22] Update user_import_data_to_catalog.sh --- .../interactive-tutorials/user_import_data_to_catalog.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/samples/interactive-tutorials/user_import_data_to_catalog.sh b/samples/interactive-tutorials/user_import_data_to_catalog.sh index ac8a1a08..05c2747d 100644 --- a/samples/interactive-tutorials/user_import_data_to_catalog.sh +++ b/samples/interactive-tutorials/user_import_data_to_catalog.sh @@ -18,9 +18,9 @@ export GOOGLE_APPLICATION_CREDENTIALS=~/key.json # Change the working directory -cd ~/cloudshell_open/java-retail/samples/interactive-tutorials/ +cd ~/cloudshell_open/java-retail/samples/interactive-tutorials/ || exit -# Create a GCS bucket and upload the product data to the bucket +# Run the sample for creating the GCS bucket and extract the output of that execution output=$(mvn compile exec:java -Dexec.mainClass="product.setup.ProductsCreateGcsBucket") # Get the bucket name and store it in the env variable BUCKET_NAME @@ -31,6 +31,6 @@ export BUCKET_NAME=$bucket_name # Import products to the Retail catalog mvn compile exec:java -Dexec.mainClass="product.ImportProductsGcs" -echo ===================================== +echo "=====================================" echo "Your Retail catalog is ready to use!" -echo ===================================== +echo "=====================================" From b308d9b75a240e44fec9822c45a69421ecde1dc3 Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Tue, 5 Apr 2022 14:38:24 +0200 Subject: [PATCH 06/22] Cherry-picked "Updated README" --- samples/interactive-tutorials/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index f1e79247..f52a271e 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -36,7 +36,6 @@ The Retail API provides you with the following possibilities to: You can find the information about the Retail services in the [documentation](https://cloud.google.com/retail/docs) - If you would like to have a closer look at the Retail API features and try them yourself, the best option is to use the [Interactive Tutorials](https://cloud.google.com/retail/docs/overview). In the documentation chapters find the "Guide me" button, the tutorials will be launched in the CloudShell environment, and you will be able to request the Retail services and check the response with minimum time and effort. From 2c90e8e5f7a8fe0e42e42d2291b2b1e9a274df8c Mon Sep 17 00:00:00 2001 From: Sergey Borisenko Date: Mon, 25 Apr 2022 15:55:56 +0300 Subject: [PATCH 07/22] Refactoring product package (Import). --- .../product/ImportProductsBigQueryTable.java | 47 +++++----- .../main/java/product/ImportProductsGcs.java | 61 ++++++++----- .../product/ImportProductsInlineSource.java | 79 ++++++++-------- .../setup/ProductsCreateBigqueryTable.java | 7 +- .../setup/ProductsCreateGcsBucket.java | 38 ++++---- .../setup/RemoveProductsResources.java | 76 ++++++++++++++++ .../ImportProductsBigQueryTableTest.java | 50 ++++++---- .../java/product/ImportProductsGcsTest.java | 53 +++++++---- .../ImportProductsInlineSourceTest.java | 11 +-- .../ProductsCreateBigqueryTableTest.java | 91 +++++++++++++++++++ .../setup/ProductsCreateGcsBucketTest.java | 68 ++++++++++++++ .../setup/RemoveProductsResourcesTest.java | 56 ++++++++++++ 12 files changed, 486 insertions(+), 151 deletions(-) create mode 100644 samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java create mode 100644 samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java create mode 100644 samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java create mode 100644 samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java index 291720c1..9fab1fcc 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java @@ -33,11 +33,11 @@ import com.google.longrunning.Operation; import com.google.longrunning.OperationsClient; import java.io.IOException; +import java.util.concurrent.TimeUnit; public class ImportProductsBigQueryTable { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( @@ -45,24 +45,18 @@ public static void main(String[] args) throws IOException, InterruptedException String datasetId = "products"; String tableId = "products"; // TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS: - // TABLE_ID = "products_some_invalid" - String dataSchema = "product"; - // TRY THE FULL RECONCILIATION MODE HERE: - ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL; + // tableId = "products_some_invalid" - ImportProductsRequest importBigQueryRequest = - getImportProductsBigQueryRequest( - reconciliationMode, projectId, datasetId, tableId, dataSchema, branchName); - waitForOperationCompletion(importBigQueryRequest); + importProductsFromBigQuery(projectId, branchName, datasetId, tableId); } - public static ImportProductsRequest getImportProductsBigQueryRequest( - ReconciliationMode reconciliationMode, - String projectId, - String datasetId, - String tableId, - String dataSchema, - String branchName) { + public static void importProductsFromBigQuery( + String projectId, String branchName, String datasetId, String tableId) + throws IOException, InterruptedException { + // TRY THE FULL RECONCILIATION MODE HERE: + ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL; + String dataSchema = "product"; + BigQuerySource bigQuerySource = BigQuerySource.newBuilder() .setProjectId(projectId) @@ -82,30 +76,31 @@ public static ImportProductsRequest getImportProductsBigQueryRequest( .build(); System.out.printf("Import products from big query table request: %s%n", importRequest); - return importRequest; - } - - public static void waitForOperationCompletion(ImportProductsRequest importRequest) - throws IOException, InterruptedException { + // Initialize client that will be used to send requests. This client only + // needs to be created once, and can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to + // safely clean up any remaining background resources. try (ProductServiceClient serviceClient = ProductServiceClient.create()) { String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); - System.out.printf("OperationName = %s\n", operationName); + System.out.printf("OperationName = %s%n", operationName); OperationsClient operationsClient = serviceClient.getOperationsClient(); Operation operation = operationsClient.getOperation(operationName); - while (!operation.getDone()) { + long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay + + while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) { // Keep polling the operation periodically until the import task is done. - Thread.sleep(30_000); + TimeUnit.SECONDS.sleep(30); operation = operationsClient.getOperation(operationName); } if (operation.hasMetadata()) { ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); System.out.printf( - "Number of successfully imported products: %s\n", metadata.getSuccessCount()); + "Number of successfully imported products: %s%n", metadata.getSuccessCount()); System.out.printf( - "Number of failures during the importing: %s\n", metadata.getFailureCount()); + "Number of failures during the importing: %s%n", metadata.getFailureCount()); } if (operation.hasResponse()) { diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java index f2ce4e18..1d498a4b 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java @@ -22,6 +22,8 @@ package product; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.api.gax.rpc.PermissionDeniedException; import com.google.cloud.ServiceOptions; import com.google.cloud.retail.v2.GcsSource; import com.google.cloud.retail.v2.ImportErrorsConfig; @@ -35,32 +37,33 @@ import com.google.longrunning.OperationsClient; import java.io.IOException; import java.util.Collections; +import java.util.concurrent.TimeUnit; public class ImportProductsGcs { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String gcsBucket = String.format("gs://%s", System.getenv("BUCKET_NAME")); - String gcsErrorBucket = String.format("%s/errors", gcsBucket); + String bucketName = System.getenv("BUCKET_NAME"); + String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT - // GCS_PRODUCTS_OBJECT = "products_some_invalid.json" + // gscProductsObject = "products_some_invalid.json" - ImportProductsRequest importGcsRequest = - getImportProductsGcsRequest(gscProductsObject, gcsBucket, gcsErrorBucket, branchName); - waitForOperationCompletion(importGcsRequest); + importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); } - public static ImportProductsRequest getImportProductsGcsRequest( - String gcsObjectName, String gcsBucket, String gcsErrorBucket, String branchName) { + public static void importProductsFromGcs( + String branchName, String bucketName, String gcsBucket, String gscProductsObject) + throws IOException, InterruptedException { + String gcsErrorBucket = String.format("%s/errors", gcsBucket); + GcsSource gcsSource = GcsSource.newBuilder() .addAllInputUris( - Collections.singleton(String.format("%s/%s", gcsBucket, gcsObjectName))) + Collections.singleton(String.format("%s/%s", gcsBucket, gscProductsObject))) .build(); ProductInputConfig inputConfig = @@ -79,39 +82,53 @@ public static ImportProductsRequest getImportProductsGcsRequest( .setErrorsConfig(errorsConfig) .build(); - System.out.println("Import products from google cloud source request: " + importRequest); + System.out.printf("Import products from google cloud source request: %s%n", importRequest); - return importRequest; - } - - public static void waitForOperationCompletion(ImportProductsRequest importRequest) - throws IOException, InterruptedException { + // Initialize client that will be used to send requests. This client only + // needs to be created once, and can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to + // safely clean up any remaining background resources. try (ProductServiceClient serviceClient = ProductServiceClient.create()) { String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); - System.out.printf("OperationName = %s\n", operationName); + + System.out.println("The operation was started."); + System.out.printf("OperationName = %s%n", operationName); OperationsClient operationsClient = serviceClient.getOperationsClient(); Operation operation = operationsClient.getOperation(operationName); - while (!operation.getDone()) { - // Keep polling the operation periodically until the import task is done. - Thread.sleep(30_000); + long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay + + while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) { + System.out.println("Please wait till operation is done."); + TimeUnit.SECONDS.sleep(30); operation = operationsClient.getOperation(operationName); } if (operation.hasMetadata()) { ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); System.out.printf( - "Number of successfully imported products: %s\n", metadata.getSuccessCount()); + "Number of successfully imported products: %s%n", metadata.getSuccessCount()); System.out.printf( - "Number of failures during the importing: %s\n", metadata.getFailureCount()); + "Number of failures during the importing: %s%n", metadata.getFailureCount()); + } else { + System.out.println("Metadata is empty."); } if (operation.hasResponse()) { ImportProductsResponse response = operation.getResponse().unpack(ImportProductsResponse.class); System.out.printf("Operation result: %s%n", response); + } else { + System.out.println("Operation result is empty."); } + } catch (InvalidArgumentException e) { + System.out.printf( + "%s%n'%s' file does not exist in the bucket. Please " + + "make sure you have followed the setting up instructions.", + e.getMessage(), gscProductsObject); + } catch (PermissionDeniedException e) { + System.out.println(e.getMessage()); } } } diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java index 3f8b2639..bf0cbf94 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java @@ -22,6 +22,7 @@ package product; +import com.google.api.gax.rpc.InvalidArgumentException; import com.google.cloud.ServiceOptions; import com.google.cloud.retail.v2.ColorInfo; import com.google.cloud.retail.v2.FulfillmentInfo; @@ -42,24 +43,23 @@ import java.util.Collections; import java.util.List; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class ImportProductsInlineSource { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts(), branchName); - waitForOperationCompletion(importRequest); + importProductsInlineSource(branchName); } - public static ImportProductsRequest getImportProductsInlineRequest( - List productsToImport, String branchName) { + public static void importProductsInlineSource(String branchName) + throws IOException, InterruptedException { ProductInlineSource inlineSource = - ProductInlineSource.newBuilder().addAllProducts(productsToImport).build(); + ProductInlineSource.newBuilder().addAllProducts(getProducts()).build(); ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setProductInlineSource(inlineSource).build(); @@ -72,7 +72,41 @@ public static ImportProductsRequest getImportProductsInlineRequest( System.out.printf("Import products from inline source request: %s%n", importRequest); - return importRequest; + // Initialize client that will be used to send requests. This client only + // needs to be created once, and can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to + // safely clean up any remaining background resources. + try (ProductServiceClient serviceClient = ProductServiceClient.create()) { + String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); + System.out.printf("OperationName = %s%n", operationName); + + OperationsClient operationsClient = serviceClient.getOperationsClient(); + Operation operation = operationsClient.getOperation(operationName); + + long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay + + while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) { + // Keep polling the operation periodically until the import task is done. + TimeUnit.SECONDS.sleep(30); + operation = operationsClient.getOperation(operationName); + } + + if (operation.hasMetadata()) { + ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); + System.out.printf( + "Number of successfully imported products: %s%n", metadata.getSuccessCount()); + System.out.printf( + "Number of failures during the importing: %s%n", metadata.getFailureCount()); + } + + if (operation.hasResponse()) { + ImportProductsResponse response = + operation.getResponse().unpack(ImportProductsResponse.class); + System.out.printf("Operation result: %s%n", response); + } + } catch (InvalidArgumentException e) { + System.out.println(e.getMessage()); + } } public static List getProducts() { @@ -175,37 +209,6 @@ public static List getProducts() { return products; } - - public static void waitForOperationCompletion(ImportProductsRequest importRequest) - throws IOException, InterruptedException { - try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); - System.out.printf("OperationName = %s\n", operationName); - - OperationsClient operationsClient = serviceClient.getOperationsClient(); - Operation operation = operationsClient.getOperation(operationName); - - while (!operation.getDone()) { - // Keep polling the operation periodically until the import task is done. - Thread.sleep(30_000); - operation = operationsClient.getOperation(operationName); - } - - if (operation.hasMetadata()) { - ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); - System.out.printf( - "Number of successfully imported products: %s\n", metadata.getSuccessCount()); - System.out.printf( - "Number of failures during the importing: %s\n", metadata.getFailureCount()); - } - - if (operation.hasResponse()) { - ImportProductsResponse response = - operation.getResponse().unpack(ImportProductsResponse.class); - System.out.printf("Operation result: %s%n", response); - } - } - } } // [END retail_import_products_from_inline_source] diff --git a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java index 361144a8..f5244b78 100644 --- a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java +++ b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java @@ -30,16 +30,15 @@ public class ProductsCreateBigqueryTable { - public static void main(String... args) throws IOException { + public static void main(String[] args) throws IOException { String dataset = "products"; String validProductsTable = "products"; String invalidProductsTable = "products_some_invalid"; String productSchemaFilePath = "src/main/resources/product_schema.json"; String validProductsSourceFile = - String.format("gs://%s/products.json", ProductsCreateGcsBucket.getBucketName()); + String.format("gs://%s/products.json", System.getenv("BUCKET_NAME")); String invalidProductsSourceFile = - String.format( - "gs://%s/products_some_invalid.json", ProductsCreateGcsBucket.getBucketName()); + String.format("gs://%s/products_some_invalid.json", System.getenv("BUCKET_NAME")); BufferedReader bufferedReader = new BufferedReader(new FileReader(productSchemaFilePath)); String jsonToString = bufferedReader.lines().collect(Collectors.joining()); diff --git a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java index 1e5caad4..3e71a816 100644 --- a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java +++ b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java @@ -26,31 +26,31 @@ public class ProductsCreateGcsBucket { - private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); + public static void main(String[] args) throws IOException { + String projectId = ServiceOptions.getDefaultProjectId(); - private static final Timestamp CURRENT_DATE = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); + Timestamp currentDate = + Timestamp.newBuilder() + .setSeconds(Instant.now().getEpochSecond()) + .setNanos(Instant.now().getNano()) + .build(); - private static final String BUCKET_NAME = - String.format("%s_products_%s", PROJECT_ID, CURRENT_DATE.getSeconds()); + String productsBucketName = + String.format("%s_products_%s", projectId, currentDate.getSeconds()); - public static void main(String... args) throws IOException { - createBucket(BUCKET_NAME); - System.out.printf("Products gcs bucket %s was created.", BUCKET_NAME); + createGcsBucketAndUploadData(productsBucketName); + } + + public static void createGcsBucketAndUploadData(String bucketName) throws IOException { + createBucket(bucketName); + System.out.printf("Products gcs bucket %s was created.%n", bucketName); - uploadObject(BUCKET_NAME, "products.json", "src/main/resources/products.json"); - System.out.printf("File 'products.json' was uploaded into bucket '%s'.", BUCKET_NAME); + uploadObject(bucketName, "products.json", "src/main/resources/products.json"); + System.out.printf("File 'products.json' was uploaded into bucket '%s'.%n", bucketName); uploadObject( - BUCKET_NAME, "products_some_invalid.json", "src/main/resources/products_some_invalid.json"); + bucketName, "products_some_invalid.json", "src/main/resources/products_some_invalid.json"); System.out.printf( - "File 'products_some_invalid.json' was uploaded into bucket '%s'.", BUCKET_NAME); - } - - public static String getBucketName() { - return BUCKET_NAME; + "File 'products_some_invalid.json' was uploaded into bucket '%s'.%n", bucketName); } } diff --git a/samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java b/samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java new file mode 100644 index 00000000..de7aea24 --- /dev/null +++ b/samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java @@ -0,0 +1,76 @@ +/* + * Copyright 2022 Google LLC + * + * 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 product.setup; + +import static setup.SetupCleanup.deleteBucket; +import static setup.SetupCleanup.deleteDataset; + +import com.google.api.gax.rpc.PermissionDeniedException; +import com.google.cloud.ServiceOptions; +import com.google.cloud.retail.v2.DeleteProductRequest; +import com.google.cloud.retail.v2.ListProductsRequest; +import com.google.cloud.retail.v2.Product; +import com.google.cloud.retail.v2.ProductServiceClient; +import com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse; +import java.io.IOException; + +public class RemoveProductsResources { + + public static void main(String[] args) throws IOException { + String projectId = ServiceOptions.getDefaultProjectId(); + String bucketName = System.getenv("BUCKET_NAME"); + String branchName = + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + + deleteBucket(bucketName); + deleteAllProducts(branchName); + deleteDataset(projectId, "products"); + } + + public static void deleteAllProducts(String branchName) throws IOException { + System.out.println("Deleting products in process, please wait..."); + + // Initialize client that will be used to send requests. This client only + // needs to be created once, and can be reused for multiple requests. After + // completing all of your requests, call the "close" method on the client to + // safely clean up any remaining background resources. + try (ProductServiceClient productServiceClient = ProductServiceClient.create()) { + ListProductsRequest listRequest = + ListProductsRequest.newBuilder().setParent(branchName).build(); + ListProductsPagedResponse products = productServiceClient.listProducts(listRequest); + + int deleteCount = 0; + + for (Product product : products.iterateAll()) { + DeleteProductRequest deleteRequest = + DeleteProductRequest.newBuilder().setName(product.getName()).build(); + + try { + productServiceClient.deleteProduct(deleteRequest); + deleteCount++; + } catch (PermissionDeniedException e) { + System.out.println( + "Ignore PermissionDenied in case the product does not exist " + + "at time of deletion."); + } + } + + System.out.printf("%s products were deleted from %s%n", deleteCount, branchName); + } + } +} diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsBigQueryTableTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsBigQueryTableTest.java index 58191f37..fce668a3 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsBigQueryTableTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsBigQueryTableTest.java @@ -17,12 +17,9 @@ package product; import static com.google.common.truth.Truth.assertThat; -import static product.ImportProductsBigQueryTable.getImportProductsBigQueryRequest; -import static product.ImportProductsBigQueryTable.waitForOperationCompletion; +import static product.ImportProductsBigQueryTable.importProductsFromBigQuery; import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.ImportProductsRequest; -import com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -30,8 +27,10 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import product.setup.ProductsCreateBigqueryTable; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +@RunWith(JUnit4.class) public class ImportProductsBigQueryTableTest { private ByteArrayOutputStream bout; @@ -39,33 +38,46 @@ public class ImportProductsBigQueryTableTest { @Before public void setUp() throws IOException, InterruptedException, ExecutionException { + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @Test + public void testValidImportProductsBigQueryTable() throws IOException, InterruptedException { String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); String datasetId = "products"; String tableId = "products"; - String dataSchema = "product"; - ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL; - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - ProductsCreateBigqueryTable.main(); - ImportProductsRequest importBigQueryRequest = - getImportProductsBigQueryRequest( - reconciliationMode, projectId, datasetId, tableId, dataSchema, branchName); - waitForOperationCompletion(importBigQueryRequest); + importProductsFromBigQuery(projectId, branchName, datasetId, tableId); + + String outputResult = bout.toString(); + + assertThat(outputResult).contains("Import products from big query table request"); + assertThat(outputResult).contains("Number of successfully imported products:"); + assertThat(outputResult).contains("Number of failures during the importing: 0"); } @Test - public void testImportProductsBigQueryTable() { + public void testInvalidImportProductsBigQueryTable() throws IOException, InterruptedException { + String projectId = ServiceOptions.getDefaultProjectId(); + String branchName = + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + String datasetId = "products"; + String tableId = "products_some_invalid"; + + importProductsFromBigQuery(projectId, branchName, datasetId, tableId); + String outputResult = bout.toString(); assertThat(outputResult).contains("Import products from big query table request"); - assertThat(outputResult).contains("Number of successfully imported products"); - assertThat(outputResult).contains("Number of failures during the importing"); + assertThat(outputResult).contains("Number of successfully imported products:"); + assertThat(outputResult).contains("Number of failures during the importing:"); } @After diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index 7ae805aa..c22852b1 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -17,11 +17,10 @@ package product; import static com.google.common.truth.Truth.assertThat; -import static product.ImportProductsGcs.getImportProductsGcsRequest; -import static product.ImportProductsGcs.waitForOperationCompletion; +import static product.ImportProductsGcs.importProductsFromGcs; +import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.ImportProductsRequest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -29,8 +28,10 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import product.setup.ProductsCreateGcsBucket; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +@RunWith(JUnit4.class) public class ImportProductsGcsTest { private ByteArrayOutputStream bout; @@ -38,32 +39,50 @@ public class ImportProductsGcsTest { @Before public void setUp() throws IOException, InterruptedException, ExecutionException { - ProductsCreateGcsBucket.main(); + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + @Test + public void testValidImportProductsGcs() throws IOException, InterruptedException { String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String gcsBucket = String.format("gs://%s", ProductsCreateGcsBucket.getBucketName()); - String gcsErrorBucket = String.format("%s/errors", gcsBucket); + String bucketName = "products_tests_bucket"; + String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - ImportProductsRequest importGcsRequest = - getImportProductsGcsRequest(gscProductsObject, gcsBucket, gcsErrorBucket, branchName); - waitForOperationCompletion(importGcsRequest); + createGcsBucketAndUploadData(bucketName); + importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); + + String outputResult = bout.toString(); + + assertThat(outputResult).contains("Import products from google cloud source request"); + assertThat(outputResult).contains("Number of successfully imported products:"); + assertThat(outputResult).contains("Number of failures during the importing: 0"); } @Test - public void testImportProductsGcs() { + public void testInvalidImportProductsGcs() throws IOException, InterruptedException { + String projectId = ServiceOptions.getDefaultProjectId(); + String branchName = + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + String bucketName = "products_tests_bucket"; + String gcsBucket = String.format("gs://%s", bucketName); + String gscProductsObject = "products_some_invalid.json"; + + createGcsBucketAndUploadData(bucketName); + importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); + String outputResult = bout.toString(); assertThat(outputResult).contains("Import products from google cloud source request"); - assertThat(outputResult).contains("Number of successfully imported products"); - assertThat(outputResult).contains("Number of failures during the importing"); + assertThat(outputResult).contains("Number of successfully imported products:"); + assertThat(outputResult).contains("Number of failures during the importing:"); } @After diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsInlineSourceTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsInlineSourceTest.java index eb4678a9..b5a424dc 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsInlineSourceTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsInlineSourceTest.java @@ -17,12 +17,9 @@ package product; import static com.google.common.truth.Truth.assertThat; -import static product.ImportProductsInlineSource.getImportProductsInlineRequest; -import static product.ImportProductsInlineSource.getProducts; -import static product.ImportProductsInlineSource.waitForOperationCompletion; +import static product.ImportProductsInlineSource.importProductsInlineSource; import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.ImportProductsRequest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; @@ -30,7 +27,10 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +@RunWith(JUnit4.class) public class ImportProductsInlineSourceTest { private ByteArrayOutputStream bout; @@ -47,8 +47,7 @@ public void setUp() throws IOException, InterruptedException, ExecutionException originalPrintStream = System.out; System.setOut(out); - ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts(), branchName); - waitForOperationCompletion(importRequest); + importProductsInlineSource(branchName); } @Test diff --git a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java new file mode 100644 index 00000000..176b6e80 --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java @@ -0,0 +1,91 @@ +/* + * Copyright 2022 Google LLC + * + * 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 product.setup; + +import static com.google.common.truth.Truth.assertThat; +import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; +import static setup.SetupCleanup.createBqDataset; +import static setup.SetupCleanup.createBqTable; +import static setup.SetupCleanup.getGson; +import static setup.SetupCleanup.uploadDataToBqTable; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ProductsCreateBigqueryTableTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + String dataset = "products"; + String validProductsTable = "products"; + String invalidProductsTable = "products_some_invalid"; + String bucketName = "products_tests_bucket"; + String productSchemaFilePath = "src/main/resources/product_schema.json"; + String validProductsSourceFile = String.format("gs://%s/products.json", bucketName); + String invalidProductsSourceFile = + String.format("gs://%s/products_some_invalid.json", bucketName); + + BufferedReader bufferedReader = new BufferedReader(new FileReader(productSchemaFilePath)); + String jsonToString = bufferedReader.lines().collect(Collectors.joining()); + jsonToString = jsonToString.replace("\"fields\"", "\"subFields\""); + Field[] fields = getGson().fromJson(jsonToString, Field[].class); + Schema productSchema = Schema.of(fields); + + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + createGcsBucketAndUploadData(bucketName); + createBqDataset(dataset); + createBqTable(dataset, validProductsTable, productSchema); + uploadDataToBqTable(dataset, validProductsTable, validProductsSourceFile, productSchema); + createBqTable(dataset, invalidProductsTable, productSchema); + uploadDataToBqTable(dataset, invalidProductsTable, invalidProductsSourceFile, productSchema); + } + + @Test + public void testProductsCreateBigQueryTable() { + String outputResult = bout.toString(); + + assertThat(outputResult).contains("Json from GCS successfully loaded in a table 'products'."); + assertThat(outputResult) + .contains("Json from GCS successfully loaded in a table 'products_some_invalid'."); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} diff --git a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java new file mode 100644 index 00000000..bcb473ac --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2022 Google LLC + * + * 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 product.setup; + +import static com.google.common.truth.Truth.assertThat; +import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ProductsCreateGcsBucketTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + + String bucketName = "products_tests_bucket"; + + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + createGcsBucketAndUploadData(bucketName); + } + + @Test + public void testProductsCreateGcsBucket() { + String outputResult = bout.toString(); + + assertThat(outputResult).contains("Products gcs bucket products_tests_bucket was created."); + assertThat(outputResult) + .contains("File 'products.json' was uploaded into bucket 'products_tests_bucket'."); + assertThat(outputResult) + .contains( + "File 'products_some_invalid.json' was uploaded into bucket 'products_tests_bucket'."); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} diff --git a/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java b/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java new file mode 100644 index 00000000..23249e3e --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java @@ -0,0 +1,56 @@ +package product.setup; + +import static com.google.common.truth.Truth.assertThat; +import static product.setup.RemoveProductsResources.deleteAllProducts; +import static setup.SetupCleanup.deleteBucket; +import static setup.SetupCleanup.deleteDataset; + +import com.google.cloud.ServiceOptions; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class RemoveProductsResourcesTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + String projectId = ServiceOptions.getDefaultProjectId(); + String bucketName = "products_tests_bucket"; + String branchName = + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + deleteBucket(bucketName); + deleteAllProducts(branchName); + deleteDataset(projectId, "products"); + } + + @Test + public void testRemoveProductsResources() { + String outputResult = bout.toString(); + + assertThat(outputResult).contains("Deleting products in process, please wait..."); + assertThat(outputResult).contains("products were deleted from"); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} From f0f490d5edaa2c3da923bf79f8f149274c5e6e2c Mon Sep 17 00:00:00 2001 From: Sergey Borisenko Date: Mon, 25 Apr 2022 15:57:29 +0300 Subject: [PATCH 08/22] Remove init package. --- .../main/java/init/CreateTestResources.java | 110 ------------------ .../main/java/init/RemoveTestResources.java | 74 ------------ .../java/init/TEST_RESOURCES_SETUP_CLEANUP.md | 49 -------- 3 files changed, 233 deletions(-) delete mode 100644 samples/interactive-tutorials/src/main/java/init/CreateTestResources.java delete mode 100644 samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java delete mode 100644 samples/interactive-tutorials/src/main/java/init/TEST_RESOURCES_SETUP_CLEANUP.md diff --git a/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java b/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java deleted file mode 100644 index 5a0f6cc6..00000000 --- a/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * 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 init; - -import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.GcsSource; -import com.google.cloud.retail.v2.ImportErrorsConfig; -import com.google.cloud.retail.v2.ImportMetadata; -import com.google.cloud.retail.v2.ImportProductsRequest; -import com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode; -import com.google.cloud.retail.v2.ImportProductsResponse; -import com.google.cloud.retail.v2.ProductInputConfig; -import com.google.cloud.retail.v2.ProductServiceClient; -import com.google.longrunning.Operation; -import com.google.longrunning.OperationsClient; -import events.setup.EventsCreateBigQueryTable; -import events.setup.EventsCreateGcsBucket; -import java.io.IOException; -import java.util.Collections; -import product.setup.ProductsCreateBigqueryTable; -import product.setup.ProductsCreateGcsBucket; - -public class CreateTestResources { - - public static void main(String... args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. - String projectId = ServiceOptions.getDefaultProjectId(); - String bucketName = System.getenv("BUCKET_NAME"); - String gcsBucket = String.format("gs://%s", System.getenv("BUCKET_NAME")); - String gcsErrorBucket = String.format("%s/errors", gcsBucket); - String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - - ProductsCreateGcsBucket.main(); - EventsCreateGcsBucket.main(); - importProductsFromGcs(bucketName, gcsErrorBucket, branchName); - ProductsCreateBigqueryTable.main(); - EventsCreateBigQueryTable.main(); - } - - public static void importProductsFromGcs( - String bucketName, String gcsErrorBucket, String branchName) - throws IOException, InterruptedException { - GcsSource gcsSource = - GcsSource.newBuilder() - .addAllInputUris( - Collections.singleton(String.format("gs://%s/%s", bucketName, "products.json"))) - .build(); - ProductInputConfig inputConfig = - ProductInputConfig.newBuilder().setGcsSource(gcsSource).build(); - System.out.println("GRS source: " + gcsSource.getInputUrisList()); - - ImportErrorsConfig errorsConfig = - ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorBucket).build(); - ImportProductsRequest importRequest = - ImportProductsRequest.newBuilder() - .setParent(branchName) - .setReconciliationMode(ReconciliationMode.INCREMENTAL) - .setInputConfig(inputConfig) - .setErrorsConfig(errorsConfig) - .build(); - System.out.println("Import products from google cloud source request: " + importRequest); - - try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); - System.out.printf("OperationName = %s\n", operationName); - - OperationsClient operationsClient = serviceClient.getOperationsClient(); - Operation operation = operationsClient.getOperation(operationName); - - while (!operation.getDone()) { - System.out.println("Please wait till operation is completed."); - // Keep polling the operation periodically until the import task is done. - Thread.sleep(30_000); - operation = operationsClient.getOperation(operationName); - } - - System.out.println("Import products operation is completed."); - - if (operation.hasMetadata()) { - ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); - System.out.printf( - "Number of successfully imported products: %s\n", metadata.getSuccessCount()); - System.out.printf( - "Number of failures during the importing: %s\n", metadata.getFailureCount()); - } - - if (operation.hasResponse()) { - ImportProductsResponse response = - operation.getResponse().unpack(ImportProductsResponse.class); - System.out.printf("Operation result: %s", response); - } - } - } -} diff --git a/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java b/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java deleted file mode 100644 index c48ceff2..00000000 --- a/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * 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 init; - -import static setup.SetupCleanup.deleteBucket; -import static setup.SetupCleanup.deleteDataset; - -import com.google.api.gax.rpc.PermissionDeniedException; -import com.google.cloud.ServiceOptions; -import com.google.cloud.retail.v2.DeleteProductRequest; -import com.google.cloud.retail.v2.ListProductsRequest; -import com.google.cloud.retail.v2.Product; -import com.google.cloud.retail.v2.ProductServiceClient; -import com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse; -import java.io.IOException; - -public class RemoveTestResources { - - public static void main(String... args) throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = ServiceOptions.getDefaultProjectId(); - String bucketName = System.getenv("BUCKET_NAME"); - String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - - deleteBucket(bucketName); - deleteAllProducts(branchName); - deleteDataset(projectId, "products"); - deleteDataset(projectId, "user_events"); - } - - public static void deleteAllProducts(String branchName) throws IOException { - System.out.println("Deleting products in process, please wait..."); - - try (ProductServiceClient productServiceClient = ProductServiceClient.create()) { - ListProductsRequest listRequest = - ListProductsRequest.newBuilder().setParent(branchName).build(); - ListProductsPagedResponse products = productServiceClient.listProducts(listRequest); - - int deleteCount = 0; - - for (Product product : products.iterateAll()) { - DeleteProductRequest deleteRequest = - DeleteProductRequest.newBuilder().setName(product.getName()).build(); - - try { - productServiceClient.deleteProduct(deleteRequest); - deleteCount++; - } catch (PermissionDeniedException e) { - System.out.println( - "Ignore PermissionDenied in case the product does not exist " - + "at time of deletion."); - } - } - - System.out.printf("%s products were deleted from %s%n", deleteCount, branchName); - } - } -} diff --git a/samples/interactive-tutorials/src/main/java/init/TEST_RESOURCES_SETUP_CLEANUP.md b/samples/interactive-tutorials/src/main/java/init/TEST_RESOURCES_SETUP_CLEANUP.md deleted file mode 100644 index 0b9df6d1..00000000 --- a/samples/interactive-tutorials/src/main/java/init/TEST_RESOURCES_SETUP_CLEANUP.md +++ /dev/null @@ -1,49 +0,0 @@ -# How to set up/ tear down the test resources - -## Required environment variables - -To successfully import the catalog data for tests, the following environment variables should be -set: - -- PROJECT_ID -- PROJECT_NUMBER -- BUCKET_NAME - -The Secret Manager name is set in .kokoro/presubmit/common.cfg file, SECRET_MANAGER_KEYS variable. - -## Import catalog data - -There is a JSON file with valid products prepared in the `product` directory: -`resources/products.json`. - -Run the `CreateTestResources` to perform the following actions: - -- create the GCS bucket ; -- upload the product data from `resources/products.json` file to the bucket; -- import products to the default branch of the Retail catalog; -- upload the product data from `resources/user_events.json` file to the bucket; -- create a BigQuery dataset and table `products`; -- insert products from resources/products.json to the created products table; -- create a BigQuery dataset and table `events`; -- insert user events from resources/user_events.json to the created events table; - -``` -mvn compile exec:java -Dexec.mainClass="init.CreateTestResources" -``` - -In the result 316 products should be created in the test project catalog. - -## Remove catalog data - -Run the `RemoveTestResources` to perform the following actions: - -- remove all objects from the GCS bucket ; -- remove the bucket; -- delete all products from the Retail catalog; -- remove all objects from the GCS bucket ; -- remove dataset `products` along with tables; -- remove dataset `user_events` along with tables; - -``` -mvn compile exec:java -Dexec.mainClass="init.RemoveTestResources" -``` \ No newline at end of file From fcb241170c7a1216f5be50271bdda501f89209b0 Mon Sep 17 00:00:00 2001 From: dfirova <93149631+dfirova@users.noreply.github.com> Date: Fri, 1 Jul 2022 11:27:17 +0300 Subject: [PATCH 09/22] Update README --- samples/interactive-tutorials/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/interactive-tutorials/README.md b/samples/interactive-tutorials/README.md index f52a271e..c2957c23 100644 --- a/samples/interactive-tutorials/README.md +++ b/samples/interactive-tutorials/README.md @@ -54,8 +54,6 @@ To prepare the work environment you should perform the following steps: - If you want to **speed up the process** of setting up the working environment, run the script java-retail/samples/interactive-tutorials/samples/interactive-tutorials/src/main/java/user_environment_setup.sh and skip the next **set up the work environment step-by-step** tutorial step: -1. Run the following command in Terminal: - ```bash bash java-retail/samples/interactive-tutorials/user_environment_setup.sh ``` From eba63578a87190434964e5db5f82d77803c37b6b Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Thu, 14 Jul 2022 09:49:56 +0300 Subject: [PATCH 10/22] Removed assuredBreak. --- .../src/main/java/product/ImportProductsBigQueryTable.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java index 9fab1fcc..38935f9d 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java @@ -87,9 +87,7 @@ public static void importProductsFromBigQuery( OperationsClient operationsClient = serviceClient.getOperationsClient(); Operation operation = operationsClient.getOperation(operationName); - long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay - - while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) { + while (!operation.getDone()) { // Keep polling the operation periodically until the import task is done. TimeUnit.SECONDS.sleep(30); operation = operationsClient.getOperation(operationName); From c2156290c8affb2b04d1e9d99828377996e952e3 Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Mon, 18 Jul 2022 09:15:49 +0300 Subject: [PATCH 11/22] PR fix: removed tests for setup/cleanup. --- .../ProductsCreateBigqueryTableTest.java | 91 ------------------- .../setup/ProductsCreateGcsBucketTest.java | 68 -------------- .../setup/RemoveProductsResourcesTest.java | 56 ------------ 3 files changed, 215 deletions(-) delete mode 100644 samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java delete mode 100644 samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java delete mode 100644 samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java diff --git a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java deleted file mode 100644 index 176b6e80..00000000 --- a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateBigqueryTableTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * 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 product.setup; - -import static com.google.common.truth.Truth.assertThat; -import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; -import static setup.SetupCleanup.createBqDataset; -import static setup.SetupCleanup.createBqTable; -import static setup.SetupCleanup.getGson; -import static setup.SetupCleanup.uploadDataToBqTable; - -import com.google.cloud.bigquery.Field; -import com.google.cloud.bigquery.Schema; -import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; -import java.io.FileReader; -import java.io.IOException; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -import java.util.stream.Collectors; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ProductsCreateBigqueryTableTest { - - private ByteArrayOutputStream bout; - private PrintStream originalPrintStream; - - @Before - public void setUp() throws IOException, InterruptedException, ExecutionException { - String dataset = "products"; - String validProductsTable = "products"; - String invalidProductsTable = "products_some_invalid"; - String bucketName = "products_tests_bucket"; - String productSchemaFilePath = "src/main/resources/product_schema.json"; - String validProductsSourceFile = String.format("gs://%s/products.json", bucketName); - String invalidProductsSourceFile = - String.format("gs://%s/products_some_invalid.json", bucketName); - - BufferedReader bufferedReader = new BufferedReader(new FileReader(productSchemaFilePath)); - String jsonToString = bufferedReader.lines().collect(Collectors.joining()); - jsonToString = jsonToString.replace("\"fields\"", "\"subFields\""); - Field[] fields = getGson().fromJson(jsonToString, Field[].class); - Schema productSchema = Schema.of(fields); - - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - - createGcsBucketAndUploadData(bucketName); - createBqDataset(dataset); - createBqTable(dataset, validProductsTable, productSchema); - uploadDataToBqTable(dataset, validProductsTable, validProductsSourceFile, productSchema); - createBqTable(dataset, invalidProductsTable, productSchema); - uploadDataToBqTable(dataset, invalidProductsTable, invalidProductsSourceFile, productSchema); - } - - @Test - public void testProductsCreateBigQueryTable() { - String outputResult = bout.toString(); - - assertThat(outputResult).contains("Json from GCS successfully loaded in a table 'products'."); - assertThat(outputResult) - .contains("Json from GCS successfully loaded in a table 'products_some_invalid'."); - } - - @After - public void tearDown() { - System.out.flush(); - System.setOut(originalPrintStream); - } -} diff --git a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java b/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java deleted file mode 100644 index bcb473ac..00000000 --- a/samples/interactive-tutorials/src/test/java/product/setup/ProductsCreateGcsBucketTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2022 Google LLC - * - * 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 product.setup; - -import static com.google.common.truth.Truth.assertThat; -import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ProductsCreateGcsBucketTest { - - private ByteArrayOutputStream bout; - private PrintStream originalPrintStream; - - @Before - public void setUp() throws IOException, InterruptedException, ExecutionException { - - String bucketName = "products_tests_bucket"; - - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - - createGcsBucketAndUploadData(bucketName); - } - - @Test - public void testProductsCreateGcsBucket() { - String outputResult = bout.toString(); - - assertThat(outputResult).contains("Products gcs bucket products_tests_bucket was created."); - assertThat(outputResult) - .contains("File 'products.json' was uploaded into bucket 'products_tests_bucket'."); - assertThat(outputResult) - .contains( - "File 'products_some_invalid.json' was uploaded into bucket 'products_tests_bucket'."); - } - - @After - public void tearDown() { - System.out.flush(); - System.setOut(originalPrintStream); - } -} diff --git a/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java b/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java deleted file mode 100644 index 23249e3e..00000000 --- a/samples/interactive-tutorials/src/test/java/product/setup/RemoveProductsResourcesTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package product.setup; - -import static com.google.common.truth.Truth.assertThat; -import static product.setup.RemoveProductsResources.deleteAllProducts; -import static setup.SetupCleanup.deleteBucket; -import static setup.SetupCleanup.deleteDataset; - -import com.google.cloud.ServiceOptions; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class RemoveProductsResourcesTest { - - private ByteArrayOutputStream bout; - private PrintStream originalPrintStream; - - @Before - public void setUp() throws IOException, InterruptedException, ExecutionException { - String projectId = ServiceOptions.getDefaultProjectId(); - String bucketName = "products_tests_bucket"; - String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - - deleteBucket(bucketName); - deleteAllProducts(branchName); - deleteDataset(projectId, "products"); - } - - @Test - public void testRemoveProductsResources() { - String outputResult = bout.toString(); - - assertThat(outputResult).contains("Deleting products in process, please wait..."); - assertThat(outputResult).contains("products were deleted from"); - } - - @After - public void tearDown() { - System.out.flush(); - System.setOut(originalPrintStream); - } -} From f6ad617f685b523dbe30dc3363bd27a8f9b59e3a Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Wed, 20 Jul 2022 08:32:36 +0300 Subject: [PATCH 12/22] PR fix: fixed the deadline. --- .../src/main/java/product/ImportProductsGcs.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java index 1d498a4b..8cf36daf 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java @@ -36,6 +36,7 @@ import com.google.longrunning.Operation; import com.google.longrunning.OperationsClient; import java.io.IOException; +import java.time.Instant; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -97,9 +98,9 @@ public static void importProductsFromGcs( OperationsClient operationsClient = serviceClient.getOperationsClient(); Operation operation = operationsClient.getOperation(operationName); - long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay + Instant deadline = Instant.now().plusSeconds(60); - while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) { + while (!operation.getDone() || Instant.now().isBefore(deadline)) { System.out.println("Please wait till operation is done."); TimeUnit.SECONDS.sleep(30); operation = operationsClient.getOperation(operationName); From 6274346d9a291d35c7e8ccef8bf235d6ce24b941 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 27 Jul 2022 16:07:23 +0000 Subject: [PATCH 13/22] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bb0be0e3..671f8f2a 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file: If you are using Gradle without BOM, add this to your dependencies ```Groovy -implementation 'com.google.cloud:google-cloud-retail:2.2.4' +implementation 'com.google.cloud:google-cloud-retail:2.3.0' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.2.4" +libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.3.0" ``` ## Authentication @@ -85,8 +85,6 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m | Write User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | | Events Create Big Query Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | | Events Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | -| Create Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | -| Remove Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | | Add Fulfillment Places | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | | Create Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/CreateProduct.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/CreateProduct.java) | | Crud Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/CrudProduct.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/CrudProduct.java) | @@ -100,6 +98,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m | Update Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/UpdateProduct.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/UpdateProduct.java) | | Products Create Bigquery Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.java) | | Products Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java) | +| Remove Products Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.java) | | Search Simple Query | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchSimpleQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/search/SearchSimpleQuery.java) | | Search With Boost Spec | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchWithBoostSpec.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/search/SearchWithBoostSpec.java) | | Search With Facet Spec | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchWithFacetSpec.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/search/SearchWithFacetSpec.java) | From dab57cebeca14f6dab047abe29393ad638a7746b Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Tue, 9 Aug 2022 10:39:35 +0300 Subject: [PATCH 14/22] PR fix: unique bucket name. --- .../src/test/java/product/ImportProductsGcsTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index c22852b1..16f55db0 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -25,6 +25,8 @@ import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; + +import com.google.cloud.Timestamp; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -51,11 +53,12 @@ public void testValidImportProductsGcs() throws IOException, InterruptedExceptio String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = "products_tests_bucket"; + String bucketName = String.format("%s_products_tests_bucket_%s", projectId, Timestamp.now()); String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; createGcsBucketAndUploadData(bucketName); + importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); String outputResult = bout.toString(); @@ -71,7 +74,7 @@ public void testInvalidImportProductsGcs() throws IOException, InterruptedExcept String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = "products_tests_bucket"; + String bucketName = String.format("%s_products_tests_invalid_bucket_%s", projectId, Timestamp.now()); String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products_some_invalid.json"; From 893e57684841af338fa5011233df7e0a3626a8ba Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Tue, 9 Aug 2022 11:12:51 +0300 Subject: [PATCH 15/22] PR fix: unique bucket name. --- .../src/test/java/product/ImportProductsGcsTest.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index 16f55db0..681195d8 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -25,8 +25,6 @@ import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; - -import com.google.cloud.Timestamp; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -53,12 +51,11 @@ public void testValidImportProductsGcs() throws IOException, InterruptedExceptio String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = String.format("%s_products_tests_bucket_%s", projectId, Timestamp.now()); + String bucketName = "products_tests_bucket"; String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; createGcsBucketAndUploadData(bucketName); - importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); String outputResult = bout.toString(); @@ -74,7 +71,7 @@ public void testInvalidImportProductsGcs() throws IOException, InterruptedExcept String branchName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = String.format("%s_products_tests_invalid_bucket_%s", projectId, Timestamp.now()); + String bucketName = "products_tests_invalid_bucket"; String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products_some_invalid.json"; From 5069936b7ef927a5e3624179fa48ab152406d4c4 Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Tue, 9 Aug 2022 12:56:05 +0300 Subject: [PATCH 16/22] PR fix: removed test. --- .../java/product/ImportProductsGcsTest.java | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index 681195d8..e2eebac1 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -65,26 +65,6 @@ public void testValidImportProductsGcs() throws IOException, InterruptedExceptio assertThat(outputResult).contains("Number of failures during the importing: 0"); } - @Test - public void testInvalidImportProductsGcs() throws IOException, InterruptedException { - String projectId = ServiceOptions.getDefaultProjectId(); - String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = "products_tests_invalid_bucket"; - String gcsBucket = String.format("gs://%s", bucketName); - String gscProductsObject = "products_some_invalid.json"; - - createGcsBucketAndUploadData(bucketName); - importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); - - String outputResult = bout.toString(); - - assertThat(outputResult).contains("Import products from google cloud source request"); - assertThat(outputResult).contains("Number of successfully imported products:"); - assertThat(outputResult).contains("Number of failures during the importing:"); - } - @After public void tearDown() { System.out.flush(); From 24ab1260d5d1cc230cb17ad11dee9dd62d1c4c68 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 11 Aug 2022 14:40:58 +0000 Subject: [PATCH 17/22] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index caac1292..c4701eb2 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file: If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-retail:2.3.0' +implementation 'com.google.cloud:google-cloud-retail:2.4.0' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.3.0" +libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.4.0" ``` ## Authentication From e7cdbb8b96154327e69b3b6f3207b43b6b766d67 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 12 Aug 2022 21:26:04 +0000 Subject: [PATCH 18/22] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 4405304e..894fdab0 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,6 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m | Events Create Big Query Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | | Events Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | | Update User Events Json | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/UpdateUserEventsJson.java) | -| Create Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | -| Remove Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/RemoveTestResources.java) | | Add Fulfillment Places | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java) | | Create Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/CreateProduct.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/CreateProduct.java) | | Crud Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/CrudProduct.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/product/CrudProduct.java) | From 8b8b2b26e43b0def290c0cb4565534b8092cc5b5 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 26 Aug 2022 17:37:37 +0000 Subject: [PATCH 19/22] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- .../src/main/java/product/ImportProductsGcs.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java index 8defe6dc..60c1d29a 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java @@ -51,7 +51,7 @@ public static void main(String[] args) throws IOException, InterruptedException String bucketName = System.getenv("BUCKET_NAME"); String gcsBucket = String.format("gs://%s", bucketName); String gcsErrorBucket = String.format("%s/errors", gcsBucket); - + // To check error handling, use an invalid catalog in request // branchName = // String.format("projects/%s/locations/global/catalogs/invalid_catalog/branches/default_branch", projectId); From 135e0a7276bba3be624761378d88a6c26daad1d5 Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Wed, 31 Aug 2022 09:18:08 +0300 Subject: [PATCH 20/22] PR fix: changed tests to be similar to events test. --- .../setup/ProductsCreateGcsBucket.java | 24 +++++++++++-------- .../java/product/ImportProductsGcsTest.java | 11 +++++---- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java index 3e71a816..8ee20779 100644 --- a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java +++ b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java @@ -26,19 +26,19 @@ public class ProductsCreateGcsBucket { - public static void main(String[] args) throws IOException { - String projectId = ServiceOptions.getDefaultProjectId(); + private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); - Timestamp currentDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); + private static final Timestamp CURRENT_DATE = + Timestamp.newBuilder() + .setSeconds(Instant.now().getEpochSecond()) + .setNanos(Instant.now().getNano()) + .build(); - String productsBucketName = - String.format("%s_products_%s", projectId, currentDate.getSeconds()); + private static final String BUCKET_NAME = + String.format("%s_products_%s", PROJECT_ID, CURRENT_DATE.getSeconds()); - createGcsBucketAndUploadData(productsBucketName); + public static void main(String... args) throws IOException { + createGcsBucketAndUploadData(BUCKET_NAME); } public static void createGcsBucketAndUploadData(String bucketName) throws IOException { @@ -53,4 +53,8 @@ public static void createGcsBucketAndUploadData(String bucketName) throws IOExce System.out.printf( "File 'products_some_invalid.json' was uploaded into bucket '%s'.%n", bucketName); } + + public static String getBucketName() { + return BUCKET_NAME; + } } diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index e2eebac1..dc33b608 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -18,18 +18,19 @@ import static com.google.common.truth.Truth.assertThat; import static product.ImportProductsGcs.importProductsFromGcs; -import static product.setup.ProductsCreateGcsBucket.createGcsBucketAndUploadData; import com.google.cloud.ServiceOptions; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; + import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import product.setup.ProductsCreateGcsBucket; @RunWith(JUnit4.class) public class ImportProductsGcsTest { @@ -47,15 +48,15 @@ public void setUp() throws IOException, InterruptedException, ExecutionException @Test public void testValidImportProductsGcs() throws IOException, InterruptedException { + ProductsCreateGcsBucket.main(); String projectId = ServiceOptions.getDefaultProjectId(); String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); - String bucketName = "products_tests_bucket"; + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + String bucketName = ProductsCreateGcsBucket.getBucketName(); String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; - createGcsBucketAndUploadData(bucketName); importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); String outputResult = bout.toString(); From 2ddf27400752bd575ee3bd965d7a7c918995944d Mon Sep 17 00:00:00 2001 From: Daria Firova Date: Thu, 1 Sep 2022 09:02:45 +0300 Subject: [PATCH 21/22] PR fix: returned test to previous state. --- .../main/java/product/ImportProductsGcs.java | 4 ++-- .../java/product/ImportProductsGcsTest.java | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java index 60c1d29a..b1d72c57 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java @@ -60,11 +60,11 @@ public static void main(String[] args) throws IOException, InterruptedException // To check error handling, use an invalid product JSON. // gcsProductsObject = "products_some_invalid.json" - importProductsFromGcs(branchName, bucketName, gcsBucket, gcsProductsObject); + importProductsFromGcs(branchName, gcsBucket, gcsProductsObject); } public static void importProductsFromGcs( - String branchName, String bucketName, String gcsBucket, String gcsProductsObject) + String branchName, String gcsBucket, String gcsProductsObject) throws IOException, InterruptedException { String gcsErrorBucket = String.format("%s/errors", gcsBucket); diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index dc33b608..813e4fda 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -40,14 +40,6 @@ public class ImportProductsGcsTest { @Before public void setUp() throws IOException, InterruptedException, ExecutionException { - bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @Test - public void testValidImportProductsGcs() throws IOException, InterruptedException { ProductsCreateGcsBucket.main(); String projectId = ServiceOptions.getDefaultProjectId(); String branchName = @@ -56,9 +48,16 @@ public void testValidImportProductsGcs() throws IOException, InterruptedExceptio String bucketName = ProductsCreateGcsBucket.getBucketName(); String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json"; + bout = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); - importProductsFromGcs(branchName, bucketName, gcsBucket, gscProductsObject); + importProductsFromGcs(branchName, gcsBucket, gscProductsObject); + } + @Test + public void testValidImportProductsGcs() { String outputResult = bout.toString(); assertThat(outputResult).contains("Import products from google cloud source request"); From 9ff707609ffa728eea7eaa237532535c6d53b142 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 8 Sep 2022 16:00:38 +0000 Subject: [PATCH 22/22] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- .../java/product/setup/ProductsCreateGcsBucket.java | 10 +++++----- .../src/test/java/product/ImportProductsGcsTest.java | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java index 8ee20779..80ed6285 100644 --- a/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java +++ b/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.java @@ -29,13 +29,13 @@ public class ProductsCreateGcsBucket { private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); private static final Timestamp CURRENT_DATE = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); + Timestamp.newBuilder() + .setSeconds(Instant.now().getEpochSecond()) + .setNanos(Instant.now().getNano()) + .build(); private static final String BUCKET_NAME = - String.format("%s_products_%s", PROJECT_ID, CURRENT_DATE.getSeconds()); + String.format("%s_products_%s", PROJECT_ID, CURRENT_DATE.getSeconds()); public static void main(String... args) throws IOException { createGcsBucketAndUploadData(BUCKET_NAME); diff --git a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java index 813e4fda..22594f6b 100644 --- a/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java +++ b/samples/interactive-tutorials/src/test/java/product/ImportProductsGcsTest.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -43,8 +42,8 @@ public void setUp() throws IOException, InterruptedException, ExecutionException ProductsCreateGcsBucket.main(); String projectId = ServiceOptions.getDefaultProjectId(); String branchName = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); + String.format( + "projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId); String bucketName = ProductsCreateGcsBucket.getBucketName(); String gcsBucket = String.format("gs://%s", bucketName); String gscProductsObject = "products.json";