From 10b5ee8ac9861216e3c507f956deffd94553e70b Mon Sep 17 00:00:00 2001 From: Kirill Mokevnin Date: Sun, 27 Aug 2023 18:30:51 -0400 Subject: [PATCH] add test data generator Signed-off-by: Kirill Mokevnin --- src/main/java/io/hexlet/blog/Application.java | 6 ++++ .../blog/controller/api/PostsController.java | 4 +++ .../controller/api/PostsControllerTest.java | 16 +++++++--- .../io/hexlet/blog/util/ModelGenerator.java | 31 +++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/test/java/io/hexlet/blog/util/ModelGenerator.java diff --git a/src/main/java/io/hexlet/blog/Application.java b/src/main/java/io/hexlet/blog/Application.java index ee384bb..7fc7218 100644 --- a/src/main/java/io/hexlet/blog/Application.java +++ b/src/main/java/io/hexlet/blog/Application.java @@ -6,6 +6,7 @@ import org.springframework.context.annotation.Bean; import lombok.AllArgsConstructor; +import net.datafaker.Faker; @SpringBootApplication @AllArgsConstructor @@ -18,4 +19,9 @@ public static void main(String[] args) { public ModelMapper getModelMapper() { return new ModelMapper(); } + + @Bean + public Faker getFaker() { + return new Faker(); + } } diff --git a/src/main/java/io/hexlet/blog/controller/api/PostsController.java b/src/main/java/io/hexlet/blog/controller/api/PostsController.java index 482d8af..38cb9c7 100644 --- a/src/main/java/io/hexlet/blog/controller/api/PostsController.java +++ b/src/main/java/io/hexlet/blog/controller/api/PostsController.java @@ -40,6 +40,7 @@ public class PostsController { private UserUtils userUtils; @GetMapping("/posts") + @ResponseStatus(HttpStatus.OK) ResponseEntity> index() { var posts = repository.findAll(); var result = posts.stream() @@ -65,6 +66,7 @@ PostDTO create(@Valid @RequestBody Post postData) throws JsonProcessingException } @GetMapping("/posts/{id}") + @ResponseStatus(HttpStatus.OK) PostDTO show(@PathVariable Long id) { var post = repository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("Not Found: " + id)); @@ -73,6 +75,7 @@ PostDTO show(@PathVariable Long id) { } @PutMapping("/posts/{id}") + @ResponseStatus(HttpStatus.OK) void update(@RequestBody @Valid Post postData, @PathVariable Long id) { var post = repository.findById(id) .orElseThrow(() -> new ResourceNotFoundException("Not Found")); @@ -81,6 +84,7 @@ void update(@RequestBody @Valid Post postData, @PathVariable Long id) { } @DeleteMapping("/posts/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) void delete(@PathVariable Long id) { repository.deleteById(id); } diff --git a/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java b/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java index 7ece8b4..cc36aee 100644 --- a/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java +++ b/src/test/java/io/hexlet/blog/controller/api/PostsControllerTest.java @@ -22,6 +22,7 @@ import io.hexlet.blog.model.Post; import io.hexlet.blog.repository.PostRepository; +import io.hexlet.blog.util.ModelGenerator; import io.hexlet.blog.util.UserUtils; @SpringBootTest @@ -34,6 +35,9 @@ public class PostsControllerTest { @Autowired private ObjectMapper om; + @Autowired + private ModelGenerator modelGenerator; + @Autowired private PostRepository postRepository; @@ -45,6 +49,10 @@ public class PostsControllerTest { @BeforeEach public void setUp() { token = jwt().jwt(builder -> builder.subject("hexlet@example.com")); + var testPost = Instancio.of(modelGenerator.getPostModel()) + .create(); + testPost.setAuthor(userUtils.getTestUser()); + postRepository.save(testPost); } @Test @@ -58,8 +66,7 @@ public void testIndex() throws Exception { @Test public void testCreate() throws Exception { - var data = Instancio.of(Post.class) - .ignore(field(Post.class, "id")) + var data = Instancio.of(modelGenerator.getPostModel()) .create(); var request = post("/api/posts") @@ -68,7 +75,7 @@ public void testCreate() throws Exception { .content(om.writeValueAsString(data)); mockMvc.perform(request) - .andExpect(status().isOk()); + .andExpect(status().isCreated()); var post = postRepository.findBySlug(data.getSlug()); assertNotNull(post.get()); @@ -76,8 +83,7 @@ public void testCreate() throws Exception { @Test public void testShow() throws Exception { - var post = Instancio.of(Post.class) - .ignore(field(Post.class, "id")) + var post = Instancio.of(modelGenerator.getPostModel()) .create(); post.setAuthor(userUtils.getTestUser()); postRepository.save(post); diff --git a/src/test/java/io/hexlet/blog/util/ModelGenerator.java b/src/test/java/io/hexlet/blog/util/ModelGenerator.java new file mode 100644 index 0000000..7cb1d4c --- /dev/null +++ b/src/test/java/io/hexlet/blog/util/ModelGenerator.java @@ -0,0 +1,31 @@ +package io.hexlet.blog.util; + +import org.instancio.Instancio; +import org.instancio.Model; +import org.instancio.Select; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import io.hexlet.blog.model.Post; +import jakarta.annotation.PostConstruct; +import lombok.Getter; +import net.datafaker.Faker; + +@Getter +@Component +public class ModelGenerator { + private Model postModel; + + @Autowired + private Faker faker; + + @PostConstruct + private void init() { + postModel = Instancio.of(Post.class) + .ignore(Select.field(Post::getId)) + .supply(Select.field(Post::getName), () -> faker.gameOfThrones().house()) + .supply(Select.field(Post::getBody), () -> faker.gameOfThrones().quote()) + .toModel(); + } +} +