Skip to content

Commit

Permalink
chore: add runtime hints for firestore starter sample (#2306)
Browse files Browse the repository at this point in the history
This test is only testing the `spring-cloud-gcp-starter-firestore` that has auto-configurations for firestore, and using client library methods. Nothing from `spring-cloud-gcp-data-firestore` module is included.

### This pr:
- Adds runtime hints needed to run sample app for firestore[ Firestore Client Library sample application](https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-gcp-firestore-sample)
- Renames sample module to be less ambiguous.
- Add uuid suffix to document ids created by the sample application.
- Adds this sample to native ci.

Note: this test uses same ci project for regular it tests and native test.
  • Loading branch information
zhumin8 authored Oct 30, 2023
1 parent 0f38f83 commit 1c23b13
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/NativeTests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
- pubsub-integration-sample
- pubsub-bus-sample
- pubsub-stream-sample
- starter-firestore-sample
steps:
- name: Get current date
id: date
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/firestore.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ From this point onward, your application will connect to your locally running em
Spring Framework on Google Cloud provides Firestore sample applications to demonstrate API usage:

- https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-gcp-data-firestore-sample[Reactive Firestore Repository sample application]:
- https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-gcp-firestore-sample[Firestore Client Library sample application]
- https://github.com/GoogleCloudPlatform/spring-cloud-gcp/tree/main/spring-cloud-gcp-samples/spring-cloud-gcp-starter-firestore-sample[Firestore Client Library sample application]

=== Test

Expand Down
4 changes: 3 additions & 1 deletion spring-cloud-gcp-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<module>spring-cloud-gcp-sql-postgres-sample</module>
<module>spring-cloud-gcp-sql-postgres-r2dbc-sample</module>
<module>spring-cloud-gcp-vision-ocr-demo</module>
<module>spring-cloud-gcp-firestore-sample</module>
<module>spring-cloud-gcp-starter-firestore-sample</module>
<module>spring-cloud-gcp-data-multi-sample</module>
<module>spring-cloud-gcp-data-firestore-sample</module>
<module>spring-cloud-gcp-bigquery-sample</module>
Expand Down Expand Up @@ -104,6 +104,7 @@
<module>spring-cloud-gcp-vision-api-sample</module>
<module>spring-cloud-gcp-secretmanager-sample</module>
<module>spring-cloud-gcp-vision-ocr-demo</module>
<module>spring-cloud-gcp-starter-firestore-sample</module>
</modules>
<build>
<plugins>
Expand Down Expand Up @@ -153,6 +154,7 @@
<it.vision>true</it.vision>
<it.trace>true</it.trace>
<it.secretmanager>true</it.secretmanager>
<it.firestore>true</it.firestore>
<gcs-resource-test-bucket>gcp-storage-resource-bucket-sample</gcs-resource-test-bucket>
<gcs-read-bucket>gcp-storage-bucket-sample-input</gcs-read-bucket>
<gcs-write-bucket>gcp-storage-bucket-sample-output</gcs-write-bucket>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This code sample demonstrates how to read from and write to Google Cloud Firesto

== Running the example

image:http://gstatic.com/cloudssh/images/open-btn.svg[link=https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspring-cloud-gcp&cloudshell_open_in_editor=spring-cloud-gcp-samples/spring-cloud-gcp-firestore-sample/README.adoc]
image:http://gstatic.com/cloudssh/images/open-btn.svg[link=https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspring-cloud-gcp&cloudshell_open_in_editor=spring-cloud-gcp-samples/spring-cloud-gcp-starter-firestore-sample/README.adoc]

. https://firebase.google.com/docs/firestore/quickstart[Create a new Cloud Firestore database in your Google Cloud project] if this has not already been done to allow Firestore to create and store entities.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-gcp-firestore-sample</artifactId>
<name>Spring Framework on Google Cloud Code Sample - Firestore</name>
<artifactId>spring-cloud-gcp-starter-firestore-sample</artifactId>
<name>Spring Framework on Google Cloud Code Sample - Firestore Client Library with Starter</name>

<!-- The Spring Framework on Google Cloud BOM will manage spring-cloud-gcp version numbers for you. -->
<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,27 @@
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportRuntimeHints;

/**
* @since 1.2
*/
@SpringBootApplication
@ImportRuntimeHints(SampleRuntimeHints.class)
public class FirestoreSampleApp {
@Autowired Firestore firestore;

@Value("${document.id.1:ada}")
private String documentIdAda;

@Value("${document.id.2:joe}")
private String documentIdJoe;

public static void main(String[] args) {
SpringApplication.run(FirestoreSampleApp.class, args);
}
Expand All @@ -56,8 +65,8 @@ public CommandLineRunner commandLineRunner() {

private void writeDocumentFromMap()
throws InterruptedException, java.util.concurrent.ExecutionException {
DocumentReference docRef = this.firestore.collection("users").document("ada");
// Add document data with id "ada" using a hashmap
DocumentReference docRef = this.firestore.collection("users").document(documentIdAda);
// Add document data with id ada-"uuid" using a hashmap
Map<String, Object> data = new HashMap<>();
data.put("name", "Ada");
data.put("phones", Arrays.asList(123, 456));
Expand All @@ -70,20 +79,20 @@ private void writeDocumentFromMap()
}

private void writeDocumentFromObject() throws ExecutionException, InterruptedException {
// Add document data with id "joe" using a custom User class
// Add document data with id joe-"uuid" using a custom User class
User data =
new User(
"Joe",
Arrays.asList(new Phone(12345, PhoneType.CELL), new Phone(54321, PhoneType.WORK)));

// .get() blocks on response
WriteResult writeResult = this.firestore.document("users/joe").set(data).get();
WriteResult writeResult = this.firestore.document("users/" + documentIdJoe).set(data).get();

System.out.println("Update time: " + writeResult.getUpdateTime());
}

private void readDocumentToMap() throws ExecutionException, InterruptedException {
DocumentReference docRef = this.firestore.document("users/ada");
DocumentReference docRef = this.firestore.document("users/" + documentIdAda);

ApiFuture<DocumentSnapshot> documentSnapshotApiFuture = docRef.get();

Expand All @@ -94,7 +103,7 @@ private void readDocumentToMap() throws ExecutionException, InterruptedException

private void readDocumentToObject() throws ExecutionException, InterruptedException {
ApiFuture<DocumentSnapshot> documentSnapshotApiFuture =
this.firestore.document("users/joe").get();
this.firestore.document("users/" + documentIdJoe).get();

User user = documentSnapshotApiFuture.get().toObject(User.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 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.example;

import java.util.Arrays;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;

/**
* Runtime hints are needed for running application with GraalVM
*/
public class SampleRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints
.reflection()
.registerTypes(
Arrays.asList(TypeReference.of(User.class),
TypeReference.of(Phone.class),
TypeReference.of(PhoneType.class)),
hint ->
hint.withMembers(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS));
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# You can use this setting to provide a separate service account JSON file specifically for Firestore.
# spring.cloud.gcp.firestore.credentials.location =

document.id.1=ada-${random.uuid}
document.id.2=joe-${random.uuid}

0 comments on commit 1c23b13

Please sign in to comment.