Browse Source

DATAJDBC-378 - Polishing

Move non integration tests to separate class.

Original Pull Request: #155
pull/157/head
Christoph Strobl 7 years ago
parent
commit
3524d7bacf
  1. 27
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java
  2. 18
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java
  3. 91
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java

27
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

@ -46,6 +46,7 @@ import org.springframework.util.Assert;
* @author Jens Schauder * @author Jens Schauder
* @author Mark Paluch * @author Mark Paluch
* @author Thomas Lang * @author Thomas Lang
* @author Christoph Strobl
*/ */
public class JdbcAggregateTemplate implements JdbcAggregateOperations { public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@ -165,8 +166,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <T> T findById(Object id, Class<T> domainType) { public <T> T findById(Object id, Class<T> domainType) {
Assert.notNull(id, "Id must not be null"); Assert.notNull(id, "Id must not be null!");
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
T entity = accessStrategy.findById(id, domainType); T entity = accessStrategy.findById(id, domainType);
if (entity != null) { if (entity != null) {
@ -182,8 +183,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <T> boolean existsById(Object id, Class<T> domainType) { public <T> boolean existsById(Object id, Class<T> domainType) {
Assert.notNull(id, "Id must not be null"); Assert.notNull(id, "Id must not be null!");
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
return accessStrategy.existsById(id, domainType); return accessStrategy.existsById(id, domainType);
} }
@ -195,7 +196,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <T> Iterable<T> findAll(Class<T> domainType) { public <T> Iterable<T> findAll(Class<T> domainType) {
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> all = accessStrategy.findAll(domainType); Iterable<T> all = accessStrategy.findAll(domainType);
publishAfterLoad(all); publishAfterLoad(all);
@ -209,8 +210,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) { public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
Assert.notNull(ids, "Ids must not be null"); Assert.notNull(ids, "Ids must not be null!");
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
Iterable<T> allById = accessStrategy.findAllById(ids, domainType); Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
publishAfterLoad(allById); publishAfterLoad(allById);
@ -224,8 +225,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <S> void delete(S aggregateRoot, Class<S> domainType) { public <S> void delete(S aggregateRoot, Class<S> domainType) {
Assert.notNull(aggregateRoot, "Aggregate root must not be null"); Assert.notNull(aggregateRoot, "Aggregate root must not be null!");
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType) IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
.getIdentifierAccessor(aggregateRoot); .getIdentifierAccessor(aggregateRoot);
@ -240,8 +241,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public <S> void deleteById(Object id, Class<S> domainType) { public <S> void deleteById(Object id, Class<S> domainType) {
Assert.notNull(id, "Id must not be null"); Assert.notNull(id, "Id must not be null!");
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
deleteTree(id, null, domainType); deleteTree(id, null, domainType);
} }
@ -253,7 +254,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override @Override
public void deleteAll(Class<?> domainType) { public void deleteAll(Class<?> domainType) {
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null!");
AggregateChange<?> change = createDeletingChange(domainType); AggregateChange<?> change = createDeletingChange(domainType);
change.executeWith(interpreter, context, converter); change.executeWith(interpreter, context, converter);
@ -274,7 +275,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
Object identifier = persistentEntity.getIdentifierAccessor(change.getEntity()).getIdentifier(); Object identifier = persistentEntity.getIdentifierAccessor(change.getEntity()).getIdentifier();
Assert.notNull(identifier, "After saving the identifier must not be null"); Assert.notNull(identifier, "After saving the identifier must not be null!");
publisher.publishEvent(new AfterSaveEvent( // publisher.publishEvent(new AfterSaveEvent( //
Identifier.of(identifier), // Identifier.of(identifier), //

18
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

@ -596,24 +596,6 @@ public class JdbcAggregateTemplateIntegrationTests {
}); });
} }
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForType() {
assertThatThrownBy(() -> template.findAllById(singleton(23L), null)).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForIds() {
assertThatThrownBy(() -> template.findAllById(null, LegoSet.class)).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdWithEmpthListMustReturnEmptyResult() {
assertThat(template.findAllById(emptyList(), LegoSet.class)).isEmpty();
}
private static NoIdMapChain4 createNoIdMapTree() { private static NoIdMapChain4 createNoIdMapTree() {
NoIdMapChain4 chain4 = new NoIdMapChain4(); NoIdMapChain4 chain4 = new NoIdMapChain4();

91
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java

@ -0,0 +1,91 @@
/*
* 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
*
* 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 org.springframework.data.jdbc.core;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import lombok.Data;
import javax.sql.DataSource;
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.context.ApplicationEventPublisher;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.RelationResolver;
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class JdbcAggregateTemplateUnitTests {
JdbcAggregateOperations template;
@Mock ApplicationEventPublisher eventPublisher;
@Mock RelationResolver relationResolver;
@Mock DataSource dataSource;
@Before
public void setUp() {
RelationalMappingContext mappingContext = new RelationalMappingContext(NamingStrategy.INSTANCE);
JdbcConverter converter = new BasicJdbcConverter(mappingContext, relationResolver);
NamedParameterJdbcOperations namedParameterJdbcOperations = new NamedParameterJdbcTemplate(dataSource);
DataAccessStrategy dataAccessStrategy = new DefaultDataAccessStrategy(new SqlGeneratorSource(mappingContext),
mappingContext, converter, namedParameterJdbcOperations);
template = new JdbcAggregateTemplate(eventPublisher, mappingContext, converter, dataAccessStrategy);
}
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForType() {
assertThatThrownBy(() -> template.findAllById(singleton(23L), null)).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForIds() {
assertThatThrownBy(() -> template.findAllById(null, SampleEntity.class))
.isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdWithEmpthListMustReturnEmptyResult() {
assertThat(template.findAllById(emptyList(), SampleEntity.class)).isEmpty();
}
@Data
private static class SampleEntity {
@Column("id1") @Id private Long id;
private String name;
}
}
Loading…
Cancel
Save