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 #6 from ralf-ueberfuhr-ars/feature/layers
add blogpost initialization
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/de/sample/schulung/spring/blog/domain/BlogPostInitializer.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,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() | ||
); | ||
} | ||
|
||
} | ||
|
||
} |
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
23 changes: 23 additions & 0 deletions
23
src/test/java/de/sample/schulung/spring/blog/domain/BlogPostServiceTests.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,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); | ||
} | ||
|
||
} |