Skip to content

Commit

Permalink
GH-20 - Polishing.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Jul 28, 2022
1 parent af99902 commit 9a52daa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,21 @@ void shouldSilentlyIgnoreNotSerializableEvents() {
@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);
Expand All @@ -230,10 +230,8 @@ void shouldDeleteCompletedEvents() {

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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
* @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 = """
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@
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;

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;
Expand Down Expand Up @@ -66,8 +64,6 @@ class JpaEventPublicationRepositoryIntegrationTests {

private static final EventSerializer eventSerializer = mock(EventSerializer.class);

@Autowired EntityManager entityManager;

@Configuration
@Import(JpaEventPublicationConfiguration.class)
static class TestConfig {
Expand Down Expand Up @@ -112,6 +108,7 @@ JpaTransactionManager transactionManager(EntityManagerFactory factory) {
}

private final JpaEventPublicationRepository repository;
private final EntityManager em;

@Test
void persistsJpaEventPublication() {
Expand Down Expand Up @@ -173,18 +170,19 @@ void shouldNotReturnCompletedEvents() {

@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);
Expand All @@ -193,10 +191,9 @@ void shouldDeleteCompletedEvents() {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,8 +28,6 @@

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;
Expand Down Expand Up @@ -68,19 +69,20 @@ public EventPublication create(EventPublication publication) {
@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) //
Expand All @@ -104,21 +106,19 @@ public Optional<EventPublication> findIncompletePublicationsByEventAndTargetIden

@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( //
Object event, PublicationTargetIdentifier targetIdentifier) {

// 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
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;
Expand Down Expand Up @@ -76,7 +74,8 @@ void shouldPersistAndUpdateEventPublication() {
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());
Expand Down Expand Up @@ -128,7 +127,8 @@ void shouldTolerateEmptyResultTest() {

var testEvent = new TestEvent("id");

assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER)).isEmpty();
assertThat(repository.findIncompletePublicationsByEventAndTargetIdentifier(testEvent, TARGET_IDENTIFIER))
.isEmpty();
}

@Test
Expand Down Expand Up @@ -160,8 +160,8 @@ void shouldReturnTheOldestEventTest() throws InterruptedException {
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)));
}
}

Expand All @@ -170,11 +170,12 @@ class DeleteCompletedPublications {

@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);
Expand All @@ -183,10 +184,9 @@ void shouldDeleteCompletedEvents() {

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);
}
}

Expand Down

0 comments on commit 9a52daa

Please sign in to comment.