diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java index e040d33b26b..36eb1779a9a 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductInProductSetManagement.java @@ -53,7 +53,7 @@ public class ProductInProductSetManagement { * @param productSetId - Id of the product set. * @throws IOException - on I/O errors. */ - public static void addProductToSet( + public static void addProductToProductSet( String projectId, String computeRegion, String productId, String productSetId) throws IOException { ProductSearchClient client = ProductSearchClient.create(); @@ -162,7 +162,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { try { ns = parser.parseArgs(args); if (ns.get("command").equals("add_product_to_product_set")) { - addProductToSet( + addProductToProductSet( projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId")); } if (ns.get("command").equals("list_products_in_product_set")) { diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java index 0d6661913d1..f0a22f106e8 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductManagement.java @@ -36,10 +36,9 @@ /** * This application demonstrates how to perform basic operations on Products. * - * For more information, see the tutorial page at + *

For more information, see the tutorial page at * https://cloud.google.com/vision/product-search/docs/ */ - public class ProductManagement { // [START vision_product_search_create_product] @@ -51,8 +50,6 @@ public class ProductManagement { * @param productId - Id of the product. * @param productDisplayName - Display name of the product. * @param productCategory - Category of the product. - * @param productDescription - Description of the product. - * @param productLabels - Labels of the product. * @throws IOException - on I/O errors. */ public static void createProduct( @@ -60,9 +57,7 @@ public static void createProduct( String computeRegion, String productId, String productDisplayName, - String productCategory, - String productDescription, - String productLabels) + String productCategory) throws IOException { ProductSearchClient client = ProductSearchClient.create(); @@ -76,12 +71,6 @@ public static void createProduct( .setName(productId) .setDisplayName(productDisplayName) .setProductCategory(productCategory) - .setDescription(productDescription) - .addProductLabels( - KeyValue.newBuilder() - .setKey(productLabels.split(",")[0].split("=")[0]) - .setValue(productLabels.split(",")[0].split("=")[1]) - .build()) .build(); Product product = client.createProduct(projectLocation.toString(), myProduct, productId); @@ -110,8 +99,8 @@ public static void listProducts(String projectId, String computeRegion) throws I System.out.println(String.format("\nProduct name: %s", product.getName())); System.out.println( String.format( - "Product id: %s", - product.getName().substring(product.getName().lastIndexOf('/') + 1))); + "Product id: %s", + product.getName().substring(product.getName().lastIndexOf('/') + 1))); System.out.println(String.format("Product display name: %s", product.getDisplayName())); System.out.println(String.format("Product category: %s", product.getProductCategory())); System.out.println("Product labels:"); @@ -192,9 +181,10 @@ public static void updateProductLabels( // Display the product information System.out.println(String.format("Product name: %s", updatedProduct.getName())); - System.out.println( - String.format( - "Updated product labels: %s", updatedProduct.getProductLabelsList().toString())); + System.out.println(String.format("Updated product labels: ")); + for (Product.KeyValue element : updatedProduct.getProductLabelsList()) { + System.out.println(String.format("%s: %s", element.getKey(), element.getValue())); + } } // [END vision_product_search_update_product_labels] @@ -261,9 +251,7 @@ public void argsHelper(String[] args, PrintStream out) throws Exception { computeRegion, ns.getString("productId"), ns.getString("productDisplayName"), - ns.getString("productCategory"), - ns.getString("productDescription"), - ns.getString("productLabels")); + ns.getString("productCategory")); } if (ns.get("command").equals("list_products")) { listProducts(projectId, computeRegion); diff --git a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java index d5ed87955fb..1b3b9c29560 100644 --- a/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java +++ b/vision/product-search/cloud-client/src/main/java/com/example/vision/ProductSetManagement.java @@ -73,6 +73,7 @@ public static void createProductSet( // Display the product set information System.out.println(String.format("Product set name: %s", productSet.getName())); + } // [END vision_product_search_create_product_set] @@ -86,10 +87,8 @@ public static void createProductSet( */ public static void listProductSets(String projectId, String computeRegion) throws IOException { ProductSearchClient client = ProductSearchClient.create(); - // A resource that represents Google Cloud Platform location. LocationName projectLocation = LocationName.of(projectId, computeRegion); - // List all the product sets available in the region. for (ProductSet productSet : client.listProductSets(projectLocation).iterateAll()) { // Display the product set information @@ -104,6 +103,7 @@ public static void listProductSets(String projectId, String computeRegion) throw System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds())); System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos())); } + } // [END vision_product_search_list_product_sets] diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java new file mode 100644 index 00000000000..92377727211 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ImportProductSetsIT.java @@ -0,0 +1,132 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +import static com.google.common.truth.Truth.assertThat; + +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; + +/** Integration (system) tests for {@link ImportProductSets}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ImportProductSetsIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String COMPUTE_REGION = "us-west1"; + private static final String GCS_URI = + "gs://java-docs-samples-testing/product-search/product_sets.csv"; + private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing"; + private static final String PRODUCT_ID_1 = "fake_product_id_for_testing_1"; + private static final String PRODUCT_ID_2 = "fake_product_id_for_testing_2"; + private static final String IMAGE_URI_1 = "shoes_1.jpg"; + private static final String IMAGE_URI_2 = "shoes_2.jpg"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_1); + ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_2); + ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + System.setOut(null); + } + + @Test + public void testImportProductSets() throws Exception { + // Act + ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_SET_ID); + + // Act + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + assertThat(got).doesNotContain(PRODUCT_ID_1); + assertThat(got).doesNotContain(PRODUCT_ID_2); + + // Act + ProductInProductSetManagement.listProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + // Assert + assertThat(got).doesNotContain(PRODUCT_ID_1); + assertThat(got).doesNotContain(PRODUCT_ID_2); + + // Act + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1); + + // Assert + assertThat(got).doesNotContain(IMAGE_URI_1); + + // Act + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2); + + // Assert + assertThat(got).doesNotContain(IMAGE_URI_2); + + // Act + ImportProductSets.importProductSets(PROJECT_ID, COMPUTE_REGION, GCS_URI); + ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).contains(PRODUCT_SET_ID); + + // Act + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + assertThat(got).contains(PRODUCT_ID_1); + assertThat(got).contains(PRODUCT_ID_2); + + // Act + ProductInProductSetManagement.listProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + // Assert + assertThat(got).contains(PRODUCT_ID_1); + assertThat(got).contains(PRODUCT_ID_2); + + // Act + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1); + + // Assert + assertThat(got).contains(IMAGE_URI_1); + + // Act + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2); + + // Assert + assertThat(got).contains(IMAGE_URI_2); + } +} diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java new file mode 100644 index 00000000000..0687d13e494 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductInProductSetManagementIT.java @@ -0,0 +1,109 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +import static com.google.common.truth.Truth.assertThat; + +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; + +/** Integration (system) tests for {@link ProductInProductSetManagement}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ProductInProductSetManagementIT { + 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_product_set_display_name_for_testing"; + private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing"; + private static final String PRODUCT_DISPLAY_NAME = "fake_product_display_name_for_testing"; + private static final String PRODUCT_CATEGORY = "apparel"; + private static final String PRODUCT_ID = "fake_product_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 testAddProductToProductSet() throws Exception { + // Act + ProductInProductSetManagement.listProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + + bout.reset(); + + // Act + ProductInProductSetManagement.addProductToProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID); + + // Assert + got = bout.toString(); + assertThat(got).contains("Product added to product set."); + } + + @Test + public void testRemoveProductFromProductSet() throws Exception { + // Act + ProductInProductSetManagement.addProductToProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID); + ProductInProductSetManagement.listProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains(PRODUCT_ID); + + bout.reset(); + + // Act + ProductInProductSetManagement.removeProductFromProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID); + ProductInProductSetManagement.listProductsInProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + } +} diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java new file mode 100644 index 00000000000..4a20a0d05b5 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductManagementIT.java @@ -0,0 +1,124 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +import static com.google.common.truth.Truth.assertThat; + +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; + +/** Integration (system) tests for {@link ProductManagement}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ProductManagementIT { + 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_product_display_name_for_testing"; + private static final String PRODUCT_CATEGORY = "homegoods"; + private static final String PRODUCT_ID = "fake_product_id_for_testing"; + private static final String KEY = "fake_key_for_testing"; + private static final String VALUE = "fake_value_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 testCreateProduct() throws Exception { + // Act + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + + bout.reset(); + + // Act + ProductManagement.createProduct( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).contains(PRODUCT_ID); + } + + @Test + public void testDeleteProduct() 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 + ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_ID); + } + + @Test + public void testUpdateProductLabels() throws Exception { + // Act + ProductManagement.createProduct( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); + ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION); + ProductManagement.getProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(KEY); + assertThat(got).doesNotContain(VALUE); + + bout.reset(); + + // Act + ProductManagement.updateProductLabels( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, KEY + "=" + VALUE); + + // Assert + got = bout.toString(); + assertThat(got).contains(KEY); + assertThat(got).contains(VALUE); + } +} diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java new file mode 100644 index 00000000000..424d6e21be7 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ProductSetManagementIT.java @@ -0,0 +1,85 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +import static com.google.common.truth.Truth.assertThat; + +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; + +/** Integration (system) tests for {@link ProductSetManagement}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ProductSetManagementIT { + 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_ID = "fake_product_set_id_for_testing"; + private static final String PRODUCT_SET_DISPLAY_NAME = + "fake_product_set_display_name_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() { + + System.setOut(null); + } + + @Test + public void testCreateDeleteProductSet() throws Exception { + // Act + ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_SET_ID); + + bout.reset(); + + // Act + ProductSetManagement.createProductSet( + PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME); + ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).contains(PRODUCT_SET_ID); + + bout.reset(); + + // Act + ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID); + ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(PRODUCT_SET_ID); + } +} diff --git a/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java b/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java new file mode 100644 index 00000000000..744a1d505d3 --- /dev/null +++ b/vision/product-search/cloud-client/src/test/java/com/example/vision/ReferenceImageManagementIT.java @@ -0,0 +1,91 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +import static com.google.common.truth.Truth.assertThat; + +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; + +/** Integration (system) tests for {@link ReferenceImageManagement}. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ReferenceImageManagementIT { + 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_product_display_name_for_testing"; + private static final String PRODUCT_CATEGORY = "apparel"; + private static final String PRODUCT_ID = "fake_product_id_for_testing"; + private static final String REFERENCE_IMAGE_ID = "fake_reference_image_id_for_testing"; + private static final String GCS_URI = + "gs://java-docs-samples-testing/product-search/shoes_1.jpg"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + ProductManagement.createProduct( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY); + } + + @After + public void tearDown() throws IOException { + ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + System.setOut(null); + } + + @Test + public void testCreateReferenceImage() throws Exception { + // Act + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain(REFERENCE_IMAGE_ID); + + bout.reset(); + + // Act + ReferenceImageManagement.createReferenceImage( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, REFERENCE_IMAGE_ID, GCS_URI); + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + + // Assert + got = bout.toString(); + assertThat(got).contains(REFERENCE_IMAGE_ID); + + bout.reset(); + + // Act + ReferenceImageManagement.deleteReferenceImage( + PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, REFERENCE_IMAGE_ID); + ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID); + + // Assert + got = bout.toString(); + assertThat(got).doesNotContain(REFERENCE_IMAGE_ID); + } +}