Skip to content

Commit 663a2f7

Browse files
authored
Merge pull request eugenp#5196 from mikewojtyna/BAEL-2067
Add BAEL-2067 DomainEvents examples
2 parents 20137eb + 7ba5b99 commit 663a2f7

18 files changed

+451
-4
lines changed

spring-data-jpa/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@
4141
<artifactId>guava</artifactId>
4242
<version>21.0</version>
4343
</dependency>
44+
45+
<!-- JUnit Jupiter dependencies -->
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-api</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-engine</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<!-- JUnit platform launcher -->
57+
<!-- To be able to run tests from IDE directly -->
58+
<dependency>
59+
<groupId>org.junit.platform</groupId>
60+
<artifactId>junit-platform-launcher</artifactId>
61+
<version>${junit-platform.version}</version>
62+
<scope>test</scope>
63+
</dependency>
4464
</dependencies>
4565

4666
</project>

spring-data-jpa/src/main/java/com/baeldung/Application.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung;
22

3-
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
43
import org.springframework.boot.SpringApplication;
54
import org.springframework.boot.autoconfigure.SpringBootApplication;
6-
import org.springframework.boot.autoconfigure.domain.EntityScan;
75
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
86

7+
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
8+
99
@SpringBootApplication
1010
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
1111
public class Application {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.Transient;
9+
10+
import org.springframework.context.ApplicationEventPublisher;
11+
12+
@Entity
13+
class Aggregate {
14+
@Transient
15+
private ApplicationEventPublisher eventPublisher;
16+
@Id
17+
private long id;
18+
19+
private Aggregate() {
20+
}
21+
22+
Aggregate(long id, ApplicationEventPublisher eventPublisher) {
23+
this.id = id;
24+
this.eventPublisher = eventPublisher;
25+
}
26+
27+
/* (non-Javadoc)
28+
* @see java.lang.Object#toString()
29+
*/
30+
@Override
31+
public String toString() {
32+
return "DomainEntity [id=" + id + "]";
33+
}
34+
35+
void domainOperation() {
36+
// some business logic
37+
if (eventPublisher != null) {
38+
eventPublisher.publishEvent(new DomainEvent());
39+
}
40+
}
41+
42+
long getId() {
43+
return id;
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
import javax.persistence.Transient;
13+
14+
import org.springframework.data.domain.AfterDomainEventPublication;
15+
import org.springframework.data.domain.DomainEvents;
16+
17+
@Entity
18+
public class Aggregate2 {
19+
@Transient
20+
private final Collection<DomainEvent> domainEvents;
21+
@Id
22+
@GeneratedValue
23+
private long id;
24+
25+
public Aggregate2() {
26+
domainEvents = new ArrayList<>();
27+
}
28+
29+
@AfterDomainEventPublication
30+
public void clearEvents() {
31+
domainEvents.clear();
32+
}
33+
34+
public void domainOperation() {
35+
// some domain operation
36+
domainEvents.add(new DomainEvent());
37+
}
38+
39+
@DomainEvents
40+
public Collection<DomainEvent> events() {
41+
return domainEvents;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface Aggregate2Repository extends CrudRepository<Aggregate2, Long> {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
import org.springframework.data.domain.AbstractAggregateRoot;
11+
12+
@Entity
13+
public class Aggregate3 extends AbstractAggregateRoot<Aggregate3> {
14+
@Id
15+
@GeneratedValue
16+
private long id;
17+
18+
public void domainOperation() {
19+
// some domain operation
20+
registerEvent(new DomainEvent());
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
/**
9+
* @author goobar
10+
*
11+
*/
12+
public interface Aggregate3Repository extends CrudRepository<Aggregate3, Long> {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface AggregateRepository extends CrudRepository<Aggregate, Long> {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.boot.SpringBootConfiguration;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.context.annotation.PropertySource;
9+
10+
@SpringBootConfiguration
11+
@EnableAutoConfiguration
12+
@PropertySource("classpath:/ddd.properties")
13+
public class DddConfig {
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
class DomainEvent {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import javax.transaction.Transactional;
7+
8+
import org.springframework.context.ApplicationEventPublisher;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class DomainService {
13+
private final ApplicationEventPublisher eventPublisher;
14+
private final AggregateRepository repository;
15+
16+
public DomainService(AggregateRepository repository, ApplicationEventPublisher eventPublisher) {
17+
this.repository = repository;
18+
this.eventPublisher = eventPublisher;
19+
}
20+
21+
@Transactional
22+
public void serviceDomainOperation(long entityId) {
23+
repository.findById(entityId)
24+
.ifPresent(entity -> {
25+
entity.domainOperation();
26+
repository.save(entity);
27+
eventPublisher.publishEvent(new DomainEvent());
28+
});
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.datasource.initialization-mode=never

spring-data-jpa/src/main/resources/persistence-multiple-db.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ jdbc.driverClassName=org.h2.Driver
33
user.jdbc.url=jdbc:h2:mem:spring_jpa_user;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
44
product.jdbc.url=jdbc:h2:mem:spring_jpa_product;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS PRODUCTS
55
jdbc.user=sa
6-
jdbc.pass=
6+
jdbc.pass=sa
77

88
# hibernate.X
99
hibernate.dialect=org.hibernate.dialect.H2Dialect

spring-data-jpa/src/main/resources/persistence.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
jdbc.driverClassName=org.h2.Driver
33
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS USERS
44
jdbc.user=sa
5-
jdbc.pass=
5+
jdbc.pass=sa
66

77
# hibernate.X
88
hibernate.dialect=org.hibernate.dialect.H2Dialect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.times;
8+
import static org.mockito.Mockito.verify;
9+
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.boot.test.mock.mockito.MockBean;
16+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
17+
18+
@SpringJUnitConfig
19+
@SpringBootTest
20+
class Aggregate2EventsIntegrationTest {
21+
@MockBean
22+
private TestEventHandler eventHandler;
23+
@Autowired
24+
private Aggregate2Repository repository;
25+
26+
// @formatter:off
27+
@DisplayName("given aggregate with @AfterDomainEventPublication,"
28+
+ " when do domain operation and save twice,"
29+
+ " then an event is published only for the first time")
30+
// @formatter:on
31+
@Test
32+
void afterDomainEvents() {
33+
// given
34+
Aggregate2 aggregate = new Aggregate2();
35+
36+
// when
37+
aggregate.domainOperation();
38+
repository.save(aggregate);
39+
repository.save(aggregate);
40+
41+
// then
42+
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
43+
}
44+
45+
@BeforeEach
46+
void beforeEach() {
47+
repository.deleteAll();
48+
}
49+
50+
// @formatter:off
51+
@DisplayName("given aggregate with @DomainEvents,"
52+
+ " when do domain operation and save,"
53+
+ " then an event is published")
54+
// @formatter:on
55+
@Test
56+
void domainEvents() {
57+
// given
58+
Aggregate2 aggregate = new Aggregate2();
59+
60+
// when
61+
aggregate.domainOperation();
62+
repository.save(aggregate);
63+
64+
// then
65+
verify(eventHandler, times(1)).handleEvent(any(DomainEvent.class));
66+
}
67+
68+
}

0 commit comments

Comments
 (0)