Skip to content

Commit

Permalink
samples: update shared config (#2443)
Browse files Browse the repository at this point in the history
* update shared config

* Update to 1.0.13

* lint

* Fix linting

* lint

* fix imports

Co-authored-by: Les Vogel <lesv@users.noreply.github.com>
  • Loading branch information
2 people authored and chingor13 committed Aug 13, 2020
1 parent ec0f0c6 commit 04a446d
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
// [END vision_product_search_tutorial_import]
import java.io.PrintStream;
import javax.swing.JPanel;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.google.cloud.vision.v1.Product;
import com.google.cloud.vision.v1.ProductName;
import com.google.cloud.vision.v1.ProductSearchClient;

import java.io.IOException;
import java.io.PrintStream;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand Down Expand Up @@ -172,7 +170,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 @@ -20,10 +20,8 @@
import com.google.cloud.vision.v1.Product.KeyValue;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.protobuf.FieldMask;

import java.io.IOException;
import java.io.PrintStream;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
import com.google.cloud.vision.v1.ProductSearchResults.Result;
import com.google.cloud.vision.v1.ProductSetName;
import com.google.protobuf.ByteString;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand All @@ -48,10 +46,9 @@
* This application demonstrates how to perform similar product search operation in Cloud Vision
* Product Search.
*
* For more information, see the tutorial page at
* <p>For more information, see the tutorial page at
* https://cloud.google.com/vision/product-search/docs/
*/

public class ProductSearch {

// [START vision_product_search_get_similar_products]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.google.cloud.vision.v1.CreateProductSetRequest;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.ProductSet;

import java.io.IOException;
import java.io.PrintStream;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

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;

import java.io.IOException;
import java.io.PrintStream;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
Expand Down Expand Up @@ -125,8 +122,7 @@ public static void getReferenceImage(

// Get the full path of the reference image.
String formattedName =
ImageName.format(
projectId, computeRegion, productId, referenceImageId);
ImageName.format(projectId, computeRegion, productId, referenceImageId);
// Get complete detail of the reference image.
ReferenceImage image = client.getReferenceImage(formattedName);
// Display the reference image information.
Expand Down Expand Up @@ -160,8 +156,7 @@ public static void deleteReferenceImage(

// Get the full path of the reference image.
String formattedName =
ImageName.format(
projectId, computeRegion, productId, referenceImageId);
ImageName.format(projectId, computeRegion, productId, referenceImageId);
// Delete the reference image.
client.deleteReferenceImage(formattedName);
System.out.println("Reference image deleted from product.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
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 {
public static void purgeOrphanProducts(String projectId, String computeRegion) throws Exception {

// String projectId = "YOUR_PROJECT_ID";
// String computeRegion = "us-central1";
Expand All @@ -39,8 +36,8 @@ public static void purgeOrphanProducts(String projectId, String computeRegion)
String parent = LocationName.format(projectId, computeRegion);

// The purge operation is async.
PurgeProductsRequest request = PurgeProductsRequest
.newBuilder()
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
Expand All @@ -57,4 +54,4 @@ public static void purgeOrphanProducts(String projectId, String computeRegion)
}
}
}
// [END vision_product_search_purge_orphan_products]
// [END vision_product_search_purge_orphan_products]
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
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, String location, String productSetId) throws Exception {

// String projectId = "YOUR_PROJECT_ID";
// String location = "us-central1";
Expand All @@ -42,13 +40,11 @@ public static void purgeProductsInProductSet(
try (ProductSearchClient client = ProductSearchClient.create()) {

String parent = LocationName.format(projectId, location);
ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig
.newBuilder()
.setProductSetId(productSetId)
.build();
ProductSetPurgeConfig productSetPurgeConfig =
ProductSetPurgeConfig.newBuilder().setProductSetId(productSetId).build();

PurgeProductsRequest request = PurgeProductsRequest
.newBuilder()
PurgeProductsRequest request =
PurgeProductsRequest.newBuilder()
.setParent(parent)
.setProductSetPurgeConfig(productSetPurgeConfig)
// The operation is irreversible and removes multiple products.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -36,11 +35,11 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_SET_ID =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_DISPLAY_NAME =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_CATEGORY = "apparel";
private static final String PRODUCT_ID = String.format("test_%s", UUID.randomUUID().toString());
private ByteArrayOutputStream bout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -36,7 +35,7 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_CATEGORY = "homegoods";
private static final String PRODUCT_ID = String.format("test_%s", UUID.randomUUID().toString());
private static final String KEY = String.format("test_%s", UUID.randomUUID().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;

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 ProductSearch}.Tests rely on pre-created product set
* that has been indexed.
**/
* Integration (system) tests for {@link ProductSearch}.Tests rely on pre-created product set that
* has been indexed.
*/
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class ProductSearchIT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -35,9 +34,9 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_SET_DISPLAY_NAME =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private ByteArrayOutputStream bout;
private PrintStream out;

Expand All @@ -56,7 +55,7 @@ public void tearDown() {
@Test
public void testCreateDeleteProductSet() throws Exception {
ProductSetManagement.createProductSet(
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
String got = bout.toString();
assertThat(got).contains(PRODUCT_SET_ID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -36,13 +35,12 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_CATEGORY = "apparel";
private static final String PRODUCT_ID = String.format("test_%s", UUID.randomUUID().toString());
private static final String REFERENCE_IMAGE_ID =
String.format("test_%s", UUID.randomUUID().toString());
private static final String GCS_URI =
"gs://java-docs-samples-testing/product-search/shoes_1.jpg";
String.format("test_%s", UUID.randomUUID().toString());
private static final String GCS_URI = "gs://java-docs-samples-testing/product-search/shoes_1.jpg";
private ByteArrayOutputStream bout;
private PrintStream out;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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,11 +34,11 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_SET_ID =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_DISPLAY_NAME =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_CATEGORY = "apparel";
private static final String PRODUCT_ID = String.format("test_%s", UUID.randomUUID().toString());
private ByteArrayOutputStream bout;
Expand All @@ -51,9 +50,9 @@ public void setUp() throws IOException {
out = new PrintStream(bout);
System.setOut(out);
ProductSetManagement.createProductSet(
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, PRODUCT_SET_DISPLAY_NAME);
ProductManagement.createProduct(
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
bout.reset();
}

Expand All @@ -68,20 +67,17 @@ public void tearDown() throws IOException {
public void testPurgeProductsInProductSet() throws Exception {
// Act
ProductInProductSetManagement.addProductToProductSet(
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
ProductManagement.listProducts(
PROJECT_ID, COMPUTE_REGION);
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);
PurgeProductsInProductSet.purgeProductsInProductSet(PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID);

ProductManagement.listProducts(
PROJECT_ID, COMPUTE_REGION);
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);

// Assert
got = bout.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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 @@ -37,7 +36,7 @@ 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 =
String.format("test_%s", UUID.randomUUID().toString());
String.format("test_%s", UUID.randomUUID().toString());
private static final String PRODUCT_CATEGORY = "homegoods";
private static final String PRODUCT_ID = String.format("test_%s", UUID.randomUUID().toString());
private ByteArrayOutputStream bout;
Expand All @@ -60,7 +59,7 @@ public void tearDown() throws IOException {
public void testPurgeOrphanProducts() throws Exception {
// Act
ProductManagement.createProduct(
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);

// Assert
Expand Down

0 comments on commit 04a446d

Please sign in to comment.