Skip to content

Commit a61d4d4

Browse files
munkhuushmglchingor13
authored andcommitted
samples: Purge Products (#1569)
* Batch delete & tests * purge products * removed TODOs comments, added UUID for tests * applied new format for purge products * fixed nits * removed javadocs headers & renamed package names * fixed checkstyle * deleted unncessary parsers * removed force params
1 parent ec71795 commit a61d4d4

8 files changed

+302
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
172172
}
173173
if (ns.get("command").equals("remove_product_from_product_set")) {
174174
removeProductFromProductSet(
175-
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
175+
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
176176
}
177177

178178
} catch (ArgumentParserException e) {

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

-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ public void argsHelper(String[] args, PrintStream out) throws Exception {
268268
if (ns.get("command").equals("delete_product")) {
269269
deleteProduct(projectId, computeRegion, ns.getString("productId"));
270270
}
271-
272271
} catch (ArgumentParserException e) {
273272
parser.handleError(e);
274273
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.example.vision;
1818

19+
import com.google.cloud.vision.v1.Image;
20+
import com.google.cloud.vision.v1.ImageName;
1921
import com.google.cloud.vision.v1.ProductSearchClient;
2022
import com.google.cloud.vision.v1.ReferenceImage;
2123

@@ -123,7 +125,7 @@ public static void getReferenceImage(
123125

124126
// Get the full path of the reference image.
125127
String formattedName =
126-
ProductSearchClient.formatImageName(
128+
ImageName.format(
127129
projectId, computeRegion, productId, referenceImageId);
128130
// Get complete detail of the reference image.
129131
ReferenceImage image = client.getReferenceImage(formattedName);
@@ -158,7 +160,7 @@ public static void deleteReferenceImage(
158160

159161
// Get the full path of the reference image.
160162
String formattedName =
161-
ProductSearchClient.formatImageName(
163+
ImageName.format(
162164
projectId, computeRegion, productId, referenceImageId);
163165
// Delete the reference image.
164166
client.deleteReferenceImage(formattedName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.snippets;
18+
19+
// [START vision_product_search_purge_orphan_products]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.vision.v1.LocationName;
22+
import com.google.cloud.vision.v1.ProductSearchClient;
23+
import com.google.cloud.vision.v1.PurgeProductsRequest;
24+
25+
import java.util.concurrent.TimeUnit;
26+
27+
28+
public class PurgeProducts {
29+
30+
// Delete the product and all its reference images.
31+
public static void purgeOrphanProducts(String projectId, String computeRegion)
32+
throws Exception {
33+
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String computeRegion = "us-central1";
36+
// boolean force = true;
37+
38+
try (ProductSearchClient client = ProductSearchClient.create()) {
39+
String parent = LocationName.format(projectId, computeRegion);
40+
41+
// The purge operation is async.
42+
PurgeProductsRequest request = PurgeProductsRequest
43+
.newBuilder()
44+
.setDeleteOrphanProducts(true)
45+
// The operation is irreversible and removes multiple products.
46+
// The user is required to pass in force=True to actually perform the
47+
// purge.
48+
// If force is not set to True, the service raises an exception.
49+
.setForce(true)
50+
.setParent(parent)
51+
.build();
52+
53+
OperationFuture response = client.purgeProductsAsync(request);
54+
response.getPollingFuture().get(90, TimeUnit.SECONDS);
55+
56+
System.out.println("Orphan products deleted.");
57+
}
58+
}
59+
}
60+
// [END vision_product_search_purge_orphan_products]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2019 Google LLC
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.snippets;
18+
19+
// [START vision_product_search_purge_products_in_product_set]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.vision.v1.BatchOperationMetadata;
22+
import com.google.cloud.vision.v1.LocationName;
23+
import com.google.cloud.vision.v1.ProductSearchClient;
24+
import com.google.cloud.vision.v1.ProductSetPurgeConfig;
25+
import com.google.cloud.vision.v1.PurgeProductsRequest;
26+
import com.google.protobuf.Empty;
27+
28+
import java.util.concurrent.TimeUnit;
29+
30+
public class PurgeProductsInProductSet {
31+
32+
// Delete all products in a product set.
33+
public static void purgeProductsInProductSet(
34+
String projectId, String location, String productSetId)
35+
throws Exception {
36+
37+
// String projectId = "YOUR_PROJECT_ID";
38+
// String location = "us-central1";
39+
// String productSetId = "YOUR_PRODUCT_SET_ID";
40+
// boolean force = true;
41+
42+
try (ProductSearchClient client = ProductSearchClient.create()) {
43+
44+
String parent = LocationName.format(projectId, location);
45+
ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig
46+
.newBuilder()
47+
.setProductSetId(productSetId)
48+
.build();
49+
50+
PurgeProductsRequest request = PurgeProductsRequest
51+
.newBuilder()
52+
.setParent(parent)
53+
.setProductSetPurgeConfig(productSetPurgeConfig)
54+
// The operation is irreversible and removes multiple products.
55+
// The user is required to pass in force=True to actually perform the
56+
// purge.
57+
// If force is not set to True, the service raises an exception.
58+
.setForce(true)
59+
.build();
60+
61+
OperationFuture<Empty, BatchOperationMetadata> response = client.purgeProductsAsync(request);
62+
response.getPollingFuture().get(90, TimeUnit.SECONDS);
63+
64+
System.out.println("Products removed from product set.");
65+
}
66+
}
67+
}
68+
// [END vision_product_search_purge_products_in_product_set]

vision/snippets/src/test/java/com/example/vision/ProductInProductSetManagementIT.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.ByteArrayOutputStream;
2222
import java.io.IOException;
2323
import java.io.PrintStream;
24+
import java.util.UUID;
25+
2426
import org.junit.After;
2527
import org.junit.Before;
2628
import org.junit.Test;
@@ -35,7 +37,7 @@ public class ProductInProductSetManagementIT {
3537
private static final String COMPUTE_REGION = "us-west1";
3638
private static final String PRODUCT_SET_DISPLAY_NAME =
3739
"fake_pdt_set_display_name_for_testing";
38-
private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing";
40+
private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing" + UUID.randomUUID();
3941
private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing";
4042
private static final String PRODUCT_CATEGORY = "apparel";
4143
private static final String PRODUCT_ID = "fake_pdt_id_for_testing";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2019 Google LLC
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 vision.snippets;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.example.vision.ProductInProductSetManagement;
22+
import com.example.vision.ProductManagement;
23+
import com.example.vision.ProductSetManagement;
24+
import com.example.vision.snippets.PurgeProductsInProductSet;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
34+
public class ProductInProductSetManagementTests {
35+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
36+
private static final String COMPUTE_REGION = "us-west1";
37+
private static final String PRODUCT_SET_DISPLAY_NAME =
38+
"fake_pdt_set_display_name_for_testing";
39+
private static final String PRODUCT_SET_ID = "fake_pdt_set_id_for_testing" + UUID.randomUUID();
40+
private static final String PRODUCT_DISPLAY_NAME = "fake_pdt_display_name_for_testing";
41+
private static final String PRODUCT_CATEGORY = "apparel";
42+
private static final String PRODUCT_ID = "fake_pdt_id_for_testing";
43+
private ByteArrayOutputStream bout;
44+
private PrintStream out;
45+
46+
@Before
47+
public void setUp() throws IOException {
48+
bout = new ByteArrayOutputStream();
49+
out = new PrintStream(bout);
50+
System.setOut(out);
51+
ProductSetManagement.createProductSet(
52+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
53+
ProductManagement.createProduct(
54+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
55+
bout.reset();
56+
}
57+
58+
@After
59+
public void tearDown() throws IOException {
60+
ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID);
61+
ProductSetManagement.deleteProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
62+
System.setOut(null);
63+
}
64+
65+
@Test
66+
public void testPurgeProductsInProductSet() throws Exception {
67+
// Act
68+
ProductInProductSetManagement.addProductToProductSet(
69+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
70+
ProductManagement.listProducts(
71+
PROJECT_ID, COMPUTE_REGION);
72+
73+
// Assert
74+
String got = bout.toString();
75+
assertThat(got).contains(PRODUCT_ID);
76+
77+
bout.reset();
78+
PurgeProductsInProductSet.purgeProductsInProductSet(
79+
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);
80+
81+
ProductManagement.listProducts(
82+
PROJECT_ID, COMPUTE_REGION);
83+
84+
// Assert
85+
got = bout.toString();
86+
assertThat(got).doesNotContain(PRODUCT_ID);
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2019 Google LLC
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 vision.snippets;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.example.vision.ProductManagement;
22+
import com.example.vision.snippets.PurgeProducts;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.PrintStream;
26+
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.JUnit4;
32+
33+
@RunWith(JUnit4.class)
34+
public class ProductManagementTests {
35+
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
private static final String COMPUTE_REGION = "us-west1";
38+
private static final String PRODUCT_DISPLAY_NAME = "fake_prod_display_name_for_testing";
39+
private static final String PRODUCT_CATEGORY = "homegoods";
40+
private static final String PRODUCT_ID = "fake_prod_id_for_testing";
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
44+
@Before
45+
public void setUp() throws IOException {
46+
bout = new ByteArrayOutputStream();
47+
out = new PrintStream(bout);
48+
System.setOut(out);
49+
}
50+
51+
@After
52+
public void tearDown() throws IOException {
53+
ProductManagement.deleteProduct(PROJECT_ID, COMPUTE_REGION, PRODUCT_ID);
54+
System.setOut(null);
55+
}
56+
57+
@Test
58+
public void testPurgeOrphanProducts() throws Exception {
59+
// Act
60+
ProductManagement.createProduct(
61+
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
62+
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
63+
64+
// Assert
65+
String got = bout.toString();
66+
assertThat(got).contains(PRODUCT_ID);
67+
68+
bout.reset();
69+
70+
// Act
71+
PurgeProducts.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION);
72+
73+
// Assert
74+
got = bout.toString();
75+
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
76+
assertThat(got).doesNotContain(PRODUCT_ID);
77+
}
78+
}

0 commit comments

Comments
 (0)