Add BAEL-2067 DomainEvents examples

This commit is contained in:
Mike Wojtyna
2018-09-08 02:05:31 +02:00
parent f47113f662
commit 7ba5b99925
18 changed files with 451 additions and 4 deletions

View File

@@ -0,0 +1,68 @@
/**
*
*/
package com.baeldung.ddd.event;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
@SpringBootTest
class Aggregate2EventsIntegrationTest {
@MockBean
private TestEventHandler eventHandler;
@Autowired
private Aggregate2Repository repository;
// @formatter:off
@DisplayName("given aggregate with @AfterDomainEventPublication,"
+ " when do domain operation and save twice,"
+ " then an event is published only for the first time")
// @formatter:on
@Test
void afterDomainEvents() {
// given
Aggregate2 aggregate = new Aggregate2();
// when
aggregate.domainOperation();
repository.save(aggregate);
repository.save(aggregate);
// then
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
}
@BeforeEach
void beforeEach() {
repository.deleteAll();
}
// @formatter:off
@DisplayName("given aggregate with @DomainEvents,"
+ " when do domain operation and save,"
+ " then an event is published")
// @formatter:on
@Test
void domainEvents() {
// given
Aggregate2 aggregate = new Aggregate2();
// when
aggregate.domainOperation();
repository.save(aggregate);
// then
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
}
}

View File

@@ -0,0 +1,63 @@
/**
*
*/
package com.baeldung.ddd.event;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
@SpringBootTest
class Aggregate3EventsIntegrationTest {
@MockBean
private TestEventHandler eventHandler;
@Autowired
private Aggregate3Repository repository;
// @formatter:off
@DisplayName("given aggregate extending AbstractAggregateRoot,"
+ " when do domain operation and save twice,"
+ " then an event is published only for the first time")
// @formatter:on
@Test
void afterDomainEvents() {
// given
Aggregate3 aggregate = new Aggregate3();
// when
aggregate.domainOperation();
repository.save(aggregate);
repository.save(aggregate);
// then
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
}
// @formatter:off
@DisplayName("given aggregate extending AbstractAggregateRoot,"
+ " when do domain operation and save,"
+ " then an event is published")
// @formatter:on
@Test
void domainEvents() {
// given
Aggregate3 aggregate = new Aggregate3();
// when
aggregate.domainOperation();
repository.save(aggregate);
// then
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
}
}

View File

@@ -0,0 +1,82 @@
package com.baeldung.ddd.event;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
@SpringBootTest
class AggregateEventsIntegrationTest {
@Autowired
private DomainService domainService;
@MockBean
private TestEventHandler eventHandler;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private AggregateRepository repository;
// @formatter:off
@DisplayName("given existing aggregate,"
+ " when do domain operation directly on aggregate,"
+ " then domain event is NOT published")
// @formatter:on
@Test
void aggregateEventsTest() {
Aggregate existingDomainEntity = new Aggregate(0, eventPublisher);
repository.save(existingDomainEntity);
// when
repository.findById(existingDomainEntity.getId())
.get()
.domainOperation();
// then
verifyZeroInteractions(eventHandler);
}
@BeforeEach
void beforeEach() {
repository.deleteAll();
}
// @formatter:off
@DisplayName("given existing aggregate,"
+ " when do domain operation on service,"
+ " then domain event is published")
// @formatter:on
@Test
void serviceEventsTest() {
Aggregate existingDomainEntity = new Aggregate(1, eventPublisher);
repository.save(existingDomainEntity);
// when
domainService.serviceDomainOperation(existingDomainEntity.getId());
// then
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
}
@TestConfiguration
public static class TestConfig {
@Bean
public DomainService domainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) {
return new DomainService(repository, eventPublisher);
}
}
}

View File

@@ -0,0 +1,12 @@
/**
*
*/
package com.baeldung.ddd.event;
import org.springframework.transaction.event.TransactionalEventListener;
interface TestEventHandler {
@TransactionalEventListener
void handleEvent(DomainEvent event);
}