Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from ralf-ueberfuhr-ars/feature/layers
Browse files Browse the repository at this point in the history
Add event for blog post created
  • Loading branch information
ralf-ueberfuhr-ars authored Dec 20, 2023
2 parents d571511 + 172bd56 commit a33cd96
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.UUID;

@Builder
@Getter
@Setter
@ToString
public class BlogPost {

private UUID id;
Expand Down
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
) {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

Expand All @@ -14,9 +16,11 @@

@Validated
@Service
@RequiredArgsConstructor
public class BlogPostService {

private final Map<UUID, BlogPost> blogPosts = new HashMap<>();
private final ApplicationEventPublisher eventPublisher;

public long count() {
return blogPosts.size();
Expand All @@ -37,6 +41,7 @@ public void create(@Valid @NotNull BlogPost blogPost) {
blogPost.setId(id);
blogPost.setTimestamp(LocalDateTime.now());
this.blogPosts.put(id, blogPost);
this.eventPublisher.publishEvent(new BlogPostCreatedEvent(blogPost));
}


Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.sample.schulung.spring.blog.domain.BlogPostService;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import org.mockito.Mockito;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -11,7 +13,8 @@ public class BlogPostControllerTests {

@Test
void shouldCreateBlogPostSuccessfully() {
final var service = new BlogPostService();
final var eventPublisher = Mockito.mock(ApplicationEventPublisher.class);
final var service = new BlogPostService(eventPublisher);
final var mapper = Mappers.getMapper(BlogPostDtoMapper.class);
final var controller = new BlogPostController(service, mapper);
final var blogPost = new BlogPostDto();
Expand All @@ -27,6 +30,8 @@ void shouldCreateBlogPostSuccessfully() {
assertThat(result.getBody().getTitle())
.isEqualTo(blogPost.getTitle());

// TODO verify eventPublisher.publishEvent?

}

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

0 comments on commit a33cd96

Please sign in to comment.