Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Post/Tab 서비스 테스트 코드 변경 #103

Merged
merged 10 commits into from
Jun 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.mail.MessagingException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -87,24 +88,16 @@ public CmdPostAndTagsDTO registPost(CmdPostAndTagsDTO post) throws NotLoginExcep

public static String sanitizeHTML(String html) {

// 공백, 줄바꿈 대체
String spacePlaceholder = "___SPACE___";
String newlinePlaceholder = "___NEWLINE___";
String htmlWithPlaceholders = html
.replaceAll(" ", spacePlaceholder)
.replaceAll("\n", newlinePlaceholder)
.replaceAll("\r\n", newlinePlaceholder);

Whitelist whitelist = Whitelist.relaxed();
// 허용되지 않을 태그와 속성 추가
Whitelist whitelist = Whitelist.basic();
// 허용되지 않을 태그, 속성 추가
whitelist.removeTags("script", "style", "head", "header", "foot", "footer");
whitelist.removeAttributes("style", "onclick");

String cleanedHtmlWithPlaceholders = Jsoup.clean(htmlWithPlaceholders, whitelist);
// 개행 사라지지 않도록 처리
Document.OutputSettings settings = new Document.OutputSettings();
settings.prettyPrint(false);

String cleanedHtml = cleanedHtmlWithPlaceholders
.replaceAll(spacePlaceholder, " ")
.replaceAll(newlinePlaceholder, "\n");
String cleanedHtml = Jsoup.clean(html,"", whitelist, settings);

return cleanedHtml;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
import org.triumers.kmsback.common.exception.WrongInputTypeException;
import org.triumers.kmsback.post.command.Application.dto.*;
import org.triumers.kmsback.post.command.domain.aggregate.vo.CmdRequestPostAI;
import org.triumers.kmsback.tab.command.Application.controller.CmdTabController;
import org.triumers.kmsback.tab.command.Application.dto.CmdTabDTO;
import org.triumers.kmsback.tab.command.Application.dto.CmdTabRelationDTO;
import org.triumers.kmsback.tab.command.Application.service.CmdTabService;
import org.triumers.kmsback.tab.command.domain.aggregate.entity.CmdTabRelation;
import org.triumers.kmsback.user.command.Application.service.AuthService;
import org.triumers.kmsback.user.command.domain.aggregate.entity.Employee;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -28,12 +35,16 @@ public class CmdPostServiceTests {
private final CmdPostService cmdPostService;
private final OpenAIService openAIService;
private final LoggedInUser loggedInUser;
private final AuthService authService;
private final CmdTabService cmdTabService;

@Autowired
public CmdPostServiceTests(CmdPostService cmdPostService, OpenAIService openAIService, LoggedInUser loggedInUser) {
public CmdPostServiceTests(CmdPostService cmdPostService, OpenAIService openAIService, LoggedInUser loggedInUser, AuthService authService, CmdTabService cmdTabService) {
this.cmdPostService = cmdPostService;
this.openAIService = openAIService;
this.loggedInUser = loggedInUser;
this.authService = authService;
this.cmdTabService = cmdTabService;
}

@BeforeEach
Expand All @@ -60,10 +71,10 @@ void modifyPost() throws NotLoginException {

List<String> modifyTags = new ArrayList<>();
modifyTags.add("개발");
modifyTags.add("tag1");
modifyTags.add("tag2");
modifyTags.add("tag3");
modifyTags.add("tag4");
modifyTags.add("tag11");
modifyTags.add("tag12");
modifyTags.add("tag13");
modifyTags.add("tag14");

CmdPostAndTagsDTO modifyPost = createTestPost("modify");
modifyPost.setTags(modifyTags);
Expand All @@ -90,7 +101,7 @@ void likePost() throws NotLoginException {

CmdPostAndTagsDTO savedPost = cmdPostService.registPost(createTestPost());

CmdLikeDTO like = new CmdLikeDTO(1, savedPost.getId());
CmdLikeDTO like = new CmdLikeDTO(null, savedPost.getId());
CmdLikeDTO likePost = cmdPostService.likePost(like);

assertThat(likePost.getId()).isNotNull();
Expand All @@ -103,7 +114,7 @@ void favoritePost() throws NotLoginException {

CmdPostAndTagsDTO savedPost = cmdPostService.registPost(createTestPost());

CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, savedPost.getId());
CmdFavoritesDTO favorite = new CmdFavoritesDTO(null, savedPost.getId());
CmdFavoritesDTO likePost = cmdPostService.favoritePost(favorite);

assertThat(likePost.getId()).isNotNull();
Expand Down Expand Up @@ -131,19 +142,32 @@ void testGPT(){
cmdPostService.requestToGPT(request);
}

private CmdPostAndTagsDTO createTestPost(){
private CmdPostAndTagsDTO createTestPost() throws NotLoginException {
return createTestPost("");
}

private CmdPostAndTagsDTO createTestPost(String type){
private CmdPostAndTagsDTO createTestPost(String type) throws NotLoginException {
List<String> tags = new ArrayList<>();
tags.add("개발");
tags.add("tag1");
tags.add("tag2");
tags.add("tag3");
tags.add("tag4");

return new CmdPostAndTagsDTO(type + "Title", type + "Content", null, LocalDateTime.now(), 1, 1, tags);
CmdTabRelationDTO tab = createTestTab();

return new CmdPostAndTagsDTO(type + "Title", type + "Content", null, LocalDateTime.now(), null, tab.getId(), tags);
}

private CmdTabRelationDTO createTestTab() throws NotLoginException {

CmdTabDTO top = new CmdTabDTO("testTop");
CmdTabDTO bottom = new CmdTabDTO("testBottom");
CmdTabRelationDTO tabRelation = new CmdTabRelationDTO(false, bottom, top);

Employee employee = authService.whoAmI();

return cmdTabService.registTab(tabRelation, employee.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import org.triumers.kmsback.post.command.Application.service.CmdPostService;
import org.triumers.kmsback.post.query.aggregate.vo.QryRequestPost;
import org.triumers.kmsback.post.query.dto.QryPostAndTagsDTO;
import org.triumers.kmsback.tab.command.Application.dto.CmdTabDTO;
import org.triumers.kmsback.tab.command.Application.dto.CmdTabRelationDTO;
import org.triumers.kmsback.tab.command.Application.service.CmdTabService;
import org.triumers.kmsback.user.command.Application.service.AuthService;
import org.triumers.kmsback.user.command.domain.aggregate.entity.Employee;
import org.triumers.kmsback.user.query.dto.QryEmployeeDTO;

import java.time.LocalDateTime;
Expand All @@ -33,13 +38,17 @@ class QryPostServiceTest {

private final QryPostService qryPostService;
private final CmdPostService cmdPostService;
private final AuthService authService;
private final CmdTabService cmdTabService;

private final LoggedInUser loggedInUser;

@Autowired
QryPostServiceTest(QryPostService qryPostService, CmdPostService cmdPostService, LoggedInUser loggedInUser) {
QryPostServiceTest(QryPostService qryPostService, CmdPostService cmdPostService, AuthService authService, CmdTabService cmdTabService, LoggedInUser loggedInUser) {
this.qryPostService = qryPostService;
this.cmdPostService = cmdPostService;
this.authService = authService;
this.cmdTabService = cmdTabService;
this.loggedInUser = loggedInUser;
}

Expand All @@ -51,11 +60,11 @@ void loginSetting() throws WrongInputTypeException {
@DisplayName("tab 게시글 리스트 조회")
void findPostListByTab() throws NotLoginException, WrongInputValueException {

registPost();
CmdPostAndTagsDTO savedPost = registPost();

PageRequest pageRequest = PageRequest.of(0, 10);

QryRequestPost request = new QryRequestPost(1, null);
QryRequestPost request = new QryRequestPost(savedPost.getTabRelationId(), null);

Page<QryPostAndTagsDTO> postList = qryPostService.findPostListByTab(request, pageRequest);

Expand All @@ -66,11 +75,11 @@ void findPostListByTab() throws NotLoginException, WrongInputValueException {
@DisplayName("회원이 속한 전체 게시글 리스트 조회")
void findAllPostListByTab() throws NotLoginException, WrongInputValueException {

registPost();
CmdPostAndTagsDTO savedPost = registPost();

PageRequest pageRequest = PageRequest.of(0, 10);

QryRequestPost request = new QryRequestPost(1, null);
QryRequestPost request = new QryRequestPost(null, null);

Page<QryPostAndTagsDTO> postList = qryPostService.findAllPostListByEmployee(request, pageRequest);

Expand All @@ -91,7 +100,10 @@ void findPostById() throws NotLoginException, WrongInputValueException {
@DisplayName("사용자가 참여한 게시글 조회")
void findPostByEmployeeId() throws NotLoginException {

List<QryPostAndTagsDTO> selectedPost = qryPostService.findPostByEmployeeId(1);
registPost();
Employee employee = authService.whoAmI();

List<QryPostAndTagsDTO> selectedPost = qryPostService.findPostByEmployeeId(employee.getId());

assertThat(selectedPost).isNotNull();
}
Expand All @@ -100,10 +112,12 @@ void findPostByEmployeeId() throws NotLoginException {
@DisplayName("사용자가 좋아요한 게시글 조회")
void findLikePostByEmployeeId() throws NotLoginException {

CmdLikeDTO like = new CmdLikeDTO(1, 1);
CmdPostAndTagsDTO post = registPost();
CmdLikeDTO like = new CmdLikeDTO(null, post.getId());
cmdPostService.likePost(like);

List<QryPostAndTagsDTO> likedPost = qryPostService.findLikePostByEmployeeId(1);
Employee employee = authService.whoAmI();
List<QryPostAndTagsDTO> likedPost = qryPostService.findLikePostByEmployeeId(employee.getId());

assertThat(likedPost).isNotNull();
}
Expand All @@ -112,10 +126,11 @@ void findLikePostByEmployeeId() throws NotLoginException {
@DisplayName("게시글id로 사용자 좋아요 여부 조회")
void isLikeByPostId() throws NotLoginException {

CmdLikeDTO like = new CmdLikeDTO(1, 1);
CmdPostAndTagsDTO post = registPost();
CmdLikeDTO like = new CmdLikeDTO(null, post.getId());
cmdPostService.likePost(like);

Boolean isLiked = qryPostService.findIsLikedByPostId(1);
Boolean isLiked = qryPostService.findIsLikedByPostId(post.getId());

assertThat(isLiked).isTrue();
}
Expand All @@ -124,10 +139,11 @@ void isLikeByPostId() throws NotLoginException {
@DisplayName("게시글id로 사용자 즐겨찾기 여부 조회")
void isFavoriteByPostId() throws NotLoginException {

CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, 1);
CmdPostAndTagsDTO post = registPost();
CmdFavoritesDTO favorite = new CmdFavoritesDTO(null, post.getId());
cmdPostService.favoritePost(favorite);

Boolean isFavorite = qryPostService.findIsFavoriteByPostId(1);
Boolean isFavorite = qryPostService.findIsFavoriteByPostId(post.getId());

assertThat(isFavorite).isTrue();
}
Expand All @@ -136,9 +152,12 @@ void isFavoriteByPostId() throws NotLoginException {
@DisplayName("사용자가 즐겨찾기한 게시글 조회")
void findFavoritePostByEmployeeId() throws NotLoginException {

CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, 1);
CmdPostAndTagsDTO post = registPost();
CmdFavoritesDTO favorite = new CmdFavoritesDTO(null, post.getId());
cmdPostService.favoritePost(favorite);
List<QryPostAndTagsDTO> favoritePost = qryPostService.findFavoritePostByEmployeeId(1);

Employee employee = authService.whoAmI();
List<QryPostAndTagsDTO> favoritePost = qryPostService.findFavoritePostByEmployeeId(employee.getId());

assertThat(favoritePost).isNotNull();
}
Expand All @@ -158,7 +177,7 @@ void findHistoryListByOriginId() throws NotLoginException, WrongInputValueExcept
void findLikeListByPostId() throws NotLoginException, WrongInputValueException {

CmdPostAndTagsDTO post = registPost();
CmdFavoritesDTO favorite = new CmdFavoritesDTO(1, post.getId());
CmdFavoritesDTO favorite = new CmdFavoritesDTO(null, post.getId());
cmdPostService.favoritePost(favorite);

List<QryEmployeeDTO> likeList = qryPostService.findLikeListByPostId(post.getId());
Expand Down Expand Up @@ -186,14 +205,27 @@ private Integer modifyPost() throws NotLoginException {
return modifyPost.getOriginId();
}

private CmdPostAndTagsDTO createTestPost(){
private CmdPostAndTagsDTO createTestPost() throws NotLoginException {
List<String> tags = new ArrayList<>();
tags.add("개발");
tags.add("tag1");
tags.add("tag2");
tags.add("tag3");
tags.add("tag4");

return new CmdPostAndTagsDTO("newTitle", "newContent", "imgurl", LocalDateTime.now(), 1, 1, tags);
CmdTabRelationDTO tab = createTestTab();

return new CmdPostAndTagsDTO("newTitle", "newContent", null, LocalDateTime.now(), null, tab.getId(), tags);
}

private CmdTabRelationDTO createTestTab() throws NotLoginException {

CmdTabDTO top = new CmdTabDTO("testTop");
CmdTabDTO bottom = new CmdTabDTO("testBottom");
CmdTabRelationDTO tabRelation = new CmdTabRelationDTO(false, bottom, top);

Employee employee = authService.whoAmI();

return cmdTabService.registTab(tabRelation, employee.getId());
}
}
Loading
Loading