#421 - Elevate R2DBC example to top level project.

Moved r2dbc example to a top-level project. Switched to new Spring Data R2DBC project coordinates.

Original pull request: #422.
This commit is contained in:
Oliver Gierke
2018-10-17 16:57:54 +02:00
committed by Mark Paluch
parent 28dd3d8728
commit d2eb42dad1
10 changed files with 37 additions and 8 deletions

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2018 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
*
* http://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 example.springdata.r2dbc.basics;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Value;
import org.springframework.data.annotation.Id;
/**
* @author Oliver Gierke
*/
@Data
@AllArgsConstructor
class Customer {
@Id Integer id;
String firstname, lastname;
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2018 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
*
* http://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 example.springdata.r2dbc.basics;
import reactor.core.publisher.Flux;
import org.springframework.data.r2dbc.repository.query.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
/**
* @author Oliver Gierke
*/
interface CustomerRepository extends ReactiveCrudRepository<Customer, Long> {
@Query("select id, firstname, lastname from customer c where c.lastname = $1")
Flux<Customer> findByLastnameLike(String lastname);
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2018 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
*
* http://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 example.springdata.r2dbc.basics;
import reactor.core.publisher.Hooks;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InfrastructureConfiguration.class)
public class CustomerRepositoryIntegrationTests {
@Autowired CustomerRepository customers;
@Autowired DatabaseClient database;
@Before
public void setUp() {
Hooks.onOperatorDebug();
List<String> statements = Arrays.asList(//
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
statements.forEach(it -> database.execute() //
.sql(it) //
.fetch() //
.rowsUpdated() //
.as(StepVerifier::create) //
.verifyComplete());
}
@Test
public void executesFindAll() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findAll() //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.assertNext(carter::equals) //
.verifyComplete();
}
@Test
public void executesAnnotatedQuery() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findByLastnameLike("Matthews") //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.verifyComplete();
}
private void insertCustomers(Customer... customers) {
this.customers.saveAll(Arrays.asList(customers))//
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2018 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
*
* http://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 example.springdata.r2dbc.basics;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.testcontainers.containers.PostgreSQLContainer;
import javax.annotation.PreDestroy;
/**
* @author Oliver Gierke
*/
@Configuration
class InfrastructureConfiguration {
private PostgreSQLContainer postgres = new PostgreSQLContainer();
@Bean
CustomerRepository customerRepository(R2dbcRepositoryFactory factory) {
return factory.getRepository(CustomerRepository.class);
}
@Bean
R2dbcRepositoryFactory repositoryFactory(DatabaseClient client) {
RelationalMappingContext context = new RelationalMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context);
}
@Bean
DatabaseClient databaseClient(ConnectionFactory factory) {
return DatabaseClient.builder() //
.connectionFactory(factory) //
.build();
}
@Bean
PostgresqlConnectionFactory connectionFactory() {
postgres.start();
PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() //
.host(postgres.getContainerIpAddress()) //
.port(postgres.getFirstMappedPort()) //
.database(postgres.getDatabaseName()) //
.username(postgres.getUsername()) //
.password(postgres.getPassword()) //
.build();
return new PostgresqlConnectionFactory(config);
}
@PreDestroy
void shutdown() {
postgres.stop();
}
}