Skip to content

Commit

Permalink
samples: Purge Products (#1569)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
munkhuushmgl authored and chingor13 committed Aug 13, 2020
1 parent ec71795 commit a61d4d4
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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<Empty, BatchOperationMetadata> 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]
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a61d4d4

Please sign in to comment.