From 3524d7bacf18e64599103e10fce12c28e7954c84 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 13 Jun 2019 09:46:29 +0200 Subject: [PATCH] DATAJDBC-378 - Polishing Move non integration tests to separate class. Original Pull Request: #155 --- .../data/jdbc/core/JdbcAggregateTemplate.java | 27 +++--- ...JdbcAggregateTemplateIntegrationTests.java | 18 ---- .../core/JdbcAggregateTemplateUnitTests.java | 91 +++++++++++++++++++ 3 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java index cdba2ed38..e409c06ba 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java +++ b/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 Mark Paluch * @author Thomas Lang + * @author Christoph Strobl */ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @@ -165,8 +166,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public T findById(Object id, Class domainType) { - Assert.notNull(id, "Id must not be null"); - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(id, "Id must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); T entity = accessStrategy.findById(id, domainType); if (entity != null) { @@ -182,8 +183,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public boolean existsById(Object id, Class domainType) { - Assert.notNull(id, "Id must not be null"); - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(id, "Id must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); return accessStrategy.existsById(id, domainType); } @@ -195,7 +196,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public Iterable findAll(Class domainType) { - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(domainType, "Domain type must not be null!"); Iterable all = accessStrategy.findAll(domainType); publishAfterLoad(all); @@ -209,8 +210,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public Iterable findAllById(Iterable ids, Class domainType) { - Assert.notNull(ids, "Ids must not be null"); - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(ids, "Ids must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); Iterable allById = accessStrategy.findAllById(ids, domainType); publishAfterLoad(allById); @@ -224,8 +225,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public void delete(S aggregateRoot, Class domainType) { - Assert.notNull(aggregateRoot, "Aggregate root must not be null"); - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(aggregateRoot, "Aggregate root must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType) .getIdentifierAccessor(aggregateRoot); @@ -240,8 +241,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override public void deleteById(Object id, Class domainType) { - Assert.notNull(id, "Id must not be null"); - Assert.notNull(domainType, "Domain type must not be null"); + Assert.notNull(id, "Id must not be null!"); + Assert.notNull(domainType, "Domain type must not be null!"); deleteTree(id, null, domainType); } @@ -253,7 +254,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { @Override 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); change.executeWith(interpreter, context, converter); @@ -274,7 +275,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations { 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( // Identifier.of(identifier), // diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java index 332794d7d..15093f3fa 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java +++ b/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() { NoIdMapChain4 chain4 = new NoIdMapChain4(); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateUnitTests.java new file mode 100644 index 000000000..cdc74b876 --- /dev/null +++ b/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; + } +}