Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Test support module #2357

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions docs/src/main/asciidoc/test.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
== Emulators support
Our tools simplify starting and stopping Cloud PubSub and Cloud Spanner emulators when you test your application.
dmitry-s marked this conversation as resolved.
Show resolved Hide resolved

In order to use it, you need to add this dependency into your `pom.xml` file:

[source,xml]
----
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<scope>test</scope>
</dependency>
----

Also, you need to have `gcloud` and the emulators installed and configured, as described later.
dmitry-s marked this conversation as resolved.
Show resolved Hide resolved

=== Cloud PubSub Emulator
In order to use our Cloud PubSub Emulator helper tools, you would need to install the emulator first.
Follow the https://cloud.google.com/pubsub/docs/emulator[installation instructions].

==== JUnit 4 Class Rule
The rule starts the emulator process before the tests run and kills it afterwards.
dmitry-s marked this conversation as resolved.
Show resolved Hide resolved
You just need to add a rule into your test class to enable it.

[source,java]
----
import org.springframework.cloud.gcp.test.PubSubEmulatorRule;

public class PubSubTests {
@ClassRule
public static PubSubEmulatorRule emulator = new PubSubEmulatorRule();

//your tests
}
----

==== Utility class
If you prefer controlling the emulator manually, you can use PubSubEmulatorHelper.

[source,java]
----
PubSubEmulatorHelper emulatorHelper = new PubSubEmulatorHelper();

emulatorHelper.startEmulator();

//your code

emulatorHelper.shutdownEmulator();
----

=== Cloud Spanner Emulator
In order to use our Cloud Spanner Emulator helper tools, you would need to install the emulator first.
Follow the https://cloud.google.com/spanner/docs/emulator[installation instructions].
Make sure you create an emulator configuration and call it `emulator`.

==== Spring Configuration
dmitry-s marked this conversation as resolved.
Show resolved Hide resolved
If you are testing your Spring application, you can use our configuration class.
It provides a bean of type Spanner, which starts the emulator before creating a client.
dmitry-s marked this conversation as resolved.
Show resolved Hide resolved
This configuration also stops the emulator when the Spring context is shut down.

In order to use it, you need to add the SpannerEmulatorSpringConfiguration.class to your configuration classes list in `@ContextConfiguration`.

[source,java]
----
import org.junit.runner.RunWith;

import org.springframework.cloud.gcp.test.SpannerEmulatorSpringConfiguration;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SpannerEmulatorSpringConfiguration.class, YourSpringConfiguration.class})
public class SpannerTemplateEmulatorTests {
//your tests
}
----

==== JUnit 4 Class Rule
If you don't use spring in your tests, you can use the class rule.

NOTE: The rule does not work with SpringRunner!
See <<Spring Configuration>> section instead.

[source,java]
----
import org.springframework.cloud.gcp.test.SpannerEmulatorRule;

public class SpannerTests {
@ClassRule
public static SpannerEmulatorRule emulator = new SpannerEmulatorRule();

//your tests
}
----



==== Utility class
If you prefer controlling the emulator manually, you can use SpannerEmulatorHelper.

[source,java]
----
SpannerEmulatorHelper emulatorHelper = new SpannerEmulatorHelper();

emulatorHelper.startEmulator();

//your code

emulatorHelper.shutdownEmulator();
----
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<module>spring-cloud-gcp-bigquery</module>
<module>spring-cloud-gcp-security-firebase</module>
<module>spring-cloud-gcp-secretmanager</module>
<module>spring-cloud-gcp-test</module>
</modules>

<properties>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-gcp-data-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2017-2018 the original author or authors.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update copyright's year

*
* 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 org.springframework.cloud.gcp.data.spanner.core.it;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.cloud.gcp.data.spanner.test.AbstractSpannerIntegrationTest;
import org.springframework.cloud.gcp.data.spanner.test.domain.Trade;
import org.springframework.cloud.gcp.test.SpannerEmulatorSpringConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
* Integration tests that use many features of the Spanner Template.
*
* @author Balint Pato
* @author Chengyuan Zhao
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SpannerEmulatorSpringConfiguration.class})
public class SpannerTemplateEmulatorTests extends AbstractSpannerIntegrationTest {

@Test
public void insertSingleRow() {

Trade trade = Trade.aTrade(null, 1);
this.spannerOperations.insert(trade);
assertThat(this.spannerOperations.count(Trade.class)).isEqualTo(1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.spanner.SpannerOptions;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.gcp.core.Credentials;
import org.springframework.cloud.gcp.core.DefaultCredentialsProvider;
import org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider;
Expand Down Expand Up @@ -104,6 +105,7 @@ public TradeRepositoryTransactionalService tradeRepositoryTransactionalService()
}

@Bean
@ConditionalOnMissingBean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sure remove these two bean definitions from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, remove the bean definitions entirely? There are several integration tests that go against real spanner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use this configuration file. I removed these beans from the emulator's test spring configuration.

public SpannerOptions spannerOptions() {
return SpannerOptions.newBuilder().setProjectId(getProjectId())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMaxSessions(10).build())
Expand All @@ -116,6 +118,7 @@ public DatabaseId databaseId() {
}

@Bean
@ConditionalOnMissingBean
public Spanner spanner(SpannerOptions spannerOptions) {
return spannerOptions.getService();
}
Expand Down
6 changes: 5 additions & 1 deletion spring-cloud-gcp-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@
<artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<version>${project.version}</version>
</dependency>
<!-- spring-cloud-gcp-starter-sql -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
Expand Down
5 changes: 5 additions & 0 deletions spring-cloud-gcp-pubsub-stream-binder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
<version>3.1.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading