Skip to content

Commit

Permalink
add test data generator
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Mokevnin <[email protected]>
  • Loading branch information
mokevnin committed Aug 27, 2023
1 parent fbd3968 commit 10b5ee8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/main/java/io/hexlet/blog/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.context.annotation.Bean;

import lombok.AllArgsConstructor;
import net.datafaker.Faker;

@SpringBootApplication
@AllArgsConstructor
Expand All @@ -18,4 +19,9 @@ public static void main(String[] args) {
public ModelMapper getModelMapper() {
return new ModelMapper();
}

@Bean
public Faker getFaker() {
return new Faker();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class PostsController {
private UserUtils userUtils;

@GetMapping("/posts")
@ResponseStatus(HttpStatus.OK)
ResponseEntity<List<PostDTO>> index() {
var posts = repository.findAll();
var result = posts.stream()
Expand All @@ -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));
Expand All @@ -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"));
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +35,9 @@ public class PostsControllerTest {
@Autowired
private ObjectMapper om;

@Autowired
private ModelGenerator modelGenerator;

@Autowired
private PostRepository postRepository;

Expand All @@ -45,6 +49,10 @@ public class PostsControllerTest {
@BeforeEach
public void setUp() {
token = jwt().jwt(builder -> builder.subject("[email protected]"));
var testPost = Instancio.of(modelGenerator.getPostModel())
.create();
testPost.setAuthor(userUtils.getTestUser());
postRepository.save(testPost);
}

@Test
Expand All @@ -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")
Expand All @@ -68,16 +75,15 @@ 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());
}

@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);
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/io/hexlet/blog/util/ModelGenerator.java
Original file line number Diff line number Diff line change
@@ -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<Post> 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();
}
}

0 comments on commit 10b5ee8

Please sign in to comment.