DATAJDBC-197 - Repositories find methods now emit AfterLoadEvents as intended.

Event publishing moved into the JdbcEntityTemplate in order to ensure events for AggregateRoots.

Removed superfluous AggregateChange from AfterCreation event.

Original pull request: #58.
This commit is contained in:
Jens Schauder
2018-04-11 12:14:12 +02:00
committed by Mark Paluch
parent d4b2eca43a
commit fbac3a5529
5 changed files with 88 additions and 138 deletions

View File

@@ -1,57 +0,0 @@
/*
* Copyright 2017-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.jdbc.core;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.mapping.event.AfterCreation;
import org.springframework.data.jdbc.mapping.event.Identifier;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
import org.springframework.jdbc.core.RowMapper;
/**
* A {@link RowMapper} that publishes events after a delegate, did the actual work of mapping a {@link ResultSet} to an
* entityInformation.
*
* @author Jens Schauder
* @since 2.0
*/
@RequiredArgsConstructor
public class EventPublishingEntityRowMapper<T> implements RowMapper<T> {
private final @NonNull RowMapper<T> delegate;
private final @NonNull JdbcPersistentEntityInformation<T, ?> entityInformation;
private final @NonNull ApplicationEventPublisher publisher;
/*
* (non-Javadoc)
* @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
*/
@Override
public T mapRow(ResultSet resultSet, int i) throws SQLException {
T instance = delegate.mapRow(resultSet, i);
publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance, null));
return instance;
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.data.jdbc.core.conversion.AggregateChange.Kind;
import org.springframework.data.jdbc.core.conversion.Interpreter;
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
import org.springframework.data.jdbc.mapping.event.AfterCreation;
import org.springframework.data.jdbc.mapping.event.AfterDelete;
import org.springframework.data.jdbc.mapping.event.AfterSave;
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
@@ -90,7 +91,12 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
@Override
public <T> T findById(Object id, Class<T> domainType) {
return accessStrategy.findById(id, domainType);
T entity = accessStrategy.findById(id, domainType);
if (entity != null) {
publishAfterCreation(id, entity);
}
return entity;
}
@Override
@@ -100,12 +106,18 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
@Override
public <T> Iterable<T> findAll(Class<T> domainType) {
return accessStrategy.findAll(domainType);
Iterable<T> all = accessStrategy.findAll(domainType);
publishAfterCreation(all);
return all;
}
@Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
return accessStrategy.findAllById(ids, domainType);
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
publishAfterCreation(allById);
return allById;
}
@Override
@@ -161,4 +173,15 @@ public class JdbcEntityTemplate implements JdbcEntityOperations {
jdbcEntityDeleteWriter.write(null, aggregateChange);
return aggregateChange;
}
private <T> void publishAfterCreation(Iterable<T> all) {
all.forEach(e -> {
publishAfterCreation(context.getRequiredPersistentEntityInformation((Class<T>) e.getClass()).getRequiredId(e), e);
});
}
private <T> void publishAfterCreation(Object id, T entity) {
publisher.publishEvent(new AfterCreation(Identifier.of(id), entity));
}
}

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.jdbc.mapping.event;
import org.springframework.data.jdbc.core.conversion.AggregateChange;
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
/**
@@ -32,9 +31,8 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
/**
* @param id of the entity
* @param entity the newly instantiated entity.
* @param change
*/
public AfterCreation(Specified id, Object entity, AggregateChange change) {
super(id, entity, change);
public AfterCreation(Specified id, Object entity) {
super(id, entity, null);
}
}

View File

@@ -1,70 +0,0 @@
/*
* Copyright 2017-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.jdbc.core;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.*;
import lombok.Value;
import java.sql.ResultSet;
import java.sql.SQLException;
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.mapping.event.AfterCreation;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
import org.springframework.jdbc.core.RowMapper;
/**
* Unit tests for {@link EventPublishingEntityRowMapper}.
*
* @author Jens Schauder
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class EventPublishingEntityRowMapperUnitTests {
@Mock RowMapper<DummyEntity> rowMapperDelegate;
@Mock JdbcPersistentEntityInformation<DummyEntity, Long> entityInformation;
@Mock ApplicationEventPublisher publisher;
@Test // DATAJDBC-99
public void eventGetsPublishedAfterInstantiation() throws SQLException {
when(rowMapperDelegate.mapRow(any(ResultSet.class), anyInt())).thenReturn(new DummyEntity(1L));
when(entityInformation.getRequiredId(any())).thenReturn(1L);
EventPublishingEntityRowMapper<?> rowMapper = new EventPublishingEntityRowMapper<>(rowMapperDelegate,
entityInformation, publisher);
ResultSet resultSet = mock(ResultSet.class);
rowMapper.mapRow(resultSet, 1);
verify(publisher).publishEvent(isA(AfterCreation.class));
}
@Value
static class DummyEntity {
@Id Long Id;
}
}

View File

@@ -21,6 +21,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.event.AfterCreation;
import org.springframework.data.jdbc.mapping.event.AfterDelete;
import org.springframework.data.jdbc.mapping.event.AfterSave;
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
@@ -42,18 +43,22 @@ public class SimpleJdbcRepositoryEventsUnitTests {
FakePublisher publisher = new FakePublisher();
DummyEntityRepository repository;
DefaultDataAccessStrategy dataAccessStrategy;
@Before
public void before() {
final JdbcMappingContext context = new JdbcMappingContext(createIdGeneratingOperations());
dataAccessStrategy = spy(new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
context //
));
JdbcRepositoryFactory factory = new JdbcRepositoryFactory( //
publisher, //
context, //
new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
context //
) //
dataAccessStrategy //
);
repository = factory.getRepository(DummyEntityRepository.class);
@@ -122,6 +127,57 @@ public class SimpleJdbcRepositoryEventsUnitTests {
);
}
@Test // DATAJDBC-197
public void publishesEventsOnFindAll() {
DummyEntity entity1 = new DummyEntity(42L);
DummyEntity entity2 = new DummyEntity(23L);
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(Class.class));
repository.findAll();
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterCreation.class, //
AfterCreation.class //
);
}
@Test // DATAJDBC-197
public void publishesEventsOnFindAllById() {
DummyEntity entity1 = new DummyEntity(42L);
DummyEntity entity2 = new DummyEntity(23L);
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAllById(any(Iterable.class), any(Class.class));
repository.findAllById(asList(42L, 23L));
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterCreation.class, //
AfterCreation.class //
);
}
@Test // DATAJDBC-197
public void publishesEventsOnFindById() {
DummyEntity entity1 = new DummyEntity(23L);
doReturn(entity1).when(dataAccessStrategy).findById(eq(23L), any(Class.class));
repository.findById(23L);
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterCreation.class //
);
}
private static NamedParameterJdbcOperations createIdGeneratingOperations() {
Answer<Integer> setIdInKeyHolder = invocation -> {