Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Showcase: Domain Events #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/classvar/examples/ExampleEntity.java
Original file line number Diff line number Diff line change
@@ -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<ExampleEntity> {

// 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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/classvar/examples/ExampleEntityCreated.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/classvar/examples/ExampleEventListener.java
Original file line number Diff line number Diff line change
@@ -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 ...
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/classvar/examples/ExampleRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.classvar.examples;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ExampleRepository extends JpaRepository<ExampleEntity, Long> {
}
Original file line number Diff line number Diff line change
@@ -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));
}
}