Skip to content

Commit

Permalink
Migrating "spring-cloud-gcp-vision" tests to JUnit5 (#1027)
Browse files Browse the repository at this point in the history
* Migrating tests to JUnit5

* code smell changes
  • Loading branch information
ddixit14 authored Mar 24, 2022
1 parent 98e5899 commit 356e1c3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.cloud.spring.vision.CloudVisionTemplate.EMPTY_RESPONSE_ERROR_MESSAGE;
import static com.google.cloud.spring.vision.CloudVisionTemplate.READ_BYTES_ERROR_MESSAGE;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -42,10 +43,8 @@
import io.grpc.Status.Code;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
Expand All @@ -56,10 +55,7 @@
*
* @since 1.1
*/
public class CloudVisionTemplateTests {

/** Used to test exception messages and types. * */
@Rule public ExpectedException expectedException = ExpectedException.none();
class CloudVisionTemplateTests {

// Resource representing a fake image blob
private static final Resource FAKE_IMAGE = new ByteArrayResource("fake_image".getBytes());
Expand All @@ -82,14 +78,14 @@ public class CloudVisionTemplateTests {

private CloudVisionTemplate cloudVisionTemplate;

@Before
public void setupVisionTemplateMock() {
@BeforeEach
void setupVisionTemplateMock() {
this.imageAnnotatorClient = Mockito.mock(ImageAnnotatorClient.class);
this.cloudVisionTemplate = new CloudVisionTemplate(this.imageAnnotatorClient);
}

@Test
public void testAddImageContext_analyzeImage() throws IOException {
void testAddImageContext_analyzeImage() throws IOException {
when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
.thenReturn(DEFAULT_API_RESPONSE);

Expand All @@ -113,7 +109,7 @@ public void testAddImageContext_analyzeImage() throws IOException {
}

@Test
public void testAddImageContext_extractText() throws IOException {
void testAddImageContext_extractText() throws IOException {
when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
.thenReturn(DEFAULT_API_RESPONSE);

Expand All @@ -137,7 +133,7 @@ public void testAddImageContext_extractText() throws IOException {
}

@Test
public void testAddInputConfig_extractText() throws IOException {
void testAddInputConfig_extractText() throws IOException {
when(this.imageAnnotatorClient.batchAnnotateFiles(any(BatchAnnotateFilesRequest.class)))
.thenReturn(DEFAULT_FILES_API_RESPONSE);

Expand All @@ -160,29 +156,28 @@ public void testAddInputConfig_extractText() throws IOException {
}

@Test
public void testEmptyClientResponseError() {
void testEmptyClientResponseError() {
when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
.thenReturn(BatchAnnotateImagesResponse.getDefaultInstance());

this.expectedException.expect(CloudVisionException.class);
this.expectedException.expectMessage(EMPTY_RESPONSE_ERROR_MESSAGE);

this.cloudVisionTemplate.analyzeImage(FAKE_IMAGE, Type.TEXT_DETECTION);
assertThatThrownBy(() -> this.cloudVisionTemplate.analyzeImage(FAKE_IMAGE, Type.TEXT_DETECTION))
.isInstanceOf(CloudVisionException.class)
.hasMessage(EMPTY_RESPONSE_ERROR_MESSAGE);
}

@Test
public void testEmptyClientAnnotateFileResponseError() {
void testEmptyClientAnnotateFileResponseError() {
when(this.imageAnnotatorClient.batchAnnotateFiles(any(BatchAnnotateFilesRequest.class)))
.thenReturn(BatchAnnotateFilesResponse.getDefaultInstance());

this.expectedException.expect(CloudVisionException.class);
this.expectedException.expectMessage(EMPTY_RESPONSE_ERROR_MESSAGE);
assertThatThrownBy(() -> this.cloudVisionTemplate.analyzeFile(FAKE_PDF, "application/pdf", Type.TEXT_DETECTION))
.isInstanceOf(CloudVisionException.class)
.hasMessage(EMPTY_RESPONSE_ERROR_MESSAGE);

this.cloudVisionTemplate.analyzeFile(FAKE_PDF, "application/pdf", Type.TEXT_DETECTION);
}

@Test
public void testExtractTextError() {
void testExtractTextError() {
AnnotateImageResponse response =
AnnotateImageResponse.newBuilder()
.setError(
Expand All @@ -197,27 +192,28 @@ public void testExtractTextError() {
when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class)))
.thenReturn(responseBatch);

this.expectedException.expect(CloudVisionException.class);
this.expectedException.expectMessage("Error Message from Vision API.");

this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE);
assertThatThrownBy(() -> this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE))
.isInstanceOf(CloudVisionException.class)
.hasMessage("Error Message from Vision API.");
}

@Test
public void testResourceReadingError() {
this.expectedException.expect(CloudVisionException.class);
this.expectedException.expectMessage(READ_BYTES_ERROR_MESSAGE);
void testResourceReadingError() {

this.cloudVisionTemplate.analyzeImage(new BadResource(), Type.LABEL_DETECTION);
Resource imageResource = new BadResource();
assertThatThrownBy(() -> this.cloudVisionTemplate.analyzeImage(imageResource, Type.LABEL_DETECTION))
.isInstanceOf(CloudVisionException.class)
.hasMessageContaining(READ_BYTES_ERROR_MESSAGE);
}

@Test
public void testFileResourceReadingError() {
this.expectedException.expect(CloudVisionException.class);
this.expectedException.expectMessage(READ_BYTES_ERROR_MESSAGE);
void testFileResourceReadingError() {

this.cloudVisionTemplate.analyzeFile(
new BadResource(), "application/pdf", Type.LABEL_DETECTION);
Resource imageResource = new BadResource();
assertThatThrownBy(() -> this.cloudVisionTemplate.analyzeFile(imageResource, "application/pdf", Type.LABEL_DETECTION))
.isInstanceOf(CloudVisionException.class)
.hasMessageContaining(READ_BYTES_ERROR_MESSAGE);
}

private static final class BadResource extends AbstractResource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,30 @@
import com.google.cloud.spring.storage.GoogleStorageLocation;
import com.google.cloud.storage.Storage;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@RunWith(SpringRunner.class)
public class DocumentOcrTemplateTests {
@ExtendWith(SpringExtension.class)
class DocumentOcrTemplateTests {

private Storage storage;

private ImageAnnotatorClient imageAnnotatorClient;

private DocumentOcrTemplate documentOcrTemplate;

@Before
public void setupDocumentTemplateMocks() {
@BeforeEach
void setupDocumentTemplateMocks() {
this.storage = Mockito.mock(Storage.class);
this.imageAnnotatorClient = Mockito.mock(ImageAnnotatorClient.class);
this.documentOcrTemplate =
new DocumentOcrTemplate(imageAnnotatorClient, storage, Runnable::run, 10);
this.documentOcrTemplate = new DocumentOcrTemplate(imageAnnotatorClient, storage, Runnable::run, 10);
}

@Test
public void testValidateGcsFileInputs() {
void testValidateGcsFileInputs() {
GoogleStorageLocation folder = GoogleStorageLocation.forFolder("bucket", "path/to/folder/");

assertThatThrownBy(() -> this.documentOcrTemplate.runOcrForDocument(folder, folder))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.cloud.spring.vision.it;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

import com.google.cloud.spring.storage.GoogleStorageLocation;
import com.google.cloud.spring.vision.DocumentOcrResultSet;
Expand All @@ -29,31 +28,23 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.concurrent.ListenableFuture;

@RunWith(SpringRunner.class)
@EnabledIfSystemProperty(named = "it.vision", matches = "true")
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {VisionTestConfiguration.class})
public class DocumentOcrTemplateIntegrationTests {
class DocumentOcrTemplateIntegrationTests {

@Autowired private DocumentOcrTemplate documentOcrTemplate;

@BeforeClass
public static void prepare() {
assumeThat(System.getProperty("it.vision"))
.as(
"Vision Sample integration tests are disabled. "
+ "Please use '-Dit.vision=true' to enable them.")
.isEqualTo("true");
}

@Test
public void testDocumentOcrTemplate()
void testDocumentOcrTemplate()
throws ExecutionException, InterruptedException, InvalidProtocolBufferException,
TimeoutException {

Expand Down Expand Up @@ -89,7 +80,7 @@ public void testDocumentOcrTemplate()
}

@Test
public void testParseOcrResultSet() throws InvalidProtocolBufferException {
void testParseOcrResultSet() throws InvalidProtocolBufferException {
GoogleStorageLocation ocrOutputPrefix =
GoogleStorageLocation.forFolder("vision-integration-test-bucket", "json_output_set/");

Expand All @@ -100,7 +91,7 @@ public void testParseOcrResultSet() throws InvalidProtocolBufferException {
}

@Test
public void testParseOcrFile() throws InvalidProtocolBufferException {
void testParseOcrFile() throws InvalidProtocolBufferException {
GoogleStorageLocation ocrOutputFile =
GoogleStorageLocation.forFile(
"vision-integration-test-bucket", "json_output_set/test_output-2-to-2.json");
Expand Down

0 comments on commit 356e1c3

Please sign in to comment.