#61 - Move Conversion-related functionality to MappingR2dbcConverter.
All conversion functionality is now pulled together into MappingR2dbcConverter. Introduce OutboundRow to provide mapping between column names and settable values. Remove identifier from SettableValue. Original pull request: #62.
This commit is contained in:
committed by
Jens Schauder
parent
169de9df0a
commit
feabe477a7
@@ -12,6 +12,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
||||
import org.springframework.data.r2dbc.function.convert.SettableValue;
|
||||
|
||||
@@ -67,7 +68,7 @@ public class DefaultReactiveDataAccessStrategyUnitTests {
|
||||
public void shouldUpdateArray() {
|
||||
|
||||
Map<String, SettableValue> columnsToUpdate = strategy
|
||||
.getColumnsToUpdate(new WithCollectionTypes(new String[] { "one", "two" }, null));
|
||||
.getOutboundRow(new WithCollectionTypes(new String[] { "one", "two" }, null));
|
||||
|
||||
Object stringArray = columnsToUpdate.get("string_array").getValue();
|
||||
|
||||
@@ -79,7 +80,7 @@ public class DefaultReactiveDataAccessStrategyUnitTests {
|
||||
public void shouldConvertListToArray() {
|
||||
|
||||
Map<String, SettableValue> columnsToUpdate = strategy
|
||||
.getColumnsToUpdate(new WithCollectionTypes(null, Arrays.asList("one", "two")));
|
||||
.getOutboundRow(new WithCollectionTypes(null, Arrays.asList("one", "two")));
|
||||
|
||||
Object stringArray = columnsToUpdate.get("string_collection").getValue();
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
||||
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link EntityRowMapper}.
|
||||
@@ -101,11 +101,8 @@ public class EntityRowMapperUnitTests {
|
||||
assertThat(result.boxedIntegers).contains(3, 11);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> EntityRowMapper<T> getRowMapper(Class<T> type) {
|
||||
RelationalPersistentEntity<T> entity = (RelationalPersistentEntity<T>) strategy.getMappingContext()
|
||||
.getRequiredPersistentEntity(type);
|
||||
return new EntityRowMapper<>(entity, strategy.getRelationalConverter());
|
||||
return new EntityRowMapper<>(type, strategy.getConverter());
|
||||
}
|
||||
|
||||
static class SimpleEntity {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2019 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.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingR2dbcConverter}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class MappingR2dbcConverterUnitTests {
|
||||
|
||||
MappingR2dbcConverter converter = new MappingR2dbcConverter(new RelationalMappingContext());
|
||||
|
||||
@Test // gh-61
|
||||
public void shouldIncludeAllPropertiesInOutboundRow() {
|
||||
|
||||
OutboundRow row = new OutboundRow();
|
||||
|
||||
converter.write(new Person("id", "Walter", "White"), row);
|
||||
|
||||
assertThat(row).containsEntry("id", new SettableValue("id", String.class));
|
||||
assertThat(row).containsEntry("firstname", new SettableValue("Walter", String.class));
|
||||
assertThat(row).containsEntry("lastname", new SettableValue("White", String.class));
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
static class Person {
|
||||
@Id String id;
|
||||
String firstname, lastname;
|
||||
}
|
||||
}
|
||||
@@ -34,15 +34,16 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.r2dbc.dialect.Database;
|
||||
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
|
||||
import org.springframework.data.r2dbc.function.TransactionalDatabaseClient;
|
||||
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;
|
||||
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
|
||||
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
|
||||
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
@@ -161,11 +162,11 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
|
||||
|
||||
Database database = Database.findDatabase(createConnectionFactory()).get();
|
||||
DefaultReactiveDataAccessStrategy dataAccessStrategy = new DefaultReactiveDataAccessStrategy(
|
||||
database.defaultDialect(), new BasicRelationalConverter(mappingContext));
|
||||
database.defaultDialect(), new MappingR2dbcConverter(mappingContext));
|
||||
TransactionalDatabaseClient client = TransactionalDatabaseClient.builder()
|
||||
.connectionFactory(createConnectionFactory()).dataAccessStrategy(dataAccessStrategy).build();
|
||||
|
||||
LegoSetRepository transactionalRepository = new R2dbcRepositoryFactory(client, mappingContext, dataAccessStrategy)
|
||||
LegoSetRepository transactionalRepository = new R2dbcRepositoryFactory(client, dataAccessStrategy)
|
||||
.getRepository(getRepositoryInterfaceType());
|
||||
|
||||
LegoSet legoSet1 = new LegoSet(null, "SCHAUFELRADBAGGER", 12);
|
||||
|
||||
@@ -19,11 +19,14 @@ import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
||||
import org.springframework.data.r2dbc.function.DatabaseClient;
|
||||
import org.springframework.data.r2dbc.function.DefaultReactiveDataAccessStrategy;
|
||||
import org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
@@ -48,7 +51,7 @@ public class R2dbcRepositoriesRegistrarTests {
|
||||
|
||||
@Bean
|
||||
public ReactiveDataAccessStrategy reactiveDataAccessStrategy() {
|
||||
return mock(ReactiveDataAccessStrategy.class);
|
||||
return new DefaultReactiveDataAccessStrategy(new PostgresDialect());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,12 +26,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
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.relational.core.conversion.BasicRelationalConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
@@ -62,7 +62,7 @@ public class StringBasedR2dbcQueryUnitTests {
|
||||
public void setUp() {
|
||||
|
||||
this.mappingContext = new RelationalMappingContext();
|
||||
this.converter = new MappingR2dbcConverter(new BasicRelationalConverter(this.mappingContext));
|
||||
this.converter = new MappingR2dbcConverter(this.mappingContext);
|
||||
this.metadata = AbstractRepositoryMetadata.getMetadata(SampleRepository.class);
|
||||
this.factory = new SpelAwareProxyProjectionFactory();
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.annotation.Id;
|
||||
@@ -41,7 +42,6 @@ import org.springframework.data.r2dbc.function.DatabaseClient;
|
||||
import org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy;
|
||||
import org.springframework.data.r2dbc.function.convert.MappingR2dbcConverter;
|
||||
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
|
||||
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
@@ -74,7 +74,7 @@ public abstract class AbstractSimpleR2dbcRepositoryIntegrationTests extends R2db
|
||||
(RelationalPersistentEntity<LegoSet>) mappingContext.getRequiredPersistentEntity(LegoSet.class));
|
||||
|
||||
this.repository = new SimpleR2dbcRepository<>(entityInformation, databaseClient,
|
||||
new MappingR2dbcConverter(new BasicRelationalConverter(mappingContext)), strategy);
|
||||
new MappingR2dbcConverter(mappingContext), strategy);
|
||||
|
||||
this.jdbc = createJdbcTemplate(createDataSource());
|
||||
try {
|
||||
|
||||
@@ -23,9 +23,11 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.r2dbc.function.DatabaseClient;
|
||||
import org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy;
|
||||
import org.springframework.data.r2dbc.function.convert.R2dbcConverter;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
|
||||
import org.springframework.data.relational.repository.support.MappingRelationalEntityInformation;
|
||||
@@ -40,31 +42,32 @@ import org.springframework.data.repository.Repository;
|
||||
public class R2dbcRepositoryFactoryUnitTests {
|
||||
|
||||
@Mock DatabaseClient databaseClient;
|
||||
@Mock R2dbcConverter r2dbcConverter;
|
||||
@Mock ReactiveDataAccessStrategy dataAccessStrategy;
|
||||
@Mock @SuppressWarnings("rawtypes") MappingContext mappingContext;
|
||||
@Mock @SuppressWarnings("rawtypes") RelationalPersistentEntity entity;
|
||||
@Mock ReactiveDataAccessStrategy dataAccessStrategy;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void before() {
|
||||
when(mappingContext.getRequiredPersistentEntity(Person.class)).thenReturn(entity);
|
||||
when(dataAccessStrategy.getConverter()).thenReturn(r2dbcConverter);
|
||||
when(r2dbcConverter.getMappingContext()).thenReturn(mappingContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void usesMappingRelationalEntityInformationIfMappingContextSet() {
|
||||
|
||||
R2dbcRepositoryFactory factory = new R2dbcRepositoryFactory(databaseClient, mappingContext, dataAccessStrategy);
|
||||
R2dbcRepositoryFactory factory = new R2dbcRepositoryFactory(databaseClient, dataAccessStrategy);
|
||||
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, dataAccessStrategy);
|
||||
R2dbcRepositoryFactory factory = new R2dbcRepositoryFactory(databaseClient, dataAccessStrategy);
|
||||
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
|
||||
Reference in New Issue
Block a user