From 19e6278b1ba96f210c48f4d1ba119829e1d1f3bb Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Mon, 26 Aug 2024 20:25:00 +0900 Subject: [PATCH 01/19] =?UTF-8?q?[#54]=20report=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 7 +- .../up/report/service/ReportService.java | 3 - .../up/board/service/BoardServiceTest.java | 87 ++++-------------- .../up/report/service/ReportService.java | 33 ------- .../up/report/service/ReportServiceTest.java | 88 +++++++++++++++++++ 5 files changed, 107 insertions(+), 111 deletions(-) delete mode 100644 src/test/java/com/speech/up/report/service/ReportService.java create mode 100644 src/test/java/com/speech/up/report/service/ReportServiceTest.java diff --git a/build.gradle b/build.gradle index 9dd6a08..57235c7 100644 --- a/build.gradle +++ b/build.gradle @@ -68,7 +68,8 @@ javadoc { bootJar{ duplicatesStrategy = DuplicatesStrategy.EXCLUDE } -tasks.withType(Test) { - // Skip tests - enabled = false + +test { + useJUnitPlatform() // JUnit 5를 사용하는 경우 + include '**/*Test.class' } \ No newline at end of file diff --git a/src/main/java/com/speech/up/report/service/ReportService.java b/src/main/java/com/speech/up/report/service/ReportService.java index d94ad2c..032a061 100644 --- a/src/main/java/com/speech/up/report/service/ReportService.java +++ b/src/main/java/com/speech/up/report/service/ReportService.java @@ -1,7 +1,5 @@ package com.speech.up.report.service; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.speech.up.record.repository.RecordRepository; @@ -14,7 +12,6 @@ @Service @RequiredArgsConstructor public class ReportService { - private static final Logger log = LoggerFactory.getLogger(ReportService.class); private final ReportRepository reportRepository; private final RecordRepository recordRepository; diff --git a/src/test/java/com/speech/up/board/service/BoardServiceTest.java b/src/test/java/com/speech/up/board/service/BoardServiceTest.java index 437142d..e862d2e 100644 --- a/src/test/java/com/speech/up/board/service/BoardServiceTest.java +++ b/src/test/java/com/speech/up/board/service/BoardServiceTest.java @@ -4,12 +4,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,15 +11,9 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import com.speech.up.auth.provider.JwtProvider; import com.speech.up.board.entity.BoardEntity; @@ -34,17 +22,12 @@ import com.speech.up.board.service.dto.BoardGetDto; import com.speech.up.board.service.dto.BoardIsUseDto; import com.speech.up.board.service.dto.BoardUpdateDto; -import com.speech.up.report.MockBoardEntity; -import com.speech.up.script.mock.MockUser; import com.speech.up.user.entity.UserEntity; import com.speech.up.user.repository.UserRepository; -import com.speech.up.user.service.UserService; -import jakarta.servlet.http.HttpServlet; -class BoardServiceTest { +public class BoardServiceTest { - private static final Logger log = LoggerFactory.getLogger(BoardServiceTest.class); @Mock UserRepository userRepository; @@ -65,48 +48,21 @@ class BoardServiceTest { @BeforeEach public void setUp() { MockitoAnnotations.openMocks(this); - - } - /*@DisplayName("BoardList 다건 조회") + @DisplayName("BoardList 다건 조회") @Test void getAllBoardList() { - // given - int page = 5; - int size = 10; - BoardGetDto.Response boardGetDto = Mockito.mock(BoardGetDto.Response.class); - - BoardEntity boardEntity = Mockito.mock(BoardEntity.class); - BoardEntity fistBoardEntity = Mockito.mock(BoardEntity.class); - given(fistBoardEntity.getBoardId()).willReturn(1L); - BoardEntity LastBoardEntity = Mockito.mock(BoardEntity.class); - given(LastBoardEntity.getBoardId()).willReturn(10L); - - - List boardEntities = List.of(fistBoardEntity,boardEntity,boardEntity,boardEntity,boardEntity,boardEntity,boardEntity,boardEntity,boardEntity,LastBoardEntity); - - - List boardGetDtoList; - - Pageable pageable = PageRequest.of(page-1, size); - - Page boardList = new PageImpl<>(boardEntities.subList(page*size,Math.min((page + 1) * size, boardEntities.size())),pageable,boardEntities.size()); - - - given(boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(any(Pageable.class))).willReturn(boardList); - - // when - - boardGetDtoList = boardService.getAllBoardList(page, size); - - // then + //given + BoardGetDto.Response response = mock(BoardGetDto.Response.class); + Pageable pageable = mock(Pageable.class); - verify(boardRepository, times(1)).findAllByIsUseTrueOrderByCreatedAtDesc(any(Pageable.class)); - // assert(boardGetDtoList.get(9).getBoardId().equals(LastBoardEntity.getBoardId())); - assertThat(boardGetDtoList.size()).isEqualTo(size); + //when + Page boardList = boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable); - }*/ + //then + when(boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable)).thenReturn(boardList); + } @DisplayName("BoardList 단건 조회") @Test @@ -121,23 +77,17 @@ void getBoardById() { MockHttpServletRequest httpRequest = new MockHttpServletRequest(); httpRequest.addHeader("Authorization", token); - BoardGetDto.Response response; - given(jwtProvider.validate(socialId)).willReturn(socialId); given(userRepository.findBySocialId(socialId)).willReturn(userEntity); given(boardRepository.findByBoardIdAndIsUseTrue(1L)).willReturn(boardEntity); // when - - response = boardService.getBoardById(1L, httpRequest); + boardService.getBoardById(1L, httpRequest); // then - verify(jwtProvider,times(1)).validate(socialId); verify(userRepository,times(1)).findBySocialId(socialId); verify(boardRepository, times(1)).findByBoardIdAndIsUseTrue(1L); - - } @DisplayName("게시판 생성 테스트") @@ -161,14 +111,11 @@ void addBoard() { given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); // when - response = boardService.addBoard(request, httpRequest); // then - assert (response.getContent()).equals(request.getContent()); assert (response.getTitle()).equals(request.getTitle()); - } @DisplayName("게시판 업데이트 테스트") @@ -189,22 +136,20 @@ void updateBoard() { given(userRepository.findByUserId(request.getUser().getUserId())).willReturn(userEntity); given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); - // when + // when response = boardService.updateBoard(request); - // then + // then assert (response.getContent()).equals(request.getContent()); assert (response.getTitle()).equals(request.getTitle()); verify(boardRepository, times(1)).save(any(BoardEntity.class)); - } @DisplayName("보드 삭제 테스트") @Test void deleteBoard() { // given - UserEntity userEntity = Mockito.mock(UserEntity.class); BoardIsUseDto.Request request = Mockito.mock(BoardIsUseDto.Request.class); @@ -218,12 +163,10 @@ void deleteBoard() { given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); // when - response = boardService.deleteBoard(request); - // then - - assertFalse(response.isUse(), " isUse의 값이 변경되지 않았습니다."); + // then + assertFalse(response.isUse(), " isUse 의 값이 변경되지 않았습니다."); } @DisplayName("게시판 총 갯수 조회") diff --git a/src/test/java/com/speech/up/report/service/ReportService.java b/src/test/java/com/speech/up/report/service/ReportService.java deleted file mode 100644 index f049ff9..0000000 --- a/src/test/java/com/speech/up/report/service/ReportService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.speech.up.report.service; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import com.speech.up.record.repository.RecordRepository; -import com.speech.up.report.repository.ReportRepository; - -@ExtendWith(MockitoExtension.class) -public class ReportService { - @Mock - private ReportRepository reportRepository; - - @Mock - private RecordRepository recordRepository; - - @InjectMocks - private ReportService reportService; - - //ReportEntity 의 객체가 잘 담기고 save 메소드가 호출되었는지 확인 - @Test - public void saveReport(){ - //given - //when - //then - } - - - -} diff --git a/src/test/java/com/speech/up/report/service/ReportServiceTest.java b/src/test/java/com/speech/up/report/service/ReportServiceTest.java new file mode 100644 index 0000000..116da70 --- /dev/null +++ b/src/test/java/com/speech/up/report/service/ReportServiceTest.java @@ -0,0 +1,88 @@ +package com.speech.up.report.service; + +import static org.awaitility.Awaitility.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.speech.up.record.entity.RecordEntity; +import com.speech.up.record.repository.RecordRepository; +import com.speech.up.report.entity.ReportEntity; +import com.speech.up.report.repository.ReportRepository; +import com.speech.up.script.entity.ScriptEntity; + +@ExtendWith(MockitoExtension.class) +public class ReportServiceTest { + @Mock + private ReportRepository reportRepository; + + @Mock + private RecordRepository recordRepository; + + @InjectMocks + private ReportService reportService; + + @DisplayName("리포트 저장하기") + @Test + public void saveReportTest(){ + + // given + RecordEntity recordEntity = mock(RecordEntity.class); // RecordEntity 모의 + String recognized = "Test Recognition"; + double score = 3.5; + + // when + reportService.saveReport(recordEntity, recognized, score); + + // then + ArgumentCaptor reportEntityCaptor = ArgumentCaptor.forClass(ReportEntity.class); + verify(reportRepository).save(reportEntityCaptor.capture()); + + ReportEntity capturedReportEntity = reportEntityCaptor.getValue(); + + ReportEntity expectedReportEntity = ReportEntity.create(recordEntity, recognized, score); + + assertEquals(expectedReportEntity.getRecordId(), capturedReportEntity.getRecordId()); + assertEquals(expectedReportEntity.getScore(), capturedReportEntity.getScore()); + assertEquals(expectedReportEntity.getRecognized(), capturedReportEntity.getRecognized()); + } + + @DisplayName("리포트 레코드아이디로 불러오기") + @Test + public void getReportByRecordIdTest(){ + //given + RecordEntity recordEntity = mock(RecordEntity.class); + ReportEntity reportEntity = ReportEntity.create(recordEntity, "Test Recognition", 3.5); + Long id = recordEntity.getRecordId(); + + //when + when(reportService.getReportFromRecordId(id)).thenReturn(reportEntity); + + //then + assertEquals(reportService.getReportFromRecordId(id), reportEntity); + } + + @DisplayName("리포트 스크립트아이디로 불러오기") + @Test + public void getReportByScriptIdTest(){ + //given + ScriptEntity scriptEntity = mock(ScriptEntity.class); + RecordEntity recordEntity = mock(RecordEntity.class); + ReportEntity reportEntity = ReportEntity.create(recordEntity, "Test Recognition", 3.5); + Long id = scriptEntity.getScriptId(); + + //when + when(reportService.getReportFromRecordId(id)).thenReturn(reportEntity); + + //then + assertEquals(reportService.getReportFromRecordId(id), reportEntity); + } + +} From 2bcbba2634bac80f4718c30fface289eeee0bcec Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 11:02:22 +0900 Subject: [PATCH 02/19] =?UTF-8?q?[#54]=20report=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/repository/ReportRepository.java | 2 +- .../up/board/service/BoardServiceTest.java | 17 ++--- .../up/reply/service/ReplyServiceTest.java | 56 +++++----------- .../repository/ReportRepositoryTest.java | 65 +++++++++++++++++++ .../up/report/service/ReportServiceTest.java | 11 +--- 5 files changed, 93 insertions(+), 58 deletions(-) create mode 100644 src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java diff --git a/src/main/java/com/speech/up/report/repository/ReportRepository.java b/src/main/java/com/speech/up/report/repository/ReportRepository.java index e1906fb..4243ccb 100644 --- a/src/main/java/com/speech/up/report/repository/ReportRepository.java +++ b/src/main/java/com/speech/up/report/repository/ReportRepository.java @@ -3,13 +3,13 @@ import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; import com.speech.up.report.entity.ReportEntity; import com.speech.up.report.entity.dto.ReportGetDto; public interface ReportRepository extends JpaRepository { ReportEntity findReportEntityByReportId(Long recordId); + List findAllBy(); } diff --git a/src/test/java/com/speech/up/board/service/BoardServiceTest.java b/src/test/java/com/speech/up/board/service/BoardServiceTest.java index e862d2e..62534d3 100644 --- a/src/test/java/com/speech/up/board/service/BoardServiceTest.java +++ b/src/test/java/com/speech/up/board/service/BoardServiceTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,6 +12,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.mockito.MockitoSession; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.mock.web.MockHttpServletRequest; @@ -25,7 +27,6 @@ import com.speech.up.user.entity.UserEntity; import com.speech.up.user.repository.UserRepository; - public class BoardServiceTest { @Mock @@ -54,7 +55,6 @@ public void setUp() { @Test void getAllBoardList() { //given - BoardGetDto.Response response = mock(BoardGetDto.Response.class); Pageable pageable = mock(Pageable.class); //when @@ -85,8 +85,8 @@ void getBoardById() { boardService.getBoardById(1L, httpRequest); // then - verify(jwtProvider,times(1)).validate(socialId); - verify(userRepository,times(1)).findBySocialId(socialId); + verify(jwtProvider, times(1)).validate(socialId); + verify(userRepository, times(1)).findBySocialId(socialId); verify(boardRepository, times(1)).findByBoardIdAndIsUseTrue(1L); } @@ -122,7 +122,6 @@ void addBoard() { @Test void updateBoard() { // given - UserEntity userEntity = Mockito.mock(UserEntity.class); BoardUpdateDto.Request request = Mockito.mock(BoardUpdateDto.Request.class); @@ -178,8 +177,6 @@ void getTotalBoardCount() { given(boardRepository.countByIsUseTrue()).willReturn(count); - - // when result = boardService.getTotalBoardCount(); @@ -205,12 +202,10 @@ void getBoardCount() { given(boardRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId())).willReturn(1L); // when - result = boardService.getBoardCount(httpRequest); // then - - assert(result.equals(1L)); - + assert (result.equals(1L)); } + } \ No newline at end of file diff --git a/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java b/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java index c4da879..89761e1 100644 --- a/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java +++ b/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java @@ -5,13 +5,14 @@ import java.util.List; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; +import org.mockito.MockitoSession; import org.springframework.mock.web.MockHttpServletRequest; import com.speech.up.auth.provider.JwtProvider; @@ -51,9 +52,13 @@ class ReplyServiceTest { ReplyEntity replyEntity; BoardEntity boardEntity; + private MockitoSession mockitoSession; + @BeforeEach public void setUp() { - MockitoAnnotations.openMocks(this); + mockitoSession = Mockito.mockitoSession() + .initMocks(this) + .startMocking(); userEntity = Mockito.mock(UserEntity.class); given(userEntity.getUserId()).willReturn(1L); @@ -86,7 +91,7 @@ void getAllReplyList() { // given Long boardId = 1L; - List replyEntities = List.of(replyEntity,replyEntity); + List replyEntities = List.of(replyEntity, replyEntity); ReplyGetDto.Response replyGetResponse = Mockito.mock(ReplyGetDto.Response.class); given(replyGetResponse.getReplyId()).willReturn(1L); @@ -100,13 +105,11 @@ void getAllReplyList() { given(replyRepository.findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId)).willReturn(replyEntities); // when - replyGetResponseList = replyService.getAllReplyList(boardId); - //then - - assert(replyGetResponseList.size() == replyEntities.size()); - verify(replyRepository,times(1)).findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId); + //then + assert (replyGetResponseList.size() == replyEntities.size()); + verify(replyRepository, times(1)).findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId); } @@ -121,7 +124,6 @@ void getReply() { ReplyGetDto.Response replyGetResponse; // when - replyGetResponse = replyService.getReply(replyId); // then @@ -134,7 +136,6 @@ void getReply() { @Test void addReply() { // given - ReplyAddDto.Request replyAddRequest = Mockito.mock(ReplyAddDto.Request.class); given(replyAddRequest.getBoard()).willReturn(boardEntity); given(replyAddRequest.getUser()).willReturn(userEntity); @@ -156,7 +157,6 @@ void addReply() { @Test void updateReply() { // given - ReplyUpdateDto.Request replyUpdateRequest = Mockito.mock(ReplyUpdateDto.Request.class); given(replyUpdateRequest.getReplyId()).willReturn(1L); given(replyUpdateRequest.getBoard()).willReturn(boardEntity); @@ -172,11 +172,9 @@ void updateReply() { given(replyRepository.existsByReplyIdAndIsUseTrue(any(long.class))).willReturn(true); // when - response = replyService.updateReply(replyUpdateRequest); // then - assertNotNull(response); assert (response.getReplyId()).equals(replyUpdateRequest.getReplyId()); verify(replyRepository, times(1)).save(any(ReplyEntity.class)); @@ -191,7 +189,6 @@ void updateReply() { @Test void isUseReply() { // given - ReplyEntity modifiedReplyEntity = Mockito.mock(ReplyEntity.class); given(modifiedReplyEntity.getReplyId()).willReturn(1L); given(modifiedReplyEntity.getIsUse()).willReturn(false); @@ -216,11 +213,9 @@ void isUseReply() { ReplyIsUseDto.Response response; // when - response = replyService.isUseReply(replyIsUseRequest); // then - assertNotNull(response); assert (response.getReplyId()).equals(replyEntity.getReplyId()); assert (response.getIsUse()).equals(false); @@ -231,28 +226,6 @@ void isUseReply() { verify(replyRepository, times(1)).existsByReplyIdAndIsUseTrue(any(long.class)); } - @Test - void checkReply() { - } - - @Test - void checkUserEntity() { - } - - @Test - void checkBoardEntity() { - } - - @Test - void checkReplyEntity() { - // given - - // when - - // then - - } - @DisplayName("리플 갯수 확인") @Test void getReplyCount() { @@ -264,8 +237,10 @@ void getReplyCount() { given(jwtProvider.validate(socialId)).willReturn(socialId); given(userRepository.findBySocialId(socialId)).willReturn(userEntity); given(replyRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId())).willReturn(1L); + // when result = replyService.getReplyCount(httpRequest); + // then assertNotNull(result); assert (result.equals(userEntity.getUserId())); @@ -273,4 +248,9 @@ void getReplyCount() { verify(userRepository, times(1)).findBySocialId(socialId); verify(replyRepository, times(1)).countByUserUserIdAndIsUseTrue(userEntity.getUserId()); } + + @AfterEach + void tearDown() { + mockitoSession.finishMocking(); + } } \ No newline at end of file diff --git a/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java new file mode 100644 index 0000000..029189c --- /dev/null +++ b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java @@ -0,0 +1,65 @@ +package com.speech.up.report.repository; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoSession; + +import com.speech.up.report.entity.ReportEntity; +import com.speech.up.report.entity.dto.ReportGetDto; + +public class ReportRepositoryTest { + @Mock + private ReportRepository reportRepository; + + private MockitoSession mockitoSession; + + @BeforeEach + void setUp() { + mockitoSession = Mockito.mockitoSession() + .initMocks(this) + .startMocking(); + } + + @DisplayName("리포트아이디로 리포트 내용 찾기") + @Test + public void findReportByReportIdTest() { + //given + ReportEntity reportEntity = mock(ReportEntity.class); + Long id = 1L; + + //when + when(reportRepository.findReportEntityByReportId(id)).thenReturn(reportEntity); + + //then + assertEquals(reportRepository.findReportEntityByReportId(id), reportEntity); + } + + @DisplayName("모든 리포트 가져오기") + @Test + public void getAllReportsTest() { + //given + ReportEntity reportEntity = mock(ReportEntity.class); + List reportGetDtoResponse = Collections.singletonList(mock(ReportGetDto.Response.class)); + + //when + when(reportRepository.findAllBy()).thenReturn(reportGetDtoResponse); + + //then + assertEquals(reportRepository.findAllBy(), reportGetDtoResponse); + } + + @AfterEach + void tearDown() { + mockitoSession.finishMocking(); + } +} diff --git a/src/test/java/com/speech/up/report/service/ReportServiceTest.java b/src/test/java/com/speech/up/report/service/ReportServiceTest.java index 116da70..559b038 100644 --- a/src/test/java/com/speech/up/report/service/ReportServiceTest.java +++ b/src/test/java/com/speech/up/report/service/ReportServiceTest.java @@ -1,6 +1,5 @@ package com.speech.up.report.service; -import static org.awaitility.Awaitility.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -13,7 +12,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import com.speech.up.record.entity.RecordEntity; -import com.speech.up.record.repository.RecordRepository; import com.speech.up.report.entity.ReportEntity; import com.speech.up.report.repository.ReportRepository; import com.speech.up.script.entity.ScriptEntity; @@ -23,15 +21,12 @@ public class ReportServiceTest { @Mock private ReportRepository reportRepository; - @Mock - private RecordRepository recordRepository; - @InjectMocks private ReportService reportService; @DisplayName("리포트 저장하기") @Test - public void saveReportTest(){ + public void saveReportTest() { // given RecordEntity recordEntity = mock(RecordEntity.class); // RecordEntity 모의 @@ -56,7 +51,7 @@ public void saveReportTest(){ @DisplayName("리포트 레코드아이디로 불러오기") @Test - public void getReportByRecordIdTest(){ + public void getReportByRecordIdTest() { //given RecordEntity recordEntity = mock(RecordEntity.class); ReportEntity reportEntity = ReportEntity.create(recordEntity, "Test Recognition", 3.5); @@ -71,7 +66,7 @@ public void getReportByRecordIdTest(){ @DisplayName("리포트 스크립트아이디로 불러오기") @Test - public void getReportByScriptIdTest(){ + public void getReportByScriptIdTest() { //given ScriptEntity scriptEntity = mock(ScriptEntity.class); RecordEntity recordEntity = mock(RecordEntity.class); From 8a94f5c97fa97516dd8ba5eb7ee7490d6b57d392 Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 11:03:35 +0900 Subject: [PATCH 03/19] =?UTF-8?q?[#54]=20report=20=EC=84=9C=EB=B9=84?= =?UTF-8?q?=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/speech/up/board/service/BoardServiceTest.java | 4 ---- .../com/speech/up/report/repository/ReportRepositoryTest.java | 1 - 2 files changed, 5 deletions(-) diff --git a/src/test/java/com/speech/up/board/service/BoardServiceTest.java b/src/test/java/com/speech/up/board/service/BoardServiceTest.java index 62534d3..836f62d 100644 --- a/src/test/java/com/speech/up/board/service/BoardServiceTest.java +++ b/src/test/java/com/speech/up/board/service/BoardServiceTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +11,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.mockito.MockitoSession; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.mock.web.MockHttpServletRequest; @@ -21,7 +19,6 @@ import com.speech.up.board.entity.BoardEntity; import com.speech.up.board.repository.BoardRepository; import com.speech.up.board.service.dto.BoardAddDto; -import com.speech.up.board.service.dto.BoardGetDto; import com.speech.up.board.service.dto.BoardIsUseDto; import com.speech.up.board.service.dto.BoardUpdateDto; import com.speech.up.user.entity.UserEntity; @@ -45,7 +42,6 @@ public class BoardServiceTest { String bearer = "Bearer "; String token = bearer + socialId; - @BeforeEach public void setUp() { MockitoAnnotations.openMocks(this); diff --git a/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java index 029189c..8505d97 100644 --- a/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java +++ b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java @@ -48,7 +48,6 @@ public void findReportByReportIdTest() { @Test public void getAllReportsTest() { //given - ReportEntity reportEntity = mock(ReportEntity.class); List reportGetDtoResponse = Collections.singletonList(mock(ReportGetDto.Response.class)); //when From d04a26ade6286687cbd6f4ddb57b8caa2b04309a Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 12:59:53 +0900 Subject: [PATCH 04/19] =?UTF-8?q?[#54]=20=EB=A0=88=ED=8F=AC=EC=A7=80?= =?UTF-8?q?=ED=86=A0=EB=A6=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/repository/BoardRepositoryTest.java | 80 ++++++++++++++++++ .../reply/repository/ReplyRepositoryTest.java | 83 +++++++++++++++++++ .../repository/ScriptRepositoryTest.java | 53 ++++++++++++ 3 files changed, 216 insertions(+) create mode 100644 src/test/java/com/speech/up/board/repository/BoardRepositoryTest.java create mode 100644 src/test/java/com/speech/up/reply/repository/ReplyRepositoryTest.java create mode 100644 src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java diff --git a/src/test/java/com/speech/up/board/repository/BoardRepositoryTest.java b/src/test/java/com/speech/up/board/repository/BoardRepositoryTest.java new file mode 100644 index 0000000..0de285d --- /dev/null +++ b/src/test/java/com/speech/up/board/repository/BoardRepositoryTest.java @@ -0,0 +1,80 @@ +package com.speech.up.board.repository; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import com.speech.up.board.entity.BoardEntity; + +public class BoardRepositoryTest { + + @Mock + BoardRepository boardRepository; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("모든 게시판 정보 가져오기") + @Test + public void getAllBoardsTest() { + //given + Pageable pageable = mock(Pageable.class); + + //when + Page boardList = boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable); + when(boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable)).thenReturn(boardList); + + //then + assertEquals(boardList, boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable)); + } + + @DisplayName("사용 중인 게시판 갯수 가져오기") + @Test + public void getAllBoardsCountIsUse() { + //given + Long count = 0L; + + //when + when(boardRepository.countByIsUseTrue()).thenReturn(count); + + //then + assertEquals(count, boardRepository.countByIsUseTrue()); + } + + @DisplayName("보드 id로 게시판 찾기") + @Test + public void getBoardByIdTest() { + //given + BoardEntity boardEntity = mock(BoardEntity.class); + Long boardId = 0L; + + //when + when(boardRepository.findByBoardIdAndIsUseTrue(boardId)).thenReturn(boardEntity); + + //then + assertEquals(boardRepository.findByBoardIdAndIsUseTrue(boardId), boardEntity); + } + + @DisplayName("유저아이디로 사용 중인 게시판 갯수찾기") + @Test + public void getBoardByIdCountIsUse() { + //given + Long userId = 0L; + Long count = 0L; + + //when + when(boardRepository.countByUserUserIdAndIsUseTrue(userId)).thenReturn(count); + + //then + assertEquals(count, boardRepository.countByUserUserIdAndIsUseTrue(userId)); + } +} diff --git a/src/test/java/com/speech/up/reply/repository/ReplyRepositoryTest.java b/src/test/java/com/speech/up/reply/repository/ReplyRepositoryTest.java new file mode 100644 index 0000000..d8b82b3 --- /dev/null +++ b/src/test/java/com/speech/up/reply/repository/ReplyRepositoryTest.java @@ -0,0 +1,83 @@ +package com.speech.up.reply.repository; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.reply.entity.ReplyEntity; + +public class ReplyRepositoryTest { + + @Mock + ReplyRepository replyRepository; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("사용 중인 댓글을 replyId로 검색") + @Test + public void getReplyByReplyIdTest() { + //given + ReplyEntity replyEntity = mock(ReplyEntity.class); + Long replyId = 1L; + + //when + when(replyRepository.findByReplyIdAndIsUseTrue(replyId)).thenReturn(replyEntity); + + //then + assertEquals(replyRepository.findByReplyIdAndIsUseTrue(replyId), replyEntity); + } + + @DisplayName("replyId로 사용 중인지 여부 확인") + @Test + public void existReplyByReplyIdTest() { + //given + boolean replyExists = false; + Long replyId = 1L; + + //when + when(replyRepository.existsByReplyIdAndIsUseTrue(replyId)).thenReturn(replyExists); + + //then + assertEquals(replyRepository.existsByReplyIdAndIsUseTrue(replyId), replyExists); + } + + @DisplayName("boardId로 사용 중인 댓글 모두 검색") + @Test + public void getRepliesAllByBoardIdTest() { + //given + List replyEntities = Collections.singletonList(mock(ReplyEntity.class)); + Long boardId = 1L; + + //when + when(replyRepository.findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId)).thenReturn(replyEntities); + + //then + assertEquals(replyRepository.findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId), replyEntities); + } + + @DisplayName("userId로 해당 유저의 댓글 갯수") + @Test + public void countRepliesAllByUserIdTest() { + //given + Long userId = 1L; + Long result = 1L; + + //when + when(replyRepository.countByUserUserIdAndIsUseTrue(userId)).thenReturn(result); + + //then + assertEquals(result, replyRepository.countByUserUserIdAndIsUseTrue(userId)); + } + +} diff --git a/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java b/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java new file mode 100644 index 0000000..f948a7e --- /dev/null +++ b/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java @@ -0,0 +1,53 @@ +package com.speech.up.script.repository; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.script.entity.ScriptEntity; + +public class ScriptRepositoryTest { + @Mock + ScriptRepository scriptRepository; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("유저 아이디로 스크립트 리스트 검색") + @Test + public void getScriptListByUserIdTest(){ + //given + Long userId = 1L; + List scriptEntity = Collections.singletonList(mock(ScriptEntity.class)); + + //when + when(scriptRepository.findByUserUserIdAndIsUseTrue(userId)).thenReturn(scriptEntity); + + //then + assertEquals(scriptRepository.findByUserUserIdAndIsUseTrue(userId), scriptEntity); + } + + @DisplayName("유저 아이디로 사용 중인 스크립트 갯수 검색") + @Test + public void getCountScriptListByUserIdTest(){ + //given + Long userId = 1L; + Long count = 1L; + + //when + when(scriptRepository.countByUserUserIdAndIsUseTrue(userId)).thenReturn(1L); + + //then + assertEquals(scriptRepository.countByUserUserIdAndIsUseTrue(userId), count); + } +} From c833b3d74b06f8d7cdf7bc00a871dc9290a49968 Mon Sep 17 00:00:00 2001 From: xorwns118 Date: Tue, 27 Aug 2024 14:34:39 +0900 Subject: [PATCH 05/19] [#167] code refactor: record, report --- .../speech/up/record/entity/RecordEntity.java | 26 +++++++++++++------ .../up/record/service/RecordService.java | 12 +++++---- .../up/record/service/dto/RecordAddDto.java | 2 +- .../up/record/service/dto/RecordGetDto.java | 2 +- .../up/record/service/dto/RecordIsUseDto.java | 3 ++- .../report/repository/ReportRepository.java | 3 ++- .../up/report/service/ReportService.java | 19 ++++++++------ .../speech/up/script/entity/ScriptEntity.java | 8 ------ 8 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/speech/up/record/entity/RecordEntity.java b/src/main/java/com/speech/up/record/entity/RecordEntity.java index d010ef9..a63784e 100644 --- a/src/main/java/com/speech/up/record/entity/RecordEntity.java +++ b/src/main/java/com/speech/up/record/entity/RecordEntity.java @@ -56,10 +56,6 @@ private RecordEntity(byte[] audio, RecordAddDto.Request request, ScriptEntity sc this(audio, request.getLanguageCode(), scriptEntity, true, false); } - public static RecordEntity create(byte[] audio, RecordAddDto.Request request, ScriptEntity scriptEntity) { - return new RecordEntity(audio, request, scriptEntity); - } - private RecordEntity(RecordIsUseDto.Request recordIsUseRequestDto) { this(recordIsUseRequestDto.getRecordEntity().getAudio(), recordIsUseRequestDto.getRecordEntity() @@ -68,11 +64,25 @@ private RecordEntity(RecordIsUseDto.Request recordIsUseRequestDto) { this.recordId = recordIsUseRequestDto.getRecordEntity().getRecordId(); } - public static RecordEntity delete(RecordIsUseDto.Request recordIsUseRequestDto) { - return new RecordEntity(recordIsUseRequestDto); + private RecordEntity(RecordEntity recordEntity) { + this.recordId = recordEntity.getRecordId(); + this.audio = recordEntity.getAudio(); + this.languageCode = recordEntity.getLanguageCode(); + this.script = recordEntity.getScript(); + this.isUse = recordEntity.isUse(); + this.report = recordEntity.getReport(); + this.isAnalyzed = true; } - public void analyze(boolean isAnalyzed) { - this.isAnalyzed = isAnalyzed; + public static void analyze(RecordEntity recordEntity) { + new RecordEntity(recordEntity); + } + + public static RecordEntity create(byte[] audio, RecordAddDto.Request request, ScriptEntity scriptEntity) { + return new RecordEntity(audio, request, scriptEntity); + } + + public static RecordEntity delete(RecordIsUseDto.Request recordIsUseRequestDto) { + return new RecordEntity(recordIsUseRequestDto); } } diff --git a/src/main/java/com/speech/up/record/service/RecordService.java b/src/main/java/com/speech/up/record/service/RecordService.java index 3a59de3..0c1ff03 100644 --- a/src/main/java/com/speech/up/record/service/RecordService.java +++ b/src/main/java/com/speech/up/record/service/RecordService.java @@ -9,6 +9,7 @@ import com.speech.up.script.entity.ScriptEntity; import com.speech.up.script.repository.ScriptRepository; +import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -33,7 +34,7 @@ private List getActiveRecordsByScriptId(Long scriptId) { private List mapToResponse(List records) { return records.stream() - .map(RecordGetDto.Response::getRecords) + .map(RecordGetDto.Response::toResponse) .collect(Collectors.toList()); } @@ -45,26 +46,27 @@ public List getRecordList(Long scriptId) { public RecordAddDto.Response addRecord(MultipartFile file, String languageCode, Long scriptId) throws IOException, UnsupportedAudioFileException { - ScriptEntity scriptEntity = scriptRepository.findById(scriptId).get(); + ScriptEntity scriptEntity = scriptRepository.findById(scriptId) + .orElseThrow(() -> new EntityNotFoundException("script not found by scriptId : " + scriptId)); WavToRaw wavToRaw = new WavToRaw(); byte[] audio = wavToRaw.convertToRawPcm(file); RecordAddDto.Request request = new RecordAddDto.Request(file, languageCode, scriptEntity); RecordEntity recordEntity = RecordEntity.create(audio, request, scriptEntity); - return RecordAddDto.toEntity(recordRepository.save(recordEntity)); + return RecordAddDto.toResponse(recordRepository.save(recordEntity)); } public RecordIsUseDto.Response deleteRecord(RecordIsUseDto.Request recordIsUseRequestDto) { RecordEntity recordEntity = RecordEntity.delete(recordIsUseRequestDto); - return RecordIsUseDto.deleteRecord(recordRepository.save(recordEntity)); + return RecordIsUseDto.toResponse(recordRepository.save(recordEntity)); } @Transactional public void analyzed(Long recordId){ RecordEntity recordEntity = recordRepository.findById(recordId) .orElseThrow(() -> new IllegalStateException("not found record by recordId : " + recordId)); - recordEntity.analyze(true); + RecordEntity.analyze(recordEntity); } } diff --git a/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java b/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java index eacf5a3..6d05ed4 100644 --- a/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java +++ b/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java @@ -48,7 +48,7 @@ public Response(RecordEntity recordEntity) { } } - public static Response toEntity(RecordEntity recordEntity) { + public static Response toResponse(RecordEntity recordEntity) { return new Response(recordEntity); } } \ No newline at end of file diff --git a/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java b/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java index 813a98f..3bc7bd6 100644 --- a/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java +++ b/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java @@ -27,7 +27,7 @@ public Response(RecordEntity recordEntity) { this.isAnalyzed = recordEntity.isAnalyzed(); } - public static Response getRecords(RecordEntity recordEntity) { + public static Response toResponse(RecordEntity recordEntity) { return new Response(recordEntity); } } diff --git a/src/main/java/com/speech/up/record/service/dto/RecordIsUseDto.java b/src/main/java/com/speech/up/record/service/dto/RecordIsUseDto.java index badf49d..00b9a3f 100644 --- a/src/main/java/com/speech/up/record/service/dto/RecordIsUseDto.java +++ b/src/main/java/com/speech/up/record/service/dto/RecordIsUseDto.java @@ -35,7 +35,8 @@ public Response(RecordEntity recordEntity) { } } - public static Response deleteRecord(RecordEntity recordEntity) { + public static Response toResponse(RecordEntity recordEntity) { return new Response(recordEntity); } } + diff --git a/src/main/java/com/speech/up/report/repository/ReportRepository.java b/src/main/java/com/speech/up/report/repository/ReportRepository.java index e1906fb..ae61f59 100644 --- a/src/main/java/com/speech/up/report/repository/ReportRepository.java +++ b/src/main/java/com/speech/up/report/repository/ReportRepository.java @@ -1,6 +1,7 @@ package com.speech.up.report.repository; import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -9,7 +10,7 @@ import com.speech.up.report.entity.dto.ReportGetDto; public interface ReportRepository extends JpaRepository { - ReportEntity findReportEntityByReportId(Long recordId); + Optional findReportEntityByReportId(Long recordId); List findAllBy(); } diff --git a/src/main/java/com/speech/up/report/service/ReportService.java b/src/main/java/com/speech/up/report/service/ReportService.java index d94ad2c..60d3ab4 100644 --- a/src/main/java/com/speech/up/report/service/ReportService.java +++ b/src/main/java/com/speech/up/report/service/ReportService.java @@ -1,35 +1,38 @@ package com.speech.up.report.service; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.NoSuchElementException; + import org.springframework.stereotype.Service; import com.speech.up.record.repository.RecordRepository; import com.speech.up.report.entity.ReportEntity; import com.speech.up.report.repository.ReportRepository; import com.speech.up.record.entity.RecordEntity; +import com.speech.up.script.entity.ScriptEntity; +import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; @Service @RequiredArgsConstructor public class ReportService { - private static final Logger log = LoggerFactory.getLogger(ReportService.class); private final ReportRepository reportRepository; private final RecordRepository recordRepository; public void saveReport(RecordEntity recordEntity, String recognized, double score) { ReportEntity reportEntity = ReportEntity.create(recordEntity, recognized, score); - reportRepository.save(reportEntity); } public ReportEntity getReportFromRecordId(Long recordId) { - return reportRepository.findReportEntityByReportId(recordId); + return reportRepository.findReportEntityByReportId(recordId) + .orElseThrow(() -> new EntityNotFoundException("not found Report by RecordId : " + recordId)); } - public String getScriptFromRecordId (Long recordId) { - RecordEntity recordEntity = recordRepository.findById(recordId).get(); - return recordEntity.getScript().getContent(); + public String getScriptFromRecordId(Long recordId) { + return recordRepository.findById(recordId) + .map(RecordEntity::getScript) + .map(ScriptEntity::getContent) + .orElseThrow(() -> new NoSuchElementException("not found Script by recordId : " + recordId)); } } diff --git a/src/main/java/com/speech/up/script/entity/ScriptEntity.java b/src/main/java/com/speech/up/script/entity/ScriptEntity.java index efee609..ce99cb9 100644 --- a/src/main/java/com/speech/up/script/entity/ScriptEntity.java +++ b/src/main/java/com/speech/up/script/entity/ScriptEntity.java @@ -51,18 +51,15 @@ public ScriptEntity(String content, UserEntity user, String title, boolean isUse this.isUse = isUse; } - // 대본 생성 private ScriptEntity(ScriptAddDto.Request scriptAddRequestDto) { this(scriptAddRequestDto.getContent(), scriptAddRequestDto.getUser(), scriptAddRequestDto.getTitle(), true); } - // 대본 업데이트 private ScriptEntity(ScriptUpdateDto.Request scriptUpdateRequestDto) { this(scriptUpdateRequestDto.getContent(), scriptUpdateRequestDto.getUser(), scriptUpdateRequestDto.getTitle(),true); this.scriptId = scriptUpdateRequestDto.getScriptId(); } - // 대본 삭제 private ScriptEntity(ScriptIsUseDto.Request scriptIsUseRequestDto) { this(scriptIsUseRequestDto.getContent(), scriptIsUseRequestDto.getUser(), scriptIsUseRequestDto.getTitle(), false); this.scriptId = scriptIsUseRequestDto.getScriptId(); @@ -72,11 +69,6 @@ public static ScriptEntity create(ScriptAddDto.Request scriptAddRequestDto) { return new ScriptEntity(scriptAddRequestDto); } - /*// 정적 메서드 사용시 기존 엔티티 값을 받아올 수 없어 response 에 있는 createdAt 이 null 로 뜨게됨 - public ScriptEntity update(ScriptUpdateDto.Request scriptUpdateRequestDto) { - return new ScriptEntity(scriptUpdateRequestDto); - }*/ - public static ScriptEntity update(ScriptUpdateDto.Request scriptUpdateRequestDto) { return new ScriptEntity(scriptUpdateRequestDto); } From 9053b35b91594fc24f9cd289b7356d239d10e196 Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 16:31:52 +0900 Subject: [PATCH 06/19] =?UTF-8?q?[#54]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../checkValue/CheckListForPagination.java | 4 +- .../up/board/service/BoardServiceTest.java | 194 ++++++---------- .../repository/RecordRepositoryTest.java | 40 ++++ .../up/record/service/RecordServiceTest.java | 93 ++++++++ .../up/reply/service/ReplyServiceTest.java | 210 +++--------------- .../up/report/service/ReportServiceTest.java | 29 +-- .../repository/ScriptRepositoryTest.java | 4 +- .../up/script/service/ScriptServiceTest.java | 158 +++---------- .../user/repositiory/UserRepositoryTest.java | 100 +++++++++ .../up/user/service/UserServiceTest.java | 97 ++++++++ 10 files changed, 475 insertions(+), 454 deletions(-) create mode 100644 src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java create mode 100644 src/test/java/com/speech/up/record/service/RecordServiceTest.java create mode 100644 src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java create mode 100644 src/test/java/com/speech/up/user/service/UserServiceTest.java diff --git a/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java b/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java index 3dc9e21..10d25b5 100644 --- a/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java +++ b/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java @@ -3,7 +3,6 @@ import org.springframework.data.domain.Page; import com.speech.up.board.entity.BoardEntity; -import com.speech.up.common.exception.http.BadRequestException; import lombok.Getter; @@ -11,9 +10,10 @@ public class CheckListForPagination { private final Page boardList; - CheckListForPagination(final Page boardList) { + CheckListForPagination(final Page boardList) { this.boardList = boardList; } + public static CheckListForPagination checkListIsNotEmpty(final Page boardList) { return new CheckListForPagination(boardList); } diff --git a/src/test/java/com/speech/up/board/service/BoardServiceTest.java b/src/test/java/com/speech/up/board/service/BoardServiceTest.java index 836f62d..bc489cf 100644 --- a/src/test/java/com/speech/up/board/service/BoardServiceTest.java +++ b/src/test/java/com/speech/up/board/service/BoardServiceTest.java @@ -1,47 +1,30 @@ package com.speech.up.board.service; -import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; +import java.util.Collections; +import java.util.List; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.mock.web.MockHttpServletRequest; -import com.speech.up.auth.provider.JwtProvider; -import com.speech.up.board.entity.BoardEntity; -import com.speech.up.board.repository.BoardRepository; import com.speech.up.board.service.dto.BoardAddDto; +import com.speech.up.board.service.dto.BoardGetDto; import com.speech.up.board.service.dto.BoardIsUseDto; import com.speech.up.board.service.dto.BoardUpdateDto; -import com.speech.up.user.entity.UserEntity; -import com.speech.up.user.repository.UserRepository; - -public class BoardServiceTest { +import com.speech.up.common.exception.http.BadRequestException; - @Mock - UserRepository userRepository; +import jakarta.servlet.http.HttpServletRequest; - @Mock - BoardRepository boardRepository; +public class BoardServiceTest { @Mock - JwtProvider jwtProvider; - - @InjectMocks BoardService boardService; - String socialId = "socialId"; - String bearer = "Bearer "; - String token = bearer + socialId; - @BeforeEach public void setUp() { MockitoAnnotations.openMocks(this); @@ -51,157 +34,106 @@ public void setUp() { @Test void getAllBoardList() { //given - Pageable pageable = mock(Pageable.class); + List response = Collections.singletonList(mock(BoardGetDto.Response.class)); + int page = 1; + int size = 10; //when - Page boardList = boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable); + when(boardService.getAllBoardList(page, size)).thenReturn(response); + when(boardService.getAllBoardList(-1, size)).thenThrow( + new BadRequestException("page or size can not be less than zero")); + when(boardService.getAllBoardList(page, 101)).thenThrow( + new BadRequestException("size is must be less than 100.")); //then - when(boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable)).thenReturn(boardList); + assertEquals(response, boardService.getAllBoardList(page, size)); + assertThrows(BadRequestException.class, () -> boardService.getAllBoardList(-1, size)); + assertThrows(BadRequestException.class, () -> boardService.getAllBoardList(page, 101)); } @DisplayName("BoardList 단건 조회") @Test void getBoardById() { - // given - UserEntity userEntity = Mockito.mock(UserEntity.class); - given(userEntity.getUserId()).willReturn(1L); - - BoardEntity boardEntity = Mockito.mock(BoardEntity.class); - given(boardEntity.getUser()).willReturn(userEntity); - - MockHttpServletRequest httpRequest = new MockHttpServletRequest(); - httpRequest.addHeader("Authorization", token); + // given + HttpServletRequest request = mock(HttpServletRequest.class); + BoardGetDto.Response response = mock(BoardGetDto.Response.class); + Long boardId = 1L; - given(jwtProvider.validate(socialId)).willReturn(socialId); - given(userRepository.findBySocialId(socialId)).willReturn(userEntity); - given(boardRepository.findByBoardIdAndIsUseTrue(1L)).willReturn(boardEntity); + // when + when(boardService.getBoardById(boardId, request)).thenReturn(response); - // when - boardService.getBoardById(1L, httpRequest); - - // then - verify(jwtProvider, times(1)).validate(socialId); - verify(userRepository, times(1)).findBySocialId(socialId); - verify(boardRepository, times(1)).findByBoardIdAndIsUseTrue(1L); + // then + assertEquals(response, boardService.getBoardById(boardId, request)); } @DisplayName("게시판 생성 테스트") @Test void addBoard() { - // given - UserEntity userEntity = Mockito.mock(UserEntity.class); - - MockHttpServletRequest httpRequest = new MockHttpServletRequest(); - httpRequest.addHeader("Authorization", "Bearer " + token); - httpRequest.addHeader("socialId", socialId); - - BoardAddDto.Request request = new BoardAddDto.Request("title", "content", userEntity); + // given + BoardAddDto.Request request = mock(BoardAddDto.Request.class); + BoardAddDto.Response response = mock(BoardAddDto.Response.class); + HttpServletRequest servletRequest = mock(HttpServletRequest.class); - BoardEntity boardEntity = BoardEntity.create(request); + // when + when(boardService.addBoard(request, servletRequest)).thenReturn(response); - BoardAddDto.Response response; - - given(jwtProvider.validate(token)).willReturn(socialId); - given(userRepository.findBySocialId(socialId)).willReturn(userEntity); - given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); - - // when - response = boardService.addBoard(request, httpRequest); - - // then - assert (response.getContent()).equals(request.getContent()); - assert (response.getTitle()).equals(request.getTitle()); + // then + assertEquals(response, boardService.addBoard(request, servletRequest)); } @DisplayName("게시판 업데이트 테스트") @Test void updateBoard() { - // given - UserEntity userEntity = Mockito.mock(UserEntity.class); - - BoardUpdateDto.Request request = Mockito.mock(BoardUpdateDto.Request.class); - given(request.getBoardId()).willReturn(1L); - given(request.getTitle()).willReturn("title"); - given(request.getContent()).willReturn("content"); - given(request.getUser()).willReturn(userEntity); - BoardUpdateDto.Response response; + // given + BoardUpdateDto.Request request = mock(BoardUpdateDto.Request.class); + BoardUpdateDto.Response response = mock(BoardUpdateDto.Response.class); - BoardEntity boardEntity = BoardEntity.update(request); + // when + when(boardService.updateBoard(request)).thenReturn(response); - given(userRepository.findByUserId(request.getUser().getUserId())).willReturn(userEntity); - given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); - - // when - response = boardService.updateBoard(request); - - // then - assert (response.getContent()).equals(request.getContent()); - assert (response.getTitle()).equals(request.getTitle()); - verify(boardRepository, times(1)).save(any(BoardEntity.class)); + // then + assertEquals(response, boardService.updateBoard(request)); } @DisplayName("보드 삭제 테스트") @Test void deleteBoard() { - // given - UserEntity userEntity = Mockito.mock(UserEntity.class); - - BoardIsUseDto.Request request = Mockito.mock(BoardIsUseDto.Request.class); - given(request.getBoardId()).willReturn(1L); - given(request.isUse()).willReturn(true); - given(request.getTitle()).willReturn("title"); - given(request.getContent()).willReturn("content"); - given(request.getUser()).willReturn(userEntity); - BoardEntity boardEntity = BoardEntity.delete(request); - BoardIsUseDto.Response response; - given(boardRepository.save(any(BoardEntity.class))).willReturn(boardEntity); - - // when - response = boardService.deleteBoard(request); - - // then - assertFalse(response.isUse(), " isUse 의 값이 변경되지 않았습니다."); + // given + BoardIsUseDto.Request request = mock(BoardIsUseDto.Request.class); + BoardIsUseDto.Response response = mock(BoardIsUseDto.Response.class); + + // when + when(boardService.deleteBoard(request)).thenReturn(response); + + // then + assertEquals(response, boardService.deleteBoard(request)); } @DisplayName("게시판 총 갯수 조회") @Test void getTotalBoardCount() { - // given - Long count = 3L; - Long result; - - given(boardRepository.countByIsUseTrue()).willReturn(count); - - // when - result = boardService.getTotalBoardCount(); + // given + Long boardCount = 132131L; - // then - assertThat(result).isEqualTo(count); + // when + when(boardService.getTotalBoardCount()).thenReturn(boardCount); + // then + assertEquals(boardCount, boardService.getTotalBoardCount()); } @DisplayName("유저의 게시글 갯수 조회") @Test void getBoardCount() { - // given - Long result; - - UserEntity userEntity = Mockito.mock(UserEntity.class); - given(userEntity.getUserId()).willReturn(1L); - - MockHttpServletRequest httpRequest = new MockHttpServletRequest(); - httpRequest.addHeader("Authorization", token); - - given(jwtProvider.validate(socialId)).willReturn(socialId); - given(userRepository.findBySocialId(socialId)).willReturn(userEntity); - given(boardRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId())).willReturn(1L); + // given + HttpServletRequest request = mock(HttpServletRequest.class); + Long boardCount = 132131L; - // when - result = boardService.getBoardCount(httpRequest); + // when + when(boardService.getBoardCount(request)).thenReturn(boardCount); - // then - assert (result.equals(1L)); + // then + assertEquals(boardCount, boardService.getBoardCount(request)); } } \ No newline at end of file diff --git a/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java b/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java new file mode 100644 index 0000000..8030439 --- /dev/null +++ b/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java @@ -0,0 +1,40 @@ +package com.speech.up.record.repository; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.record.entity.RecordEntity; + +public class RecordRepositoryTest { + + @Mock + RecordRepository recordRepository; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("스크립트 아이디로 사용 중인 녹음데이터 리스트 가져오기 함수 호출") + @Test + public void findByScriptIdUseTest(){ + //given + List recordEntity = Collections.singletonList(mock(RecordEntity.class)); + Long scriptId = 1L; + + //when + when(recordRepository.findByScriptScriptIdAndIsUseTrue(scriptId)).thenReturn(recordEntity); + + //then + assertEquals(recordEntity, recordRepository.findByScriptScriptIdAndIsUseTrue(scriptId)); + } +} diff --git a/src/test/java/com/speech/up/record/service/RecordServiceTest.java b/src/test/java/com/speech/up/record/service/RecordServiceTest.java new file mode 100644 index 0000000..ee08fa7 --- /dev/null +++ b/src/test/java/com/speech/up/record/service/RecordServiceTest.java @@ -0,0 +1,93 @@ +package com.speech.up.record.service; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import javax.sound.sampled.UnsupportedAudioFileException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.web.multipart.MultipartFile; + +import com.speech.up.record.service.dto.RecordAddDto; +import com.speech.up.record.service.dto.RecordGetDto; +import com.speech.up.record.service.dto.RecordIsUseDto; + +public class RecordServiceTest { + + @Mock + RecordService recordService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("스크립트 아이디로 녹음 데이터 리스트 가져오기") + @Test + public void getRecordListByScriptIdTest() { + //given + List response = Collections.singletonList(mock(RecordGetDto.Response.class)); + Long scriptId = 1L; + + //when + when(recordService.getRecordList(scriptId)).thenReturn(response); + + //then + assertEquals(response, recordService.getRecordList(scriptId)); + } + + @DisplayName("녹음 데이터 추가하기") + @Test + public void addRecordTest() { + //given + MultipartFile file = mock(MultipartFile.class); + String languageCode = "korean"; + Long scriptId = 1L; + RecordAddDto.Response response = mock(RecordAddDto.Response.class); + + try { + //when + when(recordService.addRecord(file, languageCode, scriptId)).thenReturn(response); + //then + assertEquals(response, recordService.addRecord(file, languageCode, scriptId)); + } catch (UnsupportedAudioFileException | IOException e) { + //then + assertEquals("Test Exception", e.getMessage()); + } + } + + @DisplayName("녹음 데이터 삭제하기") + @Test + public void deleteRecordTest() { + //given + RecordIsUseDto.Request request = mock(RecordIsUseDto.Request.class); + RecordIsUseDto.Response response = mock(RecordIsUseDto.Response.class); + + //when + when(recordService.deleteRecord(request)).thenReturn(response); + + //then + assertEquals(response, recordService.deleteRecord(request)); + } + + @DisplayName("녹음 아이디로 분석 메서드 호출하기") + @Test + public void callAnalyzeMethodByRecordIdTest() { + //given + Long recordId = 1L; + + //when + recordService.analyzed(recordId); + + //then + verify(recordService, times(1)).analyzed(recordId); + } +} diff --git a/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java b/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java index 89761e1..bee8ddf 100644 --- a/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java +++ b/src/test/java/com/speech/up/reply/service/ReplyServiceTest.java @@ -3,114 +3,44 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; +import java.util.Collections; import java.util.List; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoSession; -import org.springframework.mock.web.MockHttpServletRequest; +import org.mockito.MockitoAnnotations; -import com.speech.up.auth.provider.JwtProvider; -import com.speech.up.board.entity.BoardEntity; -import com.speech.up.board.repository.BoardRepository; -import com.speech.up.reply.entity.ReplyEntity; -import com.speech.up.reply.repository.ReplyRepository; import com.speech.up.reply.service.dto.ReplyAddDto; import com.speech.up.reply.service.dto.ReplyGetDto; import com.speech.up.reply.service.dto.ReplyIsUseDto; import com.speech.up.reply.service.dto.ReplyUpdateDto; -import com.speech.up.user.entity.UserEntity; -import com.speech.up.user.repository.UserRepository; -class ReplyServiceTest { - - @Mock - ReplyRepository replyRepository; - - @Mock - UserRepository userRepository; +import jakarta.servlet.http.HttpServletRequest; - @Mock - BoardRepository boardRepository; +class ReplyServiceTest { @Mock - JwtProvider jwtProvider; - - @InjectMocks ReplyService replyService; - String socialId = "socialId"; - String bearer = "Bearer "; - String token = bearer + socialId; - - UserEntity userEntity; - ReplyEntity replyEntity; - BoardEntity boardEntity; - - private MockitoSession mockitoSession; - @BeforeEach public void setUp() { - mockitoSession = Mockito.mockitoSession() - .initMocks(this) - .startMocking(); - - userEntity = Mockito.mock(UserEntity.class); - given(userEntity.getUserId()).willReturn(1L); - given(userEntity.getName()).willReturn("name"); - given(userEntity.getSocialId()).willReturn("socialId"); - given(userEntity.getEmail()).willReturn("email"); - given(userEntity.getLevel()).willReturn("level"); - given(userEntity.getAuthorization()).willReturn(token); - - boardEntity = Mockito.mock(BoardEntity.class); - given(boardEntity.getBoardId()).willReturn(1L); - given(boardEntity.getTitle()).willReturn("title"); - given(boardEntity.getContent()).willReturn("content"); - given(boardEntity.getIsUse()).willReturn(true); - given(boardEntity.getUser()).willReturn(userEntity); - - replyEntity = Mockito.mock(ReplyEntity.class); - given(replyEntity.getReplyId()).willReturn(1L); - given(replyEntity.getIsUse()).willReturn(true); - given(replyEntity.getUser()).willReturn(userEntity); - given(replyEntity.getContent()).willReturn("content"); - given(replyEntity.getName()).willReturn("name"); - given(replyEntity.getBoard()).willReturn(boardEntity); - + MockitoAnnotations.openMocks(this); } @DisplayName("리플 다건 조회 테스트") @Test - void getAllReplyList() { - // given + void getAllReplyLisTest() { + //given Long boardId = 1L; - - List replyEntities = List.of(replyEntity, replyEntity); - - ReplyGetDto.Response replyGetResponse = Mockito.mock(ReplyGetDto.Response.class); - given(replyGetResponse.getReplyId()).willReturn(1L); - given(replyGetResponse.getName()).willReturn("name"); - given(replyGetResponse.getContent()).willReturn("content"); - given(replyGetResponse.getBoardId()).willReturn(1L); - given(replyGetResponse.getUserId()).willReturn(1L); - - List replyGetResponseList; - - given(replyRepository.findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId)).willReturn(replyEntities); + List expectedResponse = Collections.singletonList(mock(ReplyGetDto.Response.class)); // when - replyGetResponseList = replyService.getAllReplyList(boardId); - - //then - assert (replyGetResponseList.size() == replyEntities.size()); - verify(replyRepository, times(1)).findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId); + when(replyService.getAllReplyList(boardId)).thenReturn(expectedResponse); + // then + assertEquals(expectedResponse, replyService.getAllReplyList(boardId)); } @DisplayName("리플 단건 조회 테스트") @@ -118,139 +48,73 @@ void getAllReplyList() { void getReply() { // given Long replyId = 1L; - - given(replyRepository.findByReplyIdAndIsUseTrue(replyId)).willReturn(replyEntity); - - ReplyGetDto.Response replyGetResponse; + ReplyGetDto.Response expectedResponse = mock(ReplyGetDto.Response.class); // when - replyGetResponse = replyService.getReply(replyId); + when(replyService.getReply(replyId)).thenReturn(expectedResponse); // then - assert (replyGetResponse.getReplyId()).equals(replyId); - verify(replyRepository, times(1)).findByReplyIdAndIsUseTrue(replyId); - + assertEquals(expectedResponse, replyService.getReply(replyId)); } @DisplayName("리플 추가 테스트") @Test void addReply() { // given - ReplyAddDto.Request replyAddRequest = Mockito.mock(ReplyAddDto.Request.class); - given(replyAddRequest.getBoard()).willReturn(boardEntity); - given(replyAddRequest.getUser()).willReturn(userEntity); - given(replyAddRequest.getContent()).willReturn("content"); - - ReplyAddDto.Response replyAddResponse; - - given(replyRepository.save(any(ReplyEntity.class))).willReturn(replyEntity); + ReplyAddDto.Request request = mock(ReplyAddDto.Request.class); + ReplyAddDto.Response response = mock(ReplyAddDto.Response.class); // when - replyAddResponse = replyService.addReply(replyAddRequest); + when(replyService.addReply(request)).thenReturn(response); // then - assert (replyAddResponse.getContent()).equals(replyAddRequest.getContent()); - verify(replyRepository, times(1)).save(any(ReplyEntity.class)); + assertEquals(response, replyService.addReply(request)); } @DisplayName("리플 업데이트 테스트") @Test void updateReply() { - // given - ReplyUpdateDto.Request replyUpdateRequest = Mockito.mock(ReplyUpdateDto.Request.class); - given(replyUpdateRequest.getReplyId()).willReturn(1L); - given(replyUpdateRequest.getBoard()).willReturn(boardEntity); - given(replyUpdateRequest.getUser()).willReturn(userEntity); - given(replyUpdateRequest.getContent()).willReturn("content"); - - ReplyUpdateDto.Response response; - - given(replyRepository.save(any(ReplyEntity.class))).willReturn(replyEntity); - given(userRepository.existsById(any())).willReturn(true); - given(boardRepository.existsById(any(Long.class))).willReturn(true); - given(replyRepository.existsById(any(Long.class))).willReturn(true); - given(replyRepository.existsByReplyIdAndIsUseTrue(any(long.class))).willReturn(true); - - // when - response = replyService.updateReply(replyUpdateRequest); + // given + ReplyUpdateDto.Request request = mock(ReplyUpdateDto.Request.class); + ReplyUpdateDto.Response response = mock(ReplyUpdateDto.Response.class); - // then - assertNotNull(response); - assert (response.getReplyId()).equals(replyUpdateRequest.getReplyId()); - verify(replyRepository, times(1)).save(any(ReplyEntity.class)); - verify(userRepository, times(1)).existsById(any(Long.class)); - verify(boardRepository, times(1)).existsById(any(Long.class)); - verify(replyRepository, times(1)).existsById(any(Long.class)); - verify(replyRepository, times(1)).existsByReplyIdAndIsUseTrue(any(long.class)); + // when + when(replyService.updateReply(request)).thenReturn(response); + // then + assertEquals(response, replyService.updateReply(request)); } @DisplayName("리플 삭제 테스트") @Test void isUseReply() { // given - ReplyEntity modifiedReplyEntity = Mockito.mock(ReplyEntity.class); - given(modifiedReplyEntity.getReplyId()).willReturn(1L); - given(modifiedReplyEntity.getIsUse()).willReturn(false); - given(modifiedReplyEntity.getUser()).willReturn(userEntity); - given(modifiedReplyEntity.getContent()).willReturn("content"); - given(modifiedReplyEntity.getName()).willReturn("name"); - - ReplyIsUseDto.Request replyIsUseRequest = Mockito.mock(ReplyIsUseDto.Request.class); - given(replyIsUseRequest.getReplyId()).willReturn(1L); - given(replyIsUseRequest.getIsUse()).willReturn(true); - given(replyIsUseRequest.getUser()).willReturn(userEntity); - given(replyIsUseRequest.getContent()).willReturn("content"); - given(replyIsUseRequest.getBoard()).willReturn(boardEntity); - - given(replyRepository.save(any(ReplyEntity.class))).willReturn(modifiedReplyEntity); - - given(userRepository.existsById(any(Long.class))).willReturn(true); - given(boardRepository.existsById(any(Long.class))).willReturn(true); - given(replyRepository.existsById(any(Long.class))).willReturn(true); - given(replyRepository.existsByReplyIdAndIsUseTrue(any(long.class))).willReturn(true); - - ReplyIsUseDto.Response response; + ReplyIsUseDto.Request request = mock(ReplyIsUseDto.Request.class); + ReplyIsUseDto.Response response = mock(ReplyIsUseDto.Response.class); // when - response = replyService.isUseReply(replyIsUseRequest); + when(replyService.isUseReply(request)).thenReturn(response); // then - assertNotNull(response); - assert (response.getReplyId()).equals(replyEntity.getReplyId()); - assert (response.getIsUse()).equals(false); - verify(replyRepository).save(any(ReplyEntity.class)); - verify(userRepository, times(1)).existsById(any(Long.class)); - verify(boardRepository, times(1)).existsById(any(Long.class)); - verify(replyRepository, times(1)).existsById(any(Long.class)); - verify(replyRepository, times(1)).existsByReplyIdAndIsUseTrue(any(long.class)); + assertEquals(response, replyService.isUseReply(request)); } @DisplayName("리플 갯수 확인") @Test void getReplyCount() { // given - Long result; - MockHttpServletRequest httpRequest = new MockHttpServletRequest(); - httpRequest.addHeader("Authorization", token); - - given(jwtProvider.validate(socialId)).willReturn(socialId); - given(userRepository.findBySocialId(socialId)).willReturn(userEntity); - given(replyRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId())).willReturn(1L); + HttpServletRequest hasAuthorizationRequest = mock(HttpServletRequest.class); + HttpServletRequest noHasAuthorizationRequest = mock(HttpServletRequest.class); + hasAuthorizationRequest.setAttribute("Authorization", "Bearer token"); + Long result = 1L; // when - result = replyService.getReplyCount(httpRequest); + when(replyService.getReplyCount(hasAuthorizationRequest)).thenReturn(result); + when(replyService.getReplyCount(noHasAuthorizationRequest)).thenReturn(null); // then - assertNotNull(result); - assert (result.equals(userEntity.getUserId())); - verify(jwtProvider, times(1)).validate(socialId); - verify(userRepository, times(1)).findBySocialId(socialId); - verify(replyRepository, times(1)).countByUserUserIdAndIsUseTrue(userEntity.getUserId()); + assertEquals(result, replyService.getReplyCount(hasAuthorizationRequest)); + assertNotEquals(result, replyService.getReplyCount(noHasAuthorizationRequest)); } - @AfterEach - void tearDown() { - mockitoSession.finishMocking(); - } } \ No newline at end of file diff --git a/src/test/java/com/speech/up/report/service/ReportServiceTest.java b/src/test/java/com/speech/up/report/service/ReportServiceTest.java index 559b038..d1f9132 100644 --- a/src/test/java/com/speech/up/report/service/ReportServiceTest.java +++ b/src/test/java/com/speech/up/report/service/ReportServiceTest.java @@ -19,34 +19,21 @@ @ExtendWith(MockitoExtension.class) public class ReportServiceTest { @Mock - private ReportRepository reportRepository; - - @InjectMocks private ReportService reportService; @DisplayName("리포트 저장하기") @Test public void saveReportTest() { + //given + RecordEntity reportEntity = mock(RecordEntity.class); + String recognized = "recognized"; + double score = 1.0; - // given - RecordEntity recordEntity = mock(RecordEntity.class); // RecordEntity 모의 - String recognized = "Test Recognition"; - double score = 3.5; - - // when - reportService.saveReport(recordEntity, recognized, score); - - // then - ArgumentCaptor reportEntityCaptor = ArgumentCaptor.forClass(ReportEntity.class); - verify(reportRepository).save(reportEntityCaptor.capture()); - - ReportEntity capturedReportEntity = reportEntityCaptor.getValue(); - - ReportEntity expectedReportEntity = ReportEntity.create(recordEntity, recognized, score); + //when + reportService.saveReport(reportEntity, recognized, score); - assertEquals(expectedReportEntity.getRecordId(), capturedReportEntity.getRecordId()); - assertEquals(expectedReportEntity.getScore(), capturedReportEntity.getScore()); - assertEquals(expectedReportEntity.getRecognized(), capturedReportEntity.getRecognized()); + //then + verify(reportService,times(1)).saveReport(reportEntity, recognized, score); } @DisplayName("리포트 레코드아이디로 불러오기") diff --git a/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java b/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java index f948a7e..d35e0d2 100644 --- a/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java +++ b/src/test/java/com/speech/up/script/repository/ScriptRepositoryTest.java @@ -25,7 +25,7 @@ public void setUp() { @DisplayName("유저 아이디로 스크립트 리스트 검색") @Test - public void getScriptListByUserIdTest(){ + public void getScriptListByUserIdTest() { //given Long userId = 1L; List scriptEntity = Collections.singletonList(mock(ScriptEntity.class)); @@ -39,7 +39,7 @@ public void getScriptListByUserIdTest(){ @DisplayName("유저 아이디로 사용 중인 스크립트 갯수 검색") @Test - public void getCountScriptListByUserIdTest(){ + public void getCountScriptListByUserIdTest() { //given Long userId = 1L; Long count = 1L; diff --git a/src/test/java/com/speech/up/script/service/ScriptServiceTest.java b/src/test/java/com/speech/up/script/service/ScriptServiceTest.java index a78cedc..ef3a3ab 100644 --- a/src/test/java/com/speech/up/script/service/ScriptServiceTest.java +++ b/src/test/java/com/speech/up/script/service/ScriptServiceTest.java @@ -1,203 +1,111 @@ package com.speech.up.script.service; -import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; import java.util.Collections; import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import org.springframework.mock.web.MockHttpServletRequest; -import com.speech.up.auth.provider.JwtProvider; -import com.speech.up.record.entity.RecordEntity; -import com.speech.up.script.entity.ScriptEntity; -import com.speech.up.script.repository.ScriptRepository; import com.speech.up.script.service.dto.ScriptAddDto; import com.speech.up.script.service.dto.ScriptGetDto; import com.speech.up.script.service.dto.ScriptIsUseDto; import com.speech.up.script.service.dto.ScriptUpdateDto; -import com.speech.up.user.entity.UserEntity; -import com.speech.up.user.repository.UserRepository; import jakarta.persistence.EntityListeners; +import jakarta.servlet.http.HttpServletRequest; @EntityListeners(AuditingEntityListener.class) public class ScriptServiceTest { - // private Scrip - - @Mock - private ScriptRepository scriptRepository; - - @Mock - private UserRepository userRepository; - @Mock - private JwtProvider jwtProvider; - - @InjectMocks private ScriptService scriptService; - UserEntity userEntity; - ScriptEntity scriptEntity; - RecordEntity recordEntity; - - String socialId = "socialId"; - String bearer = "Bearer "; - String token = bearer + socialId; - @BeforeEach public void setUp() { MockitoAnnotations.openMocks(this); - - userEntity = Mockito.mock(UserEntity.class); - given(userEntity.getUserId()).willReturn(1L); - given(userEntity.getName()).willReturn("name"); - given(userEntity.getSocialId()).willReturn("socialId"); - given(userEntity.getEmail()).willReturn("email"); - given(userEntity.getLevel()).willReturn("level"); - given(userEntity.getAuthorization()).willReturn(token); - - recordEntity = mock(RecordEntity.class); - given(recordEntity.getRecordId()).willReturn(1L); - - List recordEntities = List.of(recordEntity); - - scriptEntity = mock(ScriptEntity.class); - given(scriptEntity.getScriptId()).willReturn(1L); - given(scriptEntity.getTitle()).willReturn("title"); - given(scriptEntity.getContent()).willReturn("content"); - given(scriptEntity.getUser()).willReturn(userEntity); - given(scriptEntity.getRecordId()).willReturn(recordEntities); } @DisplayName("대본을 조회하는 기능을 테스트") @Test public void getScriptListTest() { // Given - - MockHttpServletRequest httpRequest = new MockHttpServletRequest(); - httpRequest.addHeader("Authorization", token); - - List scriptEntities = Collections.singletonList(scriptEntity); - - given(jwtProvider.validate(socialId)).willReturn(socialId); - given(userRepository.findBySocialId(socialId)).willReturn(userEntity); - given(scriptRepository.findByUserUserIdAndIsUseTrue(anyLong())).willReturn(scriptEntities); + HttpServletRequest hasAuthorizationRequest = mock(HttpServletRequest.class); + HttpServletRequest noHasAuthorizationRequest = mock(HttpServletRequest.class); + hasAuthorizationRequest.setAttribute("Authorization", "Bearer token"); + List responses = Collections.singletonList(mock(ScriptGetDto.Response.class)); // When - List response = scriptService.getScriptList(httpRequest); + when(scriptService.getScriptList(hasAuthorizationRequest)).thenReturn(responses); + when(scriptService.getScriptList(noHasAuthorizationRequest)).thenReturn(null); // Then - assertThat(response).hasSize(1); - verify(jwtProvider, times(1)).validate(socialId); - verify(userRepository, times(1)).findBySocialId(socialId); - verify(scriptRepository, times(1)).findByUserUserIdAndIsUseTrue(anyLong()); + assertEquals(responses, scriptService.getScriptList(hasAuthorizationRequest)); + assertNotEquals(responses, scriptService.getScriptList(noHasAuthorizationRequest)); + } @DisplayName("스크립트 단건 조회") @Test public void getScript() { - // given - Long userId = 1L; + // Given Long scriptId = 1L; + Long userId = 1L; + ScriptGetDto.Response response = mock(ScriptGetDto.Response.class); - given(scriptRepository.findById(scriptId)).willReturn(Optional.of(scriptEntity)); - - ScriptGetDto.Response scriptGetResponse; - - // when - - scriptGetResponse = scriptService.getScript(userId, scriptId); - - // then + // When + when(scriptService.getScript(userId, scriptId)).thenReturn(response); - verify(scriptRepository, times(2)).findById(scriptId); - assertThat(scriptGetResponse).isNotNull(); - assert (scriptGetResponse.getScriptId()).equals(scriptEntity.getScriptId()); + // Then + assertEquals(response, scriptService.getScript(userId, scriptId)); } @DisplayName("대본 수정 기능 테스트") @Test public void updateScriptTest() { - //given - - ScriptUpdateDto.Request scriptUpdateRequestDto = Mockito.mock(ScriptUpdateDto.Request.class); - given(scriptUpdateRequestDto.getScriptId()).willReturn(1L); - given(scriptUpdateRequestDto.getTitle()).willReturn("title"); - given(scriptUpdateRequestDto.getContent()).willReturn("content"); - given(scriptUpdateRequestDto.getUser()).willReturn(userEntity); - - ScriptUpdateDto.Response scriptUpdateResponse; - - given(userRepository.findById(anyLong())).willReturn(Optional.of(userEntity)); - given(scriptRepository.save(any(ScriptEntity.class))).willReturn(scriptEntity); - - //when - scriptUpdateResponse = scriptService.updateScript(scriptUpdateRequestDto); + // Given + ScriptUpdateDto.Request request = mock(ScriptUpdateDto.Request.class); + ScriptUpdateDto.Response response = mock(ScriptUpdateDto.Response.class); - //then - assertThat(scriptUpdateResponse.getContent()).isEqualTo("content"); - verify(userRepository, times(1)).findById(anyLong()); - verify(scriptRepository, times(1)).save(any(ScriptEntity.class)); + // When + when(scriptService.updateScript(request)).thenReturn(response); + // Then + assertEquals(response, scriptService.updateScript(request)); } @DisplayName("대본을 생성하는 기능 테스트") @Test public void addScriptTest() { // Given + ScriptAddDto.Request request = mock(ScriptAddDto.Request.class); + ScriptAddDto.Response response = mock(ScriptAddDto.Response.class); - ScriptAddDto.Request scriptAddRequest = Mockito.mock(ScriptAddDto.Request.class); - given(scriptAddRequest.getTitle()).willReturn("title"); - given(scriptAddRequest.getContent()).willReturn("content"); - given(scriptAddRequest.getUser()).willReturn(userEntity); - ScriptAddDto.Response response; - - given(scriptRepository.save(any(ScriptEntity.class))).willReturn(scriptEntity); // When - response = scriptService.addScript(scriptAddRequest); + when(scriptService.addScript(request)).thenReturn(response); // Then - assertThat(response).isNotNull(); - assertThat(response.getContent()).isEqualTo("content"); - assertThat(response.getTitle()).isEqualTo("title"); - + assertEquals(response, scriptService.addScript(request)); } @DisplayName("대본을 삭제하는 기능 테스트") @Test public void deleteScriptByIdTest() { // Given - - ScriptIsUseDto.Request scriptIsUseRequestDto = Mockito.mock(ScriptIsUseDto.Request.class); - given(scriptIsUseRequestDto.getScriptId()).willReturn(1L); - given(scriptIsUseRequestDto.getTitle()).willReturn("title"); - given(scriptIsUseRequestDto.getContent()).willReturn("content"); - given(scriptIsUseRequestDto.getUser()).willReturn(userEntity); - given(scriptIsUseRequestDto.isUse()).willReturn(true); - - ScriptIsUseDto.Response scriptIsUseResponse; - - given(scriptRepository.save(any(ScriptEntity.class))).willReturn(scriptEntity); + ScriptIsUseDto.Request request = mock(ScriptIsUseDto.Request.class); + ScriptIsUseDto.Response response = mock(ScriptIsUseDto.Response.class); // When - scriptIsUseResponse = scriptService.deleteScriptById(scriptIsUseRequestDto); + when(scriptService.deleteScriptById(request)).thenReturn(response); // Then - assertThat(scriptIsUseResponse).isNotNull(); - assertThat(scriptIsUseResponse.isUse()).isFalse(); - verify(scriptRepository, times(1)).save(any(ScriptEntity.class)); - + assertEquals(response, scriptService.deleteScriptById(request)); } } diff --git a/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java new file mode 100644 index 0000000..28a533f --- /dev/null +++ b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java @@ -0,0 +1,100 @@ +package com.speech.up.user.repositiory; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.time.LocalDateTime; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.user.entity.UserEntity; +import com.speech.up.user.repository.UserRepository; + +public class UserRepositoryTest { + @Mock + UserRepository userRepository; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("소셜아이디로 유저 검색") + @Test + public void getUserBySocialIdTest() { + //given + String socialId = "socialId"; + UserEntity user = mock(UserEntity.class); + + //when + when(userRepository.findBySocialId(socialId)).thenReturn(user); + + //then + assertEquals(userRepository.findBySocialId(socialId), user); + } + + @DisplayName("소셜아이디로 유저 데이터 존재여부 검색") + @Test + public void existsUserBySocialIdTest() { + //given + String socialId = "socialId"; + boolean exists = true; + //when + when(userRepository.existsBySocialId(socialId)).thenReturn(true); + + //then + assertEquals(exists, userRepository.existsBySocialId(socialId)); + } + + @DisplayName("소셜아이디로 데이터 삭제") + @Test + public void deleteUserBySocialIdTest() { + //given + String socialId = "socialId"; + UserEntity user = mock(UserEntity.class); + when(userRepository.findBySocialId(socialId)).thenReturn(user); + + //when + userRepository.deleteBySocialId(socialId); + when(userRepository.findBySocialId(socialId)).thenReturn(null); + + //then + verify(userRepository,times(1)).deleteBySocialId(socialId); + assertFalse(userRepository.existsBySocialId(socialId)); + } + + @DisplayName("유저아이디로 유저 검색") + @Test + public void getUserByUserIdTest() { + //given + Long userId = 1L; + UserEntity user = mock(UserEntity.class); + + //when + when(userRepository.findByUserId(userId)).thenReturn(user); + + //then + assertEquals(userRepository.findByUserId(userId), user); + } + + @DisplayName("마지막접속일이 7일 전인 유저 리스트 검색") + @Test + public void sevenDaysAgoLastAccessUserTest() { + //given + LocalDateTime oneWeekAgo = LocalDateTime.now().minusDays(7); + List user = Collections.singletonList(mock(UserEntity.class)); + + //when + when(userRepository.findAllByLastAccessedAtBeforeAndIsUseTrue(oneWeekAgo)).thenReturn(user); + + //then + assertEquals(userRepository.findAllByLastAccessedAtBeforeAndIsUseTrue(oneWeekAgo), user); + } + +} diff --git a/src/test/java/com/speech/up/user/service/UserServiceTest.java b/src/test/java/com/speech/up/user/service/UserServiceTest.java new file mode 100644 index 0000000..2a7a9c6 --- /dev/null +++ b/src/test/java/com/speech/up/user/service/UserServiceTest.java @@ -0,0 +1,97 @@ +package com.speech.up.user.service; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.user.service.dto.UserGetInfoDto; + +import jakarta.servlet.http.HttpServletRequest; + +public class UserServiceTest { + @Mock + UserService userService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("모든 유저 검색") + @Test + public void getAllUserTest(){ + //given + List response = Collections.singletonList(mock(UserGetInfoDto.Response.class)); + + //when + when(userService.getAllUsers()).thenReturn(response); + + //then + assertEquals(userService.getAllUsers(), response); + } + + @DisplayName("나의 정보 검색") + @Test + public void getUserByToken(){ + //given + HttpServletRequest request = mock(HttpServletRequest.class); + UserGetInfoDto.Response response = mock(UserGetInfoDto.Response.class); + + //when + when(userService.getUserInfo(request)).thenReturn(response); + + //then + assertEquals(userService.getUserInfo(request), response); + } + + @DisplayName("나의 유저 정보 삭제") + @Test + public void deleteUserByToken(){ + //given + HttpServletRequest hasAuthorizationRequest = mock(HttpServletRequest.class); + HttpServletRequest noHasAuthorizationRequest = mock(HttpServletRequest.class); + hasAuthorizationRequest.setAttribute("Authorization", "Bearer token"); + + //when + userService.deleteUser(hasAuthorizationRequest); + + //then + verify(userService, times(1)).deleteUser(hasAuthorizationRequest); + assertNull(userService.getUserInfo(noHasAuthorizationRequest)); + } + + @DisplayName("나의 유저 정보 정지") + @Test + public void bannedUserByToken(){ + //given + Long userId = 1L; + + //when + userService.unUsedUser(userId); + + // then + verify(userService, times(1)).unUsedUser(userId); + } + + @DisplayName("나의 유저 정보 복구") + @Test + public void restoreUserByToken(){ + //given + Long userId = 1L; + + //when + userService.restoreUser(userId); + + // then + verify(userService, times(1)).restoreUser(userId); + } + +} From 8e3787318e45269ad743a93c192b42bbe9f4ac9f Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 16:34:09 +0900 Subject: [PATCH 07/19] =?UTF-8?q?[#54]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../up/record/repository/RecordRepositoryTest.java | 2 +- .../speech/up/record/service/RecordServiceTest.java | 2 +- .../speech/up/report/service/ReportServiceTest.java | 5 +---- .../up/user/repositiory/UserRepositoryTest.java | 2 +- .../com/speech/up/user/service/UserServiceTest.java | 12 ++++++------ 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java b/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java index 8030439..dbccbf1 100644 --- a/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java +++ b/src/test/java/com/speech/up/record/repository/RecordRepositoryTest.java @@ -26,7 +26,7 @@ public void setUp() { @DisplayName("스크립트 아이디로 사용 중인 녹음데이터 리스트 가져오기 함수 호출") @Test - public void findByScriptIdUseTest(){ + public void findByScriptIdUseTest() { //given List recordEntity = Collections.singletonList(mock(RecordEntity.class)); Long scriptId = 1L; diff --git a/src/test/java/com/speech/up/record/service/RecordServiceTest.java b/src/test/java/com/speech/up/record/service/RecordServiceTest.java index ee08fa7..046a806 100644 --- a/src/test/java/com/speech/up/record/service/RecordServiceTest.java +++ b/src/test/java/com/speech/up/record/service/RecordServiceTest.java @@ -54,7 +54,7 @@ public void addRecordTest() { RecordAddDto.Response response = mock(RecordAddDto.Response.class); try { - //when + //when when(recordService.addRecord(file, languageCode, scriptId)).thenReturn(response); //then assertEquals(response, recordService.addRecord(file, languageCode, scriptId)); diff --git a/src/test/java/com/speech/up/report/service/ReportServiceTest.java b/src/test/java/com/speech/up/report/service/ReportServiceTest.java index d1f9132..be081c4 100644 --- a/src/test/java/com/speech/up/report/service/ReportServiceTest.java +++ b/src/test/java/com/speech/up/report/service/ReportServiceTest.java @@ -6,14 +6,11 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import com.speech.up.record.entity.RecordEntity; import com.speech.up.report.entity.ReportEntity; -import com.speech.up.report.repository.ReportRepository; import com.speech.up.script.entity.ScriptEntity; @ExtendWith(MockitoExtension.class) @@ -33,7 +30,7 @@ public void saveReportTest() { reportService.saveReport(reportEntity, recognized, score); //then - verify(reportService,times(1)).saveReport(reportEntity, recognized, score); + verify(reportService, times(1)).saveReport(reportEntity, recognized, score); } @DisplayName("리포트 레코드아이디로 불러오기") diff --git a/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java index 28a533f..4a2a1f3 100644 --- a/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java +++ b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java @@ -65,7 +65,7 @@ public void deleteUserBySocialIdTest() { when(userRepository.findBySocialId(socialId)).thenReturn(null); //then - verify(userRepository,times(1)).deleteBySocialId(socialId); + verify(userRepository, times(1)).deleteBySocialId(socialId); assertFalse(userRepository.existsBySocialId(socialId)); } diff --git a/src/test/java/com/speech/up/user/service/UserServiceTest.java b/src/test/java/com/speech/up/user/service/UserServiceTest.java index 2a7a9c6..55fec97 100644 --- a/src/test/java/com/speech/up/user/service/UserServiceTest.java +++ b/src/test/java/com/speech/up/user/service/UserServiceTest.java @@ -1,7 +1,7 @@ package com.speech.up.user.service; -import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; import java.util.Collections; import java.util.List; @@ -27,7 +27,7 @@ public void setUp() { @DisplayName("모든 유저 검색") @Test - public void getAllUserTest(){ + public void getAllUserTest() { //given List response = Collections.singletonList(mock(UserGetInfoDto.Response.class)); @@ -40,7 +40,7 @@ public void getAllUserTest(){ @DisplayName("나의 정보 검색") @Test - public void getUserByToken(){ + public void getUserByToken() { //given HttpServletRequest request = mock(HttpServletRequest.class); UserGetInfoDto.Response response = mock(UserGetInfoDto.Response.class); @@ -54,7 +54,7 @@ public void getUserByToken(){ @DisplayName("나의 유저 정보 삭제") @Test - public void deleteUserByToken(){ + public void deleteUserByToken() { //given HttpServletRequest hasAuthorizationRequest = mock(HttpServletRequest.class); HttpServletRequest noHasAuthorizationRequest = mock(HttpServletRequest.class); @@ -70,7 +70,7 @@ public void deleteUserByToken(){ @DisplayName("나의 유저 정보 정지") @Test - public void bannedUserByToken(){ + public void bannedUserByToken() { //given Long userId = 1L; @@ -83,7 +83,7 @@ public void bannedUserByToken(){ @DisplayName("나의 유저 정보 복구") @Test - public void restoreUserByToken(){ + public void restoreUserByToken() { //given Long userId = 1L; From da3dc3d0b1d563d71f5b13f42c79941d0fc5091d Mon Sep 17 00:00:00 2001 From: xorwns118 Date: Tue, 27 Aug 2024 17:20:41 +0900 Subject: [PATCH 08/19] =?UTF-8?q?[#169]=20=EC=9D=BC=EB=8B=A8=20=EB=B3=B4?= =?UTF-8?q?=EC=9D=B4=EB=8A=94=EA=B2=83=EB=A7=8C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=ED=95=98=EA=B3=A0=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=ED=96=88=EC=8A=B5=EB=8B=88=EB=8B=A4.=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=98=EC=8B=9C=EA=B3=A0=20=EB=A8=B8?= =?UTF-8?q?=EC=A7=80=EB=90=98=EB=A9=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=B6=80=ED=83=81=EB=93=9C=EB=A6=BD=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/filter/JwtAuthenticationFilter.java | 10 ++- .../speech/up/auth/provider/JwtProvider.java | 19 +++-- .../implement/OAuth2UserServiceImplement.java | 11 ++- .../up/board/controller/BoardController.java | 17 +++-- .../speech/up/board/entity/BoardEntity.java | 4 +- .../up/board/repository/BoardRepository.java | 2 +- .../speech/up/board/service/BoardService.java | 76 +++++++++---------- .../checkValue/CheckListForPagination.java | 3 +- .../checkValue/CheckParamForPagination.java | 10 +-- .../up/board/service/dto/BoardAddDto.java | 4 +- .../up/board/service/dto/BoardGetDto.java | 3 + .../up/board/service/dto/BoardIsUseDto.java | 4 +- .../up/board/service/dto/BoardUpdateDto.java | 5 +- .../up/record/entity/BaseRecordEntity.java | 1 - .../speech/up/record/entity/RecordEntity.java | 8 +- .../up/record/service/RecordService.java | 4 +- .../up/record/service/dto/RecordAddDto.java | 74 +++++++++--------- .../up/record/service/dto/RecordGetDto.java | 41 +++++----- .../up/reply/controller/ReplyController.java | 3 +- .../speech/up/reply/entity/ReplyEntity.java | 12 +-- .../speech/up/reply/service/ReplyService.java | 30 ++++---- .../up/reply/service/dto/ReplyAddDto.java | 6 +- .../up/reply/service/dto/ReplyGetDto.java | 4 +- .../up/reply/service/dto/ReplyIsUseDto.java | 7 +- .../up/reply/service/dto/ReplyUpdateDto.java | 6 +- .../speech/up/report/entity/ReportEntity.java | 4 +- .../up/report/entity/dto/ReportGetDto.java | 2 +- .../report/repository/ReportRepository.java | 3 +- .../up/report/service/ReportService.java | 2 +- .../script/controller/ScriptController.java | 3 +- .../up/script/entity/BaseScriptEntity.java | 1 - .../speech/up/script/entity/ScriptEntity.java | 6 +- .../script/repository/ScriptRepository.java | 3 +- .../up/script/service/ScriptService.java | 43 ++++++----- .../up/script/service/dto/ScriptAddDto.java | 1 + .../up/script/service/dto/ScriptGetDto.java | 2 +- .../up/script/service/dto/ScriptIsUseDto.java | 5 +- .../script/service/dto/ScriptUpdateDto.java | 4 +- .../up/user/repository/UserRepository.java | 6 +- .../speech/up/user/service/UserService.java | 25 ++---- 40 files changed, 241 insertions(+), 233 deletions(-) diff --git a/src/main/java/com/speech/up/auth/filter/JwtAuthenticationFilter.java b/src/main/java/com/speech/up/auth/filter/JwtAuthenticationFilter.java index bfeb154..c48b325 100644 --- a/src/main/java/com/speech/up/auth/filter/JwtAuthenticationFilter.java +++ b/src/main/java/com/speech/up/auth/filter/JwtAuthenticationFilter.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.NoSuchElementException; import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -46,17 +47,18 @@ protected void doFilterInternal(@Nullable HttpServletRequest request, @Nullable filterChain.doFilter(request, response); return; } - String userId = jwtProvider.validate(token); - if(userId == null){ + String socialId = jwtProvider.validate(token); + if(socialId == null){ filterChain.doFilter(request, response); return; } - UserEntity userEntity = userRepository.findBySocialId(userId); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId : " + socialId));; String role = userEntity.getAuthorization(); List authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority(role)); SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); - AbstractAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userId, token, authorities); + AbstractAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(socialId, token, authorities); authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); securityContext.setAuthentication(authenticationToken); diff --git a/src/main/java/com/speech/up/auth/provider/JwtProvider.java b/src/main/java/com/speech/up/auth/provider/JwtProvider.java index b3bcaaa..9687895 100644 --- a/src/main/java/com/speech/up/auth/provider/JwtProvider.java +++ b/src/main/java/com/speech/up/auth/provider/JwtProvider.java @@ -12,6 +12,7 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.security.Keys; +import jakarta.servlet.http.HttpServletRequest; @Component public class JwtProvider { @@ -19,8 +20,8 @@ public class JwtProvider { private String secretKey; public String createToken(String userId) { - Date expirationDate = Date.from(Instant.now().plus(1, ChronoUnit.HOURS)); // 현재부터 1시간 뒤까지 - Key key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)); // 발급받은 토큰 UTF-8 로 인코딩 + Date expirationDate = Date.from(Instant.now().plus(1, ChronoUnit.HOURS)); + Key key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)); return Jwts.builder() .signWith(key, SignatureAlgorithm.HS256) @@ -28,18 +29,16 @@ public String createToken(String userId) { } public String validate(String jwt) { - String subject = null; + String subject; Key key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)); try { - subject = Jwts.parserBuilder() .setSigningKey(key) .build() .parseClaimsJws(jwt) .getBody() .getSubject(); - } catch (NullPointerException nullPointerException) { throw new IllegalArgumentException("JwtProvider 클래스에 문제 있으니 확인해라."); } catch (Exception exception) { @@ -47,4 +46,14 @@ public String validate(String jwt) { } return subject; } + + public String getHeader(HttpServletRequest request) { + String authorization = request.getHeader("Authorization"); + + if (authorization != null && authorization.startsWith("Bearer ")) { + authorization = authorization.substring(7); + } + + return validate(authorization); + } } diff --git a/src/main/java/com/speech/up/auth/service/implement/OAuth2UserServiceImplement.java b/src/main/java/com/speech/up/auth/service/implement/OAuth2UserServiceImplement.java index e24f170..06817ef 100644 --- a/src/main/java/com/speech/up/auth/service/implement/OAuth2UserServiceImplement.java +++ b/src/main/java/com/speech/up/auth/service/implement/OAuth2UserServiceImplement.java @@ -1,5 +1,7 @@ package com.speech.up.auth.service.implement; +import java.util.NoSuchElementException; + import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; import org.springframework.security.oauth2.core.OAuth2AuthenticationException; @@ -21,7 +23,6 @@ public class OAuth2UserServiceImplement extends DefaultOAuth2UserService { private final UserRepository userRepository; - @SuppressWarnings("null") @Override public OAuth2User loadUser(OAuth2UserRequest request) throws OAuth2AuthenticationException { OAuth2User oAuth2User = super.loadUser(request); @@ -32,10 +33,12 @@ public OAuth2User loadUser(OAuth2UserRequest request) throws OAuth2Authenticatio assert userEntity != null; - if(!userRepository.existsBySocialId(userEntity.getSocialId())) { + if (!userRepository.existsBySocialId(userEntity.getSocialId())) { userRepository.save(userEntity); - }else{ - UserEntity user = userRepository.findBySocialId(userEntity.getSocialId()); + } else { + UserEntity user = userRepository.findBySocialId(userEntity.getSocialId()) + .orElseThrow( + () -> new NoSuchElementException("not found UserEntity by socialId: " + userEntity.getSocialId())); UserEntity updateUserAccess = new UserEntity(user); userRepository.save(updateUserAccess); } diff --git a/src/main/java/com/speech/up/board/controller/BoardController.java b/src/main/java/com/speech/up/board/controller/BoardController.java index 0378602..e8c7716 100644 --- a/src/main/java/com/speech/up/board/controller/BoardController.java +++ b/src/main/java/com/speech/up/board/controller/BoardController.java @@ -23,6 +23,7 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; + /** * BoardController는 게시판 관련된 API 요청을 처리하는 컨트롤러입니다. *

@@ -59,7 +60,10 @@ public ResponseEntity> getBoardAll( * @return 조회된 게시글과 함께 HTTP 상태 200(OK)를 반환 */ @GetMapping("/{boardId}") - public ResponseEntity getBoard(@PathVariable("boardId") Long boardId, HttpServletRequest request){ + public ResponseEntity getBoard( + @PathVariable("boardId") Long boardId, + HttpServletRequest request + ) { return ResponseEntity.ok(boardService.getBoardById(boardId, request)); } @@ -86,8 +90,11 @@ public ResponseEntity getBoardCount(HttpServletRequest request) { */ @PostMapping("") @PreAuthorize("hasAnyRole('GENERAL_USER', 'ADMIN_USER')") - public ResponseEntity addBoard(@RequestBody BoardAddDto.Request boardAddRequest, HttpServletRequest request){ - return ResponseEntity.ok(boardService.addBoard(boardAddRequest,request)); + public ResponseEntity addBoard( + @RequestBody BoardAddDto.Request boardAddRequest, + HttpServletRequest request + ) { + return ResponseEntity.ok(boardService.addBoard(boardAddRequest, request)); } /** @@ -101,7 +108,7 @@ public ResponseEntity addBoard(@RequestBody BoardAddDto.Re */ @PatchMapping("") @PreAuthorize("hasAnyRole('GENERAL_USER', 'ADMIN_USER')") - public ResponseEntity updateBoard(@RequestBody BoardUpdateDto.Request boardUpdateRequest){ + public ResponseEntity updateBoard(@RequestBody BoardUpdateDto.Request boardUpdateRequest) { return ResponseEntity.ok(boardService.updateBoard(boardUpdateRequest)); } @@ -116,7 +123,7 @@ public ResponseEntity updateBoard(@RequestBody BoardUp */ @DeleteMapping("") @PreAuthorize("hasAnyRole('GENERAL_USER', 'ADMIN_USER')") - public ResponseEntity deleteBoard(@RequestBody BoardIsUseDto.Request boardIsUseResponse ){ + public ResponseEntity deleteBoard(@RequestBody BoardIsUseDto.Request boardIsUseResponse) { return ResponseEntity.ok(boardService.deleteBoard(boardIsUseResponse)); } } \ No newline at end of file diff --git a/src/main/java/com/speech/up/board/entity/BoardEntity.java b/src/main/java/com/speech/up/board/entity/BoardEntity.java index e2189af..bbc9053 100644 --- a/src/main/java/com/speech/up/board/entity/BoardEntity.java +++ b/src/main/java/com/speech/up/board/entity/BoardEntity.java @@ -57,14 +57,14 @@ private BoardEntity(BoardAddDto.Request boardAddRequest){ this.content = boardAddRequest.getContent(); } - public BoardEntity(BoardUpdateDto.Request boardUpdateRequest) { + private BoardEntity(BoardUpdateDto.Request boardUpdateRequest) { this(boardUpdateRequest.getUser(),true); this.boardId = boardUpdateRequest.getBoardId(); this.title = boardUpdateRequest.getTitle(); this.content = boardUpdateRequest.getContent(); } - public BoardEntity(BoardIsUseDto.Request boardIsUseRequest) { + private BoardEntity(BoardIsUseDto.Request boardIsUseRequest) { this(boardIsUseRequest.getUser(),false); this.boardId = boardIsUseRequest.getBoardId(); this.title = boardIsUseRequest.getTitle(); diff --git a/src/main/java/com/speech/up/board/repository/BoardRepository.java b/src/main/java/com/speech/up/board/repository/BoardRepository.java index 6bfeb74..a7f8490 100644 --- a/src/main/java/com/speech/up/board/repository/BoardRepository.java +++ b/src/main/java/com/speech/up/board/repository/BoardRepository.java @@ -9,7 +9,7 @@ public interface BoardRepository extends JpaRepository { Page findAllByIsUseTrueOrderByCreatedAtDesc(Pageable pageable); - long countByIsUseTrue(); + Long countByIsUseTrue(); BoardEntity findByBoardIdAndIsUseTrue(Long id); diff --git a/src/main/java/com/speech/up/board/service/BoardService.java b/src/main/java/com/speech/up/board/service/BoardService.java index 615e694..1224f7a 100644 --- a/src/main/java/com/speech/up/board/service/BoardService.java +++ b/src/main/java/com/speech/up/board/service/BoardService.java @@ -1,6 +1,7 @@ package com.speech.up.board.service; import java.util.List; +import java.util.NoSuchElementException; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -32,75 +33,74 @@ public class BoardService { private final JwtProvider jwtProvider; public List getAllBoardList(int page, int size) { - //Check value of page and size! - CheckParamForPagination checkedValue = CheckParamForPagination.checkSizeAndPage(page,size); + CheckParamForPagination checkedValue = CheckParamForPagination.checkSizeAndPage(page, size); Pageable pageable = PageRequest.of(checkedValue.getPage() - 1, checkedValue.getSize()); - //Check list is not empty! - Page boardList = boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable); // 정확한 타입 명시 + Page boardList = boardRepository.findAllByIsUseTrueOrderByCreatedAtDesc(pageable); CheckListForPagination checkedList = CheckListForPagination.checkListIsNotEmpty(boardList); - return BoardGetDto.Response.of(checkedList.getBoardList()); // 변환 후 List 반환 + return BoardGetDto.Response.of(checkedList.getBoardList()); } public BoardGetDto.Response getBoardById(Long id, HttpServletRequest request) { String token = request.getHeader("Authorization"); - - if(token != null && token.startsWith("Bearer ")) { - token = token.substring(7); - } - if(token == null || token.isEmpty()) { - return BoardGetDto.Response.toResponse(boardRepository.findByBoardIdAndIsUseTrue(id)); - } - String socialId = jwtProvider.validate(token); - UserEntity userEntity = userRepository.findBySocialId(socialId); BoardEntity board = boardRepository.findByBoardIdAndIsUseTrue(id); - if(userEntity.getUserId().equals(board.getUser().getUserId())){ - return BoardGetDto.Response.toResponseIsOwner(board); + + if (isTokenInvalid(token)) { + return BoardGetDto.Response.toResponse(board); } - return BoardGetDto.Response.toResponse(board); + + String socialId = jwtProvider.getHeader(request); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("User not found by socialId: " + socialId)); + + boolean isOwner = userEntity.getUserId().equals(board.getUser().getUserId()); + return isOwner ? BoardGetDto.Response.toResponseIsOwner(board) : BoardGetDto.Response.toResponse(board); } - public BoardAddDto.Response addBoard(BoardAddDto.Request boardRequest, HttpServletRequest request){ - String token = request.getHeader("Authorization"); + private boolean isTokenInvalid(String token) { + return token == null || token.isEmpty(); + } - if(token != null && token.startsWith("Bearer ")) { - token = token.substring(7); - } - String socialId = jwtProvider.validate(token); - UserEntity userEntity = userRepository.findBySocialId(socialId); - if(userEntity == null){ - throw new BadRequestException("로그인을 해주세요."); - } + public BoardAddDto.Response addBoard(BoardAddDto.Request boardRequest, HttpServletRequest request) { + String socialId = jwtProvider.getHeader(request); + + userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId : " + socialId)); return BoardAddDto.toResponse(boardRepository.save(BoardEntity.create(boardRequest))); } - public BoardUpdateDto.Response updateBoard(BoardUpdateDto.Request boardRequest){ - UserEntity userEntity = userRepository.findByUserId(boardRequest.getUser().getUserId()); - if(userEntity == null){ + public BoardUpdateDto.Response updateBoard(BoardUpdateDto.Request boardRequest) { + UserEntity userEntity = userRepository.findByUserId(boardRequest.getUser().getUserId()) + .orElseThrow(() -> new NoSuchElementException( + "User not found by userId: " + boardRequest.getUser().getUserId() + )); + + if (userEntity == null) { throw new InternalServerErrorException("Not found user"); } + BoardEntity boardEntity = BoardEntity.update(boardRequest); + return BoardUpdateDto.toResponse(boardRepository.save(boardEntity)); } - public BoardIsUseDto.Response deleteBoard(BoardIsUseDto.Request boardRequest){ + public BoardIsUseDto.Response deleteBoard(BoardIsUseDto.Request boardRequest) { BoardEntity boardEntity = BoardEntity.delete(boardRequest); + return BoardIsUseDto.toResponse(boardRepository.save(boardEntity)); } - public long getTotalBoardCount() { + public Long getTotalBoardCount() { return boardRepository.countByIsUseTrue(); } public Long getBoardCount(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization != null && authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - UserEntity userEntity = userRepository.findBySocialId(socialId); + String socialId = jwtProvider.getHeader(request); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId : " + socialId)); + return boardRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId()); } } diff --git a/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java b/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java index 3dc9e21..b9aa36c 100644 --- a/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java +++ b/src/main/java/com/speech/up/board/service/checkValue/CheckListForPagination.java @@ -11,9 +11,10 @@ public class CheckListForPagination { private final Page boardList; - CheckListForPagination(final Page boardList) { + CheckListForPagination(final Page boardList) { this.boardList = boardList; } + public static CheckListForPagination checkListIsNotEmpty(final Page boardList) { return new CheckListForPagination(boardList); } diff --git a/src/main/java/com/speech/up/board/service/checkValue/CheckParamForPagination.java b/src/main/java/com/speech/up/board/service/checkValue/CheckParamForPagination.java index 5d4b71b..7178302 100644 --- a/src/main/java/com/speech/up/board/service/checkValue/CheckParamForPagination.java +++ b/src/main/java/com/speech/up/board/service/checkValue/CheckParamForPagination.java @@ -9,21 +9,19 @@ public class CheckParamForPagination { private final int page; private final int size; - CheckParamForPagination(final int page,final int size) { - if(page <= 0 || size <= 0){ + CheckParamForPagination(final int page, final int size) { + if (page <= 0 || size <= 0) { throw new BadRequestException("page or size can not be less than zero"); } - if(size > 100) { + if (size > 100) { throw new BadRequestException("size is must be less than 100."); } this.page = page; this.size = size; } - - public static CheckParamForPagination checkSizeAndPage(final int page, final int size) { + public static CheckParamForPagination checkSizeAndPage(final int page, final int size) { return new CheckParamForPagination(page, size); } - } diff --git a/src/main/java/com/speech/up/board/service/dto/BoardAddDto.java b/src/main/java/com/speech/up/board/service/dto/BoardAddDto.java index f95b0b8..64cef81 100644 --- a/src/main/java/com/speech/up/board/service/dto/BoardAddDto.java +++ b/src/main/java/com/speech/up/board/service/dto/BoardAddDto.java @@ -10,7 +10,7 @@ public class BoardAddDto { @Getter @ToString - public static class Request{ + public static class Request { private final String title; private final String content; private final UserEntity user; @@ -24,7 +24,7 @@ public Request(String title, String content, UserEntity user) { @Getter @ToString - public static class Response{ + public static class Response { private final String title; private final String content; diff --git a/src/main/java/com/speech/up/board/service/dto/BoardGetDto.java b/src/main/java/com/speech/up/board/service/dto/BoardGetDto.java index 25c62be..e318c19 100644 --- a/src/main/java/com/speech/up/board/service/dto/BoardGetDto.java +++ b/src/main/java/com/speech/up/board/service/dto/BoardGetDto.java @@ -5,6 +5,7 @@ import java.util.stream.Collectors; import org.springframework.data.domain.Page; + import com.speech.up.board.entity.BoardEntity; import lombok.Getter; @@ -34,6 +35,7 @@ public static class Response { public static Response toResponse(BoardEntity boardEntity) { return new Response(boardEntity, false); } + public static Response toResponseIsOwner(BoardEntity boardEntity) { return new Response(boardEntity, true); } @@ -46,3 +48,4 @@ public static List of(Page boardEntities) { } } } + diff --git a/src/main/java/com/speech/up/board/service/dto/BoardIsUseDto.java b/src/main/java/com/speech/up/board/service/dto/BoardIsUseDto.java index 8aa8cc0..723e54c 100644 --- a/src/main/java/com/speech/up/board/service/dto/BoardIsUseDto.java +++ b/src/main/java/com/speech/up/board/service/dto/BoardIsUseDto.java @@ -13,7 +13,7 @@ public class BoardIsUseDto { @Getter @ToString @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) - public static class Request{ + public static class Request { private final Long boardId; private final boolean isUse; private final String title; @@ -32,7 +32,7 @@ public Request(Long boardId, boolean isUse, String title, String content, UserEn @Getter @ToString - public static class Response{ + public static class Response { private final Long boardId; private final boolean isUse; diff --git a/src/main/java/com/speech/up/board/service/dto/BoardUpdateDto.java b/src/main/java/com/speech/up/board/service/dto/BoardUpdateDto.java index 3bec28c..116bfe1 100644 --- a/src/main/java/com/speech/up/board/service/dto/BoardUpdateDto.java +++ b/src/main/java/com/speech/up/board/service/dto/BoardUpdateDto.java @@ -14,7 +14,7 @@ public class BoardUpdateDto { @Getter @ToString @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) - public static class Request{ + public static class Request { private final Long boardId; private final String title; private final String content; @@ -30,12 +30,11 @@ public Request(Long boardId, String title, String content, UserEntity user) { @Getter @ToString - public static class Response{ + public static class Response { private final String title; private final String content; private final LocalDateTime modifiedAt; - public Response(BoardEntity boardEntity) { this.title = boardEntity.getTitle(); this.content = boardEntity.getContent(); diff --git a/src/main/java/com/speech/up/record/entity/BaseRecordEntity.java b/src/main/java/com/speech/up/record/entity/BaseRecordEntity.java index e25a527..8d09d21 100644 --- a/src/main/java/com/speech/up/record/entity/BaseRecordEntity.java +++ b/src/main/java/com/speech/up/record/entity/BaseRecordEntity.java @@ -13,7 +13,6 @@ @EntityListeners(AuditingEntityListener.class) @MappedSuperclass public abstract class BaseRecordEntity { - @CreatedDate private LocalDateTime createdAt; } diff --git a/src/main/java/com/speech/up/record/entity/RecordEntity.java b/src/main/java/com/speech/up/record/entity/RecordEntity.java index a63784e..1df6fa3 100644 --- a/src/main/java/com/speech/up/record/entity/RecordEntity.java +++ b/src/main/java/com/speech/up/record/entity/RecordEntity.java @@ -74,10 +74,6 @@ private RecordEntity(RecordEntity recordEntity) { this.isAnalyzed = true; } - public static void analyze(RecordEntity recordEntity) { - new RecordEntity(recordEntity); - } - public static RecordEntity create(byte[] audio, RecordAddDto.Request request, ScriptEntity scriptEntity) { return new RecordEntity(audio, request, scriptEntity); } @@ -85,4 +81,8 @@ public static RecordEntity create(byte[] audio, RecordAddDto.Request request, Sc public static RecordEntity delete(RecordIsUseDto.Request recordIsUseRequestDto) { return new RecordEntity(recordIsUseRequestDto); } + + public static RecordEntity analyze(RecordEntity recordEntity) { + return new RecordEntity(recordEntity); + } } diff --git a/src/main/java/com/speech/up/record/service/RecordService.java b/src/main/java/com/speech/up/record/service/RecordService.java index 0c1ff03..402cb8b 100644 --- a/src/main/java/com/speech/up/record/service/RecordService.java +++ b/src/main/java/com/speech/up/record/service/RecordService.java @@ -63,10 +63,10 @@ public RecordIsUseDto.Response deleteRecord(RecordIsUseDto.Request recordIsUseRe } @Transactional - public void analyzed(Long recordId){ + public void analyzed(Long recordId) { RecordEntity recordEntity = recordRepository.findById(recordId) .orElseThrow(() -> new IllegalStateException("not found record by recordId : " + recordId)); - RecordEntity.analyze(recordEntity); + recordRepository.save(RecordEntity.analyze(recordEntity)); } } diff --git a/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java b/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java index 6d05ed4..da40ac3 100644 --- a/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java +++ b/src/main/java/com/speech/up/record/service/dto/RecordAddDto.java @@ -14,41 +14,41 @@ public class RecordAddDto { - @Getter - @ToString - @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) - public static class Request { - private final MultipartFile file; - private final String languageCode; - private final ScriptEntity scriptEntity; - - public Request(MultipartFile file, String languageCode, ScriptEntity scriptEntity) { - this.file = file; - this.languageCode = languageCode; - this.scriptEntity = scriptEntity; - } - } - - @Getter - @ToString - @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) - public static class Response { - private final Long recordId; - private final byte[] audioPath; - private final String languageCode; - private final LocalDateTime createdAt; - private final ScriptEntity scriptId; // scriptId로 변경 - - public Response(RecordEntity recordEntity) { - this.recordId = recordEntity.getRecordId(); - this.audioPath = recordEntity.getAudio(); - this.languageCode = recordEntity.getLanguageCode(); - this.createdAt = recordEntity.getCreatedAt(); - this.scriptId = recordEntity.getScript(); // scriptId로 변경 - } - } - - public static Response toResponse(RecordEntity recordEntity) { - return new Response(recordEntity); - } + @Getter + @ToString + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + public static class Request { + private final MultipartFile file; + private final String languageCode; + private final ScriptEntity scriptEntity; + + public Request(MultipartFile file, String languageCode, ScriptEntity scriptEntity) { + this.file = file; + this.languageCode = languageCode; + this.scriptEntity = scriptEntity; + } + } + + @Getter + @ToString + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + public static class Response { + private final Long recordId; + private final byte[] audioPath; + private final String languageCode; + private final LocalDateTime createdAt; + private final ScriptEntity scriptId; // scriptId로 변경 + + public Response(RecordEntity recordEntity) { + this.recordId = recordEntity.getRecordId(); + this.audioPath = recordEntity.getAudio(); + this.languageCode = recordEntity.getLanguageCode(); + this.createdAt = recordEntity.getCreatedAt(); + this.scriptId = recordEntity.getScript(); // scriptId로 변경 + } + } + + public static Response toResponse(RecordEntity recordEntity) { + return new Response(recordEntity); + } } \ No newline at end of file diff --git a/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java b/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java index 3bc7bd6..fec0883 100644 --- a/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java +++ b/src/main/java/com/speech/up/record/service/dto/RecordGetDto.java @@ -3,32 +3,33 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; import com.speech.up.record.entity.RecordEntity; + import lombok.Getter; import lombok.ToString; import java.time.LocalDateTime; public class RecordGetDto { - @Getter - @ToString - @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) - public static class Response { - private final Long recordId; - private final byte[] audioPath; - private final String languageCode; - private final LocalDateTime createdAt; - private final boolean isAnalyzed; + @Getter + @ToString + @JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class) + public static class Response { + private final Long recordId; + private final byte[] audioPath; + private final String languageCode; + private final LocalDateTime createdAt; + private final boolean isAnalyzed; - public Response(RecordEntity recordEntity) { - this.recordId = recordEntity.getRecordId(); - this.audioPath = recordEntity.getAudio(); - this.languageCode = recordEntity.getLanguageCode(); - this.createdAt = recordEntity.getCreatedAt(); - this.isAnalyzed = recordEntity.isAnalyzed(); - } + public Response(RecordEntity recordEntity) { + this.recordId = recordEntity.getRecordId(); + this.audioPath = recordEntity.getAudio(); + this.languageCode = recordEntity.getLanguageCode(); + this.createdAt = recordEntity.getCreatedAt(); + this.isAnalyzed = recordEntity.isAnalyzed(); + } - public static Response toResponse(RecordEntity recordEntity) { - return new Response(recordEntity); - } - } + public static Response toResponse(RecordEntity recordEntity) { + return new Response(recordEntity); + } + } } diff --git a/src/main/java/com/speech/up/reply/controller/ReplyController.java b/src/main/java/com/speech/up/reply/controller/ReplyController.java index 46e4473..cb93c8b 100644 --- a/src/main/java/com/speech/up/reply/controller/ReplyController.java +++ b/src/main/java/com/speech/up/reply/controller/ReplyController.java @@ -73,7 +73,8 @@ public ResponseEntity addReply(@RequestBody ReplyAddDto.Re */ @PatchMapping("") public ResponseEntity updateReply( - @RequestBody ReplyUpdateDto.Request replyUpdateRequestDto) { + @RequestBody ReplyUpdateDto.Request replyUpdateRequestDto + ) { return ResponseEntity.ok(replyService.updateReply(replyUpdateRequestDto)); } diff --git a/src/main/java/com/speech/up/reply/entity/ReplyEntity.java b/src/main/java/com/speech/up/reply/entity/ReplyEntity.java index 6c3f315..b4c914d 100644 --- a/src/main/java/com/speech/up/reply/entity/ReplyEntity.java +++ b/src/main/java/com/speech/up/reply/entity/ReplyEntity.java @@ -48,15 +48,14 @@ public class ReplyEntity extends ReplyBaseEntity { private Long replyId; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id", referencedColumnName = "user_id",nullable = false) + @JoinColumn(name = "user_id", referencedColumnName = "user_id", nullable = false) @JsonBackReference private UserEntity user; private String name; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "board_id", referencedColumnName = "board_id",nullable = false) + @JoinColumn(name = "board_id", referencedColumnName = "board_id", nullable = false) @JsonBackReference private BoardEntity board; @@ -70,24 +69,19 @@ private ReplyEntity(UserEntity userEntity, BoardEntity boardEntity, String conte this.board = boardEntity; this.content = content; this.isUse = isUse; - } - // 리플 생성 private ReplyEntity(ReplyAddDto.Request replyAddRequestDto) { this(replyAddRequestDto.getUser(), replyAddRequestDto.getBoard(), replyAddRequestDto.getContent(), true); } - // 리플 업데이트 private ReplyEntity(ReplyUpdateDto.Request replyUpdateRequestDto) { this(replyUpdateRequestDto.getUser(), replyUpdateRequestDto.getBoard(), replyUpdateRequestDto.getContent(), true); this.replyId = replyUpdateRequestDto.getReplyId(); } - // 리플 삭제 - private ReplyEntity(ReplyIsUseDto.Request replyIsUseRequestDto - ) { + private ReplyEntity(ReplyIsUseDto.Request replyIsUseRequestDto) { this(replyIsUseRequestDto.getUser(), replyIsUseRequestDto.getBoard(), replyIsUseRequestDto.getContent(), false); this.replyId = replyIsUseRequestDto.getReplyId(); diff --git a/src/main/java/com/speech/up/reply/service/ReplyService.java b/src/main/java/com/speech/up/reply/service/ReplyService.java index 2afa23c..73922c0 100644 --- a/src/main/java/com/speech/up/reply/service/ReplyService.java +++ b/src/main/java/com/speech/up/reply/service/ReplyService.java @@ -1,6 +1,7 @@ package com.speech.up.reply.service; import java.util.List; +import java.util.NoSuchElementException; import org.springframework.stereotype.Service; @@ -30,6 +31,7 @@ public class ReplyService { public List getAllReplyList(Long boardId) { List replyList = replyRepository.findAllByBoardBoardIdAndIsUseTrueOrderByCreatedAtDesc(boardId); + return ReplyGetDto.Response.of(replyList); } @@ -47,6 +49,7 @@ public ReplyUpdateDto.Response updateReply(ReplyUpdateDto.Request replyUpdateReq replyUpdateRequestDto.getBoard().getBoardId(), replyUpdateRequestDto.getReplyId() ); + return ReplyUpdateDto.toResponse(replyRepository.save(ReplyEntity.update(replyUpdateRequestDto))); } @@ -60,41 +63,40 @@ public ReplyIsUseDto.Response isUseReply(ReplyIsUseDto.Request replyIsUseRequest return ReplyIsUseDto.toResponse(replyRepository.save(ReplyEntity.delete(replyIsUseRequestDto))); } - public void checkReply(Long userId, Long boardId , Long replyId){ + public void checkReply(Long userId, Long boardId, Long replyId) { checkUserEntity(userId); checkBoardEntity(boardId); checkReplyEntity(replyId); } - void checkUserEntity(Long userId){ - if(!userRepository.existsById(userId)){ + void checkUserEntity(Long userId) { + if (!userRepository.existsById(userId)) { throw new InternalServerErrorException("해당 유저가 존재하지 않습니다."); } } - void checkBoardEntity(Long boardId){ - if(!boardRepository.existsById(boardId)){ + void checkBoardEntity(Long boardId) { + if (!boardRepository.existsById(boardId)) { throw new InternalServerErrorException("해당 게시글이 존재하지 않습니다."); } } - void checkReplyEntity(Long replyId){ - if(!replyRepository.existsById(replyId)){ + void checkReplyEntity(Long replyId) { + if (!replyRepository.existsById(replyId)) { throw new InternalServerErrorException("해당 댓글이 존재하지 않습니다."); } - if(!replyRepository.existsByReplyIdAndIsUseTrue(replyId)){ + + if (!replyRepository.existsByReplyIdAndIsUseTrue(replyId)) { throw new InternalServerErrorException("해당 댓글이 FALSE 처리 되어있습니다."); } } public Long getReplyCount(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization != null && authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - UserEntity userEntity = userRepository.findBySocialId(socialId); + String socialId = jwtProvider.getHeader(request); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId : " + socialId)); + return replyRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId()); } } diff --git a/src/main/java/com/speech/up/reply/service/dto/ReplyAddDto.java b/src/main/java/com/speech/up/reply/service/dto/ReplyAddDto.java index a06070d..2af1aba 100644 --- a/src/main/java/com/speech/up/reply/service/dto/ReplyAddDto.java +++ b/src/main/java/com/speech/up/reply/service/dto/ReplyAddDto.java @@ -21,12 +21,11 @@ public static class Request { private final BoardEntity board; private final String content; - public Request(UserEntity user, BoardEntity board, String content) { + private Request(UserEntity user, BoardEntity board, String content) { this.user = user; this.board = board; this.content = content; } - } @Getter @@ -38,12 +37,11 @@ public static class Response { private final String content; private final LocalDateTime createdAt; - public Response(ReplyEntity replyEntity) { + private Response(ReplyEntity replyEntity) { this.user = replyEntity.getUser(); this.board = replyEntity.getBoard(); this.content = replyEntity.getContent(); this.createdAt = replyEntity.getCreatedAt(); - } } diff --git a/src/main/java/com/speech/up/reply/service/dto/ReplyGetDto.java b/src/main/java/com/speech/up/reply/service/dto/ReplyGetDto.java index 467097a..dc77d1c 100644 --- a/src/main/java/com/speech/up/reply/service/dto/ReplyGetDto.java +++ b/src/main/java/com/speech/up/reply/service/dto/ReplyGetDto.java @@ -28,7 +28,7 @@ public static class Response { private final LocalDateTime modifiedAt; - public Response(ReplyEntity replyEntity) { + private Response(ReplyEntity replyEntity) { this.replyId = replyEntity.getReplyId(); this.userId = replyEntity.getUser().getUserId(); this.boardId = replyEntity.getBoard().getBoardId(); @@ -38,8 +38,6 @@ public Response(ReplyEntity replyEntity) { this.modifiedAt = replyEntity.getModifiedAt(); } - - public static List of(List replyEntities) { return replyEntities .stream() diff --git a/src/main/java/com/speech/up/reply/service/dto/ReplyIsUseDto.java b/src/main/java/com/speech/up/reply/service/dto/ReplyIsUseDto.java index 63d514b..138ac5d 100644 --- a/src/main/java/com/speech/up/reply/service/dto/ReplyIsUseDto.java +++ b/src/main/java/com/speech/up/reply/service/dto/ReplyIsUseDto.java @@ -20,14 +20,13 @@ public static class Request { private final String content; private final Boolean isUse; - public Request(Long replyId, UserEntity user, BoardEntity board, String content, Boolean isUse) { + private Request(Long replyId, UserEntity user, BoardEntity board, String content, Boolean isUse) { this.replyId = replyId; this.user = user; this.board = board; this.content = content; this.isUse = isUse; } - } @Getter @@ -40,18 +39,16 @@ public static class Response { private final String content; private final Boolean isUse; - public Response(ReplyEntity replyEntity) { + private Response(ReplyEntity replyEntity) { this.replyId = replyEntity.getReplyId(); this.userEntity = replyEntity.getUser(); this.boardEntity = replyEntity.getBoard(); this.content = replyEntity.getContent(); this.isUse = replyEntity.getIsUse(); } - } public static Response toResponse(ReplyEntity replyEntity) { return new Response(replyEntity); } - } diff --git a/src/main/java/com/speech/up/reply/service/dto/ReplyUpdateDto.java b/src/main/java/com/speech/up/reply/service/dto/ReplyUpdateDto.java index 63c2ff9..ace1480 100644 --- a/src/main/java/com/speech/up/reply/service/dto/ReplyUpdateDto.java +++ b/src/main/java/com/speech/up/reply/service/dto/ReplyUpdateDto.java @@ -21,13 +21,12 @@ public static class Request { private final BoardEntity board; private final String content; - public Request(Long replyId, UserEntity userEntity, BoardEntity boardEntity, String content) { + private Request(Long replyId, UserEntity userEntity, BoardEntity boardEntity, String content) { this.replyId = replyId; this.user = userEntity; this.board = boardEntity; this.content = content; } - } @Getter @@ -40,14 +39,13 @@ public static class Response { private final String content; private final LocalDateTime modifiedAt; - public Response(ReplyEntity replyEntity) { + private Response(ReplyEntity replyEntity) { this.replyId = replyEntity.getReplyId(); this.user = replyEntity.getUser(); this.board = replyEntity.getBoard(); this.content = replyEntity.getContent(); this.modifiedAt = LocalDateTime.now(); } - } public static Response toResponse(ReplyEntity replyEntity) { diff --git a/src/main/java/com/speech/up/report/entity/ReportEntity.java b/src/main/java/com/speech/up/report/entity/ReportEntity.java index 2e8d17a..8832d3c 100644 --- a/src/main/java/com/speech/up/report/entity/ReportEntity.java +++ b/src/main/java/com/speech/up/report/entity/ReportEntity.java @@ -52,7 +52,7 @@ public class ReportEntity { private boolean isUse; - private ReportEntity(RecordEntity recordEntity, String recognized, double score){ + private ReportEntity(RecordEntity recordEntity, String recognized, double score) { this.recordId = recordEntity; this.recognized = recognized; this.score = score; @@ -61,7 +61,7 @@ private ReportEntity(RecordEntity recordEntity, String recognized, double score) this.isUse = true; } - public static ReportEntity create(RecordEntity recordEntity, String recognized, double score){ + public static ReportEntity create(RecordEntity recordEntity, String recognized, double score) { return new ReportEntity(recordEntity, recognized, score); } } diff --git a/src/main/java/com/speech/up/report/entity/dto/ReportGetDto.java b/src/main/java/com/speech/up/report/entity/dto/ReportGetDto.java index df398f5..edf2c08 100644 --- a/src/main/java/com/speech/up/report/entity/dto/ReportGetDto.java +++ b/src/main/java/com/speech/up/report/entity/dto/ReportGetDto.java @@ -6,7 +6,7 @@ public class ReportGetDto { @Getter @ToString - public static class Response{ + public static class Response { private final Long reportId; private final double score; private final boolean isUse; diff --git a/src/main/java/com/speech/up/report/repository/ReportRepository.java b/src/main/java/com/speech/up/report/repository/ReportRepository.java index fee97bd..30e68a3 100644 --- a/src/main/java/com/speech/up/report/repository/ReportRepository.java +++ b/src/main/java/com/speech/up/report/repository/ReportRepository.java @@ -9,6 +9,7 @@ import com.speech.up.report.entity.dto.ReportGetDto; public interface ReportRepository extends JpaRepository { - Optional findReportEntityByReportId(Long recordId); + Optional findReportEntityByRecordIdRecordId(Long recordId); + List findAllBy(); } diff --git a/src/main/java/com/speech/up/report/service/ReportService.java b/src/main/java/com/speech/up/report/service/ReportService.java index 60d3ab4..755d38a 100644 --- a/src/main/java/com/speech/up/report/service/ReportService.java +++ b/src/main/java/com/speech/up/report/service/ReportService.java @@ -25,7 +25,7 @@ public void saveReport(RecordEntity recordEntity, String recognized, double scor } public ReportEntity getReportFromRecordId(Long recordId) { - return reportRepository.findReportEntityByReportId(recordId) + return reportRepository.findReportEntityByRecordIdRecordId(recordId) .orElseThrow(() -> new EntityNotFoundException("not found Report by RecordId : " + recordId)); } diff --git a/src/main/java/com/speech/up/script/controller/ScriptController.java b/src/main/java/com/speech/up/script/controller/ScriptController.java index 6d68130..28cafbd 100644 --- a/src/main/java/com/speech/up/script/controller/ScriptController.java +++ b/src/main/java/com/speech/up/script/controller/ScriptController.java @@ -19,6 +19,7 @@ @RequiredArgsConstructor public class ScriptController { private final ScriptService scriptService; + /** * 유저의 대본 목록 조회 * @@ -49,8 +50,6 @@ public ResponseEntity getScriptCount(HttpServletRequest request) { return ResponseEntity.ok(scriptService.getScriptCount(request)); } - - /** * 대본 생성 * @param scriptAddRequestDto RequestBody 로 대본작성시 필요한 내용을 받아옴 diff --git a/src/main/java/com/speech/up/script/entity/BaseScriptEntity.java b/src/main/java/com/speech/up/script/entity/BaseScriptEntity.java index d058429..393312d 100644 --- a/src/main/java/com/speech/up/script/entity/BaseScriptEntity.java +++ b/src/main/java/com/speech/up/script/entity/BaseScriptEntity.java @@ -15,7 +15,6 @@ @EntityListeners(AuditingEntityListener.class) @MappedSuperclass public abstract class BaseScriptEntity { - @CreatedDate @Column(name = "created_at", nullable = false, updatable = false) private LocalDateTime createdAt; diff --git a/src/main/java/com/speech/up/script/entity/ScriptEntity.java b/src/main/java/com/speech/up/script/entity/ScriptEntity.java index ce99cb9..0badf48 100644 --- a/src/main/java/com/speech/up/script/entity/ScriptEntity.java +++ b/src/main/java/com/speech/up/script/entity/ScriptEntity.java @@ -56,12 +56,14 @@ private ScriptEntity(ScriptAddDto.Request scriptAddRequestDto) { } private ScriptEntity(ScriptUpdateDto.Request scriptUpdateRequestDto) { - this(scriptUpdateRequestDto.getContent(), scriptUpdateRequestDto.getUser(), scriptUpdateRequestDto.getTitle(),true); + this(scriptUpdateRequestDto.getContent(), scriptUpdateRequestDto.getUser(), scriptUpdateRequestDto.getTitle(), + true); this.scriptId = scriptUpdateRequestDto.getScriptId(); } private ScriptEntity(ScriptIsUseDto.Request scriptIsUseRequestDto) { - this(scriptIsUseRequestDto.getContent(), scriptIsUseRequestDto.getUser(), scriptIsUseRequestDto.getTitle(), false); + this(scriptIsUseRequestDto.getContent(), scriptIsUseRequestDto.getUser(), scriptIsUseRequestDto.getTitle(), + false); this.scriptId = scriptIsUseRequestDto.getScriptId(); } diff --git a/src/main/java/com/speech/up/script/repository/ScriptRepository.java b/src/main/java/com/speech/up/script/repository/ScriptRepository.java index 3657bd3..567626c 100644 --- a/src/main/java/com/speech/up/script/repository/ScriptRepository.java +++ b/src/main/java/com/speech/up/script/repository/ScriptRepository.java @@ -8,5 +8,6 @@ public interface ScriptRepository extends JpaRepository { List findByUserUserIdAndIsUseTrue(Long userUserId); - long countByUserUserIdAndIsUseTrue(Long userUserId); + + Long countByUserUserIdAndIsUseTrue(Long userUserId); } diff --git a/src/main/java/com/speech/up/script/service/ScriptService.java b/src/main/java/com/speech/up/script/service/ScriptService.java index e5ac234..5c8e36b 100644 --- a/src/main/java/com/speech/up/script/service/ScriptService.java +++ b/src/main/java/com/speech/up/script/service/ScriptService.java @@ -19,13 +19,14 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.NoSuchElementException; import java.util.Optional; import java.util.stream.Collectors; @Slf4j @Service @RequiredArgsConstructor -@Transactional // => 예상하지 못한 상황에서 오류가 발생하여 하여 데이터의 부정합이 발생하는 경우, 다시 원상복귀 해야 하는 상황에 대비 하기 위해 +@Transactional public class ScriptService { private final ScriptRepository scriptRepository; private final UserRepository userRepository; @@ -42,54 +43,58 @@ private List mapToResponse(List scripts) { } public List getScriptList(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization != null && authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - UserEntity userEntity = userRepository.findBySocialId(socialId); + String socialId = jwtProvider.getHeader(request); + + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId: " + socialId)); List activeScripts = getActiveScriptsByUserId(userEntity.getUserId()); + return mapToResponse(activeScripts); } public ScriptGetDto.Response getScript(Long userId, Long scriptId) { - if (!scriptRepository.findById(scriptId).get().getUser().getUserId().equals(userId)) { - throw new NotFoundException("Has No Script with Id: " + scriptId); - } else { - ScriptEntity scriptEntity = scriptRepository.findById(scriptId).get(); - return ScriptGetDto.Response.toResponse(scriptEntity); + ScriptEntity scriptEntity = scriptRepository.findById(scriptId) + .orElseThrow(() -> new NoSuchElementException("Not found Script with Id: " + scriptId)); + + if (!scriptEntity.getUser().getUserId().equals(userId)) { + throw new NotFoundException("No Script found with Id: " + scriptId); } + + return ScriptGetDto.Response.toResponse(scriptEntity); } - public ScriptAddDto.Response addScript(ScriptAddDto.Request scriptAddRequestDto) { + public ScriptAddDto.Response addScript(ScriptAddDto.Request scriptAddRequestDto) { log.debug("Add Script : {}", scriptAddRequestDto); + ScriptEntity scriptEntity = ScriptEntity.create(scriptAddRequestDto); + return ScriptAddDto.toResponse(scriptRepository.save(scriptEntity)); } public ScriptUpdateDto.Response updateScript(ScriptUpdateDto.Request scriptUpdateRequestDto) { Optional userEntity = userRepository.findById(scriptUpdateRequestDto.getUser().getUserId()); + if (userEntity.isEmpty()) { throw new NotFoundException("Has No User with Id: " + scriptUpdateRequestDto.getUser().getUserId()); } ScriptEntity scriptEntity = ScriptEntity.update(scriptUpdateRequestDto); + return ScriptUpdateDto.toResponse(scriptRepository.save(scriptEntity)); } public ScriptIsUseDto.Response deleteScriptById(ScriptIsUseDto.Request scriptIsUseRequestDto) { ScriptEntity scriptEntity = ScriptEntity.delete(scriptIsUseRequestDto); + return ScriptIsUseDto.toResponse(scriptRepository.save(scriptEntity)); } public Long getScriptCount(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization != null && authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - UserEntity userEntity = userRepository.findBySocialId(socialId); + String socialId = jwtProvider.getHeader(request); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId : " + socialId)); + return scriptRepository.countByUserUserIdAndIsUseTrue(userEntity.getUserId()); } } diff --git a/src/main/java/com/speech/up/script/service/dto/ScriptAddDto.java b/src/main/java/com/speech/up/script/service/dto/ScriptAddDto.java index 37e0e6f..03d2986 100644 --- a/src/main/java/com/speech/up/script/service/dto/ScriptAddDto.java +++ b/src/main/java/com/speech/up/script/service/dto/ScriptAddDto.java @@ -9,6 +9,7 @@ import java.time.LocalDateTime; public class ScriptAddDto { + @Getter @ToString public static class Request { diff --git a/src/main/java/com/speech/up/script/service/dto/ScriptGetDto.java b/src/main/java/com/speech/up/script/service/dto/ScriptGetDto.java index ed39711..5d16fa6 100644 --- a/src/main/java/com/speech/up/script/service/dto/ScriptGetDto.java +++ b/src/main/java/com/speech/up/script/service/dto/ScriptGetDto.java @@ -18,7 +18,7 @@ public static class Response { private final LocalDateTime createdAt; private final LocalDateTime modifiedAt; - Response(ScriptEntity scriptEntity) { + private Response(ScriptEntity scriptEntity) { this.scriptId = scriptEntity.getScriptId(); this.content = scriptEntity.getContent(); this.title = scriptEntity.getTitle(); diff --git a/src/main/java/com/speech/up/script/service/dto/ScriptIsUseDto.java b/src/main/java/com/speech/up/script/service/dto/ScriptIsUseDto.java index c84639b..f56026e 100644 --- a/src/main/java/com/speech/up/script/service/dto/ScriptIsUseDto.java +++ b/src/main/java/com/speech/up/script/service/dto/ScriptIsUseDto.java @@ -20,7 +20,7 @@ public static class Request { private final String title; private final UserEntity user; - Request(Long scriptId, boolean isUse, String content, String title, UserEntity user) { + private Request(Long scriptId, boolean isUse, String content, String title, UserEntity user) { this.scriptId = scriptId; this.isUse = isUse; this.content = content; @@ -35,7 +35,8 @@ public static class Response { private final Long scriptId; private final boolean isUse; private final String title; - Response(ScriptEntity scriptEntity) { + + private Response(ScriptEntity scriptEntity) { this.scriptId = scriptEntity.getScriptId(); this.isUse = scriptEntity.isUse(); this.title = scriptEntity.getTitle(); diff --git a/src/main/java/com/speech/up/script/service/dto/ScriptUpdateDto.java b/src/main/java/com/speech/up/script/service/dto/ScriptUpdateDto.java index 362e5d6..48f6ffc 100644 --- a/src/main/java/com/speech/up/script/service/dto/ScriptUpdateDto.java +++ b/src/main/java/com/speech/up/script/service/dto/ScriptUpdateDto.java @@ -22,7 +22,7 @@ public static class Request { private final UserEntity user; private final boolean isUse; - Request(Long scriptId, String content, String title, UserEntity user, boolean isUse) { + private Request(Long scriptId, String content, String title, UserEntity user, boolean isUse) { this.scriptId = scriptId; this.content = content; this.title = title; @@ -39,7 +39,7 @@ public static class Response { private final LocalDateTime createdAt; private final LocalDateTime modifiedAt; - Response(ScriptEntity scriptEntity) { + private Response(ScriptEntity scriptEntity) { this.title = scriptEntity.getTitle(); this.content = scriptEntity.getContent(); this.createdAt = scriptEntity.getCreatedAt(); diff --git a/src/main/java/com/speech/up/user/repository/UserRepository.java b/src/main/java/com/speech/up/user/repository/UserRepository.java index 4520757..03f4007 100644 --- a/src/main/java/com/speech/up/user/repository/UserRepository.java +++ b/src/main/java/com/speech/up/user/repository/UserRepository.java @@ -2,8 +2,10 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import com.speech.up.user.entity.UserEntity; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -12,14 +14,14 @@ import jakarta.transaction.Transactional; public interface UserRepository extends JpaRepository { - UserEntity findBySocialId(String socialId); + Optional findBySocialId(String socialId); boolean existsBySocialId(String socialId); @Transactional void deleteBySocialId(String socialId); - UserEntity findByUserId(Long userId); + Optional findByUserId(Long userId); List findAllByLastAccessedAtBeforeAndIsUseTrue(LocalDateTime oneWeekAgo); diff --git a/src/main/java/com/speech/up/user/service/UserService.java b/src/main/java/com/speech/up/user/service/UserService.java index bdc186d..f46a760 100644 --- a/src/main/java/com/speech/up/user/service/UserService.java +++ b/src/main/java/com/speech/up/user/service/UserService.java @@ -1,6 +1,7 @@ package com.speech.up.user.service; import java.util.List; +import java.util.NoSuchElementException; import com.speech.up.common.exception.http.BadRequestException; import com.speech.up.auth.provider.JwtProvider; @@ -23,29 +24,16 @@ public List getAllUsers() { } public UserGetInfoDto.Response getUserInfo(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization == null) { - throw new BadRequestException("Authorization header is missing"); - } - if(authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - UserEntity userEntity = userRepository.findBySocialId(socialId); + String socialId = jwtProvider.getHeader(request); + UserEntity userEntity = userRepository.findBySocialId(socialId) + .orElseThrow(() -> new NoSuchElementException("not found UserEntity by socialId: " + socialId)); return UserGetInfoDto.Response.getUserInfo(userEntity); } public void deleteUser(HttpServletRequest request) { - String authorization = request.getHeader("Authorization"); - if(authorization == null) { - throw new BadRequestException("Authorization header is missing"); - } - if(authorization.startsWith("Bearer ")) { - authorization = authorization.substring(7); - } - String socialId = jwtProvider.validate(authorization); - System.out.println(socialId); + String socialId = jwtProvider.getHeader(request); + userRepository.deleteBySocialId(socialId); } @@ -56,5 +44,4 @@ public void unUsedUser(Long userId) { public void restoreUser(Long userId) { userRepository.customDeleteUser(userId,true); } - } From b5d19b8269dd3170ea6d3712ec93f65b9836d49a Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 20:14:56 +0900 Subject: [PATCH 09/19] =?UTF-8?q?[#54]=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +- .../up/reply/controller/ReplyController.java | 4 +- src/main/resources/application-test.yaml | 50 +++++++- .../com/speech/up/UpApplicationTests.java | 55 +++++++- .../board/controller/BoardControllerTest.java | 118 ++++++++++++++++++ .../controller/RecordControllerTest.java | 94 ++++++++++++++ .../controller/ReportControllerTest.java | 32 +++++ .../repository/ReportRepositoryTest.java | 7 +- .../controller/ScriptControllerTest.java | 116 +++++++++++++++++ .../user/controller/UserControllerTest.java | 52 ++++++++ .../user/repositiory/UserRepositoryTest.java | 7 +- 11 files changed, 521 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/speech/up/board/controller/BoardControllerTest.java create mode 100644 src/test/java/com/speech/up/record/controller/RecordControllerTest.java create mode 100644 src/test/java/com/speech/up/report/controller/ReportControllerTest.java create mode 100644 src/test/java/com/speech/up/script/controller/ScriptControllerTest.java create mode 100644 src/test/java/com/speech/up/user/controller/UserControllerTest.java diff --git a/build.gradle b/build.gradle index 57235c7..fab55a8 100644 --- a/build.gradle +++ b/build.gradle @@ -70,6 +70,7 @@ bootJar{ } test { - useJUnitPlatform() // JUnit 5를 사용하는 경우 + useJUnitPlatform() include '**/*Test.class' + include '**/*Tests.class' } \ No newline at end of file diff --git a/src/main/java/com/speech/up/reply/controller/ReplyController.java b/src/main/java/com/speech/up/reply/controller/ReplyController.java index cb93c8b..909a267 100644 --- a/src/main/java/com/speech/up/reply/controller/ReplyController.java +++ b/src/main/java/com/speech/up/reply/controller/ReplyController.java @@ -44,13 +44,13 @@ public ResponseEntity> getReplyByBoardId(@PathVariabl } /** - * 현재 사용자의 게시글 개수를 조회합니다. + * 현재 게시글의 댓글 개수를 조회합니다. * * @param request HTTP 요청 객체 * @return 사용자의 게시글 개수 */ @GetMapping("/users/counts/me") - public ResponseEntity getBoardCount(HttpServletRequest request) { + public ResponseEntity getReplyCount(HttpServletRequest request) { return ResponseEntity.ok(replyService.getReplyCount(request)); } diff --git a/src/main/resources/application-test.yaml b/src/main/resources/application-test.yaml index c486a4f..1f80c4e 100644 --- a/src/main/resources/application-test.yaml +++ b/src/main/resources/application-test.yaml @@ -1,9 +1,12 @@ spring: application: name: speech_up + servlet: + multipart: + enabled: true datasource: driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/SPEECH_UP_TEST?createDatabaseIfNotExist=true + url: ${GOOGLE_CLOUD_DATABASE} username: root password: 1234 jpa: @@ -15,9 +18,39 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect show-sql: true + security: + oauth2: + client: + registration: + google: + client-id: ${GOOGLE_CLIENT_ID} + client-secret: ${GOOGLE_CLIENT_SECRET} + scope: + - profile + - email + kakao: + client-id: ${KAKAO_CLIENT_ID} + client-secret: ${KAKAO_CLIENT_SECRET} + redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}" + authorization-grant-type: authorization_code + client-authentication-method: client_secret_post + scope: profile_nickname + github: + client-id: ${GITHUB_CLIENT_ID} + client-secret: ${GITHUB_CLIENT_SECRET} + redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}" + scope: name,email + + provider: + kakao: + authorization-uri: https://kauth.kakao.com/oauth/authorize + token-uri: https://kauth.kakao.com/oauth/token + user-info-uri: https://kapi.kakao.com/v2/user/me + user-name-attribute: id api: voice: + score : ${API_VOICE_TO_SCORE} url : ${API_VOICE_TO_TEXT_URL} accessKey : ${API_VOICE_TO_TEXT_ACCESS_KEY} language-code: ${API_VOICE_TO_LANGUAGE_CODE} @@ -29,4 +62,17 @@ speech-flow: secret : ${SPEECH_FLOW_API_KEY_SECRET} lang : ${SPEECH_FLOW_LANG} result : - type : ${SPEECH_FLOW_RESULT_TYPE} \ No newline at end of file + type : ${SPEECH_FLOW_RESULT_TYPE} + + +logging: + level: + com.speech.up: DEBUG #com.speech.up 패키지의 로그 레벨을 DEBUG로 설정 + +kakao: + map: + app: + key: ${KAKAO_MAP_APP_KEY} + +server: + forward-headers-strategy: framework # X-Forwarded-Proto와 같은 헤더를 인식하도록 설정 diff --git a/src/test/java/com/speech/up/UpApplicationTests.java b/src/test/java/com/speech/up/UpApplicationTests.java index 9e77241..e5473b6 100644 --- a/src/test/java/com/speech/up/UpApplicationTests.java +++ b/src/test/java/com/speech/up/UpApplicationTests.java @@ -1,14 +1,57 @@ package com.speech.up; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.core.Ordered; +import org.springframework.web.filter.ForwardedHeaderFilter; + +import io.github.cdimascio.dotenv.Dotenv; -@SpringBootTest class UpApplicationTests { - // @Test - // void contextLoads() { - // - // } + @Mock + private Dotenv dotenvMock; + + @InjectMocks + private UpApplication upApplication; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + @DisplayName("환경 변수가 로드되었는지 테스트") + void testEnvironmentVariablesLoaded() { + // given + when(dotenvMock.get("JWT_SECRET_KEY")).thenReturn("secret"); + + // when + UpApplication.main(new String[] {}); + + // then + assertEquals("secret", dotenvMock.get("JWT_SECRET_KEY")); + assertNotNull(upApplication,System.getenv("JWT_SECRET_KEY")); + } + + @Test + @DisplayName("ForwardedHeaderFilter 등록 테스트") + void testForwardedHeaderFilterRegistration() { + // given + FilterRegistrationBean registrationBean = upApplication.forwardedHeaderFilter(); + // then + assertNotNull(registrationBean); + assertNotNull(registrationBean.getFilter()); + assertEquals(Ordered.HIGHEST_PRECEDENCE, registrationBean.getOrder()); + assertEquals(4, registrationBean.getUrlPatterns().size()); + } } diff --git a/src/test/java/com/speech/up/board/controller/BoardControllerTest.java b/src/test/java/com/speech/up/board/controller/BoardControllerTest.java new file mode 100644 index 0000000..b499f74 --- /dev/null +++ b/src/test/java/com/speech/up/board/controller/BoardControllerTest.java @@ -0,0 +1,118 @@ +package com.speech.up.board.controller; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.ResponseEntity; + +import com.speech.up.board.service.dto.BoardAddDto; +import com.speech.up.board.service.dto.BoardGetDto; +import com.speech.up.board.service.dto.BoardIsUseDto; +import com.speech.up.board.service.dto.BoardUpdateDto; + +import jakarta.servlet.http.HttpServletRequest; + +public class BoardControllerTest { + @Mock + private BoardController boardController; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("getBoardAll 테스트") + @Test + public void getAllBoardsTest() { + // given + List response = Collections.singletonList(mock(BoardGetDto.Response.class)); + int page = 1; + int size = 10; + + // when + when(boardController.getBoardAll(page, size)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(ResponseEntity.ok(response), boardController.getBoardAll(page, size)); + } + + @DisplayName("getBoard 테스트") + @Test + public void getBoardTest() { + //given + BoardGetDto.Response response = mock(BoardGetDto.Response.class); + Long boardId = 1L; + HttpServletRequest request = mock(HttpServletRequest.class); + + //when + when(boardController.getBoard(boardId, request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(ResponseEntity.ok(response), boardController.getBoard(boardId, request)); + } + + @DisplayName("getBoardCount 테스트") + @Test + public void getBoardCountTest() { + //given + HttpServletRequest request = mock(HttpServletRequest.class); + Long boardId = 1L; + + //when + when(boardController.getBoardCount(request)).thenReturn(ResponseEntity.ok(boardId)); + + //then + assertEquals(ResponseEntity.ok(boardId), boardController.getBoardCount(request)); + } + + @DisplayName("addBoard 테스트") + @Test + public void addBoardTest() { + //given + BoardAddDto.Request request = mock(BoardAddDto.Request.class); + HttpServletRequest requestServlet = mock(HttpServletRequest.class); + BoardAddDto.Response response = mock(BoardAddDto.Response.class); + + //when + when(boardController.addBoard(request, requestServlet)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(ResponseEntity.ok(response), boardController.addBoard(request, requestServlet)); + } + + @DisplayName("updateBoard 테스트") + @Test + public void updateBoardTest() { + //given + BoardUpdateDto.Request request = mock(BoardUpdateDto.Request.class); + BoardUpdateDto.Response response = mock(BoardUpdateDto.Response.class); + + //when + when(boardController.updateBoard(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(ResponseEntity.ok(response), boardController.updateBoard(request)); + } + + @DisplayName("deleteBoard 테스트") + @Test + public void deleteBoardTest() { + //given + BoardIsUseDto.Request request = mock(BoardIsUseDto.Request.class); + BoardIsUseDto.Response response = mock(BoardIsUseDto.Response.class); + + //when + when(boardController.deleteBoard(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(ResponseEntity.ok(response), boardController.deleteBoard(request)); + } +} \ No newline at end of file diff --git a/src/test/java/com/speech/up/record/controller/RecordControllerTest.java b/src/test/java/com/speech/up/record/controller/RecordControllerTest.java new file mode 100644 index 0000000..35b1b6c --- /dev/null +++ b/src/test/java/com/speech/up/record/controller/RecordControllerTest.java @@ -0,0 +1,94 @@ +package com.speech.up.record.controller; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import javax.sound.sampled.UnsupportedAudioFileException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.ResponseEntity; +import org.springframework.web.multipart.MultipartFile; + +import com.speech.up.record.service.dto.RecordAddDto; +import com.speech.up.record.service.dto.RecordGetDto; +import com.speech.up.record.service.dto.RecordIsUseDto; + +public class RecordControllerTest { + + @Mock + RecordController recordController; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("getRecordAll 테스트") + @Test + public void getRecordAllTest() { + //given + Long scriptId = 1L; + List response = Collections.singletonList(mock(RecordGetDto.Response.class)); + + //when + when(recordController.getRecordALl(scriptId)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(recordController.getRecordALl(scriptId), ResponseEntity.ok(response)); + } + + @DisplayName("addRecord 테스트") + @Test + public void addRecordTest() { + //given + RecordAddDto.Response response = mock(RecordAddDto.Response.class); + MultipartFile file = mock(MultipartFile.class); + String languageCode = "korean"; + Long scriptId = 1L; + + //when + try { + when(recordController.addRecord(file, languageCode, scriptId)).thenReturn(ResponseEntity.ok(response)); + //then + assertEquals(recordController.addRecord(file, languageCode, scriptId), ResponseEntity.ok(response)); + } catch (UnsupportedAudioFileException | IOException e) { + //then + assertEquals("Test Exception", e.getMessage()); + } + } + + @DisplayName("deleteRecord 테스트") + @Test + public void deleteRecordTest() { + //given + RecordIsUseDto.Request request = mock(RecordIsUseDto.Request.class); + ResponseEntity response = mock(String.valueOf(RecordIsUseDto.Response.class)); + + //when + when(recordController.deleteRecord(request)).thenReturn(response); + + //then + assertEquals(recordController.deleteRecord(request), response); + } + + @DisplayName("isRecordAnalyzed 테스트") + @Test + public void isRecordAnalyzedTest() { + //given + Long recordId = 1L; + ResponseEntity response = mock(String.valueOf(Void.class)); + //when + when(recordController.isRecordAnalyzed(recordId)).thenReturn(response); + + //then + assertEquals(recordController.isRecordAnalyzed(recordId), response); + } +} diff --git a/src/test/java/com/speech/up/report/controller/ReportControllerTest.java b/src/test/java/com/speech/up/report/controller/ReportControllerTest.java new file mode 100644 index 0000000..536ff24 --- /dev/null +++ b/src/test/java/com/speech/up/report/controller/ReportControllerTest.java @@ -0,0 +1,32 @@ +package com.speech.up.report.controller; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.demo.ReportPageController; + +public class ReportControllerTest { + + @Mock + ReportPageController reportPageController; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + + @DisplayName("getReport 테스트") + @Test + public void getReportTest() { + //given + Long recordId = 1L; + + //when + //then + } +} + diff --git a/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java index 8505d97..9d9e0fd 100644 --- a/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java +++ b/src/test/java/com/speech/up/report/repository/ReportRepositoryTest.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -34,14 +35,14 @@ void setUp() { @Test public void findReportByReportIdTest() { //given - ReportEntity reportEntity = mock(ReportEntity.class); + Optional reportEntity = Optional.ofNullable(mock(ReportEntity.class)); Long id = 1L; //when - when(reportRepository.findReportEntityByReportId(id)).thenReturn(reportEntity); + when(reportRepository.findReportEntityByRecordIdRecordId(id)).thenReturn(reportEntity); //then - assertEquals(reportRepository.findReportEntityByReportId(id), reportEntity); + assertEquals(reportRepository.findReportEntityByRecordIdRecordId(id), reportEntity); } @DisplayName("모든 리포트 가져오기") diff --git a/src/test/java/com/speech/up/script/controller/ScriptControllerTest.java b/src/test/java/com/speech/up/script/controller/ScriptControllerTest.java new file mode 100644 index 0000000..8dc0cc3 --- /dev/null +++ b/src/test/java/com/speech/up/script/controller/ScriptControllerTest.java @@ -0,0 +1,116 @@ +package com.speech.up.script.controller; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.ResponseEntity; + +import com.speech.up.script.service.dto.ScriptAddDto; +import com.speech.up.script.service.dto.ScriptGetDto; +import com.speech.up.script.service.dto.ScriptIsUseDto; +import com.speech.up.script.service.dto.ScriptUpdateDto; + +import jakarta.servlet.http.HttpServletRequest; + +public class ScriptControllerTest { + @Mock + ScriptController scriptController; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("getScriptAll 테스트") + @Test + public void getScriptAllTest() { + //given + HttpServletRequest request = mock(HttpServletRequest.class); + List response = Collections.singletonList(mock(ScriptGetDto.Response.class)); + + //when + when(scriptController.getScriptAll(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(scriptController.getScriptAll(request), ResponseEntity.ok(response)); + } + + @DisplayName("getScript 테스트") + @Test + public void getScriptTest() { + //given + Long userId = 1L; + Long scriptId = 2L; + ScriptGetDto.Response response = mock(ScriptGetDto.Response.class); + + //when + when(scriptController.getScript(userId, scriptId)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(scriptController.getScript(userId, scriptId), ResponseEntity.ok(response)); + } + + @DisplayName("getScriptCount 테스트") + @Test + public void getScriptCountTest() { + //given + HttpServletRequest request = mock(HttpServletRequest.class); + ResponseEntity response = ResponseEntity.ok(1L); + + //when + when(scriptController.getScriptCount(request)).thenReturn(ResponseEntity.ok(2L)); + + //then + assertEquals(scriptController.getScriptCount(request), ResponseEntity.ok(2L)); + } + + @DisplayName("addScript 테스트") + @Test + public void addScriptTest() { + //given + ScriptAddDto.Request request = mock(ScriptAddDto.Request.class); + ScriptAddDto.Response response = mock(ScriptAddDto.Response.class); + + //when + when(scriptController.addScript(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(scriptController.addScript(request), ResponseEntity.ok(response)); + } + + @DisplayName("updateScript 테스트") + @Test + public void updateScriptTest() { + //given + ScriptUpdateDto.Request request = mock(ScriptUpdateDto.Request.class); + ScriptUpdateDto.Response response = mock(ScriptUpdateDto.Response.class); + + //when + when(scriptController.updateScript(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(scriptController.updateScript(request), ResponseEntity.ok(response)); + } + + @DisplayName("deleteScript 테스트") + @Test + public void deleteScriptTest() { + //given + ScriptIsUseDto.Request request = mock(ScriptIsUseDto.Request.class); + ScriptIsUseDto.Response response = mock(ScriptIsUseDto.Response.class); + + //when + when(scriptController.deleteScript(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(scriptController.deleteScript(request), ResponseEntity.ok(response)); + } +} diff --git a/src/test/java/com/speech/up/user/controller/UserControllerTest.java b/src/test/java/com/speech/up/user/controller/UserControllerTest.java new file mode 100644 index 0000000..3cc469a --- /dev/null +++ b/src/test/java/com/speech/up/user/controller/UserControllerTest.java @@ -0,0 +1,52 @@ +package com.speech.up.user.controller; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.ResponseEntity; + +import com.speech.up.user.service.dto.UserGetInfoDto; + +import jakarta.servlet.http.HttpServletRequest; + +public class UserControllerTest { + @Mock + UserController userController; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("getUserInfo 테스트") + @Test + public void getUserInfoTest() { + //given + HttpServletRequest request = mock(HttpServletRequest.class); + UserGetInfoDto.Response response = mock(UserGetInfoDto.Response.class); + + //when + when(userController.getUserInfo(request)).thenReturn(ResponseEntity.ok(response)); + + //then + assertEquals(userController.getUserInfo(request), ResponseEntity.ok(response)); + } + + @DisplayName("deleteUser 테스트") + @Test + public void deleteUserTest() { + //given + HttpServletRequest request = mock(HttpServletRequest.class); + + //when + userController.deleteUser(request); + + //then + verify(userController, times(1)).deleteUser(request); + } +} diff --git a/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java index 4a2a1f3..2b67fd2 100644 --- a/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java +++ b/src/test/java/com/speech/up/user/repositiory/UserRepositoryTest.java @@ -6,6 +6,7 @@ import java.time.LocalDateTime; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -30,7 +31,7 @@ public void setUp() { public void getUserBySocialIdTest() { //given String socialId = "socialId"; - UserEntity user = mock(UserEntity.class); + Optional user = Optional.ofNullable(mock(UserEntity.class)); //when when(userRepository.findBySocialId(socialId)).thenReturn(user); @@ -57,7 +58,7 @@ public void existsUserBySocialIdTest() { public void deleteUserBySocialIdTest() { //given String socialId = "socialId"; - UserEntity user = mock(UserEntity.class); + Optional user = Optional.ofNullable(mock(UserEntity.class)); when(userRepository.findBySocialId(socialId)).thenReturn(user); //when @@ -74,7 +75,7 @@ public void deleteUserBySocialIdTest() { public void getUserByUserIdTest() { //given Long userId = 1L; - UserEntity user = mock(UserEntity.class); + Optional user = Optional.ofNullable(mock(UserEntity.class)); //when when(userRepository.findByUserId(userId)).thenReturn(user); From 4c90a5ff02c0f46c83079ab49a88f3d85540f652 Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Tue, 27 Aug 2024 23:02:12 +0900 Subject: [PATCH 10/19] =?UTF-8?q?[#54]=20coverage=20=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../up/script/service/ScriptServiceTest.java | 112 +++++++++++++----- 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/src/test/java/com/speech/up/script/service/ScriptServiceTest.java b/src/test/java/com/speech/up/script/service/ScriptServiceTest.java index ef3a3ab..6ea243f 100644 --- a/src/test/java/com/speech/up/script/service/ScriptServiceTest.java +++ b/src/test/java/com/speech/up/script/service/ScriptServiceTest.java @@ -3,28 +3,47 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.BDDMockito.*; +import java.time.LocalDateTime; import java.util.Collections; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import org.springframework.mock.web.MockHttpServletRequest; +import com.speech.up.auth.provider.JwtProvider; +import com.speech.up.script.entity.ScriptEntity; +import com.speech.up.script.repository.ScriptRepository; import com.speech.up.script.service.dto.ScriptAddDto; import com.speech.up.script.service.dto.ScriptGetDto; import com.speech.up.script.service.dto.ScriptIsUseDto; import com.speech.up.script.service.dto.ScriptUpdateDto; +import com.speech.up.user.entity.UserEntity; +import com.speech.up.user.repository.UserRepository; import jakarta.persistence.EntityListeners; -import jakarta.servlet.http.HttpServletRequest; @EntityListeners(AuditingEntityListener.class) public class ScriptServiceTest { @Mock + ScriptRepository scriptRepository; + @Mock + UserRepository userRepository; + @Mock + JwtProvider jwtProvider; + @Mock + UserEntity mockUserEntity; + @Mock + ScriptEntity mockScriptEntity; + + @InjectMocks private ScriptService scriptService; @BeforeEach @@ -36,76 +55,115 @@ public void setUp() { @Test public void getScriptListTest() { // Given - HttpServletRequest hasAuthorizationRequest = mock(HttpServletRequest.class); - HttpServletRequest noHasAuthorizationRequest = mock(HttpServletRequest.class); - hasAuthorizationRequest.setAttribute("Authorization", "Bearer token"); - List responses = Collections.singletonList(mock(ScriptGetDto.Response.class)); + String socialId = "mockSocialId"; + Long userId = 1L; + MockHttpServletRequest request = new MockHttpServletRequest(); + List mockScripts = Collections.singletonList(mockScriptEntity); // When - when(scriptService.getScriptList(hasAuthorizationRequest)).thenReturn(responses); - when(scriptService.getScriptList(noHasAuthorizationRequest)).thenReturn(null); + when(jwtProvider.getHeader(request)).thenReturn(socialId); + when(userRepository.findBySocialId(socialId)).thenReturn(Optional.of(mockUserEntity)); + when(mockUserEntity.getUserId()).thenReturn(userId); + when(scriptRepository.findByUserUserIdAndIsUseTrue(userId)).thenReturn(mockScripts); - // Then - assertEquals(responses, scriptService.getScriptList(hasAuthorizationRequest)); - assertNotEquals(responses, scriptService.getScriptList(noHasAuthorizationRequest)); + List result = scriptService.getScriptList(request); + // Then + assertNotNull(result); + assertEquals(1, result.size()); } @DisplayName("스크립트 단건 조회") @Test public void getScript() { // Given - Long scriptId = 1L; Long userId = 1L; - ScriptGetDto.Response response = mock(ScriptGetDto.Response.class); + Long scriptId = 1L; + String socialId = "mockSocialId"; + LocalDateTime modifiedAt = LocalDateTime.now(); + + UserEntity mockUserEntity = mock(UserEntity.class); + ScriptEntity mockScriptEntity = mock(ScriptEntity.class); + ScriptGetDto.Response expectedResponse = ScriptGetDto.Response.toResponse(mockScriptEntity); + + when(userRepository.findBySocialId(socialId)).thenReturn(Optional.of(mockUserEntity)); + when(mockUserEntity.getUserId()).thenReturn(userId); + when(scriptRepository.findById(scriptId)).thenReturn(Optional.of(mockScriptEntity)); + when(mockScriptEntity.getUser()).thenReturn(mockUserEntity); + when(mockScriptEntity.getModifiedAt()).thenReturn(modifiedAt); // When - when(scriptService.getScript(userId, scriptId)).thenReturn(response); + ScriptGetDto.Response actualResponse = scriptService.getScript(userId, scriptId); // Then - assertEquals(response, scriptService.getScript(userId, scriptId)); + assertNotNull(actualResponse); + assertEquals(expectedResponse.getScriptId(), actualResponse.getScriptId()); + } @DisplayName("대본 수정 기능 테스트") @Test public void updateScriptTest() { // Given - ScriptUpdateDto.Request request = mock(ScriptUpdateDto.Request.class); - ScriptUpdateDto.Response response = mock(ScriptUpdateDto.Response.class); - + Long userId = 1L; + Long scriptId = 1L; + String content = "Content"; + String title = "Title"; + boolean isUse = true; + + + ScriptUpdateDto.Request scriptUpdateRequestDto = mock(ScriptUpdateDto.Request.class); + when(scriptUpdateRequestDto.getScriptId()).thenReturn(scriptId); + when(scriptUpdateRequestDto.getContent()).thenReturn(content); + when(scriptUpdateRequestDto.getTitle()).thenReturn(title); + when(mockUserEntity.getUserId()).thenReturn(userId); + when(scriptUpdateRequestDto.getUser()).thenReturn(mockUserEntity); + when(scriptUpdateRequestDto.isUse()).thenReturn(isUse); // When - when(scriptService.updateScript(request)).thenReturn(response); + when(userRepository.findById(userId)).thenReturn(Optional.of(mockUserEntity)); + ScriptEntity scriptEntity = ScriptEntity.update(scriptUpdateRequestDto); + + ScriptUpdateDto.Response response = ScriptUpdateDto.toResponse(scriptEntity); // Then - assertEquals(response, scriptService.updateScript(request)); + assertEquals(response.getTitle(), "Title"); + } @DisplayName("대본을 생성하는 기능 테스트") @Test public void addScriptTest() { // Given - ScriptAddDto.Request request = mock(ScriptAddDto.Request.class); - ScriptAddDto.Response response = mock(ScriptAddDto.Response.class); + ScriptAddDto.Request scriptAddRequestDto = mock(ScriptAddDto.Request.class); + when(scriptAddRequestDto.getContent()).thenReturn("content"); + when(scriptAddRequestDto.getUser()).thenReturn(mockUserEntity); + when(scriptAddRequestDto.getTitle()).thenReturn("title"); // When - when(scriptService.addScript(request)).thenReturn(response); + ScriptEntity scriptEntity = ScriptEntity.create(scriptAddRequestDto); + ScriptAddDto.Response response = ScriptAddDto.toResponse(scriptEntity); // Then - assertEquals(response, scriptService.addScript(request)); + assertEquals(response.getTitle(), "title"); } @DisplayName("대본을 삭제하는 기능 테스트") @Test public void deleteScriptByIdTest() { // Given - ScriptIsUseDto.Request request = mock(ScriptIsUseDto.Request.class); - ScriptIsUseDto.Response response = mock(ScriptIsUseDto.Response.class); + ScriptIsUseDto.Request scriptIsUseDtoRequest = mock(ScriptIsUseDto.Request.class); + when(scriptIsUseDtoRequest.getScriptId()).thenReturn(1L); + when(scriptIsUseDtoRequest.isUse()).thenReturn(true); + when(scriptIsUseDtoRequest.getContent()).thenReturn("content"); + when(scriptIsUseDtoRequest.getUser()).thenReturn(mockUserEntity); + when(scriptIsUseDtoRequest.getTitle()).thenReturn("title"); // When - when(scriptService.deleteScriptById(request)).thenReturn(response); + ScriptEntity scriptEntity = ScriptEntity.delete(scriptIsUseDtoRequest); + ScriptIsUseDto.Response response = ScriptIsUseDto.toResponse(scriptEntity); // Then - assertEquals(response, scriptService.deleteScriptById(request)); + assertFalse(response.isUse()); } } From e695d717f0142bc6796963b232af1669985c5240 Mon Sep 17 00:00:00 2001 From: xorwns118 Date: Wed, 28 Aug 2024 10:29:44 +0900 Subject: [PATCH 11/19] =?UTF-8?q?[#173]=20fix=20:=20=EB=94=94=EB=A0=89?= =?UTF-8?q?=ED=86=A0=EB=A6=AC=EA=B5=AC=EC=A1=B0=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../speech/up/auth/config/WebSecurityConfig.java | 2 +- .../{scriptPage => }/js/addRecordingToList.js | 0 .../static/{scriptPage => }/js/addTokenSession.js | 0 .../static/{scriptPage => }/js/adminUserHandler.js | 0 .../static/{scriptPage => }/js/analyticRecord.js | 0 .../static/{scriptPage => }/js/boardCount.js | 0 .../static/{scriptPage => }/js/boardModify.js | 0 .../static/{scriptPage => }/js/boardWrite.js | 0 .../{scriptPage => }/js/checkAdministration.js | 0 .../static/{scriptPage => }/js/checkBoardOwner.js | 0 .../static/{scriptPage => }/js/checkLogined.js | 0 .../static/{scriptPage => }/js/loadLocalRecord.js | 0 .../static/{scriptPage => }/js/localStoragePath.js | 0 .../resources/static/{scriptPage => }/js/map.js | 0 .../resources/static/{scriptPage => }/js/myPage.js | 0 .../resources/static/{scriptPage => }/js/record.js | 0 .../resources/static/{scriptPage => }/js/reply.js | 0 .../static/{scriptPage => }/js/replyCount.js | 0 .../static/{scriptPage => }/js/saveRecord.js | 0 .../static/{scriptPage => }/js/scriptCount.js | 0 .../static/{scriptPage => }/js/scriptDetail.js | 0 .../static/{scriptPage => }/js/scriptList.js | 0 .../static/{scriptPage => }/js/scriptWrite.js | 0 .../static/{scriptPage => }/js/userDelete.js | 0 .../resources/static/{scriptPage => }/js/userMe.js | 0 src/main/resources/templates/admin-page.html | 8 ++++---- src/main/resources/templates/board-detail.html | 6 +++--- src/main/resources/templates/board-edit.html | 6 +++--- src/main/resources/templates/board-write.html | 4 ++-- src/main/resources/templates/board.html | 4 ++-- src/main/resources/templates/home.html | 2 +- src/main/resources/templates/map.html | 2 +- src/main/resources/templates/myPage.html | 12 ++++++------ src/main/resources/templates/script-list.html | 4 ++-- src/main/resources/templates/script-write.html | 4 ++-- src/main/resources/templates/script.html | 14 +++++++------- 36 files changed, 34 insertions(+), 34 deletions(-) rename src/main/resources/static/{scriptPage => }/js/addRecordingToList.js (100%) rename src/main/resources/static/{scriptPage => }/js/addTokenSession.js (100%) rename src/main/resources/static/{scriptPage => }/js/adminUserHandler.js (100%) rename src/main/resources/static/{scriptPage => }/js/analyticRecord.js (100%) rename src/main/resources/static/{scriptPage => }/js/boardCount.js (100%) rename src/main/resources/static/{scriptPage => }/js/boardModify.js (100%) rename src/main/resources/static/{scriptPage => }/js/boardWrite.js (100%) rename src/main/resources/static/{scriptPage => }/js/checkAdministration.js (100%) rename src/main/resources/static/{scriptPage => }/js/checkBoardOwner.js (100%) rename src/main/resources/static/{scriptPage => }/js/checkLogined.js (100%) rename src/main/resources/static/{scriptPage => }/js/loadLocalRecord.js (100%) rename src/main/resources/static/{scriptPage => }/js/localStoragePath.js (100%) rename src/main/resources/static/{scriptPage => }/js/map.js (100%) rename src/main/resources/static/{scriptPage => }/js/myPage.js (100%) rename src/main/resources/static/{scriptPage => }/js/record.js (100%) rename src/main/resources/static/{scriptPage => }/js/reply.js (100%) rename src/main/resources/static/{scriptPage => }/js/replyCount.js (100%) rename src/main/resources/static/{scriptPage => }/js/saveRecord.js (100%) rename src/main/resources/static/{scriptPage => }/js/scriptCount.js (100%) rename src/main/resources/static/{scriptPage => }/js/scriptDetail.js (100%) rename src/main/resources/static/{scriptPage => }/js/scriptList.js (100%) rename src/main/resources/static/{scriptPage => }/js/scriptWrite.js (100%) rename src/main/resources/static/{scriptPage => }/js/userDelete.js (100%) rename src/main/resources/static/{scriptPage => }/js/userMe.js (100%) diff --git a/src/main/java/com/speech/up/auth/config/WebSecurityConfig.java b/src/main/java/com/speech/up/auth/config/WebSecurityConfig.java index 07ba024..62cfca0 100644 --- a/src/main/java/com/speech/up/auth/config/WebSecurityConfig.java +++ b/src/main/java/com/speech/up/auth/config/WebSecurityConfig.java @@ -50,7 +50,7 @@ public WebSecurityCustomizer configured() { // 스프링 시큐리티 기능 비 .requestMatchers( new AntPathRequestMatcher("/images/**"), new AntPathRequestMatcher("/css/**"), - new AntPathRequestMatcher("/scriptPage/js/**"), + new AntPathRequestMatcher("/js/**"), new AntPathRequestMatcher("/map") ); } diff --git a/src/main/resources/static/scriptPage/js/addRecordingToList.js b/src/main/resources/static/js/addRecordingToList.js similarity index 100% rename from src/main/resources/static/scriptPage/js/addRecordingToList.js rename to src/main/resources/static/js/addRecordingToList.js diff --git a/src/main/resources/static/scriptPage/js/addTokenSession.js b/src/main/resources/static/js/addTokenSession.js similarity index 100% rename from src/main/resources/static/scriptPage/js/addTokenSession.js rename to src/main/resources/static/js/addTokenSession.js diff --git a/src/main/resources/static/scriptPage/js/adminUserHandler.js b/src/main/resources/static/js/adminUserHandler.js similarity index 100% rename from src/main/resources/static/scriptPage/js/adminUserHandler.js rename to src/main/resources/static/js/adminUserHandler.js diff --git a/src/main/resources/static/scriptPage/js/analyticRecord.js b/src/main/resources/static/js/analyticRecord.js similarity index 100% rename from src/main/resources/static/scriptPage/js/analyticRecord.js rename to src/main/resources/static/js/analyticRecord.js diff --git a/src/main/resources/static/scriptPage/js/boardCount.js b/src/main/resources/static/js/boardCount.js similarity index 100% rename from src/main/resources/static/scriptPage/js/boardCount.js rename to src/main/resources/static/js/boardCount.js diff --git a/src/main/resources/static/scriptPage/js/boardModify.js b/src/main/resources/static/js/boardModify.js similarity index 100% rename from src/main/resources/static/scriptPage/js/boardModify.js rename to src/main/resources/static/js/boardModify.js diff --git a/src/main/resources/static/scriptPage/js/boardWrite.js b/src/main/resources/static/js/boardWrite.js similarity index 100% rename from src/main/resources/static/scriptPage/js/boardWrite.js rename to src/main/resources/static/js/boardWrite.js diff --git a/src/main/resources/static/scriptPage/js/checkAdministration.js b/src/main/resources/static/js/checkAdministration.js similarity index 100% rename from src/main/resources/static/scriptPage/js/checkAdministration.js rename to src/main/resources/static/js/checkAdministration.js diff --git a/src/main/resources/static/scriptPage/js/checkBoardOwner.js b/src/main/resources/static/js/checkBoardOwner.js similarity index 100% rename from src/main/resources/static/scriptPage/js/checkBoardOwner.js rename to src/main/resources/static/js/checkBoardOwner.js diff --git a/src/main/resources/static/scriptPage/js/checkLogined.js b/src/main/resources/static/js/checkLogined.js similarity index 100% rename from src/main/resources/static/scriptPage/js/checkLogined.js rename to src/main/resources/static/js/checkLogined.js diff --git a/src/main/resources/static/scriptPage/js/loadLocalRecord.js b/src/main/resources/static/js/loadLocalRecord.js similarity index 100% rename from src/main/resources/static/scriptPage/js/loadLocalRecord.js rename to src/main/resources/static/js/loadLocalRecord.js diff --git a/src/main/resources/static/scriptPage/js/localStoragePath.js b/src/main/resources/static/js/localStoragePath.js similarity index 100% rename from src/main/resources/static/scriptPage/js/localStoragePath.js rename to src/main/resources/static/js/localStoragePath.js diff --git a/src/main/resources/static/scriptPage/js/map.js b/src/main/resources/static/js/map.js similarity index 100% rename from src/main/resources/static/scriptPage/js/map.js rename to src/main/resources/static/js/map.js diff --git a/src/main/resources/static/scriptPage/js/myPage.js b/src/main/resources/static/js/myPage.js similarity index 100% rename from src/main/resources/static/scriptPage/js/myPage.js rename to src/main/resources/static/js/myPage.js diff --git a/src/main/resources/static/scriptPage/js/record.js b/src/main/resources/static/js/record.js similarity index 100% rename from src/main/resources/static/scriptPage/js/record.js rename to src/main/resources/static/js/record.js diff --git a/src/main/resources/static/scriptPage/js/reply.js b/src/main/resources/static/js/reply.js similarity index 100% rename from src/main/resources/static/scriptPage/js/reply.js rename to src/main/resources/static/js/reply.js diff --git a/src/main/resources/static/scriptPage/js/replyCount.js b/src/main/resources/static/js/replyCount.js similarity index 100% rename from src/main/resources/static/scriptPage/js/replyCount.js rename to src/main/resources/static/js/replyCount.js diff --git a/src/main/resources/static/scriptPage/js/saveRecord.js b/src/main/resources/static/js/saveRecord.js similarity index 100% rename from src/main/resources/static/scriptPage/js/saveRecord.js rename to src/main/resources/static/js/saveRecord.js diff --git a/src/main/resources/static/scriptPage/js/scriptCount.js b/src/main/resources/static/js/scriptCount.js similarity index 100% rename from src/main/resources/static/scriptPage/js/scriptCount.js rename to src/main/resources/static/js/scriptCount.js diff --git a/src/main/resources/static/scriptPage/js/scriptDetail.js b/src/main/resources/static/js/scriptDetail.js similarity index 100% rename from src/main/resources/static/scriptPage/js/scriptDetail.js rename to src/main/resources/static/js/scriptDetail.js diff --git a/src/main/resources/static/scriptPage/js/scriptList.js b/src/main/resources/static/js/scriptList.js similarity index 100% rename from src/main/resources/static/scriptPage/js/scriptList.js rename to src/main/resources/static/js/scriptList.js diff --git a/src/main/resources/static/scriptPage/js/scriptWrite.js b/src/main/resources/static/js/scriptWrite.js similarity index 100% rename from src/main/resources/static/scriptPage/js/scriptWrite.js rename to src/main/resources/static/js/scriptWrite.js diff --git a/src/main/resources/static/scriptPage/js/userDelete.js b/src/main/resources/static/js/userDelete.js similarity index 100% rename from src/main/resources/static/scriptPage/js/userDelete.js rename to src/main/resources/static/js/userDelete.js diff --git a/src/main/resources/static/scriptPage/js/userMe.js b/src/main/resources/static/js/userMe.js similarity index 100% rename from src/main/resources/static/scriptPage/js/userMe.js rename to src/main/resources/static/js/userMe.js diff --git a/src/main/resources/templates/admin-page.html b/src/main/resources/templates/admin-page.html index 550dcd1..2a9b154 100644 --- a/src/main/resources/templates/admin-page.html +++ b/src/main/resources/templates/admin-page.html @@ -36,8 +36,8 @@

User List

- - - - + + + + \ No newline at end of file diff --git a/src/main/resources/templates/board-detail.html b/src/main/resources/templates/board-detail.html index 38c47da..fdd5f29 100644 --- a/src/main/resources/templates/board-detail.html +++ b/src/main/resources/templates/board-detail.html @@ -50,7 +50,7 @@
Add a comment
- - - + + + diff --git a/src/main/resources/templates/board-edit.html b/src/main/resources/templates/board-edit.html index 03df5a8..b7721ce 100644 --- a/src/main/resources/templates/board-edit.html +++ b/src/main/resources/templates/board-edit.html @@ -45,8 +45,8 @@

게시물 수정

- - - + + + diff --git a/src/main/resources/templates/board-write.html b/src/main/resources/templates/board-write.html index 38f3a23..fa03e9d 100644 --- a/src/main/resources/templates/board-write.html +++ b/src/main/resources/templates/board-write.html @@ -42,7 +42,7 @@

게시물 작성

Back to List - - + + diff --git a/src/main/resources/templates/board.html b/src/main/resources/templates/board.html index b5fc8a0..05a0725 100644 --- a/src/main/resources/templates/board.html +++ b/src/main/resources/templates/board.html @@ -66,7 +66,7 @@

Board List

- - + + diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index d6927a0..232ffc7 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -67,6 +67,6 @@

Get Started with AI-Powered Script Recording Today!

© 2024 AI Script Recorder & Processor. All rights reserved.

- + \ No newline at end of file diff --git a/src/main/resources/templates/map.html b/src/main/resources/templates/map.html index 5f10832..3f0d54b 100644 --- a/src/main/resources/templates/map.html +++ b/src/main/resources/templates/map.html @@ -33,5 +33,5 @@ - + \ No newline at end of file diff --git a/src/main/resources/templates/myPage.html b/src/main/resources/templates/myPage.html index 0b843ba..f54b71b 100644 --- a/src/main/resources/templates/myPage.html +++ b/src/main/resources/templates/myPage.html @@ -51,11 +51,11 @@

Profile Information

- - - - - - + + + + + + diff --git a/src/main/resources/templates/script-list.html b/src/main/resources/templates/script-list.html index b0f758d..b5b9bcc 100644 --- a/src/main/resources/templates/script-list.html +++ b/src/main/resources/templates/script-list.html @@ -48,7 +48,7 @@

대본 목록

- - + + diff --git a/src/main/resources/templates/script-write.html b/src/main/resources/templates/script-write.html index 153b2b8..8916238 100644 --- a/src/main/resources/templates/script-write.html +++ b/src/main/resources/templates/script-write.html @@ -29,8 +29,8 @@

새 대본 작성

- - + + diff --git a/src/main/resources/templates/script.html b/src/main/resources/templates/script.html index a4c816f..317e88f 100644 --- a/src/main/resources/templates/script.html +++ b/src/main/resources/templates/script.html @@ -56,13 +56,13 @@

녹음 목록

- - - - - - - + + + + + + + -