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

@ -41,6 +41,26 @@
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<!-- JUnit Jupiter dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit platform launcher -->
<!-- To be able to run tests from IDE directly -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,11 +1,11 @@
package com.baeldung;
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
public class Application {

View File

@ -0,0 +1,46 @@
/**
*
*/
package com.baeldung.ddd.event;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
import org.springframework.context.ApplicationEventPublisher;
@Entity
class Aggregate {
@Transient
private ApplicationEventPublisher eventPublisher;
@Id
private long id;
private Aggregate() {
}
Aggregate(long id, ApplicationEventPublisher eventPublisher) {
this.id = id;
this.eventPublisher = eventPublisher;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "DomainEntity [id=" + id + "]";
}
void domainOperation() {
// some business logic
if (eventPublisher != null) {
eventPublisher.publishEvent(new DomainEvent());
}
}
long getId() {
return id;
}
}

View File

@ -0,0 +1,44 @@
/**
*
*/
package com.baeldung.ddd.event;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import org.springframework.data.domain.AfterDomainEventPublication;
import org.springframework.data.domain.DomainEvents;
@Entity
public class Aggregate2 {
@Transient
private final Collection<DomainEvent> domainEvents;
@Id
@GeneratedValue
private long id;
public Aggregate2() {
domainEvents = new ArrayList<>();
}
@AfterDomainEventPublication
public void clearEvents() {
domainEvents.clear();
}
public void domainOperation() {
// some domain operation
domainEvents.add(new DomainEvent());
}
@DomainEvents
public Collection<DomainEvent> events() {
return domainEvents;
}
}

View File

@ -0,0 +1,10 @@
/**
*
*/
package com.baeldung.ddd.event;
import org.springframework.data.repository.CrudRepository;
public interface Aggregate2Repository extends CrudRepository<Aggregate2, Long> {
}

View File

@ -0,0 +1,23 @@
/**
*
*/
package com.baeldung.ddd.event;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.springframework.data.domain.AbstractAggregateRoot;
@Entity
public class Aggregate3 extends AbstractAggregateRoot<Aggregate3> {
@Id
@GeneratedValue
private long id;
public void domainOperation() {
// some domain operation
registerEvent(new DomainEvent());
}
}

View File

@ -0,0 +1,14 @@
/**
*
*/
package com.baeldung.ddd.event;
import org.springframework.data.repository.CrudRepository;
/**
* @author goobar
*
*/
public interface Aggregate3Repository extends CrudRepository<Aggregate3, Long> {
}

View File

@ -0,0 +1,10 @@
/**
*
*/
package com.baeldung.ddd.event;
import org.springframework.data.repository.CrudRepository;
public interface AggregateRepository extends CrudRepository<Aggregate, Long> {
}

View File

@ -0,0 +1,15 @@
/**
*
*/
package com.baeldung.ddd.event;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
@SpringBootConfiguration
@EnableAutoConfiguration
@PropertySource("classpath:/ddd.properties")
public class DddConfig {
}

View File

@ -0,0 +1,8 @@
/**
*
*/
package com.baeldung.ddd.event;
class DomainEvent {
}

View File

@ -0,0 +1,31 @@
/**
*
*/
package com.baeldung.ddd.event;
import javax.transaction.Transactional;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
@Service
public class DomainService {
private final ApplicationEventPublisher eventPublisher;
private final AggregateRepository repository;
public DomainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) {
this.repository = repository;
this.eventPublisher = eventPublisher;
}
@Transactional
public void serviceDomainOperation(long entityId) {
repository.findById(entityId)
.ifPresent(entity -> {
entity.domainOperation();
repository.save(entity);
eventPublisher.publishEvent(new DomainEvent());
});
}
}

View File

@ -0,0 +1 @@
spring.datasource.initialization-mode=never

View File

@ -3,7 +3,7 @@ jdbc.driverClassName=org.h2.Driver
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
jdbc.user=sa
jdbc.pass=
jdbc.pass=sa
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect

View File

@ -2,7 +2,7 @@
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
jdbc.user=sa
jdbc.pass=
jdbc.pass=sa
# hibernate.X
hibernate.dialect=org.hibernate.dialect.H2Dialect

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