diff --git a/src/main/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceImpl.java b/src/main/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceImpl.java index 70cec4bf..c8e9256e 100644 --- a/src/main/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceImpl.java +++ b/src/main/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceImpl.java @@ -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; @@ -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; } diff --git a/src/test/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceTests.java b/src/test/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceTests.java index 3551026c..63bd9e8e 100644 --- a/src/test/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceTests.java +++ b/src/test/java/org/triumers/kmsback/post/command/Application/service/CmdPostServiceTests.java @@ -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; @@ -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 @@ -60,10 +71,10 @@ void modifyPost() throws NotLoginException { List 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); @@ -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(); @@ -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(); @@ -131,11 +142,11 @@ 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 tags = new ArrayList<>(); tags.add("개발"); tags.add("tag1"); @@ -143,7 +154,20 @@ private CmdPostAndTagsDTO createTestPost(String type){ 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()); } } diff --git a/src/test/java/org/triumers/kmsback/post/query/service/QryPostServiceTest.java b/src/test/java/org/triumers/kmsback/post/query/service/QryPostServiceTest.java index 38ac75d7..2de80e15 100644 --- a/src/test/java/org/triumers/kmsback/post/query/service/QryPostServiceTest.java +++ b/src/test/java/org/triumers/kmsback/post/query/service/QryPostServiceTest.java @@ -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; @@ -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; } @@ -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 postList = qryPostService.findPostListByTab(request, pageRequest); @@ -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 postList = qryPostService.findAllPostListByEmployee(request, pageRequest); @@ -91,7 +100,10 @@ void findPostById() throws NotLoginException, WrongInputValueException { @DisplayName("사용자가 참여한 게시글 조회") void findPostByEmployeeId() throws NotLoginException { - List selectedPost = qryPostService.findPostByEmployeeId(1); + registPost(); + Employee employee = authService.whoAmI(); + + List selectedPost = qryPostService.findPostByEmployeeId(employee.getId()); assertThat(selectedPost).isNotNull(); } @@ -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 likedPost = qryPostService.findLikePostByEmployeeId(1); + Employee employee = authService.whoAmI(); + List likedPost = qryPostService.findLikePostByEmployeeId(employee.getId()); assertThat(likedPost).isNotNull(); } @@ -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(); } @@ -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(); } @@ -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 favoritePost = qryPostService.findFavoritePostByEmployeeId(1); + + Employee employee = authService.whoAmI(); + List favoritePost = qryPostService.findFavoritePostByEmployeeId(employee.getId()); assertThat(favoritePost).isNotNull(); } @@ -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 likeList = qryPostService.findLikeListByPostId(post.getId()); @@ -186,7 +205,7 @@ private Integer modifyPost() throws NotLoginException { return modifyPost.getOriginId(); } - private CmdPostAndTagsDTO createTestPost(){ + private CmdPostAndTagsDTO createTestPost() throws NotLoginException { List tags = new ArrayList<>(); tags.add("개발"); tags.add("tag1"); @@ -194,6 +213,19 @@ private CmdPostAndTagsDTO createTestPost(){ 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()); } } \ No newline at end of file diff --git a/src/test/java/org/triumers/kmsback/tab/command/Application/service/CmdTabServiceTests.java b/src/test/java/org/triumers/kmsback/tab/command/Application/service/CmdTabServiceTests.java index b47fd06f..77b22588 100644 --- a/src/test/java/org/triumers/kmsback/tab/command/Application/service/CmdTabServiceTests.java +++ b/src/test/java/org/triumers/kmsback/tab/command/Application/service/CmdTabServiceTests.java @@ -6,10 +6,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; +import org.triumers.kmsback.common.exception.NotLoginException; import org.triumers.kmsback.tab.command.Application.dto.CmdJoinEmployeeDTO; import org.triumers.kmsback.tab.command.Application.dto.CmdTabDTO; import org.triumers.kmsback.tab.command.Application.dto.CmdTabRelationDTO; import org.triumers.kmsback.tab.command.domain.repository.CmdJoinEmployeeRepository; +import org.triumers.kmsback.user.command.Application.service.AuthService; +import org.triumers.kmsback.user.command.domain.aggregate.entity.Employee; import static org.assertj.core.api.Assertions.assertThat; @@ -19,50 +22,61 @@ class CmdTabServiceTests { private final CmdTabService cmdTabService; private final CmdJoinEmployeeRepository cmdJoinEmployeeRepository; - - private final int EMPLOYEE_ID = 1; - private final int TAB_RELATION_ID = 999; + private final AuthService authService; @Autowired - CmdTabServiceTests(CmdTabService cmdTabService, CmdJoinEmployeeRepository cmdJoinEmployeeRepository) { + CmdTabServiceTests(CmdTabService cmdTabService, CmdJoinEmployeeRepository cmdJoinEmployeeRepository, AuthService authService) { this.cmdTabService = cmdTabService; this.cmdJoinEmployeeRepository = cmdJoinEmployeeRepository; + this.authService = authService; } -// @Test -// @DisplayName("탭 참여자 등록") -// void addEmployeeTab(){ -// -// CmdJoinEmployeeDTO employee = new CmdJoinEmployeeDTO(false, EMPLOYEE_ID, TAB_RELATION_ID); -// -// CmdJoinEmployeeDTO savedEmployee = cmdTabService.addEmployeeTab(employee); -// -// assertThat(savedEmployee.getId()).isNotNull(); -// } -// -// @Test -// @DisplayName("탭 참여자 삭제") -// void deleteEmployeeTab(){ -// -// CmdJoinEmployeeDTO employee = new CmdJoinEmployeeDTO(false, EMPLOYEE_ID, TAB_RELATION_ID); -// cmdTabService.addEmployeeTab(employee); -// -// CmdJoinEmployeeDTO deletedEmployee = cmdTabService.deleteEmployeeTab(employee); -// -// assertThat(cmdJoinEmployeeRepository.findById(deletedEmployee.getId())).isEmpty(); -// } + @Test + @DisplayName("탭 참여자 등록") + void addEmployeeTab() throws NotLoginException { + + Employee employee = authService.whoAmI(); + CmdTabRelationDTO tab = createTestTab(); + + CmdJoinEmployeeDTO joinEmployee = new CmdJoinEmployeeDTO(false, employee.getId(), tab.getId()); + + CmdJoinEmployeeDTO savedEmployee = cmdTabService.addEmployeeTab(joinEmployee); + + assertThat(savedEmployee.getId()).isNotNull(); + } @Test - @DisplayName("탭 추가") - void registTab(){ - boolean isPublic = false; - CmdTabDTO top = new CmdTabDTO("top"); - CmdTabDTO bottom = new CmdTabDTO("bottom"); + @DisplayName("탭 참여자 삭제") + void deleteEmployeeTab() throws NotLoginException { + + Employee employee = authService.whoAmI(); + CmdTabRelationDTO tab = createTestTab(); + + CmdJoinEmployeeDTO joinEmployee = new CmdJoinEmployeeDTO(false, employee.getId(), tab.getId()); + cmdTabService.addEmployeeTab(joinEmployee); - CmdTabRelationDTO tabRelation = new CmdTabRelationDTO(isPublic, bottom, top); + CmdJoinEmployeeDTO deletedEmployee = cmdTabService.deleteEmployeeTab(joinEmployee); - CmdTabRelationDTO registTab = cmdTabService.registTab(tabRelation, EMPLOYEE_ID); + assertThat(cmdJoinEmployeeRepository.findById(deletedEmployee.getId())).isEmpty(); + } + + @Test + @DisplayName("탭 추가") + void registTab() throws NotLoginException { + + CmdTabRelationDTO registTab = createTestTab(); assertThat(registTab.getId()).isNotNull(); } + + 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()); + } } \ No newline at end of file