Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the Product Search tests. #1195

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
/**
* This application demonstrates how to perform basic operations on Products.
*
* 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 ProductManagement {

// [START vision_product_search_create_product]
Expand All @@ -51,18 +50,14 @@ 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(
String projectId,
String computeRegion,
String productId,
String productDisplayName,
String productCategory,
String productDescription,
String productLabels)
String productCategory)
throws IOException {
ProductSearchClient client = ProductSearchClient.create();

Expand All @@ -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);

Expand Down Expand Up @@ -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:");
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand All @@ -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]

Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "java-docs-samples-testing";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switch "java-docs-samples-testing" to 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 = "java-docs-samples-testing";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}
Loading