From 159b57a0c56aed22f566c9e54ca712acae0ddabf Mon Sep 17 00:00:00 2001 From: Sergey Borisenko <78493601+sborisenkox@users.noreply.github.com> Date: Wed, 16 Mar 2022 02:06:36 +0200 Subject: [PATCH] chore(samples): Retail Tutorials. Events (write, rejoin, purge) (#303) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add user events: write, rejoin, purge. * Add kokoro configuration. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Refactor code. * Tests fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Refactoring code. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix test fails. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix test fails. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix tests. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot Co-authored-by: Neenu Shaji --- README.md | 3 + samples/interactive-tutorials/pom.xml | 20 ++++- .../src/main/java/events/PurgeUserEvent.java | 75 +++++++++++++++++ .../src/main/java/events/RejoinUserEvent.java | 77 ++++++++++++++++++ .../src/main/java/events/WriteUserEvent.java | 80 ++++++++++++++++++ .../java/product/AddFulfillmentPlaces.java | 3 +- .../product/ImportProductsBigQueryTable.java | 3 +- .../main/java/product/ImportProductsGcs.java | 3 +- .../product/ImportProductsInlineSource.java | 3 +- .../java/product/RemoveFulfillmentPlaces.java | 3 +- .../src/main/java/product/SetInventory.java | 3 +- .../src/main/java/setup/SetupCleanup.java | 81 ++++++++++++++++++- .../test/java/events/PurgeUserEventTest.java | 81 +++++++++++++++++++ .../test/java/events/RejoinUserEventTest.java | 81 +++++++++++++++++++ .../test/java/events/WriteUserEventTest.java | 80 ++++++++++++++++++ .../java/init/RemoveTestResourcesTest.java | 6 ++ 16 files changed, 586 insertions(+), 16 deletions(-) create mode 100644 samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java create mode 100644 samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java create mode 100644 samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java create mode 100644 samples/interactive-tutorials/src/test/java/events/PurgeUserEventTest.java create mode 100644 samples/interactive-tutorials/src/test/java/events/RejoinUserEventTest.java create mode 100644 samples/interactive-tutorials/src/test/java/events/WriteUserEventTest.java diff --git a/README.md b/README.md index a1fbe898..4cd0fa35 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | +| Purge User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.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/PurgeUserEvent.java) | +| Rejoin User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.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/RejoinUserEvent.java) | +| 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) | diff --git a/samples/interactive-tutorials/pom.xml b/samples/interactive-tutorials/pom.xml index 6d57d5c5..7b253f75 100644 --- a/samples/interactive-tutorials/pom.xml +++ b/samples/interactive-tutorials/pom.xml @@ -25,6 +25,18 @@ UTF-8 + + + + com.google.cloud + libraries-bom + 24.3.0 + pom + import + + + + com.google.cloud @@ -52,6 +64,12 @@ gson 2.9.0 + + com.google.truth + truth + 1.1.3 + test + @@ -61,7 +79,7 @@ exec-maven-plugin 3.0.0 - false + true diff --git a/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java b/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java new file mode 100644 index 00000000..fec215ca --- /dev/null +++ b/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java @@ -0,0 +1,75 @@ +/* + * 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. + */ + +// [START retail_purge_user_event] + +/* + * Purge user events into a catalog from inline source using Retail API + */ + +package events; + +import static setup.SetupCleanup.writeUserEvent; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.retail.v2.PurgeMetadata; +import com.google.cloud.retail.v2.PurgeUserEventsRequest; +import com.google.cloud.retail.v2.PurgeUserEventsResponse; +import com.google.cloud.retail.v2.UserEventServiceClient; +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +public class PurgeUserEvent { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = System.getenv("PROJECT_ID"); + String defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + // visitorId generated randomly. + String visitorId = UUID.randomUUID().toString(); + + callPurgeUserEvents(defaultCatalog, visitorId); + } + + public static void callPurgeUserEvents(String defaultCatalog, String visitorId) + throws IOException, ExecutionException, InterruptedException { + writeUserEvent(visitorId); + + // 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 (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { + PurgeUserEventsRequest purgeUserEventsRequest = + PurgeUserEventsRequest.newBuilder() + // TO CHECK ERROR HANDLING SET INVALID FILTER HERE: + .setFilter(String.format("visitorId=\"%s\"", visitorId)) + .setParent(defaultCatalog) + .setForce(true) + .build(); + System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest); + + OperationFuture purgeOperation = + userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest); + + System.out.printf("The purge operation was started: %s%n", purgeOperation.getName()); + } + } +} + +// [END retail_purge_user_event] diff --git a/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java b/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java new file mode 100644 index 00000000..ddb06354 --- /dev/null +++ b/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java @@ -0,0 +1,77 @@ +/* + * 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. + */ + +// [START retail_rejoin_user_event] + +/* + * Rejoin user events into a catalog from inline source using Retail API + */ + +package events; + +import static setup.SetupCleanup.purgeUserEvent; +import static setup.SetupCleanup.writeUserEvent; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.retail.v2.RejoinUserEventsMetadata; +import com.google.cloud.retail.v2.RejoinUserEventsRequest; +import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope; +import com.google.cloud.retail.v2.RejoinUserEventsResponse; +import com.google.cloud.retail.v2.UserEventServiceClient; +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +public class RejoinUserEvent { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = System.getenv("PROJECT_ID"); + String defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + // visitorId generated randomly. + String visitorId = UUID.randomUUID().toString(); + + callRejoinUserEvents(defaultCatalog, visitorId); + } + + public static void callRejoinUserEvents(String defaultCatalog, String visitorId) + throws IOException, ExecutionException, InterruptedException { + writeUserEvent(visitorId); + + // 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 (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { + RejoinUserEventsRequest rejoinUserEventsRequest = + RejoinUserEventsRequest.newBuilder() + .setParent(defaultCatalog) + .setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS) + .build(); + System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest); + + OperationFuture rejoinOperation = + userEventServiceClient.rejoinUserEventsAsync(rejoinUserEventsRequest); + + System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName()); + } + + purgeUserEvent(visitorId); + } +} + +// [END retail_rejoin_user_event] diff --git a/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java b/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java new file mode 100644 index 00000000..26a9d18d --- /dev/null +++ b/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java @@ -0,0 +1,80 @@ +/* + * 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. + */ + +// [START retail_write_user_event] + +/* + * Write user events into a catalog from inline source using Retail API + */ + +package events; + +import static setup.SetupCleanup.purgeUserEvent; + +import com.google.cloud.retail.v2.UserEvent; +import com.google.cloud.retail.v2.UserEventServiceClient; +import com.google.cloud.retail.v2.WriteUserEventRequest; +import com.google.protobuf.Timestamp; +import java.io.IOException; +import java.time.Instant; +import java.util.UUID; +import java.util.concurrent.ExecutionException; + +public class WriteUserEvent { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = System.getenv("PROJECT_ID"); + String defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + // visitorId generated randomly. + String visitorId = UUID.randomUUID().toString(); + + writeUserEvent(defaultCatalog, visitorId); + purgeUserEvent(visitorId); + } + + public static void writeUserEvent(String defaultCatalog, String visitorId) throws IOException { + // 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 (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { + Timestamp timestamp = + Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build(); + + UserEvent userEvent = + UserEvent.newBuilder() + .setEventType("home-page-view") + .setVisitorId(visitorId) + .setEventTime(timestamp) + .build(); + System.out.println(userEvent); + + WriteUserEventRequest writeUserEventRequest = + WriteUserEventRequest.newBuilder() + .setUserEvent(userEvent) + .setParent(defaultCatalog) + .build(); + System.out.printf("Write user event request: %s%n", writeUserEventRequest); + + userEventServiceClient.writeUserEvent(writeUserEventRequest); + System.out.printf("Written user event: %s%n", userEvent); + } + } +} + +// [END retail_write_user_event] diff --git a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java index d3549e1d..990c748f 100644 --- a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java @@ -39,8 +39,7 @@ public static void main(String[] args) throws IOException, InterruptedException String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/" - + "default_branch/products/%s", + "projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s", projectId, generatedProductId); Timestamp currentDate = Timestamp.newBuilder() diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java index 456ffcea..d64054ef 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java @@ -38,8 +38,7 @@ public class ImportProductsBigQueryTable { private static final String PROJECT_ID = System.getenv("PROJECT_ID"); private static final String DEFAULT_CATALOG = String.format( - "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", - PROJECT_ID); + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID); private static final String DATASET_ID = "products"; private static final String TABLE_ID = "products"; // TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS: diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java index d56b529b..5855dfa0 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsGcs.java @@ -40,8 +40,7 @@ public class ImportProductsGcs { private static final String PROJECT_ID = System.getenv("PROJECT_ID"); private static final String DEFAULT_CATALOG = String.format( - "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", - PROJECT_ID); + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID); private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME")); private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET); private static final String GCS_PRODUCTS_OBJECT = "products.json"; diff --git a/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java b/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java index 0c72b14c..e0465e99 100644 --- a/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java +++ b/samples/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java @@ -47,8 +47,7 @@ public class ImportProductsInlineSource { private static final String PROJECT_ID = System.getenv("PROJECT_ID"); private static final String DEFAULT_CATALOG = String.format( - "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", - PROJECT_ID); + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID); public static void main(String[] args) throws IOException, InterruptedException { ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts()); diff --git a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java index 5082c0cf..92df0953 100644 --- a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java @@ -40,8 +40,7 @@ public static void main(String[] args) throws IOException, InterruptedException String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( - "projects/%s/locations/global/catalogs/default_catalog/branches/" - + "default_branch/products/%s", + "projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s", projectId, generatedProductId); Timestamp currentDate = Timestamp.newBuilder() diff --git a/samples/interactive-tutorials/src/main/java/product/SetInventory.java b/samples/interactive-tutorials/src/main/java/product/SetInventory.java index 424b45bd..89d90fa0 100644 --- a/samples/interactive-tutorials/src/main/java/product/SetInventory.java +++ b/samples/interactive-tutorials/src/main/java/product/SetInventory.java @@ -46,8 +46,7 @@ public static void main(String[] args) throws IOException, InterruptedException String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( - "projects/%s/locations/global/catalogs/default_catalog/" - + "branches/default_branch/products/%s", + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/0/products/%s", projectId, generatedProductId); createProduct(generatedProductId); diff --git a/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java b/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java index 462a572c..3be2093d 100644 --- a/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java +++ b/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java @@ -18,6 +18,7 @@ import static com.google.cloud.storage.StorageClass.STANDARD; +import com.google.api.gax.longrunning.OperationFuture; import com.google.api.gax.paging.Page; import com.google.api.gax.rpc.NotFoundException; import com.google.cloud.bigquery.BigQuery; @@ -47,7 +48,14 @@ import com.google.cloud.retail.v2.Product; import com.google.cloud.retail.v2.Product.Availability; import com.google.cloud.retail.v2.Product.Type; +import com.google.cloud.retail.v2.ProductDetail; import com.google.cloud.retail.v2.ProductServiceClient; +import com.google.cloud.retail.v2.PurgeMetadata; +import com.google.cloud.retail.v2.PurgeUserEventsRequest; +import com.google.cloud.retail.v2.PurgeUserEventsResponse; +import com.google.cloud.retail.v2.UserEvent; +import com.google.cloud.retail.v2.UserEventServiceClient; +import com.google.cloud.retail.v2.WriteUserEventRequest; import com.google.cloud.storage.Blob; import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; @@ -59,20 +67,87 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializer; +import com.google.protobuf.Int32Value; +import com.google.protobuf.Timestamp; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.time.Instant; import java.util.Arrays; +import java.util.Collections; +import java.util.UUID; +import java.util.concurrent.ExecutionException; public class SetupCleanup { private static final String PROJECT_ID = System.getenv("PROJECT_ID"); + private static final String DEFAULT_CATALOG = + String.format("projects/%s/locations/global/catalogs/default_catalog", PROJECT_ID); private static final Storage STORAGE = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService(); private static final String DEFAULT_BRANCH_NAME = - String.format( - "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", - PROJECT_ID); + String.format("projects/%s/locations/global/catalogs/default_catalog/branches/0", PROJECT_ID); + + public static UserEvent getUserEvent(String visitorId) { + int value = 3; + + Timestamp timestamp = Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build(); + + Product product = Product.newBuilder().setId(UUID.randomUUID().toString()).build(); + + ProductDetail productDetail = + ProductDetail.newBuilder() + .setProduct(product) + .setQuantity(Int32Value.newBuilder().setValue(value).build()) + .build(); + + UserEvent userEvent = + UserEvent.newBuilder() + .setEventType("detail-page-view") + .setVisitorId(visitorId) + .setEventTime(timestamp) + .addAllProductDetails(Collections.singletonList(productDetail)) + .build(); + System.out.println(userEvent); + + return userEvent; + } + + public static UserEvent writeUserEvent(String visitorId) throws IOException { + // 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 (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { + WriteUserEventRequest writeUserEventRequest = + WriteUserEventRequest.newBuilder() + .setUserEvent(getUserEvent(visitorId)) + .setParent(DEFAULT_CATALOG) + .build(); + + UserEvent userEvent = userEventServiceClient.writeUserEvent(writeUserEventRequest); + System.out.printf("The user event is written. %n%s%n", userEvent); + return userEvent; + } + } + + public static void purgeUserEvent(String visitorId) + throws IOException, ExecutionException, 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 (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { + PurgeUserEventsRequest purgeUserEventsRequest = + PurgeUserEventsRequest.newBuilder() + .setFilter(String.format("visitorId=\"%s\"", visitorId)) + .setParent(DEFAULT_CATALOG) + .setForce(true) + .build(); + + OperationFuture purgeOperation = + userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest); + System.out.printf("The purge operation was started: %s%n", purgeOperation.getName()); + } + } public static Product generateProduct() { float price = 30.0f; diff --git a/samples/interactive-tutorials/src/test/java/events/PurgeUserEventTest.java b/samples/interactive-tutorials/src/test/java/events/PurgeUserEventTest.java new file mode 100644 index 00000000..8d7e40aa --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/events/PurgeUserEventTest.java @@ -0,0 +1,81 @@ +/* + * 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 events; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class PurgeUserEventTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + private PrintStream out; + + private String projectId; + private String defaultCatalog; + private String visitorId; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + projectId = System.getenv("PROJECT_ID"); + defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + visitorId = UUID.randomUUID().toString(); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @Test + public void testPurgeUserEvent() throws IOException, ExecutionException, InterruptedException { + PurgeUserEvent.callPurgeUserEvents(defaultCatalog, visitorId); + String got = bout.toString(); + + assertThat(got).contains("The user event is written"); + assertThat(got).contains("Purge user events request"); + assertThat(got).contains("The purge operation was started"); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} diff --git a/samples/interactive-tutorials/src/test/java/events/RejoinUserEventTest.java b/samples/interactive-tutorials/src/test/java/events/RejoinUserEventTest.java new file mode 100644 index 00000000..0c3bf223 --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/events/RejoinUserEventTest.java @@ -0,0 +1,81 @@ +/* + * 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 events; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RejoinUserEventTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + private PrintStream out; + + private String projectId; + private String defaultCatalog; + private String visitorId; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + projectId = System.getenv("PROJECT_ID"); + defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + visitorId = UUID.randomUUID().toString(); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @Test + public void testPurgeUserEvent() throws IOException, ExecutionException, InterruptedException { + RejoinUserEvent.callRejoinUserEvents(defaultCatalog, visitorId); + String got = bout.toString(); + + assertThat(got).contains("The user event is written"); + assertThat(got).contains("Rejoin user events request"); + assertThat(got).contains("The rejoin operation was started"); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} diff --git a/samples/interactive-tutorials/src/test/java/events/WriteUserEventTest.java b/samples/interactive-tutorials/src/test/java/events/WriteUserEventTest.java new file mode 100644 index 00000000..6b14ff49 --- /dev/null +++ b/samples/interactive-tutorials/src/test/java/events/WriteUserEventTest.java @@ -0,0 +1,80 @@ +/* + * 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 events; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class WriteUserEventTest { + + private ByteArrayOutputStream bout; + private PrintStream originalPrintStream; + private PrintStream out; + + private String projectId; + private String defaultCatalog; + private String visitorId; + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, InterruptedException, ExecutionException { + projectId = System.getenv("PROJECT_ID"); + defaultCatalog = + String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); + visitorId = UUID.randomUUID().toString(); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @Test + public void testPurgeUserEvent() throws IOException { + WriteUserEvent.writeUserEvent(defaultCatalog, visitorId); + String got = bout.toString(); + + assertThat(got).contains("Write user event request"); + assertThat(got).contains("Written user event"); + } + + @After + public void tearDown() { + System.out.flush(); + System.setOut(originalPrintStream); + } +} diff --git a/samples/interactive-tutorials/src/test/java/init/RemoveTestResourcesTest.java b/samples/interactive-tutorials/src/test/java/init/RemoveTestResourcesTest.java index f9398537..9a5b066f 100644 --- a/samples/interactive-tutorials/src/test/java/init/RemoveTestResourcesTest.java +++ b/samples/interactive-tutorials/src/test/java/init/RemoveTestResourcesTest.java @@ -20,6 +20,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -45,4 +46,9 @@ public void testRemoveTestResources() { Assert.assertTrue(output.matches("(?s)^(.*Deleting products in process, please wait.*)$")); Assert.assertTrue(output.matches("(?s)^(.*products were deleted from.*)$")); } + + @After + public void restoreRemove() throws IOException { + Runtime.getRuntime().exec("mvn compile exec:java -Dexec.mainClass=init.CreateTestResources"); + } }