-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
ec71795
commit a61d4d4
Showing
8 changed files
with
302 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
vision/snippets/src/main/java/com/example/vision/snippets/PurgeProducts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
68 changes: 68 additions & 0 deletions
68
vision/snippets/src/main/java/com/example/vision/snippets/PurgeProductsInProductSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
vision/snippets/src/test/java/vision/snippets/ProductInProductSetManagementTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
vision/snippets/src/test/java/vision/snippets/ProductManagementTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |