Skip to content

Commit

Permalink
fix: add runtime hints for vision (#1991)
Browse files Browse the repository at this point in the history
* fix: add runtime hints for Vision and set up CI

---------

Co-authored-by: Mridula <[email protected]>
Co-authored-by: mpeddada1 <[email protected]>
  • Loading branch information
3 people authored Jul 13, 2023
1 parent ac01830 commit dcbf202
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 9 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/NativeTests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,34 @@ jobs:
../mvnw \
--batch-mode \
--define aggregate=true \
--activate-profiles native-sample-config \
surefire-report:report-only
working-directory: ./spring-cloud-gcp-samples
- name: Native Tests in Modules
id: intTest3
continue-on-error: true
run: |
./mvnw \
--batch-mode \
--activate-profiles spring-native,\!default \
--define maven.javadoc.skip=true \
test
- name: Retry on Failure
id: intTest4
if: steps.intTest3.outcome == 'failure'
run: |
./mvnw \
--batch-mode \
--activate-profiles spring-native,\!default \
--define maven.javadoc.skip=true \
test
- name: Aggregate Report
run: |
./mvnw \
--batch-mode \
--define aggregate=true \
--activate-profiles spring-native,\!default \
surefire-report:report-only
- name: Archive logs
if: always()
continue-on-error: true
Expand Down
57 changes: 56 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@
<profiles>
<profile>
<id>spring-native</id>
<modules>
<module>spring-cloud-gcp-vision</module>
</modules>

<dependencies>
<dependency>
Expand All @@ -376,7 +379,8 @@
</includes>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<systemPropertyVariables>
<!--integration tests are not invoked unless the relevant system property is set to true here. -->
<!--integration tests are not invoked unless the relevant system property is set to true here. -->
<it.vision>true</it.vision>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down Expand Up @@ -407,6 +411,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<executions>
<execution>
<id>process-test-aot</id>
Expand Down Expand Up @@ -457,6 +462,56 @@
<module>spring-cloud-gcp-samples</module>
</modules>
</profile>
<profile>
<!-- cloud RAD generation -->
<id>docFX</id>
<activation>
<property>
<!-- activate with -P docFX -->
<name>docFX</name>
</property>
<jdk>17</jdk>
</activation>
<properties>
<!-- default config values -->
<docletName>java-docfx-doclet-1.6.0</docletName>
<outputpath>${project.build.directory}/docfx-yml</outputpath>
<projectname>spring-framework-on-google-cloud</projectname>
<excludeclasses></excludeclasses>
<excludePackages></excludePackages>
<source>17</source>
<sourceFileExclude></sourceFileExclude>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<doclet>com.microsoft.doclet.DocFxDoclet</doclet>
<useStandardDocletOptions>false</useStandardDocletOptions>
<!-- custom config with -Dproperty=value -->
<docletPath>${env.KOKORO_GFILE_DIR}/${docletName}.jar</docletPath>
<additionalOptions>
-outputpath ${outputpath}
-projectname ${projectname}
-excludeclasses ${excludeclasses}:
-excludepackages ${excludePackages}:
-disable-changelog true
</additionalOptions>
<doclint>none</doclint>
<show>protected</show>
<nohelp>true</nohelp>
<source>${source}</source>
<sourceFileExcludes>
<exclude>${sourceFileExclude}</exclude>
</sourceFileExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!-- Code Coverage -->
<profile>
Expand Down
2 changes: 2 additions & 0 deletions spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<id>native-sample-config</id>
<modules>
<module>spring-cloud-gcp-logging-sample</module>
<module>spring-cloud-gcp-vision-api-sample</module>
</modules>
<build>
<plugins>
Expand Down Expand Up @@ -132,6 +133,7 @@
</includes>
<systemPropertyVariables>
<it.logging>true</it.logging>
<it.vision>true</it.vision>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.spring.vision.CloudVisionTemplate;
import com.google.cloud.spring.vision.nativeimage.TestRuntimeHints;
import java.util.List;
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.context.annotation.ImportRuntimeHints;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -32,6 +34,7 @@
@EnabledIfSystemProperty(named = "it.vision", matches = "true")
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {VisionTestConfiguration.class})
@ImportRuntimeHints(TestRuntimeHints.class)
class CloudVisionTemplateIntegrationTests {

@Autowired private CloudVisionTemplate cloudVisionTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
import com.google.cloud.spring.storage.GoogleStorageLocation;
import com.google.cloud.spring.vision.DocumentOcrResultSet;
import com.google.cloud.spring.vision.DocumentOcrTemplate;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.vision.v1.TextAnnotation;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -41,17 +47,38 @@
@ContextConfiguration(classes = {VisionTestConfiguration.class})
class DocumentOcrTemplateIntegrationTests {

private static final String BUCKET_NAME = "vision-integration-test-bucket";
private static GoogleStorageLocation outputLocationPrefix;
@Autowired private DocumentOcrTemplate documentOcrTemplate;

@AfterAll
static void deleteStorageResources() {
Storage storage = StorageOptions.getDefaultInstance().getService();
Blob outputBlob1 =
storage.get(
BlobId.of(
outputLocationPrefix.getBucketName(),
outputLocationPrefix.getBlobName() + "output-1-to-2.json"));
Blob outputBlob2 =
storage.get(
BlobId.of(
outputLocationPrefix.getBucketName(),
outputLocationPrefix.getBlobName() + "output-3-to-4.json"));
storage.delete(outputBlob1.getBlobId());
storage.delete(outputBlob2.getBlobId());
}

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

GoogleStorageLocation document =
GoogleStorageLocation.forFile("vision-integration-test-bucket", "test.pdf");
GoogleStorageLocation outputLocationPrefix =
GoogleStorageLocation.forFile("vision-integration-test-bucket", "it_output/test-");
GoogleStorageLocation document = GoogleStorageLocation.forFile(BUCKET_NAME, "test.pdf");
outputLocationPrefix =
GoogleStorageLocation.forFile(
BUCKET_NAME, String.format("it_output/test-%s-", UUID.randomUUID()));

CompletableFuture<DocumentOcrResultSet> result =
this.documentOcrTemplate.runOcrForDocument(document, outputLocationPrefix);
Expand Down Expand Up @@ -82,7 +109,7 @@ void testDocumentOcrTemplate()
@Test
void testParseOcrResultSet() throws InvalidProtocolBufferException {
GoogleStorageLocation ocrOutputPrefix =
GoogleStorageLocation.forFolder("vision-integration-test-bucket", "json_output_set/");
GoogleStorageLocation.forFolder(BUCKET_NAME, "json_output_set/");

DocumentOcrResultSet result = this.documentOcrTemplate.readOcrOutputFileSet(ocrOutputPrefix);

Expand All @@ -93,8 +120,7 @@ void testParseOcrResultSet() throws InvalidProtocolBufferException {
@Test
void testParseOcrFile() throws InvalidProtocolBufferException {
GoogleStorageLocation ocrOutputFile =
GoogleStorageLocation.forFile(
"vision-integration-test-bucket", "json_output_set/test_output-2-to-2.json");
GoogleStorageLocation.forFile(BUCKET_NAME, "json_output_set/test_output-2-to-2.json");

DocumentOcrResultSet pages = this.documentOcrTemplate.readOcrOutputFile(ocrOutputFile);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022-2023 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
*
* https://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.google.cloud.spring.vision.nativeimage;

import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class TestRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
Resource dummyPdf = new ClassPathResource("documents/single-page-dummy.pdf");
Resource multiPagePdf = new ClassPathResource("documents/multi-page-dummy.pdf");
hints.resources().registerResource(dummyPdf);
hints.resources().registerResource(multiPagePdf);
}
}

0 comments on commit dcbf202

Please sign in to comment.