Commit 80bb9c50 authored by Stephane Nicoll's avatar Stephane Nicoll

Add smoke test for r2dbc support

See gh-19988
Co-authored-by: 's avatarMark Paluch <mpaluch@pivotal.io>
parent 45e60587
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"))
}
/*
* 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();
}
}
/*
* 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<City> findCities() {
return this.repository.findAll();
}
@GetMapping("/cities/{id}")
public Mono<City> findCityById(@PathVariable long id) {
return this.repository.findById(id);
}
}
/*
* 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<City, Long> {
}
/*
* 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();
};
}
}
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');
/*
* 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();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment