From 80bb9c5064ae9a265a7c7806d4e3095bd74a0cbd Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 24 Feb 2020 14:39:41 +0100 Subject: [PATCH] Add smoke test for r2dbc support See gh-19988 Co-authored-by: Mark Paluch --- .../build.gradle | 16 +++++ .../main/java/smoketest/data/r2dbc/City.java | 61 ++++++++++++++++++ .../smoketest/data/r2dbc/CityController.java | 45 +++++++++++++ .../smoketest/data/r2dbc/CityRepository.java | 23 +++++++ .../data/r2dbc/SampleR2dbcApplication.java | 45 +++++++++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/database-init.sql | 10 +++ .../r2dbc/SampleR2dbcApplicationTests.java | 64 +++++++++++++++++++ 8 files changed, 265 insertions(+) create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/build.gradle create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/City.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityController.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityRepository.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/SampleR2dbcApplication.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/application.properties create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/database-init.sql create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/test/java/smoketest/data/r2dbc/SampleR2dbcApplicationTests.java diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/build.gradle new file mode 100644 index 00000000000..c18db766961 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/build.gradle @@ -0,0 +1,16 @@ +plugins { + id "java" + id "org.springframework.boot.conventions" +} + +description = "Spring Boot Data R2DBC smoke test" + +dependencies { + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-r2dbc")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-webflux")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator")) + + runtimeOnly("io.r2dbc:r2dbc-h2") + + testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/City.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/City.java new file mode 100644 index 00000000000..2e30c9b6dd2 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/City.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * 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 smoketest.data.r2dbc; + +import org.springframework.data.annotation.Id; + +public class City { + + @Id + private Long id; + + private String name; + + private String state; + + private String country; + + protected City() { + } + + public City(String name, String country) { + this.name = name; + this.country = country; + } + + public Long getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public String getState() { + return this.state; + } + + public String getCountry() { + return this.country; + } + + @Override + public String toString() { + return getName() + "," + getState() + "," + getCountry(); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityController.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityController.java new file mode 100644 index 00000000000..f335d4c3f1c --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityController.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * 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 smoketest.data.r2dbc; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CityController { + + private final CityRepository repository; + + public CityController(CityRepository repository) { + this.repository = repository; + } + + @GetMapping("/cities") + public Flux findCities() { + return this.repository.findAll(); + } + + @GetMapping("/cities/{id}") + public Mono findCityById(@PathVariable long id) { + return this.repository.findById(id); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityRepository.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityRepository.java new file mode 100644 index 00000000000..71082ce3f30 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/CityRepository.java @@ -0,0 +1,23 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * 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 smoketest.data.r2dbc; + +import org.springframework.data.repository.reactive.ReactiveCrudRepository; + +public interface CityRepository extends ReactiveCrudRepository { + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/SampleR2dbcApplication.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/SampleR2dbcApplication.java new file mode 100644 index 00000000000..48665a74c61 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/java/smoketest/data/r2dbc/SampleR2dbcApplication.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * 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 smoketest.data.r2dbc; + +import io.r2dbc.spi.ConnectionFactory; + +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.data.r2dbc.connectionfactory.init.ResourceDatabasePopulator; + +@SpringBootApplication +public class SampleR2dbcApplication { + + public static void main(String[] args) { + SpringApplication.run(SampleR2dbcApplication.class, args); + } + + @Bean + public ApplicationRunner initializeDatabase(ConnectionFactory connectionFactory, ResourceLoader resourceLoader) { + return (arguments) -> { + Resource[] scripts = new Resource[] { resourceLoader.getResource("classpath:database-init.sql") }; + new ResourceDatabasePopulator(scripts).execute(connectionFactory).block(); + }; + + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/application.properties new file mode 100644 index 00000000000..749e3804603 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/application.properties @@ -0,0 +1 @@ +management.endpoint.health.show-details=always diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/database-init.sql b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/database-init.sql new file mode 100644 index 00000000000..9b61dc66b2e --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/main/resources/database-init.sql @@ -0,0 +1,10 @@ +CREATE TABLE CITY ( + id INTEGER IDENTITY PRIMARY KEY, + name VARCHAR(30), + state VARCHAR(30), + country VARCHAR(30) +); + +INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2000, 'Washington', 'DC', 'US'); +INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2001, 'San Francisco', 'CA', 'US'); + diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/test/java/smoketest/data/r2dbc/SampleR2dbcApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/test/java/smoketest/data/r2dbc/SampleR2dbcApplicationTests.java new file mode 100644 index 00000000000..a6230de63b0 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-data-r2dbc/src/test/java/smoketest/data/r2dbc/SampleR2dbcApplicationTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * 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 smoketest.data.r2dbc; + +import javax.sql.DataSource; + +import net.minidev.json.JSONArray; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.context.ApplicationContext; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.r2dc.generate-unique-name=true") +class SampleR2dbcApplicationTests { + + @Autowired + private WebTestClient webClient; + + @Autowired + private ApplicationContext applicationContext; + + @Test + void citiesEndpointReturnInitialState() { + this.webClient.get().uri("/cities").exchange().expectBody().jsonPath("$[*].id") + .isEqualTo(new JSONArray().appendElement(2000).appendElement(2001)); + } + + @Test + void citiesEndpointByIdWithExistingIdReturnCity() { + this.webClient.get().uri("/cities/2001").exchange().expectBody().jsonPath("$.name").isEqualTo("San Francisco"); + } + + @Test + void healthEndpointHasR2dbcEntry() { + this.webClient.get().uri("/actuator/health").exchange().expectStatus().isOk().expectBody() + .jsonPath("components.r2dbc.status").isEqualTo("UP").jsonPath("components.r2dbc.details.database") + .isEqualTo("H2"); + } + + @Test + void dataSourceIsNotAutoConfigured() { + assertThat(this.applicationContext.getBeansOfType(DataSource.class)).isEmpty(); + } + +}