GH-20 - Polishing.
This commit is contained in:
@@ -207,21 +207,21 @@ class JdbcEventPublicationRepositoryIntegrationTests {
|
||||
@Nested
|
||||
class DeleteCompletedPublications {
|
||||
|
||||
@Test
|
||||
// GH-20
|
||||
@Test // GH-20
|
||||
void shouldDeleteCompletedEvents() {
|
||||
TestEvent testEvent1 = new TestEvent("abc");
|
||||
String serializedEvent1 = "{\"eventId\":\"abc\"}";
|
||||
TestEvent testEvent2 = new TestEvent("def");
|
||||
String serializedEvent2 = "{\"eventId\":\"def\"}";
|
||||
|
||||
var testEvent1 = new TestEvent("abc");
|
||||
var serializedEvent1 = "{\"eventId\":\"abc\"}";
|
||||
var testEvent2 = new TestEvent("def");
|
||||
var serializedEvent2 = "{\"eventId\":\"def\"}";
|
||||
|
||||
when(serializer.serialize(testEvent1)).thenReturn(serializedEvent1);
|
||||
when(serializer.deserialize(serializedEvent1, TestEvent.class)).thenReturn(testEvent1);
|
||||
when(serializer.serialize(testEvent2)).thenReturn(serializedEvent2);
|
||||
when(serializer.deserialize(serializedEvent2, TestEvent.class)).thenReturn(testEvent2);
|
||||
|
||||
CompletableEventPublication publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
CompletableEventPublication publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
var publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
var publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
|
||||
repository.create(publication1);
|
||||
repository.create(publication2);
|
||||
@@ -230,10 +230,8 @@ class JdbcEventPublicationRepositoryIntegrationTests {
|
||||
|
||||
repository.deleteCompletedPublications();
|
||||
|
||||
var eventPublications = operations.query(
|
||||
"SELECT * FROM EVENT_PUBLICATION", (rs, __) -> rs.getString("SERIALIZED_EVENT"));
|
||||
assertThat(eventPublications).hasSize(1);
|
||||
assertThat(eventPublications.get(0)).isEqualTo(serializedEvent2);
|
||||
assertThat(operations.query("SELECT * FROM EVENT_PUBLICATION", (rs, __) -> rs.getString("SERIALIZED_EVENT")))
|
||||
.hasSize(1).element(0).isEqualTo(serializedEvent2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +38,15 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* @author Björn Kieling
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class JpaEventPublicationRepository implements EventPublicationRepository {
|
||||
class JpaEventPublicationRepository implements EventPublicationRepository {
|
||||
|
||||
private static String BY_EVENT_AND_LISTENER_ID = """
|
||||
select p
|
||||
select p
|
||||
from JpaEventPublication p
|
||||
where
|
||||
p.serializedEvent = ?1
|
||||
and p.listenerId = ?2
|
||||
and p.completionDate is null
|
||||
where
|
||||
p.serializedEvent = ?1
|
||||
and p.listenerId = ?2
|
||||
and p.completionDate is null
|
||||
""";
|
||||
|
||||
private static String INCOMPLETE = """
|
||||
@@ -56,11 +57,11 @@ public class JpaEventPublicationRepository implements EventPublicationRepository
|
||||
""";
|
||||
|
||||
private static final String DELETE_COMPLETED = """
|
||||
delete
|
||||
from JpaEventPublication p
|
||||
where
|
||||
p.completionDate is not null
|
||||
""";
|
||||
delete
|
||||
from JpaEventPublication p
|
||||
where
|
||||
p.completionDate is not null
|
||||
""";
|
||||
|
||||
private final EntityManager entityManager;
|
||||
private final EventSerializer serializer;
|
||||
|
||||
@@ -18,11 +18,10 @@ package org.springframework.modulith.events.jpa;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Value;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -30,7 +29,6 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
@@ -66,8 +64,6 @@ class JpaEventPublicationRepositoryIntegrationTests {
|
||||
|
||||
private static final EventSerializer eventSerializer = mock(EventSerializer.class);
|
||||
|
||||
@Autowired EntityManager entityManager;
|
||||
|
||||
@Configuration
|
||||
@Import(JpaEventPublicationConfiguration.class)
|
||||
static class TestConfig {
|
||||
@@ -112,6 +108,7 @@ class JpaEventPublicationRepositoryIntegrationTests {
|
||||
}
|
||||
|
||||
private final JpaEventPublicationRepository repository;
|
||||
private final EntityManager em;
|
||||
|
||||
@Test
|
||||
void persistsJpaEventPublication() {
|
||||
@@ -173,18 +170,19 @@ class JpaEventPublicationRepositoryIntegrationTests {
|
||||
|
||||
@Test // GH-20
|
||||
void shouldDeleteCompletedEvents() {
|
||||
TestEvent testEvent1 = new TestEvent("abc");
|
||||
String serializedEvent1 = "{\"eventId\":\"abc\"}";
|
||||
TestEvent testEvent2 = new TestEvent("def");
|
||||
String serializedEvent2 = "{\"eventId\":\"def\"}";
|
||||
|
||||
var testEvent1 = new TestEvent("abc");
|
||||
var serializedEvent1 = "{\"eventId\":\"abc\"}";
|
||||
var testEvent2 = new TestEvent("def");
|
||||
var serializedEvent2 = "{\"eventId\":\"def\"}";
|
||||
|
||||
when(eventSerializer.serialize(testEvent1)).thenReturn(serializedEvent1);
|
||||
when(eventSerializer.deserialize(serializedEvent1, TestEvent.class)).thenReturn(testEvent1);
|
||||
when(eventSerializer.serialize(testEvent2)).thenReturn(serializedEvent2);
|
||||
when(eventSerializer.deserialize(serializedEvent2, TestEvent.class)).thenReturn(testEvent2);
|
||||
|
||||
CompletableEventPublication publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
CompletableEventPublication publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
var publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
var publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
|
||||
repository.create(publication1);
|
||||
repository.create(publication2);
|
||||
@@ -193,10 +191,9 @@ class JpaEventPublicationRepositoryIntegrationTests {
|
||||
|
||||
repository.deleteCompletedPublications();
|
||||
|
||||
var eventPublications = entityManager.createQuery(
|
||||
"select p from JpaEventPublication p", JpaEventPublication.class).getResultList();
|
||||
assertThat(eventPublications).hasSize(1);
|
||||
assertThat(eventPublications.get(0).getSerializedEvent()).isEqualTo(serializedEvent2);
|
||||
assertThat(em.createQuery("select p from JpaEventPublication p", JpaEventPublication.class).getResultList())
|
||||
.hasSize(1) //
|
||||
.element(0).extracting(JpaEventPublication::getSerializedEvent).isEqualTo(serializedEvent2);
|
||||
}
|
||||
|
||||
@Value
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package org.springframework.modulith.events.mongodb;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@@ -25,8 +28,6 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.modulith.events.CompletableEventPublication;
|
||||
import org.springframework.modulith.events.EventPublication;
|
||||
@@ -68,19 +69,20 @@ class MongoDbEventPublicationRepository implements EventPublicationRepository {
|
||||
@Override
|
||||
public EventPublication update(CompletableEventPublication publication) {
|
||||
|
||||
return findDocumentsByEventAndTargetIdentifierAndCompletionDateNull(publication.getEvent(), publication.getTargetIdentifier())
|
||||
.stream()
|
||||
.findFirst()
|
||||
.map(document -> document.setCompletionDate(publication.getCompletionDate().orElse(null)))
|
||||
.map(mongoTemplate::save)
|
||||
.map(this::documentToDomain)
|
||||
.orElse(publication);
|
||||
return findDocumentsByEventAndTargetIdentifierAndCompletionDateNull(publication.getEvent(),
|
||||
publication.getTargetIdentifier()) //
|
||||
.stream() //
|
||||
.findFirst() //
|
||||
.map(document -> document.setCompletionDate(publication.getCompletionDate().orElse(null))) //
|
||||
.map(mongoTemplate::save) //
|
||||
.map(this::documentToDomain) //
|
||||
.orElse(publication);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EventPublication> findIncompletePublications() {
|
||||
|
||||
var query = Query.query(Criteria.where("completionDate").isNull());
|
||||
var query = query(where("completionDate").isNull());
|
||||
|
||||
return mongoTemplate.find(query, MongoDbEventPublication.class).stream() //
|
||||
.map(this::documentToDomain) //
|
||||
@@ -104,8 +106,7 @@ class MongoDbEventPublicationRepository implements EventPublicationRepository {
|
||||
|
||||
@Override
|
||||
public void deleteCompletedPublications() {
|
||||
var query = Query.query(Criteria.where("completionDate").ne(null));
|
||||
mongoTemplate.remove(query, MongoDbEventPublication.class);
|
||||
mongoTemplate.remove(query(where("completionDate").ne(null)), MongoDbEventPublication.class);
|
||||
}
|
||||
|
||||
private List<MongoDbEventPublication> findDocumentsByEventAndTargetIdentifierAndCompletionDateNull( //
|
||||
@@ -113,12 +114,11 @@ class MongoDbEventPublicationRepository implements EventPublicationRepository {
|
||||
|
||||
// we need to enforce writing of the type information
|
||||
var eventAsMongoType = mongoTemplate.getConverter().convertToMongoType(event, TypeInformation.of(Object.class));
|
||||
var query = Query //
|
||||
.query(Criteria //
|
||||
.where("event").is(eventAsMongoType) //
|
||||
var query = query(
|
||||
where("event").is(eventAsMongoType) //
|
||||
.and("listenerId").is(targetIdentifier.getValue()) //
|
||||
.and("completionDate").isNull()) //
|
||||
.with(Sort.by("publicationDate").ascending());
|
||||
.with(Sort.by("publicationDate").ascending());
|
||||
|
||||
return mongoTemplate.find(query, MongoDbEventPublication.class);
|
||||
}
|
||||
|
||||
@@ -28,8 +28,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.modulith.events.CompletableEventPublication;
|
||||
import org.springframework.modulith.events.EventPublication;
|
||||
import org.springframework.modulith.events.PublicationTargetIdentifier;
|
||||
@@ -76,7 +74,8 @@ class MongoDbEventPublicationRepositoryTest extends WithEmbeddedMongoDb {
|
||||
assertThat(eventPublications.get(0).getEvent()).isEqualTo(publication.getEvent());
|
||||
assertThat(eventPublications.get(0).getTargetIdentifier()).isEqualTo(publication.getTargetIdentifier());
|
||||
|
||||
assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER)).isPresent();
|
||||
assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER))
|
||||
.isPresent();
|
||||
|
||||
// Complete publication
|
||||
repository.update(publication.markCompleted());
|
||||
@@ -128,7 +127,8 @@ class MongoDbEventPublicationRepositoryTest extends WithEmbeddedMongoDb {
|
||||
|
||||
var testEvent = new TestEvent("id");
|
||||
|
||||
assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER)).isEmpty();
|
||||
assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER))
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -160,8 +160,8 @@ class MongoDbEventPublicationRepositoryTest extends WithEmbeddedMongoDb {
|
||||
var actual = repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER);
|
||||
|
||||
assertThat(actual).hasValueSatisfying(it -> //
|
||||
assertThat(it.getPublicationDate()) //
|
||||
.isCloseTo(publicationOld.getPublicationDate(), within(1, ChronoUnit.MILLIS)));
|
||||
assertThat(it.getPublicationDate()) //
|
||||
.isCloseTo(publicationOld.getPublicationDate(), within(1, ChronoUnit.MILLIS)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,11 +170,12 @@ class MongoDbEventPublicationRepositoryTest extends WithEmbeddedMongoDb {
|
||||
|
||||
@Test // GH-20
|
||||
void shouldDeleteCompletedEvents() {
|
||||
TestEvent testEvent1 = new TestEvent("abc");
|
||||
TestEvent testEvent2 = new TestEvent("def");
|
||||
|
||||
CompletableEventPublication publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
CompletableEventPublication publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
var testEvent1 = new TestEvent("abc");
|
||||
var testEvent2 = new TestEvent("def");
|
||||
|
||||
var publication1 = CompletableEventPublication.of(testEvent1, TARGET_IDENTIFIER);
|
||||
var publication2 = CompletableEventPublication.of(testEvent2, TARGET_IDENTIFIER);
|
||||
|
||||
repository.create(publication1);
|
||||
repository.create(publication2);
|
||||
@@ -183,10 +184,9 @@ class MongoDbEventPublicationRepositoryTest extends WithEmbeddedMongoDb {
|
||||
|
||||
repository.deleteCompletedPublications();
|
||||
|
||||
var eventPublications = mongoTemplate.findAll(MongoDbEventPublication.class);
|
||||
|
||||
assertThat(eventPublications).hasSize(1);
|
||||
assertThat(eventPublications.get(0).getEvent()).isEqualTo(testEvent2);
|
||||
assertThat(mongoTemplate.findAll(MongoDbEventPublication.class)) //
|
||||
.hasSize(1) //
|
||||
.element(0).extracting(MongoDbEventPublication::getEvent).isEqualTo(testEvent2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user