This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ralf-ueberfuhr-ars/feature/layers
Add event for blog post created
- Loading branch information
Showing
7 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/de/sample/schulung/spring/blog/domain/BlogPostCreatedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
public record BlogPostCreatedEvent( | ||
BlogPost blogPost | ||
) { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/de/sample/schulung/spring/blog/domain/BlogPostEventLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class BlogPostEventLogger { | ||
|
||
@EventListener | ||
void logBlogPostCreated(BlogPostCreatedEvent evt) { | ||
log.debug("BlogPost created: {}", evt.blogPost()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
server: | ||
port: ${SERVER_PORT:9080} | ||
logging: | ||
level: | ||
de.sample.schulung.spring.blog.domain.BlogPostEventLogger: debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/de/sample/schulung/spring/blog/domain/BlogPostEventsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.sample.schulung.spring.blog.domain; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.event.ApplicationEvents; | ||
import org.springframework.test.context.event.RecordApplicationEvents; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@SpringBootTest | ||
@RecordApplicationEvents | ||
public class BlogPostEventsTests { | ||
|
||
@Autowired | ||
BlogPostService service; | ||
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | ||
@Autowired | ||
ApplicationEvents events; | ||
|
||
@Test | ||
void shouldPublishEventOnCreate() { | ||
final var newBlogPost = BlogPost | ||
.builder() | ||
.title("test") | ||
.content("test content with 10 chars") | ||
.build(); | ||
service.create(newBlogPost); | ||
|
||
assertThat(events.stream(BlogPostCreatedEvent.class)) | ||
.hasSize(1) | ||
.first() | ||
.extracting(BlogPostCreatedEvent::blogPost) | ||
.isSameAs(newBlogPost); | ||
} | ||
|
||
@Test | ||
void shouldNotPublishEventOnInvalidCreate() { | ||
final var newBlogPost = BlogPost | ||
.builder() | ||
.content("test content with 10 chars") | ||
.build(); | ||
assertThatThrownBy(() -> service.create(newBlogPost)) | ||
.isInstanceOf(ConstraintViolationException.class); | ||
assertThat(events.stream(BlogPostCreatedEvent.class)) | ||
.isEmpty(); | ||
} | ||
} |