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 #6 from ralf-ueberfuhr-ars/feature/layers
Browse files Browse the repository at this point in the history
add blogpost initialization
  • Loading branch information
ralf-ueberfuhr-ars authored Dec 20, 2023
2 parents b5177ad + 83cfe59 commit d571511
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.sample.schulung.spring.blog.domain;

import lombok.RequiredArgsConstructor;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class BlogPostInitializer {

private final BlogPostService service;

@EventListener(ContextRefreshedEvent.class)
void initBlogPosts() {
if(service.count() == 0) {
service.create(
BlogPost
.builder()
.title("Welcome to the blog!")
.content("This is a great blog :)")
.build()
);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class BlogPostService {

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

public long count() {
return blogPosts.size();
}

public Stream<BlogPost> findAll() {
return blogPosts.values().stream();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.sample.schulung.spring.blog.domain;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
public class BlogPostServiceTests {

@Autowired
BlogPostService service;

@Test
void shouldNotBeEmpty() {
assertThat(service.count())
.isGreaterThan(0L);
assertThat(service.findAll())
.hasAtLeastOneElementOfType(BlogPost.class);
}

}

0 comments on commit d571511

Please sign in to comment.