-
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
12 changed files
with
50 additions
and
54 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
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
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ public User getCurrentUser() { | |
} | ||
|
||
public User getTestUser() { | ||
return userRepository.findByEmail("[email protected]").get(); | ||
return userRepository.findByEmail("[email protected]") | ||
.orElseThrow(() -> new RuntimeException("User doesn't exist")); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
|
@@ -24,6 +25,7 @@ | |
|
||
import io.hexlet.blog.dto.PostUpdateDTO; | ||
import io.hexlet.blog.mapper.PostMapper; | ||
import io.hexlet.blog.model.Post; | ||
import io.hexlet.blog.repository.PostRepository; | ||
import io.hexlet.blog.util.ModelGenerator; | ||
import io.hexlet.blog.util.UserUtils; | ||
|
@@ -52,17 +54,19 @@ public class PostsControllerTest { | |
|
||
private JwtRequestPostProcessor token; | ||
|
||
private Post testPost; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
token = jwt().jwt(builder -> builder.subject("[email protected]")); | ||
var testPost = Instancio.of(modelGenerator.getPostModel()) | ||
testPost = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
testPost.setAuthor(userUtils.getTestUser()); | ||
postRepository.save(testPost); | ||
} | ||
|
||
@Test | ||
public void testIndex() throws Exception { | ||
postRepository.save(testPost); | ||
var result = mockMvc.perform(get("/api/posts").with(token)) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
|
@@ -72,10 +76,7 @@ public void testIndex() throws Exception { | |
|
||
@Test | ||
public void testCreate() throws Exception { | ||
var data = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
data.setAuthor(userUtils.getTestUser()); | ||
var dto = postMapper.map(data); | ||
var dto = postMapper.map(testPost); | ||
|
||
var request = post("/api/posts") | ||
.with(token) | ||
|
@@ -85,49 +86,52 @@ public void testCreate() throws Exception { | |
mockMvc.perform(request) | ||
.andExpect(status().isCreated()); | ||
|
||
var post = postRepository.findBySlug(data.getSlug()).get(); | ||
var post = postRepository.findBySlug(testPost.getSlug()).get(); | ||
assertNotNull(post); | ||
assertThat(post.getName()).isEqualTo(data.getName()); | ||
assertThat(post.getName()).isEqualTo(testPost.getName()); | ||
} | ||
|
||
@Test | ||
public void testUpdate() throws Exception { | ||
var post = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
post.setAuthor(userUtils.getTestUser()); | ||
postRepository.save(post); | ||
postRepository.save(testPost); | ||
|
||
var data = new PostUpdateDTO(); | ||
data.setName(JsonNullable.of("new name")); | ||
|
||
var request = put("/api/posts/" + post.getId()) | ||
var request = put("/api/posts/" + testPost.getId()) | ||
.with(token) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(om.writeValueAsString(data)); | ||
|
||
mockMvc.perform(request) | ||
.andExpect(status().isOk()); | ||
|
||
post = postRepository.findById(post.getId()).get(); | ||
assertThat(post.getName()).isEqualTo(data.getName().get()); | ||
testPost = postRepository.findById(testPost.getId()).get(); | ||
assertThat(testPost.getName()).isEqualTo(data.getName().get()); | ||
} | ||
|
||
@Test | ||
public void testShow() throws Exception { | ||
var post = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
post.setAuthor(userUtils.getTestUser()); | ||
postRepository.save(post); | ||
postRepository.save(testPost); | ||
|
||
var request = get("/api/posts/" + post.getId()).with(jwt()); | ||
var request = get("/api/posts/" + testPost.getId()).with(jwt()); | ||
var result = mockMvc.perform(request) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
var body = result.getResponse().getContentAsString(); | ||
assertThatJson(body).and( | ||
v -> v.node("slug").isEqualTo(post.getSlug()), | ||
v -> v.node("name").isEqualTo(post.getName()), | ||
v -> v.node("body").isEqualTo(post.getBody()) | ||
); | ||
v -> v.node("slug").isEqualTo(testPost.getSlug()), | ||
v -> v.node("name").isEqualTo(testPost.getName()), | ||
v -> v.node("body").isEqualTo(testPost.getBody())); | ||
} | ||
|
||
@Test | ||
public void testDestroy() throws Exception { | ||
postRepository.save(testPost); | ||
var request = delete("/api/posts/" + testPost.getId()).with(jwt()); | ||
mockMvc.perform(request) | ||
.andExpect(status().isNoContent()); | ||
|
||
assertThat(postRepository.existsById(testPost.getId())).isEqualTo(false); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
import java.util.HashMap; | ||
|
||
import org.instancio.Instancio; | ||
import org.instancio.Select; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -47,10 +46,12 @@ public class UsersControllerTest { | |
|
||
private JwtRequestPostProcessor token; | ||
|
||
private User testUser; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
token = jwt().jwt(builder -> builder.subject("[email protected]")); | ||
var testUser = Instancio.of(modelGenerator.getUserModel()) | ||
testUser = Instancio.of(modelGenerator.getUserModel()) | ||
.create(); | ||
userRepository.save(testUser); | ||
} | ||
|
@@ -63,24 +64,19 @@ public void testIndex() throws Exception { | |
|
||
@Test | ||
public void testUpdate() throws Exception { | ||
var user = Instancio.of(User.class) | ||
.ignore(Select.field(User::getId)) | ||
.supply(Select.field(User::getEmail), () -> faker.internet().emailAddress()) | ||
.create(); | ||
userRepository.save(user); | ||
|
||
var data = new HashMap<>(); | ||
data.put("firstName", "Mike"); | ||
|
||
var request = put("/api/users/" + user.getId()) | ||
var request = put("/api/users/" + testUser.getId()) | ||
.with(token) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(om.writeValueAsString(data)); | ||
|
||
mockMvc.perform(request) | ||
.andExpect(status().isOk()); | ||
|
||
user = userRepository.findById(user.getId()).get(); | ||
var user = userRepository.findById(testUser.getId()).get(); | ||
assertThat(user.getFirstName()).isEqualTo(("Mike")); | ||
} | ||
} |
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