-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kirill Mokevnin <[email protected]>
- Loading branch information
Showing
4 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -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("[email protected]")); | ||
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,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); | ||
|
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,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(); | ||
} | ||
} | ||
|