-
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 branch 'dev' of https://github.com/TG-WinG/tech-blog into dev
- Loading branch information
Showing
7 changed files
with
218 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ public class PostQuery { | |
|
||
private String keyword = ""; | ||
private Set<String> hashtag; | ||
private boolean me = 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
6 changes: 6 additions & 0 deletions
6
src/main/java/kr/tgwing/tech/blog/exception/post/UserNotLoggedInException.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,6 @@ | ||
package kr.tgwing.tech.blog.exception.post; | ||
|
||
import kr.tgwing.tech.common.exception.CommonException; | ||
|
||
public class UserNotLoggedInException extends CommonException { | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/test/java/kr/tgwing/tech/annotation/IntegrationTest.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,20 @@ | ||
package kr.tgwing.tech.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
/** | ||
* CustomTest | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
@AutoConfigureMockMvc | ||
public @interface IntegrationTest {} |
171 changes: 171 additions & 0 deletions
171
src/test/java/kr/tgwing/tech/blog/BlogIntegrationTest.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,171 @@ | ||
package kr.tgwing.tech.blog; | ||
|
||
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.result.MockMvcResultMatchers.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import kr.tgwing.tech.annotation.IntegrationTest; | ||
import kr.tgwing.tech.blog.entity.Comment; | ||
import kr.tgwing.tech.blog.entity.Hashtag; | ||
import kr.tgwing.tech.blog.entity.Post; | ||
import kr.tgwing.tech.blog.entity.Reply; | ||
import kr.tgwing.tech.blog.repository.CommentRepository; | ||
import kr.tgwing.tech.blog.repository.PostRepository; | ||
import kr.tgwing.tech.blog.repository.ReplyRepository; | ||
import kr.tgwing.tech.user.entity.User; | ||
import kr.tgwing.tech.user.repository.UserRepository; | ||
|
||
/** | ||
* PostIntegrationTest | ||
*/ | ||
@IntegrationTest | ||
public class BlogIntegrationTest { | ||
|
||
@Autowired MockMvc mvc; | ||
|
||
@BeforeAll | ||
static void prepare_test_data( | ||
@Autowired BCryptPasswordEncoder bCryptPasswordEncoder, | ||
@Autowired UserRepository userRepository, | ||
@Autowired PostRepository postRepository, | ||
@Autowired CommentRepository commentRepository, | ||
@Autowired ReplyRepository replyRepository | ||
) { | ||
User writer1 = User.builder() | ||
.studentNumber("2018000000") | ||
.phoneNumber("01000000000") | ||
.email("[email protected]") | ||
.name("늙은이") | ||
.password("12345678") | ||
.birth(LocalDate.parse("1999-01-01")) | ||
.build(); | ||
User writer2 = User.builder() | ||
.studentNumber("2022000000") | ||
.phoneNumber("01011111111") | ||
.email("[email protected]") | ||
.name("젊은이") | ||
.password("12345678") | ||
.birth(LocalDate.parse("2003-01-01")) | ||
.build(); | ||
writer1.hashPassword(bCryptPasswordEncoder); | ||
writer2.hashPassword(bCryptPasswordEncoder); | ||
|
||
userRepository.save(writer1); | ||
userRepository.save(writer2); | ||
|
||
Post post1 = Post.builder() | ||
.title("sample blog 1") | ||
.content("sample content 1") | ||
.thumbnail("sample thumbnail 1") | ||
.writer(writer1) | ||
.build(); | ||
Post post2 = Post.builder() | ||
.title("sample blog 2") | ||
.content("sample content 2") | ||
.thumbnail("sample thumbnail 2") | ||
.writer(writer2) | ||
.build(); | ||
Hashtag tag1 = Hashtag.builder() | ||
.name("tag1") | ||
.post(post1) | ||
.build(); | ||
Hashtag tag2 = Hashtag.builder() | ||
.name("tag2") | ||
.post(post2) | ||
.build(); | ||
Hashtag tag3 = Hashtag.builder() | ||
.name("tag2") | ||
.post(post1) | ||
.build(); | ||
Comment comment1 = Comment.builder() | ||
.content("sample comment 1") | ||
.post(post1) | ||
.writer(writer2) | ||
.build(); | ||
Reply reply1 = Reply.builder() | ||
.content("sample reply 1") | ||
.post(post1) | ||
.comment(comment1) | ||
.writer(writer1) | ||
.build(); | ||
|
||
post1.getHashtags().add(tag1); | ||
post1.getHashtags().add(tag3); | ||
post2.getHashtags().add(tag2); | ||
|
||
post1.getComments().add(comment1); | ||
post1.increaseCommentCount(); | ||
comment1.getReplies().add(reply1); | ||
post1.increaseCommentCount(); | ||
|
||
postRepository.save(post1); | ||
postRepository.save(post2); | ||
commentRepository.save(comment1); | ||
replyRepository.save(reply1); | ||
} | ||
|
||
@Test | ||
void get_first_page_of_posts() throws Exception { | ||
mvc.perform(get("/post")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.totalElements").value(2)); | ||
} | ||
|
||
@Test | ||
void get_a_post_by_id() throws Exception { | ||
mvc.perform(get("/post/{postId}", 1)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.id").value(1)) | ||
.andExpect(jsonPath("$.title").value("sample blog 1")) | ||
.andExpect(jsonPath("$.content").value("sample content 1")) | ||
.andExpect(jsonPath("$.thumbnail").value("sample thumbnail 1")) | ||
.andExpect(jsonPath("$.likeCount").value(0)) | ||
.andExpect(jsonPath("$.commentCount").value(2)); | ||
} | ||
|
||
@Test | ||
void get_posts_title_contain_keyword() throws Exception { | ||
mvc.perform(get("/post?keyword=1")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.totalElements").value(1)); | ||
} | ||
|
||
@Test | ||
void get_posts_has_hashtag() throws Exception { | ||
mvc.perform(get("/post?hashtag=tag1,tag2")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.totalElements").value(2)); | ||
} | ||
|
||
@Test | ||
void get_my_posts() throws Exception { | ||
mvc.perform(post("/login") | ||
.param("username", "2018000000") | ||
.param("password", "12345678")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().exists("Authorization")) | ||
.andDo((result) -> { | ||
String token = result.getResponse().getHeader("Authorization"); | ||
mvc.perform(get("/post?me=true").header("Authorization", token)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.totalElements").value(1)); | ||
}); | ||
} | ||
|
||
@Test | ||
void throw_when_get_my_posts_but_not_logged_in() throws Exception { | ||
mvc.perform(get("/post?me=true")) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
} |