diff --git a/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java index d770c71a44d..cfd21998ec8 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -172,7 +172,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { } if (ns.get("command").equals("remove_product_from_product_set")) { removeProductFromProductSet( - projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); + projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); } } catch (ArgumentParserException e) { diff --git a/vision/snippets/src/main/java/com/example/vision/ProductManagement.java b/vision/snippets/src/main/java/com/example/vision/ProductManagement.java index 198ab5525e5..ce3cc9bb6f7 100644 --- a/vision/snippets/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/snippets/src/main/java/com/example/vision/ProductManagement.java @@ -268,7 +268,6 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { if (ns.get("command").equals("delete_product")) { deleteProduct(projectId, computeRegion, ns.getString("productId")); } - } catch (ArgumentParserException e) { parser.handleError(e); } diff --git a/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java b/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java index cf04dfc85f2..7598950d5bb 100644 --- a/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java +++ b/vision/snippets/src/main/java/com/example/vision/ReferenceImageManagement.java @@ -16,6 +16,8 @@ package com.example.vision; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageName; import com.google.cloud.vision.v1.ProductSearchClient; import com.google.cloud.vision.v1.ReferenceImage; @@ -123,7 +125,7 @@ public static void getReferenceImage( // Get the full path of the reference image. String formattedName = - ProductSearchClient.formatImageName( + ImageName.format( projectId, computeRegion, productId, referenceImageId); // Get complete detail of the reference image. ReferenceImage image = client.getReferenceImage(formattedName); @@ -158,7 +160,7 @@ public static void deleteReferenceImage( // Get the full path of the reference image. String formattedName = - ProductSearchClient.formatImageName( + ImageName.format( projectId, computeRegion, productId, referenceImageId); // Delete the reference image. client.deleteReferenceImage(formattedName); diff --git a/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProducts.java b/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProducts.java new file mode 100644 index 00000000000..03ecafd4685 --- /dev/null +++ b/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProducts.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 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 com.example.vision.snippets; + +// [START vision_product_search_purge_orphan_products] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.vision.v1.LocationName; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.PurgeProductsRequest; + +import java.util.concurrent.TimeUnit; + + +public class PurgeProducts { + + // Delete the product and all its reference images. + public static void purgeOrphanProducts(String projectId, String computeRegion) + throws Exception { + + // String projectId = "YOUR_PROJECT_ID"; + // String computeRegion = "us-central1"; + // boolean force = true; + + try (ProductSearchClient client = ProductSearchClient.create()) { + String parent = LocationName.format(projectId, computeRegion); + + // The purge operation is async. + PurgeProductsRequest request = PurgeProductsRequest + .newBuilder() + .setDeleteOrphanProducts(true) + // The operation is irreversible and removes multiple products. + // The user is required to pass in force=True to actually perform the + // purge. + // If force is not set to True, the service raises an exception. + .setForce(true) + .setParent(parent) + .build(); + + OperationFuture response = client.purgeProductsAsync(request); + response.getPollingFuture().get(90, TimeUnit.SECONDS); + + System.out.println("Orphan products deleted."); + } + } +} +// [END vision_product_search_purge_orphan_products] \ No newline at end of file diff --git a/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java b/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java new file mode 100644 index 00000000000..975f89f11bd --- /dev/null +++ b/vision/snippets/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java @@ -0,0 +1,68 @@ +/* + * Copyright 2019 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 com.example.vision.snippets; + +// [START vision_product_search_purge_products_in_product_set] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.vision.v1.BatchOperationMetadata; +import com.google.cloud.vision.v1.LocationName; +import com.google.cloud.vision.v1.ProductSearchClient; +import com.google.cloud.vision.v1.ProductSetPurgeConfig; +import com.google.cloud.vision.v1.PurgeProductsRequest; +import com.google.protobuf.Empty; + +import java.util.concurrent.TimeUnit; + +public class PurgeProductsInProductSet { + + // Delete all products in a product set. + public static void purgeProductsInProductSet( + String projectId, String location, String productSetId) + throws Exception { + + // String projectId = "YOUR_PROJECT_ID"; + // String location = "us-central1"; + // String productSetId = "YOUR_PRODUCT_SET_ID"; + // boolean force = true; + + try (ProductSearchClient client = ProductSearchClient.create()) { + + String parent = LocationName.format(projectId, location); + ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig + .newBuilder() + .setProductSetId(productSetId) + .build(); + + PurgeProductsRequest request = PurgeProductsRequest + .newBuilder() + .setParent(parent) + .setProductSetPurgeConfig(productSetPurgeConfig) + // The operation is irreversible and removes multiple products. + // The user is required to pass in force=True to actually perform the + // purge. + // If force is not set to True, the service raises an exception. + .setForce(true) + .build(); + + OperationFuture response = client.purgeProductsAsync(request); + response.getPollingFuture().get(90, TimeUnit.SECONDS); + + System.out.println("Products removed from product set."); + } + } +} +// [END vision_product_search_purge_products_in_product_set] diff --git a/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java index 4143b7abb74..16ca689bf40 100644 --- a/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java +++ b/vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -21,6 +21,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,7 +37,7 @@ public class ProductInProductSetManagementIT { private static final String COMPUTE_REGION = "us-west1"; private static final String PRODUCT_SET_DISPLAY_NAME = "fake_pdt_set_display_name_for_testing"; - private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing"; + private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing" + UUID.randomUUID(); private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing"; private static final String PRODUCT_CATEGORY = "apparel"; private static final String PRODUCT_ID = "fake_pdt_id_for_testing"; diff --git a/vision/snippets/src/test/java/vision/snippets/ProductInProductSetManagementTests.java b/vision/snippets/src/test/java/vision/snippets/ProductInProductSetManagementTests.java new file mode 100644 index 00000000000..bb3b7b3f7ff --- /dev/null +++ b/vision/snippets/src/test/java/vision/snippets/ProductInProductSetManagementTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2019 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 vision.snippets; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.ProductInProductSetManagement; +import com.example.vision.ProductManagement; +import com.example.vision.ProductSetManagement; +import com.example.vision.snippets.PurgeProductsInProductSet; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ProductInProductSetManagementTests { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String COMPUTE_REGION = "us-west1"; + private static final String PRODUCT_SET_DISPLAY_NAME = + "fake_pdt_set_display_name_for_testing"; + private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing" + UUID.randomUUID(); + private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing"; + private static final String PRODUCT_CATEGORY = "apparel"; + private static final String PRODUCT_ID = "fake_pdt_id_for_testing"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + ProductSetManagement.createProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME); + ProductManagement.createProduct( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + System.setOut(null); + } + + @Test + public void testPurgeProductsInProductSet() throws Exception { + // Act + ProductInProductSetManagement.addProductToProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID); + ProductManagement.listProducts( + PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).contains(PRODUCT_ID); + + bout.reset(); + PurgeProductsInProductSet.purgeProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + ProductManagement.listProducts( + PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + } +} diff --git a/vision/snippets/src/test/java/vision/snippets/ProductManagementTests.java b/vision/snippets/src/test/java/vision/snippets/ProductManagementTests.java new file mode 100644 index 00000000000..73404ca13d3 --- /dev/null +++ b/vision/snippets/src/test/java/vision/snippets/ProductManagementTests.java @@ -0,0 +1,78 @@ +/* + * Copyright 2019 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 vision.snippets; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.ProductManagement; +import com.example.vision.snippets.PurgeProducts; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +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 ProductManagementTests { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String COMPUTE_REGION = "us-west1"; + private static final String PRODUCT_DISPLAY_NAME = "fake_prod_display_name_for_testing"; + private static final String PRODUCT_CATEGORY = "homegoods"; + private static final String PRODUCT_ID = "fake_prod_id_for_testing"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + System.setOut(null); + } + + @Test + public void testPurgeOrphanProducts() throws Exception { + // Act + ProductManagement.createProduct( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).contains(PRODUCT_ID); + + bout.reset(); + + // Act + PurgeProducts.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + assertThat(got).doesNotContain(PRODUCT_ID); + } +}