#2 - Create relational and r2dbc packages.

Move types into relational and r2dbc packages in preparation for a later module separation.
This commit is contained in:
Mark Paluch
2018-06-21 08:44:03 +02:00
parent cee7479cd0
commit 8b006abbe5
43 changed files with 212 additions and 158 deletions

View File

@@ -0,0 +1,230 @@
/*
* 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 org.springframework.data.r2dbc.function;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.Sort.Order.*;
import io.r2dbc.spi.ConnectionFactory;
import lombok.Data;
import reactor.core.publisher.Hooks;
import reactor.test.StepVerifier;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.mapping.Table;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Integration tests for {@link DatabaseClient} against PostgreSQL.
*
* @author Mark Paluch
*/
public class DatabaseClientIntegrationTests extends R2dbcIntegrationTestSupport {
private ConnectionFactory connectionFactory;
private JdbcTemplate jdbc;
@Before
public void before() {
Hooks.onOperatorDebug();
connectionFactory = createConnectionFactory();
String tableToCreate = "CREATE TABLE IF NOT EXISTS legoset (\n"
+ " id integer CONSTRAINT id PRIMARY KEY,\n" + " name varchar(255) NOT NULL,\n"
+ " manual integer NULL\n" + ");";
jdbc = createJdbcTemplate(createDataSource());
jdbc.execute(tableToCreate);
jdbc.execute("DELETE FROM legoset");
}
@Test
public void executeInsert() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.execute().sql("INSERT INTO legoset (id, name, manual) VALUES($1, $2, $3)") //
.bind(0, 42055) //
.bind(1, "SCHAUFELRADBAGGER") //
.bindNull("$3") //
.fetch().rowsUpdated() //
.as(StepVerifier::create) //
.expectNext(1) //
.verifyComplete();
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
}
@Test
public void executeSelect() {
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
// TODO: Driver/Decode does not support decoding null values?
databaseClient.execute().sql("SELECT id, name, manual FROM legoset") //
.as(LegoSet.class) //
.fetch().all() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(42055);
assertThat(actual.getName()).isEqualTo("SCHAUFELRADBAGGER");
assertThat(actual.getManual()).isEqualTo(12);
}).verifyComplete();
}
@Test
public void insert() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.insert().into("legoset")//
.value("id", 42055) //
.value("name", "SCHAUFELRADBAGGER") //
.nullValue("manual") //
.exchange() //
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all()) //
.as(StepVerifier::create) //
.expectNext(42055).verifyComplete();
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
}
@Test
public void insertWithoutResult() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.insert().into("legoset")//
.value("id", 42055) //
.value("name", "SCHAUFELRADBAGGER") //
.nullValue("manual") //
.then() //
.as(StepVerifier::create) //
.verifyComplete();
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
}
@Test
public void insertTypedObject() {
LegoSet legoSet = new LegoSet();
legoSet.setId(42055);
legoSet.setName("SCHAUFELRADBAGGER");
legoSet.setManual(12);
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.insert().into(LegoSet.class)//
.using(legoSet).exchange() //
.flatMapMany(it -> it.extract((r, m) -> r.get("id", Integer.class)).all()).as(StepVerifier::create) //
.expectNext(42055).verifyComplete();
assertThat(jdbc.queryForMap("SELECT id, name, manual FROM legoset")).containsEntry("id", 42055);
}
@Test
public void select() {
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.select().from(LegoSet.class) //
.project("id", "name", "manual") //
.orderBy(Sort.by("id")) //
.fetch().all() //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual.getId()).isEqualTo(42055);
assertThat(actual.getName()).isEqualTo("SCHAUFELRADBAGGER");
assertThat(actual.getManual()).isEqualTo(12);
}).verifyComplete();
}
@Test
public void selectOrderByIdDesc() {
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42068, 'FLUGHAFEN-LÖSCHFAHRZEUG', 13)");
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.select().from(LegoSet.class) //
.orderBy(Sort.by(desc("id"))) //
.fetch().all() //
.map(LegoSet::getId) //
.as(StepVerifier::create) //
.expectNext(42068, 42064, 42055) //
.verifyComplete();
}
@Test
public void selectOrderPaged() {
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42068, 'FLUGHAFEN-LÖSCHFAHRZEUG', 13)");
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.select().from(LegoSet.class) //
.orderBy(Sort.by(desc("id"))) //
.page(PageRequest.of(1, 1)).fetch().all() //
.map(LegoSet::getId) //
.as(StepVerifier::create) //
.expectNext(42064) //
.verifyComplete();
}
@Test
public void selectTypedLater() {
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
jdbc.execute("INSERT INTO legoset (id, name, manual) VALUES(42068, 'FLUGHAFEN-LÖSCHFAHRZEUG', 13)");
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.select().from("legoset") //
.orderBy(Sort.by(desc("id"))) //
.as(LegoSet.class) //
.fetch().all() //
.map(LegoSet::getId) //
.as(StepVerifier::create) //
.expectNext(42068, 42064, 42055) //
.verifyComplete();
}
@Data
@Table("legoset")
static class LegoSet {
int id;
String name;
Integer manual;
}
}

View File

@@ -0,0 +1,157 @@
/*
* 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 org.springframework.data.r2dbc.repository;
import static org.assertj.core.api.Assertions.*;
import io.r2dbc.spi.ConnectionFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.Table;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory}.
*
* @author Mark Paluch
*/
public class R2dbcRepositoryIntegrationTests extends R2dbcIntegrationTestSupport {
private static JdbcMappingContext mappingContext = new JdbcMappingContext();
private ConnectionFactory connectionFactory;
private DatabaseClient databaseClient;
private LegoSetRepository repository;
private JdbcTemplate jdbc;
@Before
public void before() {
Hooks.onOperatorDebug();
this.connectionFactory = createConnectionFactory();
this.databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory)
.dataAccessStrategy(new DefaultReactiveDataAccessStrategy(mappingContext, new EntityInstantiators())).build();
this.jdbc = createJdbcTemplate(createDataSource());
String tableToCreate = "CREATE TABLE IF NOT EXISTS repo_legoset (\n" + " id SERIAL PRIMARY KEY,\n"
+ " name varchar(255) NOT NULL,\n" + " manual integer NULL\n" + ");";
this.jdbc.execute("DROP TABLE IF EXISTS repo_legoset");
this.jdbc.execute(tableToCreate);
this.repository = new R2dbcRepositoryFactory(databaseClient, mappingContext).getRepository(LegoSetRepository.class);
}
@Test
public void shouldInsertNewItems() {
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
}
@Test
public void shouldFindItemsByManual() {
shouldInsertNewItems();
repository.findByManual(13) //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual.getName()).isEqualTo("FORSCHUNGSSCHIFF");
}) //
.verifyComplete();
}
@Test
public void shouldFindItemsByNameLike() {
shouldInsertNewItems();
repository.findByNameContains("%F%") //
.map(LegoSet::getName) //
.collectList() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF");
}).verifyComplete();
}
@Test
public void shouldFindApplyingProjection() {
shouldInsertNewItems();
repository.findAsProjection() //
.map(Named::getName) //
.collectList() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF");
}).verifyComplete();
}
interface LegoSetRepository extends ReactiveCrudRepository<LegoSet, Integer> {
@Query("SELECT * FROM repo_legoset WHERE name like $1")
Flux<LegoSet> findByNameContains(String name);
@Query("SELECT * FROM repo_legoset")
Flux<Named> findAsProjection();
@Query("SELECT * FROM repo_legoset WHERE manual = $1")
Mono<LegoSet> findByManual(int manual);
}
@Data
@Table("repo_legoset")
@AllArgsConstructor
@NoArgsConstructor
static class LegoSet {
@Id Integer id;
String name;
Integer manual;
}
interface Named {
String getName();
}
}

View File

@@ -0,0 +1,141 @@
/*
* 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 org.springframework.data.r2dbc.repository.query;
import static org.assertj.core.api.Assertions.*;
import reactor.core.publisher.Mono;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
/**
* Unit test for {@link R2dbcQueryMethod}.
*
* @author Mark Paluch
*/
public class R2dbcQueryMethodUnitTests {
JdbcMappingContext context;
@Before
public void setUp() {
context = new JdbcMappingContext();
}
@Test
public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception {
R2dbcQueryMethod queryMethod = queryMethod(SampleRepository.class, "method");
RelationalEntityMetadata<?> metadata = queryMethod.getEntityInformation();
assertThat(metadata.getJavaType()).isAssignableFrom(Contact.class);
assertThat(metadata.getTableName()).isEqualTo("contact");
}
@Test
public void detectsTableNameFromRepoTypeIfReturnTypeNotAssignable() throws Exception {
R2dbcQueryMethod queryMethod = queryMethod(SampleRepository.class, "differentTable");
RelationalEntityMetadata<?> metadata = queryMethod.getEntityInformation();
assertThat(metadata.getJavaType()).isAssignableFrom(Address.class);
assertThat(metadata.getTableName()).isEqualTo("contact");
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullMappingContext() throws Exception {
Method method = PersonRepository.class.getMethod("findMonoByLastname", String.class, Pageable.class);
new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class),
new SpelAwareProxyProjectionFactory(), null);
}
@Test(expected = IllegalStateException.class)
public void rejectsMonoPageableResult() throws Exception {
queryMethod(PersonRepository.class, "findMonoByLastname", String.class, Pageable.class);
}
@Test
public void createsQueryMethodObjectForMethodReturningAnInterface() throws Exception {
queryMethod(SampleRepository.class, "methodReturningAnInterface");
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public void throwsExceptionOnWrappedPage() throws Exception {
queryMethod(PersonRepository.class, "findMonoPageByLastname", String.class, Pageable.class);
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public void throwsExceptionOnWrappedSlice() throws Exception {
queryMethod(PersonRepository.class, "findMonoSliceByLastname", String.class, Pageable.class);
}
@Test
public void fallsBackToRepositoryDomainTypeIfMethodDoesNotReturnADomainType() throws Exception {
R2dbcQueryMethod method = queryMethod(PersonRepository.class, "deleteByUserName", String.class);
assertThat(method.getEntityInformation().getJavaType()).isAssignableFrom(Contact.class);
}
private R2dbcQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception {
Method method = repository.getMethod(name, parameters);
ProjectionFactory factory = new SpelAwareProxyProjectionFactory();
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(repository), factory, context);
}
interface PersonRepository extends Repository<Contact, Long> {
Mono<Contact> findMonoByLastname(String lastname, Pageable pageRequest);
Mono<Page<Contact>> findMonoPageByLastname(String lastname, Pageable pageRequest);
Mono<Slice<Contact>> findMonoSliceByLastname(String lastname, Pageable pageRequest);
void deleteByUserName(String userName);
}
interface SampleRepository extends Repository<Contact, Long> {
List<Contact> method();
List<Address> differentTable();
Customer methodReturningAnInterface();
}
interface Customer {}
static class Contact {}
static class Address {}
}

View File

@@ -0,0 +1,106 @@
/*
* 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 org.springframework.data.r2dbc.repository.query;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.function.DatabaseClient.GenericExecuteSpec;
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
import org.springframework.data.repository.query.ExtensionAwareQueryMethodEvaluationContextProvider;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link StringBasedR2dbcQuery}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class StringBasedR2dbcQueryUnitTests {
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
@Mock private DatabaseClient databaseClient;
@Mock private GenericExecuteSpec bindSpec;
private JdbcMappingContext mappingContext;
private MappingR2dbcConverter converter;
private ProjectionFactory factory;
private RepositoryMetadata metadata;
@Before
@SuppressWarnings("unchecked")
public void setUp() {
this.mappingContext = new JdbcMappingContext();
this.converter = new MappingR2dbcConverter(this.mappingContext);
this.metadata = AbstractRepositoryMetadata.getMetadata(SampleRepository.class);
this.factory = new SpelAwareProxyProjectionFactory();
when(bindSpec.bind(anyString(), any())).thenReturn(bindSpec);
}
@Test
public void bindsSimplePropertyCorrectly() {
StringBasedR2dbcQuery query = getQueryMethod("findByLastname", String.class);
R2dbcParameterAccessor accessor = new R2dbcParameterAccessor(query.getQueryMethod(), "White");
BindableQuery stringQuery = query.createQuery(accessor);
assertThat(stringQuery.get()).isEqualTo("SELECT * FROM person WHERE lastname = $1");
assertThat(stringQuery.bind(bindSpec)).isNotNull();
verify(bindSpec).bind("$1", "White");
}
private StringBasedR2dbcQuery getQueryMethod(String name, Class<?>... args) {
Method method = ReflectionUtils.findMethod(SampleRepository.class, name, args);
R2dbcQueryMethod queryMethod = new R2dbcQueryMethod(method, metadata, factory, converter.getMappingContext());
return new StringBasedR2dbcQuery(queryMethod, databaseClient, converter, PARSER,
ExtensionAwareQueryMethodEvaluationContextProvider.DEFAULT);
}
@SuppressWarnings("unused")
private interface SampleRepository extends Repository<Person, String> {
@Query("SELECT * FROM person WHERE lastname = $1")
Person findByLastname(String lastname);
}
static class Person {
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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 org.springframework.data.r2dbc.repository.support;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
import org.springframework.data.relational.repository.support.MappingRelationalEntityInformation;
import org.springframework.data.repository.Repository;
/**
* Unit test for {@link R2dbcRepositoryFactory}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class R2dbcRepositoryFactoryUnitTests {
@Mock DatabaseClient databaseClient;
@Mock @SuppressWarnings("rawtypes") MappingContext mappingContext;
@Mock @SuppressWarnings("rawtypes") JdbcPersistentEntity entity;
@Before
@SuppressWarnings("unchecked")
public void before() {
when(mappingContext.getRequiredPersistentEntity(Person.class)).thenReturn(entity);
}
@Test
@SuppressWarnings("unchecked")
public void usesMappingJdbcEntityInformationIfMappingContextSet() {
R2dbcRepositoryFactory factory = new R2dbcRepositoryFactory(databaseClient, mappingContext);
RelationalEntityInformation<Person, Long> entityInformation = factory.getEntityInformation(Person.class);
assertThat(entityInformation).isInstanceOf(MappingRelationalEntityInformation.class);
}
@Test
@SuppressWarnings("unchecked")
public void createsRepositoryWithIdTypeLong() {
R2dbcRepositoryFactory factory = new R2dbcRepositoryFactory(databaseClient, mappingContext);
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
assertThat(repository).isNotNull();
}
interface MyPersonRepository extends Repository<Person, Long> {}
static class Person {}
}

View File

@@ -0,0 +1,342 @@
/*
* 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 org.springframework.data.r2dbc.repository.support;
import static org.assertj.core.api.Assertions.*;
import io.r2dbc.spi.ConnectionFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
import org.springframework.data.jdbc.core.mapping.Table;
import org.springframework.data.jdbc.testing.R2dbcIntegrationTestSupport;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
import org.springframework.data.relational.repository.support.MappingRelationalEntityInformation;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Integration tests for {@link SimpleR2dbcRepository}.
*
* @author Mark Paluch
*/
public class SimpleR2dbcRepositoryIntegrationTests extends R2dbcIntegrationTestSupport {
private static JdbcMappingContext mappingContext = new JdbcMappingContext();
private ConnectionFactory connectionFactory;
private DatabaseClient databaseClient;
private SimpleR2dbcRepository<LegoSet, Integer> repository;
private JdbcTemplate jdbc;
@Before
public void before() {
Hooks.onOperatorDebug();
this.connectionFactory = createConnectionFactory();
this.databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory)
.dataAccessStrategy(new DefaultReactiveDataAccessStrategy(mappingContext, new EntityInstantiators())).build();
RelationalEntityInformation<LegoSet, Integer> entityInformation = new MappingRelationalEntityInformation<>(
(JdbcPersistentEntity<LegoSet>) mappingContext.getRequiredPersistentEntity(LegoSet.class));
this.repository = new SimpleR2dbcRepository<>(entityInformation, databaseClient,
new MappingR2dbcConverter(mappingContext));
this.jdbc = createJdbcTemplate(createDataSource());
String tableToCreate = "CREATE TABLE IF NOT EXISTS repo_legoset (\n" + " id SERIAL PRIMARY KEY,\n"
+ " name varchar(255) NOT NULL,\n" + " manual integer NULL\n" + ");";
this.jdbc.execute("DROP TABLE IF EXISTS repo_legoset");
this.jdbc.execute(tableToCreate);
}
@Test
public void shouldSaveNewObject() {
LegoSet legoSet = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
repository.save(legoSet) //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual.getId()).isNotNull();
})
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT * FROM repo_legoset");
assertThat(map).containsEntry("name", "SCHAUFELRADBAGGER").containsEntry("manual", 12).containsKey("id");
}
@Test
public void shouldUpdateObject() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
legoSet.setManual(14);
repository.save(legoSet) //
.as(StepVerifier::create) //
.expectNextCount(1) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT * FROM repo_legoset");
assertThat(map).containsEntry("name", "SCHAUFELRADBAGGER").containsEntry("manual", 14).containsKey("id");
}
@Test
public void shouldSaveObjectsUsingIterable() {
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
repository.saveAll(Arrays.asList(legoSet1, legoSet2)) //
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 2L);
}
@Test
public void shouldSaveObjectsUsingPublisher() {
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
LegoSet legoSet2 = new LegoSet(null, "FORSCHUNGSSCHIFF", 13);
repository.saveAll(Flux.just(legoSet1, legoSet2)) //
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 2L);
}
@Test
public void shouldFindById() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
repository.findById(42055) //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual.getId()).isEqualTo(42055);
assertThat(actual.getName()).isEqualTo("SCHAUFELRADBAGGER");
assertThat(actual.getManual()).isEqualTo(12);
}).verifyComplete();
}
@Test
public void shouldExistsById() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
repository.existsById(42055) //
.as(StepVerifier::create) //
.expectNext(true)//
.verifyComplete();
repository.existsById(42) //
.as(StepVerifier::create) //
.expectNext(false)//
.verifyComplete();
}
@Test
public void shouldExistsByIdPublisher() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
repository.existsById(Mono.just(42055)) //
.as(StepVerifier::create) //
.expectNext(true)//
.verifyComplete();
repository.existsById(Mono.just(42)) //
.as(StepVerifier::create) //
.expectNext(false)//
.verifyComplete();
}
@Test
public void shouldFindByAll() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
repository.findAll() //
.map(LegoSet::getName) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF");
}).verifyComplete();
}
@Test
public void shouldFindAllByIdUsingIterable() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
repository.findAllById(Arrays.asList(42055, 42064)) //
.map(LegoSet::getName) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF");
}).verifyComplete();
}
@Test
public void shouldFindAllByIdUsingPublisher() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
repository.findAllById(Flux.just(42055, 42064)) //
.map(LegoSet::getName) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(actual -> {
assertThat(actual).hasSize(2).contains("SCHAUFELRADBAGGER", "FORSCHUNGSSCHIFF");
}).verifyComplete();
}
@Test
public void shouldCount() {
repository.count() //
.as(StepVerifier::create) //
.expectNext(0L) //
.verifyComplete();
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42064, 'FORSCHUNGSSCHIFF', 13)");
repository.count() //
.as(StepVerifier::create) //
.expectNext(2L) //
.verifyComplete();
}
@Test
public void shouldDeleteById() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
repository.deleteById(42055) //
.as(StepVerifier::create) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 0L);
}
@Test
public void shouldDeleteByIdPublisher() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
repository.deleteById(Mono.just(42055)) //
.as(StepVerifier::create) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 0L);
}
@Test
public void shouldDelete() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
repository.delete(legoSet) //
.as(StepVerifier::create) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 0L);
}
@Test
public void shouldDeleteAllUsingIterable() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
repository.deleteAll(Collections.singletonList(legoSet)) //
.as(StepVerifier::create) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 0L);
}
@Test
public void shouldDeleteAllUsingPublisher() {
jdbc.execute("INSERT INTO repo_legoset (id, name, manual) VALUES(42055, 'SCHAUFELRADBAGGER', 12)");
LegoSet legoSet = new LegoSet(42055, "SCHAUFELRADBAGGER", 12);
repository.deleteAll(Mono.just(legoSet)) //
.as(StepVerifier::create) //
.verifyComplete();
Map<String, Object> map = jdbc.queryForMap("SELECT COUNT(*) FROM repo_legoset");
assertThat(map).containsEntry("count", 0L);
}
@Data
@Table("repo_legoset")
@AllArgsConstructor
@NoArgsConstructor
static class LegoSet {
@Id Integer id;
String name;
Integer manual;
}
}