diff --git a/src/main/java/com/classvar/examples/ExampleCommandProcessor.java b/src/main/java/com/classvar/examples/ExampleCommandProcessor.java new file mode 100644 index 0000000..eb121a4 --- /dev/null +++ b/src/main/java/com/classvar/examples/ExampleCommandProcessor.java @@ -0,0 +1,21 @@ +package com.classvar.examples; + +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; + +@Service +public class ExampleCommandProcessor { + + private ExampleRepository exampleRepository; + + public ExampleCommandProcessor(ExampleRepository exampleRepository) { + this.exampleRepository = exampleRepository; + } + + @Transactional + public void doExampleOperation() { + ExampleEntity toCreate = new ExampleEntity("name"); + exampleRepository.save(toCreate); + } +} diff --git a/src/main/java/com/classvar/examples/ExampleEntity.java b/src/main/java/com/classvar/examples/ExampleEntity.java new file mode 100644 index 0000000..66467c2 --- /dev/null +++ b/src/main/java/com/classvar/examples/ExampleEntity.java @@ -0,0 +1,30 @@ +package com.classvar.examples; + +import org.springframework.data.domain.AbstractAggregateRoot; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class ExampleEntity extends AbstractAggregateRoot { + + // MySQL + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + protected ExampleEntity() {} + + public ExampleEntity(String name) { + this.name = name; + this.registerEvent(new ExampleEntityCreated(this)); + } + + public String getName() { + return this.name; + } +} diff --git a/src/main/java/com/classvar/examples/ExampleEntityCreated.java b/src/main/java/com/classvar/examples/ExampleEntityCreated.java new file mode 100644 index 0000000..570797c --- /dev/null +++ b/src/main/java/com/classvar/examples/ExampleEntityCreated.java @@ -0,0 +1,14 @@ +package com.classvar.examples; + +public class ExampleEntityCreated { + + private ExampleEntity entity; + + public ExampleEntityCreated(ExampleEntity entity) { + this.entity = entity; + } + + public ExampleEntity getEntity() { + return this.entity; + } +} diff --git a/src/main/java/com/classvar/examples/ExampleEventListener.java b/src/main/java/com/classvar/examples/ExampleEventListener.java new file mode 100644 index 0000000..96842f3 --- /dev/null +++ b/src/main/java/com/classvar/examples/ExampleEventListener.java @@ -0,0 +1,13 @@ +package com.classvar.examples; + +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class ExampleEventListener { + + @EventListener + public void handle(ExampleEntityCreated event) { + // ... do something ... + } +} diff --git a/src/main/java/com/classvar/examples/ExampleRepository.java b/src/main/java/com/classvar/examples/ExampleRepository.java new file mode 100644 index 0000000..1ae3ff4 --- /dev/null +++ b/src/main/java/com/classvar/examples/ExampleRepository.java @@ -0,0 +1,6 @@ +package com.classvar.examples; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ExampleRepository extends JpaRepository { +} diff --git a/src/test/java/com/classvar/examples/ExampleEntityDomainEventTest.java b/src/test/java/com/classvar/examples/ExampleEntityDomainEventTest.java new file mode 100644 index 0000000..7e82f63 --- /dev/null +++ b/src/test/java/com/classvar/examples/ExampleEntityDomainEventTest.java @@ -0,0 +1,27 @@ +package com.classvar.examples; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@SpringBootTest +public class ExampleEntityDomainEventTest { + + @Autowired + ExampleCommandProcessor processor; + + @MockBean + ExampleEventListener listener; + + @Test + public void DomainEventIsPublishedAfterTransaction() { + processor.doExampleOperation(); + + verify(listener, times(1)).handle(Mockito.isA(ExampleEntityCreated.class)); + } +}