From f9c73b3b321994f2da888d2434e1a6c5c6c14f54 Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Tue, 8 Nov 2022 10:23:51 -0500 Subject: [PATCH 1/3] fix: Add centralized retries to all java-vision integration tests. Simplify. --- .../google/cloud/vision/it/ITSystemTest.java | 359 ++++++++---------- 1 file changed, 159 insertions(+), 200 deletions(-) diff --git a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java index ce6ca73c26d7..900aedeb9839 100644 --- a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java +++ b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java @@ -75,17 +75,25 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.List; import java.util.UUID; -import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +@RunWith(Parameterized.class) public class ITSystemTest { + @Parameterized.Parameters + public static Object[][] parameters() { + return new Object[30][0]; + } + private static ImageAnnotatorClient imageAnnotatorClient; private static ProductSearchClient productSearchClient; private static Product product; @@ -121,6 +129,7 @@ public class ITSystemTest { SAMPLE_URI = String.format("https://storage-download.googleapis.com/%s/vision/", GCS_BUCKET); } + private static final int RETRIES = 3; private static final String COMPUTE_REGION = "us-west1"; private static final String LOCATION_NAME = LocationName.of(PROJECT_ID, COMPUTE_REGION).toString(); @@ -222,8 +231,8 @@ public static void tearDown() { imageAnnotatorClient.close(); } - private static List getResponsesList( - String image, Type type, boolean isGcs) throws IOException { + private static AnnotateImageResponse getResponsesList(String image, Type type, boolean isGcs) + throws IOException { ImageSource imgSource; Image img; if (isGcs) { @@ -234,74 +243,109 @@ private static List getResponsesList( img = Image.newBuilder().setContent(imgBytes).build(); } Feature feat = Feature.newBuilder().setType(type).build(); - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - BatchAnnotateImagesResponse response = + + return request(AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build()); + } + + private static AnnotateImageResponse request(AnnotateImageRequest request) { + return request(request, RETRIES); + } + + private static AnnotateImageResponse request(AnnotateImageRequest request, int retries) { + try { + return executeRequest(request); + + } catch (StatusRuntimeException ex) { + if (retries <= 0) { + throw ex; + } + System.out.println("Retrying in 30s: " + ex.getMessage()); + try { + TimeUnit.SECONDS.sleep(30); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + return request(request, retries - 1); + } + + private static AnnotateImageResponse executeRequest(AnnotateImageRequest request) { + BatchAnnotateImagesResponse batchResponse = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request)); - return response.getResponsesList(); + + List responses = batchResponse.getResponsesList(); + if (responses.isEmpty()) { + System.out.println("Request: " + request); + System.out.println("Response: " + batchResponse); + Assert.fail("Empty response."); + } + + AnnotateImageResponse response = assertSingleEntry(responses); + int errorCode = response.getError().getCode(); + if (errorCode != 0) { + throw new StatusRuntimeException(Status.fromCodeValue(errorCode)); + } + return response; + } + + private static T assertSingleEntry(Collection collection) { + assertEquals(1, collection.size()); + return collection.iterator().next(); + } + + private static List assertNotEmpty(AnnotateImageResponse response, List list) { + Assert.assertFalse("Empty response list: " + response, list.isEmpty()); + return list; } @Test public void detectFacesTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("face_no_surprise.jpg", Type.FACE_DETECTION, false); - for (AnnotateImageResponse res : responses) { - for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { - assertEquals(Likelihood.LIKELY, annotation.getAngerLikelihood()); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getJoyLikelihood()); - assertEquals(Likelihood.LIKELY, annotation.getSurpriseLikelihood()); - } + for (FaceAnnotation annotation : assertNotEmpty(res, res.getFaceAnnotationsList())) { + assertEquals(Likelihood.LIKELY, annotation.getAngerLikelihood()); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getJoyLikelihood()); + assertEquals(Likelihood.LIKELY, annotation.getSurpriseLikelihood()); } } @Test public void detectFacesGcsTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("face/face_no_surprise.jpg", Type.FACE_DETECTION, true); - for (AnnotateImageResponse res : responses) { - for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { - assertEquals(Likelihood.LIKELY, annotation.getAngerLikelihood()); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getJoyLikelihood()); - assertEquals(Likelihood.LIKELY, annotation.getSurpriseLikelihood()); - } + for (FaceAnnotation annotation : assertNotEmpty(res, res.getFaceAnnotationsList())) { + assertEquals(Likelihood.LIKELY, annotation.getAngerLikelihood()); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getJoyLikelihood()); + assertEquals(Likelihood.LIKELY, annotation.getSurpriseLikelihood()); } } @Test public void detectLabelsTest() throws IOException { - List responses = - getResponsesList("wakeupcat.jpg", Type.LABEL_DETECTION, false); + AnnotateImageResponse res = getResponsesList("wakeupcat.jpg", Type.LABEL_DETECTION, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLabelAnnotationsList())) { + actual.add(annotation.getDescription()); } assertThat(actual).contains("Whiskers"); } @Test public void detectLabelsGcsTest() throws IOException { - List responses = - getResponsesList("label/wakeupcat.jpg", Type.LABEL_DETECTION, true); + AnnotateImageResponse res = getResponsesList("label/wakeupcat.jpg", Type.LABEL_DETECTION, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLabelAnnotationsList())) { + actual.add(annotation.getDescription()); } assertThat(actual).contains("Whiskers"); } @Test public void detectLandmarksTest() throws IOException { - List responses = - getResponsesList("landmark.jpg", Type.LANDMARK_DETECTION, false); + AnnotateImageResponse res = getResponsesList("landmark.jpg", Type.LANDMARK_DETECTION, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLandmarkAnnotationsList())) { + actual.add(annotation.getDescription()); } assertThat(actual).contains("Palace of Fine Arts"); } @@ -315,109 +359,57 @@ public void detectLandmarksUrlTest() throws Exception { AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - int maxTries = 3; - List actual = - retryIfEmptyOrStatusRuntimeException(() -> addResponsesToList(request), maxTries); - assertThat(actual).contains("Palace of Fine Arts"); - } - - private static List retryIfEmptyOrStatusRuntimeException( - Callable> callable, int retries) throws Exception { - Assert.assertTrue("No more retries available.", retries >= 0); - - try { - List result = callable.call(); - if (result.isEmpty()) { - System.out.println("Retrying due to empty response list... "); - TimeUnit.SECONDS.sleep(30); - return retryIfEmptyOrStatusRuntimeException(callable, retries - 1); - } - return result; - - } catch (StatusRuntimeException ex) { - if (retries == 0) { - throw ex; - } - System.out.println("Retrying due to request throttling or DOS prevention... "); - TimeUnit.SECONDS.sleep(30); - return retryIfEmptyOrStatusRuntimeException(callable, retries - 1); - } - } - - private List addResponsesToList(AnnotateImageRequest request) { List actual = new ArrayList<>(); - - BatchAnnotateImagesResponse response = - imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request)); - List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.getError().getCode() == 14) { - throw new StatusRuntimeException(Status.UNAVAILABLE); - } - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - actual.add(annotation.getDescription()); - } + AnnotateImageResponse res = request(request); + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLandmarkAnnotationsList())) { + actual.add(annotation.getDescription()); } - return actual; + assertThat(actual).contains("Palace of Fine Arts"); } @Test public void detectLandmarksGcsTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("landmark/pofa.jpg", Type.LANDMARK_DETECTION, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLandmarkAnnotationsList())) { + actual.add(annotation.getDescription()); } assertThat(actual).contains("Palace of Fine Arts"); } @Test public void detectLogosTest() throws IOException { - List responses = - getResponsesList("logos.png", Type.LOGO_DETECTION, false); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - assertEquals("Google", annotation.getDescription()); - } + AnnotateImageResponse res = getResponsesList("logos.png", Type.LOGO_DETECTION, false); + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLogoAnnotationsList())) { + assertEquals("Google", annotation.getDescription()); } } @Test public void detectLogosGcsTest() throws IOException { - List responses = - getResponsesList("logo/logo_google.png", Type.LOGO_DETECTION, true); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - assertEquals("Google", annotation.getDescription()); - } + AnnotateImageResponse res = getResponsesList("logo/logo_google.png", Type.LOGO_DETECTION, true); + for (EntityAnnotation annotation : assertNotEmpty(res, res.getLogoAnnotationsList())) { + assertEquals("Google", annotation.getDescription()); } } @Test public void detectTextTest() throws IOException { - List responses = - getResponsesList("text.jpg", Type.TEXT_DETECTION, false); + AnnotateImageResponse res = getResponsesList("text.jpg", Type.TEXT_DETECTION, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getTextAnnotationsList())) { + actual.add(annotation.getDescription()); } assertThat(actual).contains("PS4"); } @Test public void detectTextGcsTest() throws IOException { - List responses = - getResponsesList("text/screen.jpg", Type.TEXT_DETECTION, true); + AnnotateImageResponse res = getResponsesList("text/screen.jpg", Type.TEXT_DETECTION, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - actual.add(annotation.getDescription()); - } + for (EntityAnnotation annotation : assertNotEmpty(res, res.getTextAnnotationsList())) { + actual.add(annotation.getDescription()); } String joinedActual = String.join(" ", actual); assertThat(joinedActual).contains("37%"); @@ -425,64 +417,53 @@ public void detectTextGcsTest() throws IOException { @Test public void detectPropertiesTest() throws IOException { - List responses = - getResponsesList("landmark.jpg", Type.IMAGE_PROPERTIES, false); + AnnotateImageResponse res = getResponsesList("landmark.jpg", Type.IMAGE_PROPERTIES, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - actual.add(color.getPixelFraction()); - } + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : assertNotEmpty(res, colors.getColorsList())) { + actual.add(color.getPixelFraction()); } assertThat(actual).contains((float) 0.14140345); } @Test public void detectPropertiesGcsTest() throws IOException { - List responses = - getResponsesList("landmark/pofa.jpg", Type.IMAGE_PROPERTIES, true); + AnnotateImageResponse res = getResponsesList("landmark/pofa.jpg", Type.IMAGE_PROPERTIES, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - actual.add(color.getPixelFraction()); - } + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : assertNotEmpty(res, colors.getColorsList())) { + actual.add(color.getPixelFraction()); } assertThat(actual).contains((float) 0.14140345); } @Test public void detectSafeSearchTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, false); - for (AnnotateImageResponse res : responses) { - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); - } + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); } @Test public void detectWebEntitiesTest() throws IOException { - List responses = getResponsesList("city.jpg", Type.WEB_DETECTION, false); + AnnotateImageResponse res = getResponsesList("city.jpg", Type.WEB_DETECTION, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) { - actual.add(entity.getDescription()); - } + for (WebDetection.WebEntity entity : + assertNotEmpty(res, res.getWebDetection().getWebEntitiesList())) { + actual.add(entity.getDescription()); } assertThat(actual).contains("Skyscraper"); } @Test public void detectSafeSearchGcsTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("label/wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, true); - for (AnnotateImageResponse res : responses) { - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); - assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); - } + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); + assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); } @Test @@ -496,14 +477,11 @@ public void detectWebEntitiesGcsTest() { AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); - BatchAnnotateImagesResponse response = - imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request)); - List responses = response.getResponsesList(); + AnnotateImageResponse res = request(request); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) { - actual.add(entity.getDescription()); - } + for (WebDetection.WebEntity entity : + assertNotEmpty(res, res.getWebDetection().getWebEntitiesList())) { + actual.add(entity.getDescription()); } assertThat(actual).contains("Palace of Fine Arts"); } @@ -524,14 +502,12 @@ public void detectWebEntitiesIncludeGeoResultsTest() throws IOException { .setImageContext(imageContext) .setImage(img) .build(); - BatchAnnotateImagesResponse response = - imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request)); - List responses = response.getResponsesList(); + + AnnotateImageResponse res = request(request); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) { - actual.add(entity.getDescription()); - } + for (WebDetection.WebEntity entity : + assertNotEmpty(res, res.getWebDetection().getWebEntitiesList())) { + actual.add(entity.getDescription()); } List expectedResults = new ArrayList<>(); expectedResults.add("Electra Tower"); @@ -556,29 +532,23 @@ public void detectWebEntitiesIncludeGeoResultsGcsTest() { .setImageContext(imageContext) .setImage(img) .build(); - BatchAnnotateImagesResponse response = - imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request)); - List responses = response.getResponsesList(); + AnnotateImageResponse res = request(request); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) { - actual.add(entity.getDescription()); - } + for (WebDetection.WebEntity entity : + assertNotEmpty(res, res.getWebDetection().getWebEntitiesList())) { + actual.add(entity.getDescription()); } assertThat(actual).contains("Palace of Fine Arts"); } @Test public void detectCropHintsTest() throws IOException { - List responses = - getResponsesList("wakeupcat.jpg", Type.CROP_HINTS, false); + AnnotateImageResponse res = getResponsesList("wakeupcat.jpg", Type.CROP_HINTS, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - CropHintsAnnotation annotation = imgResponse.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - for (Vertex vertex : hint.getBoundingPoly().getVerticesList()) { - actual.add(vertex.getX()); - } + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : assertNotEmpty(res, annotation.getCropHintsList())) { + for (Vertex vertex : assertNotEmpty(res, hint.getBoundingPoly().getVerticesList())) { + actual.add(vertex.getX()); } } assertEquals(Arrays.asList(210, 476, 476, 210), actual); @@ -586,15 +556,12 @@ public void detectCropHintsTest() throws IOException { @Test public void detectCropHintsGcsTest() throws IOException { - List responses = - getResponsesList("label/wakeupcat.jpg", Type.CROP_HINTS, true); + AnnotateImageResponse res = getResponsesList("label/wakeupcat.jpg", Type.CROP_HINTS, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse imgResponse : responses) { - CropHintsAnnotation annotation = imgResponse.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - for (Vertex vertex : hint.getBoundingPoly().getVerticesList()) { - actual.add(vertex.getX()); - } + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : assertNotEmpty(res, annotation.getCropHintsList())) { + for (Vertex vertex : assertNotEmpty(res, hint.getBoundingPoly().getVerticesList())) { + actual.add(vertex.getX()); } } assertEquals(Arrays.asList(210, 476, 476, 210), actual); @@ -602,37 +569,30 @@ public void detectCropHintsGcsTest() throws IOException { @Test public void detectDocumentTextTest() throws IOException { - List responses = - getResponsesList("text.jpg", Type.DOCUMENT_TEXT_DETECTION, false); + AnnotateImageResponse res = getResponsesList("text.jpg", Type.DOCUMENT_TEXT_DETECTION, false); String actual = ""; - for (AnnotateImageResponse imgResponse : responses) { - TextAnnotation annotation = imgResponse.getFullTextAnnotation(); - actual = annotation.getText(); - } + TextAnnotation annotation = res.getFullTextAnnotation(); + actual = annotation.getText(); assertThat(actual).contains("After preparation is complete"); } @Test public void detectDocumentTextGcs() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("text/screen.jpg", Type.DOCUMENT_TEXT_DETECTION, true); String actual = ""; - for (AnnotateImageResponse imgResponse : responses) { - TextAnnotation annotation = imgResponse.getFullTextAnnotation(); - actual = annotation.getText(); - } + TextAnnotation annotation = res.getFullTextAnnotation(); + actual = annotation.getText(); assertThat(actual).contains("After preparation is complete"); } @Test public void detectLocalizedObjectsTest() throws IOException { - List responses = - getResponsesList("puppies.jpg", Type.OBJECT_LOCALIZATION, false); + AnnotateImageResponse res = getResponsesList("puppies.jpg", Type.OBJECT_LOCALIZATION, false); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { - actual.add(entity.getName()); - } + for (LocalizedObjectAnnotation entity : + assertNotEmpty(res, res.getLocalizedObjectAnnotationsList())) { + actual.add(entity.getName()); } assertThat(actual).contains("Dog"); } @@ -659,13 +619,12 @@ public void getProductTest() { @Test public void detectLocalizedObjectsGcsTest() throws IOException { - List responses = + AnnotateImageResponse res = getResponsesList("object_localization/puppies.jpg", Type.OBJECT_LOCALIZATION, true); List actual = new ArrayList<>(); - for (AnnotateImageResponse res : responses) { - for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { - actual.add(entity.getName()); - } + for (LocalizedObjectAnnotation entity : + assertNotEmpty(res, res.getLocalizedObjectAnnotationsList())) { + actual.add(entity.getName()); } assertThat(actual).contains("Dog"); } From bcc3d546bb22448fbb0970bbb06c53f53fa6168f Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Tue, 8 Nov 2022 10:26:43 -0500 Subject: [PATCH 2/3] fix: Simplify and remove @Parameterized from java-vision ITSystemTest.java --- .../google/cloud/vision/it/ITSystemTest.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java index 900aedeb9839..3ae4bca6d2ab 100644 --- a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java +++ b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java @@ -83,27 +83,16 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -@RunWith(Parameterized.class) public class ITSystemTest { - @Parameterized.Parameters - public static Object[][] parameters() { - return new Object[30][0]; - } - private static ImageAnnotatorClient imageAnnotatorClient; private static ProductSearchClient productSearchClient; private static Product product; private static String formatProductName; - private static String productName; private static ProductSet productSet; - private static String productSetName; private static String formatProductSetName; private static ReferenceImage referenceImage; - private static String referenceImageName; private static String formatReferenceImageName; private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); private static final String ID = UUID.randomUUID().toString().substring(0, 8); @@ -162,7 +151,7 @@ public static void setUp() throws IOException { .setProduct(createProduct) .build(); product = productSearchClient.createProduct(productRequest); - productName = getName(product.getName()); + String productName = getName(product.getName()); formatProductName = ProductName.of(PROJECT_ID, COMPUTE_REGION, productName).toString(); /* create product set */ @@ -175,7 +164,7 @@ public static void setUp() throws IOException { .setProductSetId(PRODUCT_SET_ID) .build(); productSet = productSearchClient.createProductSet(productSetRequest); - productSetName = getName(productSet.getName()); + String productSetName = getName(productSet.getName()); formatProductSetName = ProductSetName.of(PROJECT_ID, COMPUTE_REGION, productSetName).toString(); /* add product to product set */ @@ -195,7 +184,7 @@ public static void setUp() throws IOException { .setReferenceImage(createReferenceImage) .build(); referenceImage = productSearchClient.createReferenceImage(imageRequest); - referenceImageName = getName(referenceImage.getName()); + String referenceImageName = getName(referenceImage.getName()); formatReferenceImageName = ReferenceImageName.of(PROJECT_ID, COMPUTE_REGION, productName, referenceImageName) .toString(); From 09239aa67eb197dfeb3edd49ff79ff2d8863b4a2 Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Tue, 8 Nov 2022 10:29:33 -0500 Subject: [PATCH 3/3] fix: Rename method for clarity and simplify. --- .../google/cloud/vision/it/ITSystemTest.java | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java index 3ae4bca6d2ab..890579508ac5 100644 --- a/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java +++ b/java-vision/google-cloud-vision/src/test/java/com/google/cloud/vision/it/ITSystemTest.java @@ -220,7 +220,7 @@ public static void tearDown() { imageAnnotatorClient.close(); } - private static AnnotateImageResponse getResponsesList(String image, Type type, boolean isGcs) + private static AnnotateImageResponse requestAnnotatedImage(String image, Type type, boolean isGcs) throws IOException { ImageSource imgSource; Image img; @@ -290,7 +290,7 @@ private static List assertNotEmpty(AnnotateImageResponse response, List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLabelAnnotationsList())) { actual.add(annotation.getDescription()); @@ -321,7 +321,8 @@ public void detectLabelsTest() throws IOException { @Test public void detectLabelsGcsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("label/wakeupcat.jpg", Type.LABEL_DETECTION, true); + AnnotateImageResponse res = + requestAnnotatedImage("label/wakeupcat.jpg", Type.LABEL_DETECTION, true); List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLabelAnnotationsList())) { actual.add(annotation.getDescription()); @@ -331,7 +332,8 @@ public void detectLabelsGcsTest() throws IOException { @Test public void detectLandmarksTest() throws IOException { - AnnotateImageResponse res = getResponsesList("landmark.jpg", Type.LANDMARK_DETECTION, false); + AnnotateImageResponse res = + requestAnnotatedImage("landmark.jpg", Type.LANDMARK_DETECTION, false); List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLandmarkAnnotationsList())) { actual.add(annotation.getDescription()); @@ -359,7 +361,7 @@ public void detectLandmarksUrlTest() throws Exception { @Test public void detectLandmarksGcsTest() throws IOException { AnnotateImageResponse res = - getResponsesList("landmark/pofa.jpg", Type.LANDMARK_DETECTION, true); + requestAnnotatedImage("landmark/pofa.jpg", Type.LANDMARK_DETECTION, true); List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLandmarkAnnotationsList())) { actual.add(annotation.getDescription()); @@ -369,7 +371,7 @@ public void detectLandmarksGcsTest() throws IOException { @Test public void detectLogosTest() throws IOException { - AnnotateImageResponse res = getResponsesList("logos.png", Type.LOGO_DETECTION, false); + AnnotateImageResponse res = requestAnnotatedImage("logos.png", Type.LOGO_DETECTION, false); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLogoAnnotationsList())) { assertEquals("Google", annotation.getDescription()); } @@ -377,7 +379,8 @@ public void detectLogosTest() throws IOException { @Test public void detectLogosGcsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("logo/logo_google.png", Type.LOGO_DETECTION, true); + AnnotateImageResponse res = + requestAnnotatedImage("logo/logo_google.png", Type.LOGO_DETECTION, true); for (EntityAnnotation annotation : assertNotEmpty(res, res.getLogoAnnotationsList())) { assertEquals("Google", annotation.getDescription()); } @@ -385,7 +388,7 @@ public void detectLogosGcsTest() throws IOException { @Test public void detectTextTest() throws IOException { - AnnotateImageResponse res = getResponsesList("text.jpg", Type.TEXT_DETECTION, false); + AnnotateImageResponse res = requestAnnotatedImage("text.jpg", Type.TEXT_DETECTION, false); List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getTextAnnotationsList())) { actual.add(annotation.getDescription()); @@ -395,7 +398,7 @@ public void detectTextTest() throws IOException { @Test public void detectTextGcsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("text/screen.jpg", Type.TEXT_DETECTION, true); + AnnotateImageResponse res = requestAnnotatedImage("text/screen.jpg", Type.TEXT_DETECTION, true); List actual = new ArrayList<>(); for (EntityAnnotation annotation : assertNotEmpty(res, res.getTextAnnotationsList())) { actual.add(annotation.getDescription()); @@ -406,7 +409,7 @@ public void detectTextGcsTest() throws IOException { @Test public void detectPropertiesTest() throws IOException { - AnnotateImageResponse res = getResponsesList("landmark.jpg", Type.IMAGE_PROPERTIES, false); + AnnotateImageResponse res = requestAnnotatedImage("landmark.jpg", Type.IMAGE_PROPERTIES, false); List actual = new ArrayList<>(); DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); for (ColorInfo color : assertNotEmpty(res, colors.getColorsList())) { @@ -417,7 +420,8 @@ public void detectPropertiesTest() throws IOException { @Test public void detectPropertiesGcsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("landmark/pofa.jpg", Type.IMAGE_PROPERTIES, true); + AnnotateImageResponse res = + requestAnnotatedImage("landmark/pofa.jpg", Type.IMAGE_PROPERTIES, true); List actual = new ArrayList<>(); DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); for (ColorInfo color : assertNotEmpty(res, colors.getColorsList())) { @@ -429,7 +433,7 @@ public void detectPropertiesGcsTest() throws IOException { @Test public void detectSafeSearchTest() throws IOException { AnnotateImageResponse res = - getResponsesList("wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, false); + requestAnnotatedImage("wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, false); SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); @@ -437,7 +441,7 @@ public void detectSafeSearchTest() throws IOException { @Test public void detectWebEntitiesTest() throws IOException { - AnnotateImageResponse res = getResponsesList("city.jpg", Type.WEB_DETECTION, false); + AnnotateImageResponse res = requestAnnotatedImage("city.jpg", Type.WEB_DETECTION, false); List actual = new ArrayList<>(); for (WebDetection.WebEntity entity : assertNotEmpty(res, res.getWebDetection().getWebEntitiesList())) { @@ -449,7 +453,7 @@ public void detectWebEntitiesTest() throws IOException { @Test public void detectSafeSearchGcsTest() throws IOException { AnnotateImageResponse res = - getResponsesList("label/wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, true); + requestAnnotatedImage("label/wakeupcat.jpg", Type.SAFE_SEARCH_DETECTION, true); SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); assertEquals(Likelihood.VERY_UNLIKELY, annotation.getAdult()); assertEquals(Likelihood.VERY_UNLIKELY, annotation.getRacy()); @@ -532,7 +536,7 @@ public void detectWebEntitiesIncludeGeoResultsGcsTest() { @Test public void detectCropHintsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("wakeupcat.jpg", Type.CROP_HINTS, false); + AnnotateImageResponse res = requestAnnotatedImage("wakeupcat.jpg", Type.CROP_HINTS, false); List actual = new ArrayList<>(); CropHintsAnnotation annotation = res.getCropHintsAnnotation(); for (CropHint hint : assertNotEmpty(res, annotation.getCropHintsList())) { @@ -545,7 +549,7 @@ public void detectCropHintsTest() throws IOException { @Test public void detectCropHintsGcsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("label/wakeupcat.jpg", Type.CROP_HINTS, true); + AnnotateImageResponse res = requestAnnotatedImage("label/wakeupcat.jpg", Type.CROP_HINTS, true); List actual = new ArrayList<>(); CropHintsAnnotation annotation = res.getCropHintsAnnotation(); for (CropHint hint : assertNotEmpty(res, annotation.getCropHintsList())) { @@ -558,26 +562,26 @@ public void detectCropHintsGcsTest() throws IOException { @Test public void detectDocumentTextTest() throws IOException { - AnnotateImageResponse res = getResponsesList("text.jpg", Type.DOCUMENT_TEXT_DETECTION, false); - String actual = ""; + AnnotateImageResponse res = + requestAnnotatedImage("text.jpg", Type.DOCUMENT_TEXT_DETECTION, false); TextAnnotation annotation = res.getFullTextAnnotation(); - actual = annotation.getText(); + String actual = annotation.getText(); assertThat(actual).contains("After preparation is complete"); } @Test public void detectDocumentTextGcs() throws IOException { AnnotateImageResponse res = - getResponsesList("text/screen.jpg", Type.DOCUMENT_TEXT_DETECTION, true); - String actual = ""; + requestAnnotatedImage("text/screen.jpg", Type.DOCUMENT_TEXT_DETECTION, true); TextAnnotation annotation = res.getFullTextAnnotation(); - actual = annotation.getText(); + String actual = annotation.getText(); assertThat(actual).contains("After preparation is complete"); } @Test public void detectLocalizedObjectsTest() throws IOException { - AnnotateImageResponse res = getResponsesList("puppies.jpg", Type.OBJECT_LOCALIZATION, false); + AnnotateImageResponse res = + requestAnnotatedImage("puppies.jpg", Type.OBJECT_LOCALIZATION, false); List actual = new ArrayList<>(); for (LocalizedObjectAnnotation entity : assertNotEmpty(res, res.getLocalizedObjectAnnotationsList())) { @@ -609,7 +613,7 @@ public void getProductTest() { @Test public void detectLocalizedObjectsGcsTest() throws IOException { AnnotateImageResponse res = - getResponsesList("object_localization/puppies.jpg", Type.OBJECT_LOCALIZATION, true); + requestAnnotatedImage("object_localization/puppies.jpg", Type.OBJECT_LOCALIZATION, true); List actual = new ArrayList<>(); for (LocalizedObjectAnnotation entity : assertNotEmpty(res, res.getLocalizedObjectAnnotationsList())) {