Add PostService Test 추가

This commit is contained in:
Daeil Choi
2023-02-06 11:13:13 +09:00
parent ad10828483
commit 20ccbd7e13
10 changed files with 133 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package com.example.springsecuritystudy.config;
import javax.annotation.PostConstruct;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.example.springsecuritystudy.notice.NoticeService;
import com.example.springsecuritystudy.post.PostService;
@@ -13,6 +14,7 @@ import lombok.RequiredArgsConstructor;
@Configuration
@RequiredArgsConstructor
@Profile(value = "!test")
public class InitializeConfig {
private final UserService userService;

View File

@@ -8,6 +8,7 @@ import javax.persistence.Lob;
import com.example.springsecuritystudy.model.BaseTimeEntity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -23,6 +24,7 @@ public class Notice extends BaseTimeEntity {
@Lob
private String content;
@Builder
public Notice(String title, String content) {
this.title = title;
this.content = content;

View File

@@ -13,6 +13,7 @@ import com.example.springsecuritystudy.model.BaseTimeEntity;
import com.example.springsecuritystudy.user.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -33,6 +34,7 @@ public class Post extends BaseTimeEntity {
@JoinColumn(name = "USER_ID")
private User user;
@Builder
public Post(String title, String content, User user) {
this.title = title;
this.content = content;

View File

@@ -12,6 +12,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@@ -28,6 +29,7 @@ public class User implements UserDetails {
private String password;
private String authority;
@Builder
public User(String username, String password, String authority) {
this.username = username;
this.password = password;

View File

@@ -17,12 +17,22 @@ public class UserService {
public User signup(String username, String password) {
alreadyRegisteredUser(username);
return userRepository.save(new User(username, passwordEncoder.encode(password), "ROLE_USER"));
User user = User.builder()
.username(username)
.password(passwordEncoder.encode(password))
.authority("ROLE_USER")
.build();
return userRepository.save(user);
}
public User signupAdmin(String username, String password) {
alreadyRegisteredUser(username);
return userRepository.save(new User(username, passwordEncoder.encode(password), "ROLE_ADMIN"));
User user = User.builder()
.username(username)
.password(passwordEncoder.encode(password))
.authority("ROLE_ADMIN")
.build();
return userRepository.save(user);
}
public User findByUsername(String username) {

View File

@@ -9,6 +9,8 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.example.springsecuritystudy.helper.WithMockAdmin;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

View File

@@ -0,0 +1,11 @@
package com.example.springsecuritystudy.helper;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest
@ActiveProfiles(value = "test")
@Transactional
public class TestConfig {
}

View File

@@ -1,4 +1,4 @@
package com.example.springsecuritystudy;
package com.example.springsecuritystudy.helper;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@@ -3,14 +3,13 @@ package com.example.springsecuritystudy.notice;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.example.springsecuritystudy.WithMockAdmin;
import com.example.springsecuritystudy.helper.TestConfig;
import com.example.springsecuritystudy.helper.WithMockAdmin;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
@@ -19,9 +18,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@Transactional
class NoticeControllerTest {
class NoticeControllerTest extends TestConfig {
@Autowired
private NoticeRepository noticeRepository;

View File

@@ -0,0 +1,96 @@
package com.example.springsecuritystudy.post;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springsecuritystudy.helper.TestConfig;
import com.example.springsecuritystudy.user.User;
import com.example.springsecuritystudy.user.UserService;
import static org.assertj.core.api.BDDAssertions.*;
class PostServiceTest extends TestConfig {
@Autowired
private PostService postService;
@Autowired
private UserService userService;
@Test
void findByUser_유저가_게시물조회() {
//given
User user = userService.signup("user", "user");
postService.savePost(user, "title1", "content1");
postService.savePost(user, "title2", "content2");
//when
List<Post> posts = postService.findByUser(user);
//then
then(posts.size()).isEqualTo(2);
Post post1 = posts.get(0);
Post post2 = posts.get(1);
// post1 = title2
then(post1.getUser().getUsername()).isEqualTo("user");
then(post1.getTitle()).isEqualTo("title2");
then(post1.getContent()).isEqualTo("content2");
// post2 = title1
then(post2.getUser().getUsername()).isEqualTo("user");
then(post2.getTitle()).isEqualTo("title1");
then(post2.getContent()).isEqualTo("content1");
}
@Test
void findByUser_어드민이_조회() {
// given
User admin = userService.signupAdmin("admin", "admin");
User user1 = userService.signup("user1", "user1");
User user2 = userService.signup("user2", "user2");
postService.savePost(user1, "title1", "content1");
postService.savePost(user1, "title2", "content2");
postService.savePost(user2, "title3", "content3");
// when
List<Post> posts = postService.findByUser(admin);
// then
then(posts.size()).isEqualTo(3);
Post post1 = posts.get(0);
Post post2 = posts.get(1);
Post post3 = posts.get(2);
// post1 = title3
then(post1.getUser().getUsername()).isEqualTo("user2");
then(post1.getTitle()).isEqualTo("title3");
then(post1.getContent()).isEqualTo("content3");
// post1 = title2
then(post2.getUser().getUsername()).isEqualTo("user1");
then(post2.getTitle()).isEqualTo("title2");
then(post2.getContent()).isEqualTo("content2");
// post1 = title1
then(post3.getUser().getUsername()).isEqualTo("user1");
then(post3.getTitle()).isEqualTo("title1");
then(post3.getContent()).isEqualTo("content1");
}
@Test
void savePost() {
// given
User user = userService.signup("user", "user");
// when
postService.savePost(user, "title", "content");
// then
then(postService.findByUser(user).size()).isEqualTo(1);
}
@Test
void deletePost() {
// given
User user = userService.signup("user", "user");
Post post = postService.savePost(user, "title", "content");
// when
postService.deletePost(user, post.getId());
// then
then(postService.findByUser(user).size()).isZero();
}
}