From 6b1973d68830ee93f860f6e9312655518ae082fa Mon Sep 17 00:00:00 2001 From: Sergey Borisenko <78493601+sborisenkox@users.noreply.github.com> Date: Wed, 13 Jul 2022 23:22:42 +0300 Subject: [PATCH] docs(samples): Refactoring product package (CRUD) (#417) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update README * the bash scripts are added * Update README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update user_environment_setup.sh * Update user_import_data_to_catalog.sh * Update README.md * Refactoring product package (CRUD). * Minor fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: tetiana-karasova Co-authored-by: t-karasova Co-authored-by: t-karasova <91195610+t-karasova@users.noreply.github.com> Co-authored-by: Owl Bot Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> --- .../java/product/AddFulfillmentPlaces.java | 52 +++++++------ .../src/main/java/product/CreateProduct.java | 49 +++++++------ .../src/main/java/product/CrudProduct.java | 38 +++++++--- .../src/main/java/product/DeleteProduct.java | 4 + .../src/main/java/product/GetProduct.java | 8 +- .../java/product/RemoveFulfillmentPlaces.java | 62 ++++++---------- .../src/main/java/product/SetInventory.java | 73 +++++++++---------- .../src/main/java/product/UpdateProduct.java | 56 +++++++------- .../src/main/java/setup/SetupCleanup.java | 29 ++++++-- .../product/AddFulfillmentPlacesTest.java | 14 +--- .../test/java/product/CreateProductTest.java | 3 + .../test/java/product/CrudProductTest.java | 3 + .../test/java/product/DeleteProductTest.java | 3 + .../src/test/java/product/GetProductTest.java | 3 + .../product/RemoveFulfillmentPlacesTest.java | 22 +----- .../test/java/product/SetInventoryTest.java | 3 + .../test/java/product/UpdateProductTest.java | 3 + 17 files changed, 215 insertions(+), 210 deletions(-) diff --git a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java index 6b46e5bd..34fb3321 100644 --- a/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java @@ -31,57 +31,55 @@ import java.io.IOException; import java.time.Instant; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class AddFulfillmentPlaces { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s", projectId, generatedProductId); - Timestamp currentDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); + createProduct(generatedProductId); - System.out.printf("Add fulfilment places with current date: %s", currentDate); - addFulfillmentPlaces(productName, currentDate, "store2"); + addFulfillmentPlaces(productName, "store2"); getProduct(productName); deleteProduct(productName); } - public static void addFulfillmentPlaces(String productName, Timestamp timestamp, String placeId) + public static void addFulfillmentPlaces(String productName, String placeId) throws IOException, InterruptedException { - AddFulfillmentPlacesRequest addFulfillmentRequest = - getAddFulfillmentRequest(productName, timestamp, placeId); - try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.addFulfillmentPlacesAsync(addFulfillmentRequest); - /* - This is a long-running operation and its result is not immediately - present with get operations,thus we simulate wait with sleep method. - */ - System.out.println("Add fulfillment places, wait 30 seconds: "); - Thread.sleep(30_000); - } - } + Timestamp currentDate = + Timestamp.newBuilder() + .setSeconds(Instant.now().getEpochSecond()) + .setNanos(Instant.now().getNano()) + .build(); - public static AddFulfillmentPlacesRequest getAddFulfillmentRequest( - String productName, Timestamp timestamp, String placeId) { - AddFulfillmentPlacesRequest addfulfillmentPlacesRequest = + System.out.printf("Add fulfilment places with current date: %s", currentDate); + + AddFulfillmentPlacesRequest addFulfillmentPlacesRequest = AddFulfillmentPlacesRequest.newBuilder() .setProduct(productName) .setType("pickup-in-store") .addPlaceIds(placeId) - .setAddTime(timestamp) + .setAddTime(currentDate) .setAllowMissing(true) .build(); - System.out.println("Add fulfillment request " + addfulfillmentPlacesRequest); + System.out.println("Add fulfillment request " + addFulfillmentPlacesRequest); - return addfulfillmentPlacesRequest; + // 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()) { + serviceClient.addFulfillmentPlacesAsync(addFulfillmentPlacesRequest); + // This is a long-running operation and its result is not immediately + // present with get operations,thus we simulate wait with sleep method. + System.out.println("Add fulfillment places, wait 30 seconds: "); + TimeUnit.SECONDS.sleep(30); + } } } diff --git a/samples/interactive-tutorials/src/main/java/product/CreateProduct.java b/samples/interactive-tutorials/src/main/java/product/CreateProduct.java index ee48cd41..811d9fdd 100644 --- a/samples/interactive-tutorials/src/main/java/product/CreateProduct.java +++ b/samples/interactive-tutorials/src/main/java/product/CreateProduct.java @@ -37,7 +37,6 @@ public class CreateProduct { public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( @@ -50,42 +49,44 @@ public static void main(String[] args) throws IOException { // call the Retail API to create product public static Product createProduct(String productId, String branchName) throws IOException { + float price = 30.0f; + float originalPrice = 35.5f; + + PriceInfo priceInfo = + PriceInfo.newBuilder() + .setPrice(price) + .setOriginalPrice(originalPrice) + .setCurrencyCode("USD") + .build(); + + Product generatedProduct = + Product.newBuilder() + .setTitle("Nest Mini") + .setType(Type.PRIMARY) + .addCategories("Speakers and displays") + .addBrands("Google") + .setPriceInfo(priceInfo) + .setAvailability(Availability.IN_STOCK) + .build(); + CreateProductRequest createProductRequest = CreateProductRequest.newBuilder() - .setProduct(generateProduct()) + .setProduct(generatedProduct) .setProductId(productId) .setParent(branchName) .build(); System.out.printf("Create product request: %s%n", createProductRequest); + // 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()) { Product createdProduct = serviceClient.createProduct(createProductRequest); System.out.printf("Created product: %s%n", createdProduct); return createdProduct; } } - - // generate product for create - public static Product generateProduct() { - float price = 30.0f; - float originalPrice = 35.5f; - - PriceInfo priceInfo = - PriceInfo.newBuilder() - .setPrice(price) - .setOriginalPrice(originalPrice) - .setCurrencyCode("USD") - .build(); - - return Product.newBuilder() - .setTitle("Nest Mini") - .setType(Type.PRIMARY) - .addCategories("Speakers and displays") - .addBrands("Google") - .setPriceInfo(priceInfo) - .setAvailability(Availability.IN_STOCK) - .build(); - } } // [END retail_create_product] diff --git a/samples/interactive-tutorials/src/main/java/product/CrudProduct.java b/samples/interactive-tutorials/src/main/java/product/CrudProduct.java index db9e040c..429f8788 100644 --- a/samples/interactive-tutorials/src/main/java/product/CrudProduct.java +++ b/samples/interactive-tutorials/src/main/java/product/CrudProduct.java @@ -39,7 +39,6 @@ public class CrudProduct { public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String generatedProductId = UUID.randomUUID().toString(); String branchName = @@ -53,7 +52,7 @@ public static void main(String[] args) throws IOException { deleteProduct(productName); } - // generate product for create + // Generate product for create public static Product generateProduct() { float price = 30.0f; float originalPrice = 35.5f; @@ -75,7 +74,7 @@ public static Product generateProduct() { .build(); } - // generate product for update + // Generate product for update public static Product generateProductForUpdate(String productId, String productName) { final float price = 20.0f; final float originalPrice = 25.5f; @@ -99,7 +98,7 @@ public static Product generateProductForUpdate(String productId, String productN .build(); } - // call the Retail API to create product + // Call the Retail API to create product public static Product createProduct(String productId, String branchName) throws IOException { CreateProductRequest createProductRequest = CreateProductRequest.newBuilder() @@ -109,19 +108,28 @@ public static Product createProduct(String productId, String branchName) throws .build(); System.out.printf("Create product request: %s%n", createProductRequest); - Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest); - System.out.printf("Created product: %s%n", createdProduct); - - return createdProduct; + // 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()) { + Product createdProduct = serviceClient.createProduct(createProductRequest); + System.out.printf("Created product: %s%n", createdProduct); + return createdProduct; + } } - // get product + // Get product public static Product getProduct(String productName) throws IOException { Product product = Product.newBuilder().build(); GetProductRequest getProductRequest = GetProductRequest.newBuilder().setName(productName).build(); + // 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()) { product = serviceClient.getProduct(getProductRequest); System.out.println("Get product response: " + product); @@ -132,7 +140,7 @@ public static Product getProduct(String productName) throws IOException { } } - // update product + // Update product public static void updateProduct(Product originalProduct, String productName) throws IOException { UpdateProductRequest updateProductRequest = UpdateProductRequest.newBuilder() @@ -141,18 +149,26 @@ public static void updateProduct(Product originalProduct, String productName) th .build(); System.out.printf("Update product request: %s%n", updateProductRequest); + // 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()) { Product updatedProduct = serviceClient.updateProduct(updateProductRequest); System.out.printf("Updated product: %s%n", updatedProduct); } } - // delete product + // Delete product public static void deleteProduct(String productName) throws IOException { DeleteProductRequest deleteProductRequest = DeleteProductRequest.newBuilder().setName(productName).build(); System.out.printf("Delete product request %s%n", deleteProductRequest); + // 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()) { serviceClient.deleteProduct(deleteProductRequest); System.out.printf("Product %s was deleted.%n", productName); diff --git a/samples/interactive-tutorials/src/main/java/product/DeleteProduct.java b/samples/interactive-tutorials/src/main/java/product/DeleteProduct.java index af2d9e3b..2797db26 100644 --- a/samples/interactive-tutorials/src/main/java/product/DeleteProduct.java +++ b/samples/interactive-tutorials/src/main/java/product/DeleteProduct.java @@ -44,6 +44,10 @@ public static void deleteProduct(String productName) throws IOException { DeleteProductRequest.newBuilder().setName(productName).build(); System.out.printf("Delete product request %s%n", deleteProductRequest); + // 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()) { serviceClient.deleteProduct(deleteProductRequest); System.out.printf("Product %s was deleted.%n", productName); diff --git a/samples/interactive-tutorials/src/main/java/product/GetProduct.java b/samples/interactive-tutorials/src/main/java/product/GetProduct.java index debf4b4f..d5daf4cd 100644 --- a/samples/interactive-tutorials/src/main/java/product/GetProduct.java +++ b/samples/interactive-tutorials/src/main/java/product/GetProduct.java @@ -49,8 +49,12 @@ public static Product getProduct(String productName) throws IOException { GetProductRequest getProductRequest = GetProductRequest.newBuilder().setName(productName).build(); - try { - product = ProductServiceClient.create().getProduct(getProductRequest); + // 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()) { + product = serviceClient.getProduct(getProductRequest); System.out.println("Get product response: " + product); return product; } catch (NotFoundException e) { diff --git a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java index 4e40d82b..14f64dee 100644 --- a/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java +++ b/samples/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java @@ -30,76 +30,56 @@ import com.google.protobuf.Timestamp; import java.io.IOException; import java.time.Instant; -import java.time.temporal.ChronoUnit; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class RemoveFulfillmentPlaces { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s", projectId, generatedProductId); - Timestamp currentDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); - /* - * The time when the fulfillment updates are issued. If set with outdated time - * (yesterday), the fulfillment information will not updated. - */ - Timestamp outdatedDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); createProduct(generatedProductId); - System.out.printf("Remove fulfilment places with current date: %s", currentDate); - removeFulfillmentPlaces(productName, currentDate, "store0"); - getProduct(productName); - System.out.printf("Remove outdated fulfilment places: %s", outdatedDate); - removeFulfillmentPlaces(productName, outdatedDate, "store1"); + removeFulfillmentPlaces(productName, "store0"); getProduct(productName); deleteProduct(productName); } // remove fulfillment places to product - public static void removeFulfillmentPlaces( - String productName, Timestamp timestamp, String storeId) + public static void removeFulfillmentPlaces(String productName, String storeId) throws IOException, InterruptedException { - RemoveFulfillmentPlacesRequest removeFulfillmentRequest = - getRemoveFulfillmentRequest(productName, timestamp, storeId); - - try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest); - /* - This is a long-running operation and its result is not immediately - present with get operations,thus we simulate wait with sleep method. - */ - System.out.println("Remove fulfillment places, wait 30 seconds."); - Thread.sleep(30_000); - } - } + Timestamp currentDate = + Timestamp.newBuilder() + .setSeconds(Instant.now().getEpochSecond()) + .setNanos(Instant.now().getNano()) + .build(); - // remove fulfillment request - public static RemoveFulfillmentPlacesRequest getRemoveFulfillmentRequest( - String productName, Timestamp timestamp, String storeId) { + System.out.printf("Remove fulfilment places with current date: %s", currentDate); RemoveFulfillmentPlacesRequest removeFulfillmentRequest = RemoveFulfillmentPlacesRequest.newBuilder() .setProduct(productName) .setType("pickup-in-store") .addPlaceIds(storeId) - .setRemoveTime(timestamp) + .setRemoveTime(currentDate) .setAllowMissing(true) .build(); System.out.println("Remove fulfillment request " + removeFulfillmentRequest); - return removeFulfillmentRequest; + // 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()) { + serviceClient.removeFulfillmentPlacesAsync(removeFulfillmentRequest); + // This is a long-running operation and its result is not immediately + // present with get operations,thus we simulate wait with sleep method. + System.out.println("Remove fulfillment places, wait 30 seconds."); + TimeUnit.SECONDS.sleep(30); + } } } diff --git a/samples/interactive-tutorials/src/main/java/product/SetInventory.java b/samples/interactive-tutorials/src/main/java/product/SetInventory.java index d9e4ea9f..fbf260db 100644 --- a/samples/interactive-tutorials/src/main/java/product/SetInventory.java +++ b/samples/interactive-tutorials/src/main/java/product/SetInventory.java @@ -38,11 +38,11 @@ import java.time.Instant; import java.util.Arrays; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class SetInventory { public static void main(String[] args) throws IOException, InterruptedException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String generatedProductId = UUID.randomUUID().toString(); String productName = @@ -57,21 +57,10 @@ public static void main(String[] args) throws IOException, InterruptedException } public static void setInventory(String productName) throws IOException, InterruptedException { - SetInventoryRequest setInventoryRequest = getSetInventoryRequest(productName); - - try (ProductServiceClient serviceClient = ProductServiceClient.create()) { - serviceClient.setInventoryAsync(setInventoryRequest); - } - - /* - This is a long-running operation and its result is not immediately - present with get operations,thus we simulate wait with sleep method. - */ - System.out.println("Set inventory, wait 30 seconds."); - Thread.sleep(30_000); - } + float price = 15.0f; + float originalPrice = 20.0f; + float cost = 8.0f; - public static SetInventoryRequest getSetInventoryRequest(String productName) { // The request timestamp Timestamp requestTime = Timestamp.newBuilder() @@ -86,23 +75,6 @@ public static SetInventoryRequest getSetInventoryRequest(String productName) { "price_info", "availability", "fulfillment_info", "available_quantity")) .build(); - SetInventoryRequest setInventoryRequest = - SetInventoryRequest.newBuilder() - .setInventory(getProductWithInventoryInfo(productName)) - .setSetTime(requestTime) - .setAllowMissing(true) - .setSetMask(setMask) - .build(); - System.out.printf("Set inventory request: %s%n", setInventoryRequest); - - return setInventoryRequest; - } - - public static Product getProductWithInventoryInfo(String productName) { - float price = 15.0f; - float originalPrice = 20.0f; - float cost = 8.0f; - PriceInfo priceInfo = PriceInfo.newBuilder() .setPrice(price) @@ -117,13 +89,36 @@ public static Product getProductWithInventoryInfo(String productName) { .addAllPlaceIds(Arrays.asList("store1", "store2")) .build(); - return Product.newBuilder() - .setName(productName) - .setPriceInfo(priceInfo) - .addFulfillmentInfo(fulfillmentInfo) - .setAvailability(Availability.IN_STOCK) - .setAvailableQuantity(Int32Value.newBuilder().setValue(5).build()) - .build(); + Product product = + Product.newBuilder() + .setName(productName) + .setPriceInfo(priceInfo) + .addFulfillmentInfo(fulfillmentInfo) + .setAvailability(Availability.IN_STOCK) + .setAvailableQuantity(Int32Value.newBuilder().setValue(5).build()) + .build(); + + SetInventoryRequest setInventoryRequest = + SetInventoryRequest.newBuilder() + .setInventory(product) + .setSetTime(requestTime) + .setAllowMissing(true) + .setSetMask(setMask) + .build(); + System.out.printf("Set inventory request: %s%n", setInventoryRequest); + + // 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()) { + serviceClient.setInventoryAsync(setInventoryRequest); + } + + // This is a long-running operation and its result is not immediately + // present with get operations,thus we simulate wait with sleep method. + System.out.println("Set inventory, wait 30 seconds."); + TimeUnit.SECONDS.sleep(30); } } diff --git a/samples/interactive-tutorials/src/main/java/product/UpdateProduct.java b/samples/interactive-tutorials/src/main/java/product/UpdateProduct.java index e25b15b7..2fc3a155 100644 --- a/samples/interactive-tutorials/src/main/java/product/UpdateProduct.java +++ b/samples/interactive-tutorials/src/main/java/product/UpdateProduct.java @@ -38,7 +38,6 @@ public class UpdateProduct { public static void main(String[] args) throws IOException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String branchName = String.format( @@ -50,8 +49,9 @@ public static void main(String[] args) throws IOException { deleteProduct(createdProduct.getName()); } - // generate product for update - public static Product generateProductForUpdate(String productId, String branchName) { + // call the Retail API to update product + public static void updateProduct(Product originalProduct, String defaultBranchName) + throws IOException { final float price = 20.0f; final float originalPrice = 25.5f; @@ -62,35 +62,31 @@ public static Product generateProductForUpdate(String productId, String branchNa .setCurrencyCode("EUR") .build(); - return Product.newBuilder() - .setId(productId) - .setName(branchName + "/products/" + productId) - .setTitle("Updated Nest Mini") - .setType(Type.PRIMARY) - .addCategories("Updated Speakers and displays") - .addBrands("Updated Google") - .setAvailability(Availability.OUT_OF_STOCK) - .setPriceInfo(priceInfo) - .build(); - } - - // get update product request - public static UpdateProductRequest getUpdateProductRequest(Product productToUpdate) { - UpdateProductRequest updateProductRequest = - UpdateProductRequest.newBuilder().setProduct(productToUpdate).setAllowMissing(true).build(); - System.out.printf("Update product request: %s%n", updateProductRequest); - - return updateProductRequest; - } + Product generatedProduct = + Product.newBuilder() + .setId(originalProduct.getId()) + .setName(defaultBranchName + "/products/" + originalProduct.getId()) + .setTitle("Updated Nest Mini") + .setType(Type.PRIMARY) + .addCategories("Updated Speakers and displays") + .addBrands("Updated Google") + .setAvailability(Availability.OUT_OF_STOCK) + .setPriceInfo(priceInfo) + .build(); - // call the Retail API to update product - public static void updateProduct(Product originalProduct, String defaultBranchName) - 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 (ProductServiceClient serviceClient = ProductServiceClient.create()) { - Product updatedProduct = - serviceClient.updateProduct( - getUpdateProductRequest( - generateProductForUpdate(originalProduct.getId(), defaultBranchName))); + UpdateProductRequest updateProductRequest = + UpdateProductRequest.newBuilder() + .setProduct(generatedProduct) + .setAllowMissing(true) + .build(); + System.out.printf("Update product request: %s%n", updateProductRequest); + + Product updatedProduct = serviceClient.updateProduct(updateProductRequest); System.out.printf("Updated product: %s%n", updatedProduct); } } diff --git a/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java b/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java index 1c0f2bc8..06f45b9d 100644 --- a/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java +++ b/samples/interactive-tutorials/src/main/java/setup/SetupCleanup.java @@ -81,7 +81,6 @@ public class SetupCleanup { - // TODO(developer): Replace these variables before running the sample. private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); private static final String DEFAULT_CATALOG = String.format("projects/%s/locations/global/catalogs/default_catalog", PROJECT_ID); @@ -116,9 +115,10 @@ public static UserEvent getUserEvent(String visitorId) { } 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. + // 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() @@ -134,9 +134,10 @@ public static UserEvent writeUserEvent(String visitorId) throws IOException { 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. + // 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() @@ -188,6 +189,10 @@ public static Product createProduct(String productId) throws IOException { .build(); System.out.printf("Create product request: %s%n", createProductRequest); + // 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()) { Product createdProduct = serviceClient.createProduct(createProductRequest); System.out.printf("Created product: %s%n", createdProduct); @@ -201,6 +206,10 @@ public static Product getProduct(String productName) throws IOException { GetProductRequest getProductRequest = GetProductRequest.newBuilder().setName(productName).build(); + // 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()) { product = serviceClient.getProduct(getProductRequest); System.out.println("Get product response: " + product); @@ -216,6 +225,10 @@ public static void deleteProduct(String productName) throws IOException { DeleteProductRequest.newBuilder().setName(productName).build(); System.out.printf("Delete product request %s%n", deleteProductRequest); + // 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()) { serviceClient.deleteProduct(deleteProductRequest); System.out.printf("Product %s was deleted.%n", productName); @@ -356,7 +369,7 @@ public static void uploadDataToBqTable( + job.getStatus().getError()); } } catch (BigQueryException | InterruptedException e) { - System.out.println("Column not added during load append \n" + e); + System.out.printf("Column not added during load append: %s%n", e.getMessage()); } } diff --git a/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java b/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java index f29959ba..600a0aae 100644 --- a/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java +++ b/samples/interactive-tutorials/src/test/java/product/AddFulfillmentPlacesTest.java @@ -23,17 +23,18 @@ import static setup.SetupCleanup.getProduct; import com.google.cloud.ServiceOptions; -import com.google.protobuf.Timestamp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.time.Instant; import java.util.UUID; 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 AddFulfillmentPlacesTest { private ByteArrayOutputStream bout; @@ -41,26 +42,19 @@ public class AddFulfillmentPlacesTest { @Before public void setUp() throws IOException, InterruptedException, ExecutionException { - // TODO(developer): Replace these variables before running the sample. String projectId = ServiceOptions.getDefaultProjectId(); String generatedProductId = UUID.randomUUID().toString(); String productName = String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s", projectId, generatedProductId); - Timestamp currentDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); bout = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bout); originalPrintStream = System.out; System.setOut(out); createProduct(generatedProductId); - System.out.printf("Add fulfilment places with current date: %s", currentDate); - addFulfillmentPlaces(productName, currentDate, "store2"); + addFulfillmentPlaces(productName, "store2"); getProduct(productName); deleteProduct(productName); } diff --git a/samples/interactive-tutorials/src/test/java/product/CreateProductTest.java b/samples/interactive-tutorials/src/test/java/product/CreateProductTest.java index f6f1174e..ebadae2a 100644 --- a/samples/interactive-tutorials/src/test/java/product/CreateProductTest.java +++ b/samples/interactive-tutorials/src/test/java/product/CreateProductTest.java @@ -30,7 +30,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 CreateProductTest { private ByteArrayOutputStream bout; diff --git a/samples/interactive-tutorials/src/test/java/product/CrudProductTest.java b/samples/interactive-tutorials/src/test/java/product/CrudProductTest.java index 37d37658..40e0c3bb 100644 --- a/samples/interactive-tutorials/src/test/java/product/CrudProductTest.java +++ b/samples/interactive-tutorials/src/test/java/product/CrudProductTest.java @@ -32,7 +32,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 CrudProductTest { private ByteArrayOutputStream bout; diff --git a/samples/interactive-tutorials/src/test/java/product/DeleteProductTest.java b/samples/interactive-tutorials/src/test/java/product/DeleteProductTest.java index fa33f368..e4c0d4ab 100644 --- a/samples/interactive-tutorials/src/test/java/product/DeleteProductTest.java +++ b/samples/interactive-tutorials/src/test/java/product/DeleteProductTest.java @@ -28,7 +28,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 DeleteProductTest { private ByteArrayOutputStream bout; diff --git a/samples/interactive-tutorials/src/test/java/product/GetProductTest.java b/samples/interactive-tutorials/src/test/java/product/GetProductTest.java index 049c802f..6961ae68 100644 --- a/samples/interactive-tutorials/src/test/java/product/GetProductTest.java +++ b/samples/interactive-tutorials/src/test/java/product/GetProductTest.java @@ -30,7 +30,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 GetProductTest { private ByteArrayOutputStream bout; diff --git a/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java b/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java index ad5112cd..bddaca37 100644 --- a/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java +++ b/samples/interactive-tutorials/src/test/java/product/RemoveFulfillmentPlacesTest.java @@ -23,18 +23,18 @@ import static setup.SetupCleanup.getProduct; import com.google.cloud.ServiceOptions; -import com.google.protobuf.Timestamp; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.time.Instant; -import java.time.temporal.ChronoUnit; import java.util.UUID; 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 RemoveFulfillmentPlacesTest { private ByteArrayOutputStream bout; @@ -48,27 +48,13 @@ public void setUp() throws IOException, InterruptedException, ExecutionException String.format( "projects/%s/locations/global/catalogs/default_catalog/branches/0/products/%s", projectId, generatedProductId); - Timestamp currentDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); - Timestamp outdatedDate = - Timestamp.newBuilder() - .setSeconds(Instant.now().minus(1, ChronoUnit.DAYS).getEpochSecond()) - .setNanos(Instant.now().getNano()) - .build(); bout = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bout); originalPrintStream = System.out; System.setOut(out); createProduct(generatedProductId); - System.out.printf("Remove fulfilment places with current date: %s", currentDate); - removeFulfillmentPlaces(productName, currentDate, "store0"); - getProduct(productName); - System.out.printf("Remove outdated fulfilment places: %s", outdatedDate); - removeFulfillmentPlaces(productName, outdatedDate, "store1"); + removeFulfillmentPlaces(productName, "store0"); getProduct(productName); deleteProduct(productName); } diff --git a/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java b/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java index d8ba0786..747da07e 100644 --- a/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java +++ b/samples/interactive-tutorials/src/test/java/product/SetInventoryTest.java @@ -31,7 +31,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 SetInventoryTest { private ByteArrayOutputStream bout; diff --git a/samples/interactive-tutorials/src/test/java/product/UpdateProductTest.java b/samples/interactive-tutorials/src/test/java/product/UpdateProductTest.java index 542d5509..78756acf 100644 --- a/samples/interactive-tutorials/src/test/java/product/UpdateProductTest.java +++ b/samples/interactive-tutorials/src/test/java/product/UpdateProductTest.java @@ -31,7 +31,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 UpdateProductTest { private ByteArrayOutputStream bout;