Skip to content

Commit 3ff8d26

Browse files
nirupa-kumarchingor13
authored andcommitted
samples: Adding the Product Search tests. (#1195)
* Adding the Product Search tests. * Fixing review issues.
1 parent 3ab6fff commit 3ff8d26

8 files changed

+554
-25
lines changed

vision/snippets/src/main/java/com/example/vision/ProductInProductSetManagement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ProductInProductSetManagement {
5353
* @param productSetId - Id of the product set.
5454
* @throws IOException - on I/O errors.
5555
*/
56-
public static void addProductToSet(
56+
public static void addProductToProductSet(
5757
String projectId, String computeRegion, String productId, String productSetId)
5858
throws IOException {
5959
ProductSearchClient client = ProductSearchClient.create();
@@ -162,7 +162,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
162162
try {
163163
ns = parser.parseArgs(args);
164164
if (ns.get("command").equals("add_product_to_product_set")) {
165-
addProductToSet(
165+
addProductToProductSet(
166166
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
167167
}
168168
if (ns.get("command").equals("list_products_in_product_set")) {

vision/snippets/src/main/java/com/example/vision/ProductManagement.java

+9-21
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@
3636
/**
3737
* This application demonstrates how to perform basic operations on Products.
3838
*
39-
* For more information, see the tutorial page at
39+
* <p>For more information, see the tutorial page at
4040
* https://cloud.google.com/vision/product-search/docs/
4141
*/
42-
4342
public class ProductManagement {
4443

4544
// [START vision_product_search_create_product]
@@ -51,18 +50,14 @@ public class ProductManagement {
5150
* @param productId - Id of the product.
5251
* @param productDisplayName - Display name of the product.
5352
* @param productCategory - Category of the product.
54-
* @param productDescription - Description of the product.
55-
* @param productLabels - Labels of the product.
5653
* @throws IOException - on I/O errors.
5754
*/
5855
public static void createProduct(
5956
String projectId,
6057
String computeRegion,
6158
String productId,
6259
String productDisplayName,
63-
String productCategory,
64-
String productDescription,
65-
String productLabels)
60+
String productCategory)
6661
throws IOException {
6762
ProductSearchClient client = ProductSearchClient.create();
6863

@@ -76,12 +71,6 @@ public static void createProduct(
7671
.setName(productId)
7772
.setDisplayName(productDisplayName)
7873
.setProductCategory(productCategory)
79-
.setDescription(productDescription)
80-
.addProductLabels(
81-
KeyValue.newBuilder()
82-
.setKey(productLabels.split(",")[0].split("=")[0])
83-
.setValue(productLabels.split(",")[0].split("=")[1])
84-
.build())
8574
.build();
8675
Product product = client.createProduct(projectLocation.toString(), myProduct, productId);
8776

@@ -110,8 +99,8 @@ public static void listProducts(String projectId, String computeRegion) throws I
11099
System.out.println(String.format("\nProduct name: %s", product.getName()));
111100
System.out.println(
112101
String.format(
113-
"Product id: %s",
114-
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
102+
"Product id: %s",
103+
product.getName().substring(product.getName().lastIndexOf('/') + 1)));
115104
System.out.println(String.format("Product display name: %s", product.getDisplayName()));
116105
System.out.println(String.format("Product category: %s", product.getProductCategory()));
117106
System.out.println("Product labels:");
@@ -192,9 +181,10 @@ public static void updateProductLabels(
192181

193182
// Display the product information
194183
System.out.println(String.format("Product name: %s", updatedProduct.getName()));
195-
System.out.println(
196-
String.format(
197-
"Updated product labels: %s", updatedProduct.getProductLabelsList().toString()));
184+
System.out.println(String.format("Updated product labels: "));
185+
for (Product.KeyValue element : updatedProduct.getProductLabelsList()) {
186+
System.out.println(String.format("%s: %s", element.getKey(), element.getValue()));
187+
}
198188
}
199189
// [END vision_product_search_update_product_labels]
200190

@@ -261,9 +251,7 @@ public void argsHelper(String[] args, PrintStream out) throws Exception {
261251
computeRegion,
262252
ns.getString("productId"),
263253
ns.getString("productDisplayName"),
264-
ns.getString("productCategory"),
265-
ns.getString("productDescription"),
266-
ns.getString("productLabels"));
254+
ns.getString("productCategory"));
267255
}
268256
if (ns.get("command").equals("list_products")) {
269257
listProducts(projectId, computeRegion);

vision/snippets/src/main/java/com/example/vision/ProductSetManagement.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static void createProductSet(
7373

7474
// Display the product set information
7575
System.out.println(String.format("Product set name: %s", productSet.getName()));
76+
7677
}
7778
// [END vision_product_search_create_product_set]
7879

@@ -86,10 +87,8 @@ public static void createProductSet(
8687
*/
8788
public static void listProductSets(String projectId, String computeRegion) throws IOException {
8889
ProductSearchClient client = ProductSearchClient.create();
89-
9090
// A resource that represents Google Cloud Platform location.
9191
LocationName projectLocation = LocationName.of(projectId, computeRegion);
92-
9392
// List all the product sets available in the region.
9493
for (ProductSet productSet : client.listProductSets(projectLocation).iterateAll()) {
9594
// Display the product set information
@@ -104,6 +103,7 @@ public static void listProductSets(String projectId, String computeRegion) throw
104103
System.out.println(String.format("\tseconds: %s", productSet.getIndexTime().getSeconds()));
105104
System.out.println(String.format("\tnanos: %s", productSet.getIndexTime().getNanos()));
106105
}
106+
107107
}
108108
// [END vision_product_search_list_product_sets]
109109

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Integration (system) tests for {@link ImportProductSets}. */
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class ImportProductSetsIT {
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static final String COMPUTE_REGION = "us-west1";
36+
private static final String GCS_URI =
37+
"gs://java-docs-samples-testing/product-search/product_sets.csv";
38+
private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing";
39+
private static final String PRODUCT_ID_1 = "fake_product_id_for_testing_1";
40+
private static final String PRODUCT_ID_2 = "fake_product_id_for_testing_2";
41+
private static final String IMAGE_URI_1 = "shoes_1.jpg";
42+
private static final String IMAGE_URI_2 = "shoes_2.jpg";
43+
private ByteArrayOutputStream bout;
44+
private PrintStream out;
45+
46+
@Before
47+
public void setUp() {
48+
bout = new ByteArrayOutputStream();
49+
out = new PrintStream(bout);
50+
System.setOut(out);
51+
}
52+
53+
@After
54+
public void tearDown() throws IOException {
55+
ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_1);
56+
ProductManagement.deleteProduct(PROJECT_ID,COMPUTE_REGION,PRODUCT_ID_2);
57+
ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
58+
System.setOut(null);
59+
}
60+
61+
@Test
62+
public void testImportProductSets() throws Exception {
63+
// Act
64+
ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION);
65+
66+
// Assert
67+
String got = bout.toString();
68+
assertThat(got).doesNotContain(PRODUCT_SET_ID);
69+
70+
// Act
71+
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
72+
73+
// Assert
74+
assertThat(got).doesNotContain(PRODUCT_ID_1);
75+
assertThat(got).doesNotContain(PRODUCT_ID_2);
76+
77+
// Act
78+
ProductInProductSetManagement.listProductsInProductSet(
79+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
80+
81+
// Assert
82+
assertThat(got).doesNotContain(PRODUCT_ID_1);
83+
assertThat(got).doesNotContain(PRODUCT_ID_2);
84+
85+
// Act
86+
ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1);
87+
88+
// Assert
89+
assertThat(got).doesNotContain(IMAGE_URI_1);
90+
91+
// Act
92+
ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2);
93+
94+
// Assert
95+
assertThat(got).doesNotContain(IMAGE_URI_2);
96+
97+
// Act
98+
ImportProductSets.importProductSets(PROJECT_ID, COMPUTE_REGION, GCS_URI);
99+
ProductSetManagement.listProductSets(PROJECT_ID, COMPUTE_REGION);
100+
101+
// Assert
102+
got = bout.toString();
103+
assertThat(got).contains(PRODUCT_SET_ID);
104+
105+
// Act
106+
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
107+
108+
// Assert
109+
assertThat(got).contains(PRODUCT_ID_1);
110+
assertThat(got).contains(PRODUCT_ID_2);
111+
112+
// Act
113+
ProductInProductSetManagement.listProductsInProductSet(
114+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
115+
116+
// Assert
117+
assertThat(got).contains(PRODUCT_ID_1);
118+
assertThat(got).contains(PRODUCT_ID_2);
119+
120+
// Act
121+
ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_1);
122+
123+
// Assert
124+
assertThat(got).contains(IMAGE_URI_1);
125+
126+
// Act
127+
ReferenceImageManagement.listReferenceImagesOfProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID_2);
128+
129+
// Assert
130+
assertThat(got).contains(IMAGE_URI_2);
131+
}
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2018 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Integration (system) tests for {@link ProductInProductSetManagement}. */
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class ProductInProductSetManagementIT {
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
private static final String COMPUTE_REGION = "us-west1";
36+
private static final String PRODUCT_SET_DISPLAY_NAME =
37+
"fake_product_set_display_name_for_testing";
38+
private static final String PRODUCT_SET_ID = "fake_product_set_id_for_testing";
39+
private static final String PRODUCT_DISPLAY_NAME = "fake_product_display_name_for_testing";
40+
private static final String PRODUCT_CATEGORY = "apparel";
41+
private static final String PRODUCT_ID = "fake_product_id_for_testing";
42+
private ByteArrayOutputStream bout;
43+
private PrintStream out;
44+
45+
@Before
46+
public void setUp() throws IOException {
47+
bout = new ByteArrayOutputStream();
48+
out = new PrintStream(bout);
49+
System.setOut(out);
50+
ProductSetManagement.createProductSet(
51+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
52+
ProductManagement.createProduct(
53+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
54+
bout.reset();
55+
}
56+
57+
@After
58+
public void tearDown() throws IOException {
59+
ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID);
60+
ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
61+
System.setOut(null);
62+
}
63+
64+
@Test
65+
public void testAddProductToProductSet() throws Exception {
66+
// Act
67+
ProductInProductSetManagement.listProductsInProductSet(
68+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
69+
70+
// Assert
71+
String got = bout.toString();
72+
assertThat(got).doesNotContain(PRODUCT_ID);
73+
74+
bout.reset();
75+
76+
// Act
77+
ProductInProductSetManagement.addProductToProductSet(
78+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
79+
80+
// Assert
81+
got = bout.toString();
82+
assertThat(got).contains("Product added to product set.");
83+
}
84+
85+
@Test
86+
public void testRemoveProductFromProductSet() throws Exception {
87+
// Act
88+
ProductInProductSetManagement.addProductToProductSet(
89+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
90+
ProductInProductSetManagement.listProductsInProductSet(
91+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
92+
93+
// Assert
94+
String got = bout.toString();
95+
assertThat(got).contains(PRODUCT_ID);
96+
97+
bout.reset();
98+
99+
// Act
100+
ProductInProductSetManagement.removeProductFromProductSet(
101+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
102+
ProductInProductSetManagement.listProductsInProductSet(
103+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
104+
105+
// Assert
106+
got = bout.toString();
107+
assertThat(got).doesNotContain(PRODUCT_ID);
108+
}
109+
}

0 commit comments

Comments
 (0)