From 68cb7dbf99c02364b5850a992e13af73ce22c34e Mon Sep 17 00:00:00 2001 From: KEEKE132 Date: Wed, 29 May 2024 00:54:04 +0900 Subject: [PATCH] =?UTF-8?q?remove:=20Repository=20Test=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommentControllerTests.java | 61 -- .../CommentRepositoryTests.java | 77 --- .../CommentServiceTests.java | 224 +++---- .../IssueRepositoryTests.java | 81 --- .../IssueServiceTests.java | 628 +++++++++--------- 5 files changed, 426 insertions(+), 645 deletions(-) delete mode 100644 src/test/java/se/issuetrackingsystem/CommentControllerTests.java delete mode 100644 src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java delete mode 100644 src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java diff --git a/src/test/java/se/issuetrackingsystem/CommentControllerTests.java b/src/test/java/se/issuetrackingsystem/CommentControllerTests.java deleted file mode 100644 index 969e644..0000000 --- a/src/test/java/se/issuetrackingsystem/CommentControllerTests.java +++ /dev/null @@ -1,61 +0,0 @@ -//package se.issuetrackingsystem; -// -//import com.fasterxml.jackson.databind.ObjectMapper; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -//import org.springframework.boot.test.mock.mockito.MockBean; -//import org.springframework.http.MediaType; -//import org.springframework.test.web.servlet.MockMvc; -//import se.issuetrackingsystem.comment.controller.CommentController; -//import se.issuetrackingsystem.comment.domain.Comment; -//import se.issuetrackingsystem.comment.dto.CommentRequest; -//import se.issuetrackingsystem.comment.service.CommentService; -// -//import static org.mockito.Mockito.*; -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -// -//@WebMvcTest(CommentController.class) -//public class CommentControllerTests { -// -// @Autowired -// MockMvc mvc; -// -// @MockBean -// CommentService commentService; -// -// @Autowired -// ObjectMapper objectMapper; -// -// @Test -// @DisplayName("코멘트 생성") -// void testCommentCreate() throws Exception { -// CommentRequest request = new CommentRequest(); -// request.setMessage("Test Message"); -// request.setAuthorId(1L); -// -// when(commentService.create(anyLong(),anyString(),anyLong())).thenReturn(new Comment()); -// -// mvc.perform(post("/api/v1/comments") -// .param("issueId", "1") -// .contentType(MediaType.APPLICATION_JSON) -// .content(objectMapper.writeValueAsString(request))) -// .andExpect(status().isOk()); -// } -// -// @Test -// @DisplayName("코멘트 삭제") -// void testCommentDelete() throws Exception { -// //given -// doNothing().when(commentService).delete(anyLong()); -// -// //when then -// mvc.perform(delete("/api/v1/comments") -// .param("commentId", "1")) -// .andExpect(status().isOk()); -// } -// -//} diff --git a/src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java b/src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java deleted file mode 100644 index 297a507..0000000 --- a/src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java +++ /dev/null @@ -1,77 +0,0 @@ -//package se.issuetrackingsystem; -// -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -//import se.issuetrackingsystem.comment.domain.Comment; -//import se.issuetrackingsystem.comment.repository.CommentRepository; -//import se.issuetrackingsystem.issue.domain.Issue; -// -//import java.util.List; -// -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.junit.jupiter.api.Assertions.assertTrue; -// -//@DataJpaTest -//public class CommentRepositoryTests { -// @Autowired -// CommentRepository commentRepository; -// -// @Test -// @DisplayName("코멘트 만들기") -// void createIssue(){ -// //given -// Comment comment1 = new Comment(); -// comment1.setMessage("hello1"); -// -// Comment comment2 = new Comment(); -// comment2.setMessage("hello2"); -// -// //when -// Comment result1 = this.commentRepository.save(comment1); -// Comment result2 = this.commentRepository.save(comment2); -// -// //then -// assertEquals(comment1,result1); -// assertEquals(comment2,result2); -// } -// -// @Test -// @DisplayName("코멘트 찾기") -// void getIssue(){ -// //given -// Comment comment1 = new Comment(); -// comment1.setMessage("hello1"); -// this.commentRepository.save(comment1); -// -// Comment comment2 = new Comment(); -// comment2.setMessage("hello2"); -// this.commentRepository.save(comment2); -// -// //when -// Comment result1 = this.commentRepository.findById(comment1.getId()).get(); -// Comment result2 = this.commentRepository.findById(comment2.getId()).get(); -// List results = this.commentRepository.findAll(); -// -// //then -// assertEquals(comment1,result1); -// assertEquals(comment2,result2); -// assertEquals(results.size(),2); -// } -// -// @Test -// @DisplayName("이슈 삭제") -// void deleteIssue() { -// //given -// Comment comment1 = new Comment(); -// comment1.setMessage("hello1"); -// this.commentRepository.save(comment1); -// -// //when -// this.commentRepository.delete(comment1); -// -// //then -// assertTrue(this.commentRepository.findById(comment1.getId()).isEmpty()); -// } -//} diff --git a/src/test/java/se/issuetrackingsystem/CommentServiceTests.java b/src/test/java/se/issuetrackingsystem/CommentServiceTests.java index d2c8930..962f54c 100644 --- a/src/test/java/se/issuetrackingsystem/CommentServiceTests.java +++ b/src/test/java/se/issuetrackingsystem/CommentServiceTests.java @@ -1,112 +1,112 @@ -//package se.issuetrackingsystem; -// -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.extension.ExtendWith; -//import org.springframework.boot.test.mock.mockito.MockBean; -//import org.springframework.test.context.junit.jupiter.SpringExtension; -//import se.issuetrackingsystem.comment.domain.Comment; -//import se.issuetrackingsystem.comment.repository.CommentRepository; -//import se.issuetrackingsystem.comment.service.CommentService; -//import se.issuetrackingsystem.issue.domain.Issue; -//import se.issuetrackingsystem.issue.repository.IssueRepository; -//import se.issuetrackingsystem.user.domain.User; -//import se.issuetrackingsystem.user.repository.UserRepository; -// -//import java.util.Arrays; -//import java.util.List; -//import java.util.Optional; -// -//import static org.junit.jupiter.api.Assertions.*; -//import static org.mockito.Mockito.*; -// -//@ExtendWith(SpringExtension.class) -//public class CommentServiceTests { -// -// CommentService commentService; -// @MockBean -// CommentRepository commentRepository; -// @MockBean -// IssueRepository issueRepository; -// @MockBean -// UserRepository userRepository; -// -// @BeforeEach -// void setUp() { -// commentService = new CommentService(commentRepository, issueRepository, userRepository); -// } -// -// @Test -// @DisplayName("코멘트 생성") -// void commentCreate() { -// //given -// Comment comment = new Comment(); -// Issue issue = mock(Issue.class); -// User user = mock(User.class); -// comment.setMessage("Hello"); -// comment.setIssue(issue); -// comment.setAuthor(user); -// -// when(issueRepository.findById(1L)).thenReturn(Optional.of(issue)); -// when(userRepository.findById(1L)).thenReturn(Optional.of(user)); -// when(commentRepository.save(comment)).thenReturn(comment); -// -// //when -// Comment result = commentService.create(1L, "Hello", 1L); -// -// //then -// assertEquals("Hello", result.getMessage()); -// assertEquals(issue, result.getIssue()); -// assertEquals(user, result.getAuthor()); -// } -// -// @Test -// @DisplayName("코멘트 수정") -// void commentModify() { -// //given -// Comment comment = new Comment(); -// comment.setMessage("Hello"); -// -// when(commentRepository.findById(comment.getId())).thenReturn(Optional.of(comment)); -// when(commentRepository.save(comment)).thenReturn(comment); -// -// //when -// Comment result = commentService.modify(comment.getId(), "Hi"); -// -// //then -// assertEquals("Hi", result.getMessage()); -// } -// -// @Test -// @DisplayName("코멘트 리스트 찾기") -// void commentGetList() { -// //given -// Issue issue = mock(Issue.class); -// -// Comment comment1 = new Comment(); -// comment1.setIssue(issue); -// comment1.setId(1L); -// comment1.setMessage("1M"); -// -// Comment comment2 = new Comment(); -// comment2.setMessage("2M"); -// comment2.setIssue(issue); -// comment2.setId(2L); -// -// List comments = Arrays.asList( -// comment1,comment2 -// ); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// when(commentRepository.findAllByIssue(issue)).thenReturn(comments); -// -// //when -// List result = commentService.getList(issue.getId()); -// -// //then -// assertEquals(result.size(),2); -// assertEquals("1M",result.get(0).getMessage()); -// assertEquals("2M",result.get(1).getMessage()); -// } -//} +package se.issuetrackingsystem; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import se.issuetrackingsystem.comment.domain.Comment; +import se.issuetrackingsystem.comment.repository.CommentRepository; +import se.issuetrackingsystem.comment.service.CommentService; +import se.issuetrackingsystem.issue.domain.Issue; +import se.issuetrackingsystem.issue.repository.IssueRepository; +import se.issuetrackingsystem.user.domain.User; +import se.issuetrackingsystem.user.repository.UserRepository; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(SpringExtension.class) +public class CommentServiceTests { + + CommentService commentService; + @MockBean + CommentRepository commentRepository; + @MockBean + IssueRepository issueRepository; + @MockBean + UserRepository userRepository; + + @BeforeEach + void setUp() { + commentService = new CommentService(commentRepository, issueRepository, userRepository); + } + + @Test + @DisplayName("코멘트 생성") + void commentCreate() { + //given + Comment comment = new Comment(); + Issue issue = mock(Issue.class); + User user = mock(User.class); + comment.setMessage("Hello"); + comment.setIssue(issue); + comment.setAuthor(user); + + when(issueRepository.findById(1L)).thenReturn(Optional.of(issue)); + when(userRepository.findById(1L)).thenReturn(Optional.of(user)); + when(commentRepository.save(comment)).thenReturn(comment); + + //when + Comment result = commentService.create(1L, "Hello", 1L); + + //then + assertEquals("Hello", result.getMessage()); + assertEquals(issue, result.getIssue()); + assertEquals(user, result.getAuthor()); + } + + @Test + @DisplayName("코멘트 수정") + void commentModify() { + //given + Comment comment = new Comment(); + comment.setMessage("Hello"); + + when(commentRepository.findById(comment.getId())).thenReturn(Optional.of(comment)); + when(commentRepository.save(comment)).thenReturn(comment); + + //when + Comment result = commentService.modify(comment.getId(), "Hi"); + + //then + assertEquals("Hi", result.getMessage()); + } + + @Test + @DisplayName("코멘트 리스트 찾기") + void commentGetList() { + //given + Issue issue = mock(Issue.class); + + Comment comment1 = new Comment(); + comment1.setIssue(issue); + comment1.setId(1L); + comment1.setMessage("1M"); + + Comment comment2 = new Comment(); + comment2.setMessage("2M"); + comment2.setIssue(issue); + comment2.setId(2L); + + List comments = Arrays.asList( + comment1,comment2 + ); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + when(commentRepository.findAllByIssue(issue)).thenReturn(comments); + + //when + List result = commentService.getList(issue.getId()); + + //then + assertEquals(result.size(),2); + assertEquals("1M",result.get(0).getMessage()); + assertEquals("2M",result.get(1).getMessage()); + } +} diff --git a/src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java b/src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java deleted file mode 100644 index a55d0fa..0000000 --- a/src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java +++ /dev/null @@ -1,81 +0,0 @@ -//package se.issuetrackingsystem; -// -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -//import se.issuetrackingsystem.issue.domain.Issue; -//import se.issuetrackingsystem.issue.repository.IssueRepository; -// -//import java.util.List; -// -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.junit.jupiter.api.Assertions.assertTrue; -// -//@DataJpaTest -//public class IssueRepositoryTests { -// @Autowired -// IssueRepository issueRepository; -// -// @Test -// @DisplayName("이슈 만들기") -// void createIssue(){ -// //given -// Issue issue1 = new Issue(); -// issue1.setTitle("hello1"); -// issue1.setDescription("hi1"); -// -// Issue issue2 = new Issue(); -// issue2.setTitle("hello2"); -// issue2.setDescription("hi2"); -// -// //when -// Issue result1 = this.issueRepository.save(issue1); -// Issue result2 = this.issueRepository.save(issue2); -// -// //then -// assertEquals(issue1,result1); -// assertEquals(issue2,result2); -// } -// -// @Test -// @DisplayName("이슈 찾기") -// void getIssue(){ -// //given -// Issue issue1 = new Issue(); -// issue1.setTitle("hello1"); -// issue1.setDescription("hi1"); -// this.issueRepository.save(issue1); -// -// Issue issue2 = new Issue(); -// issue2.setTitle("hello2"); -// issue2.setDescription("hi2"); -// this.issueRepository.save(issue2); -// -// //when -// Issue result1 = this.issueRepository.findById(issue1.getId()).get(); -// Issue result2 = this.issueRepository.findById(issue2.getId()).get(); -// List results = this.issueRepository.findAll(); -// -// //then -// assertEquals(issue1,result1); -// assertEquals(issue2,result2); -// assertEquals(results.size(),2); -// } -// -// @Test -// @DisplayName("이슈 삭제") -// void deleteIssue() { -// //given -// Issue issue1 = new Issue(); -// issue1.setTitle("hello1"); -// issue1.setDescription("hi1"); -// this.issueRepository.save(issue1); -// -// //when -// this.issueRepository.delete(issue1); -// -// //then -// assertTrue(this.issueRepository.findById(issue1.getId()).isEmpty()); -// } -//} diff --git a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java index f14fe5c..5d36c3f 100644 --- a/src/test/java/se/issuetrackingsystem/IssueServiceTests.java +++ b/src/test/java/se/issuetrackingsystem/IssueServiceTests.java @@ -1,314 +1,314 @@ -//package se.issuetrackingsystem; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.DisplayName; -//import org.junit.jupiter.api.Test; -//import org.junit.jupiter.api.extension.ExtendWith; -//import org.springframework.boot.test.mock.mockito.MockBean; -//import org.springframework.test.context.junit.jupiter.SpringExtension; -//import org.springframework.test.util.ReflectionTestUtils; -//import se.issuetrackingsystem.common.exception.CustomException; -//import se.issuetrackingsystem.issue.domain.Issue; -//import se.issuetrackingsystem.issue.repository.IssueRepository; -//import se.issuetrackingsystem.issue.service.IssueService; -//import se.issuetrackingsystem.project.domain.Project; -//import se.issuetrackingsystem.project.repository.ProjectRepository; -//import se.issuetrackingsystem.user.domain.*; -//import se.issuetrackingsystem.user.repository.ProjectContributorRepository; -//import se.issuetrackingsystem.user.repository.UserRepository; -// -//import java.util.Arrays; -//import java.util.List; -//import java.util.Optional; -// -//import static org.junit.jupiter.api.Assertions.*; -//import static org.mockito.Mockito.*; -// -//@ExtendWith(SpringExtension.class) -//public class IssueServiceTests { -// -// IssueService issueService; -// @MockBean -// IssueRepository issueRepository; -// @MockBean -// ProjectRepository projectRepository; -// @MockBean -// UserRepository userRepository; -// @MockBean -// ProjectContributorRepository projectContributorRepository; -// -// @BeforeEach -// void setUp() { -// issueService = new IssueService(issueRepository, userRepository, projectRepository, projectContributorRepository); -// } -// -// @Test -// @DisplayName("이슈 생성 성공") -// void issueCreate() { -// //given -// Project project = mock(Project.class); -// User user = mock(User.class); -// -// when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); -// when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); -// when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); -// -// //when -// Issue result = issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR); -// -// //then -// assertEquals(result.getTitle(), "Issue1"); -// assertEquals(result.getDescription(), "issue"); -// assertEquals(result.getReporter(), user); -// assertEquals(result.getPriority(), Issue.Priority.MINOR); -// assertEquals(result.getProject(), project); -// } -// -// @Test -// @DisplayName("이슈 생성 실패-프로젝트_없음") -// void issueCreateNoProject() { -// //given -// Project project = mock(Project.class); -// User user = mock(User.class); -// -// when(projectRepository.findById(project.getId())).thenReturn(Optional.empty()); -// when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); -// when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); -// -// //when then -// assertThrows(CustomException.class, -// ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); -// } -// -// @Test -// @DisplayName("이슈 생성 실패-리포터_없음") -// void issueCreateNoReporter() { -// //given -// Project project = mock(Project.class); -// User user = mock(User.class); -// -// when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); -// when(userRepository.findById(user.getId())).thenReturn(Optional.empty()); -// when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); -// -// //when then -// assertThrows(CustomException.class, -// ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); -// } -// -// @Test -// @DisplayName("이슈 찾기 성공") -// void getIssue() { -// //given -// Issue issue = new Issue(); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// -// //when -// Issue result = issueService.getIssue(issue.getId()); -// -// //then -// assertEquals(issue, result); -// } -// -// @Test -// @DisplayName("프로젝트로 이슈들 찾기") -// void getIssues() { -// //given -// Project project = mock(Project.class); -// Issue issue1 = new Issue(); -// issue1.setProject(project); -// Issue issue2 = new Issue(); -// issue2.setProject(project); -// List issues = Arrays.asList(issue1, issue2); -// -// when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); -// when(issueRepository.findAllByProject(project)).thenReturn(issues); -// -// //when -// List result = this.issueService.getList(project.getId()); -// -// //then -// assertEquals(result.size(), 2); -// assertEquals(result.get(0), issue1); -// assertEquals(result.get(1), issue2); -// } -// -// @Test -// @DisplayName("Assignee로 이슈들 찾기") -// void getIssuesByAssignee() { -// //given -// User user = mock(User.class); -// Issue issue1 = new Issue(); -// issue1.setAssignee(user); -// Issue issue2 = new Issue(); -// issue2.setAssignee(user); -// List issues = Arrays.asList(issue1, issue2); -// -// when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); -// when(issueRepository.findAllByAssignee(user)).thenReturn(issues); -// -// //when -// List result = this.issueService.getListByAssignee(user.getId()); -// -// //then -// assertEquals(result.size(), 2); -// assertEquals(result.get(0), issue1); -// assertEquals(result.get(1), issue2); -// } -// -// @Test -// @DisplayName("프로젝트와 상태로 이슈들 찾기") -// void getIssuesWithStatus() { -// //given -// Project project = mock(Project.class); -// Issue issue1 = new Issue(); -// issue1.setProject(project); -// issue1.setStatus(Issue.Status.FIXED); -// Issue issue2 = new Issue(); -// issue2.setProject(project); -// issue2.setStatus(Issue.Status.FIXED); -// List issues = Arrays.asList(issue1, issue2); -// -// when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); -// when(issueRepository.findAllByProjectAndStatus(project, Issue.Status.FIXED)).thenReturn(issues); -// -// //then -// List result = this.issueService.getList(project.getId(), Issue.Status.FIXED); -// -// //then -// assertEquals(result.size(), 2); -// assertEquals(result.get(0), issue1); -// assertEquals(result.get(1), issue2); -// } -// -// @Test -// @DisplayName("이슈 수정 성공") -// void issueModify() { -// //given -// Issue issue = new Issue(); -// issue.setTitle("Hey"); -// issue.setDescription("Apple"); -// issue.setPriority(Issue.Priority.MINOR); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// -// //when -// Issue result = issueService.modify(issue.getId(), "Hello", "Banana", Issue.Priority.MAJOR); -// -// //then -// assertEquals(result.getTitle(), "Hello"); -// assertEquals(result.getDescription(), "Banana"); -// assertEquals(result.getPriority(), Issue.Priority.MAJOR); -// } -// -// @Test -// @DisplayName("이슈 삭제 성공") -// void issueDelete() { -// //given -// Issue issue = mock(Issue.class); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// -// //when -// Issue result = this.issueService.delete(issue.getId()); -// -// //then -// assertEquals(result, issue); -// } -// -// @Test -// @DisplayName("이슈 Assignee 설정 성공") -// void setAssignee() { -// //given -// User assignee = mock(User.class); -// PL pl = new PL(); -// Issue issue = new Issue(); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// when(userRepository.findById(any())).thenReturn(Optional.of(pl)).thenReturn(Optional.of(assignee)); -// -// //when -// Issue result = issueService.setAssignee(issue.getId(),pl.getId(),assignee.getId()); -// -// //then -// assertEquals(result.getAssignee(),assignee); -// } -// -// @Test -// @DisplayName("이슈 Assignee 설정 실패_PL이 아님") -// void setAssigneeFail() { -// //given -// User assignee = mock(User.class); -// User user = mock(User.class); -// Issue issue = new Issue(); -// -// when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); -// when(userRepository.findById(any())).thenReturn(Optional.of(user)).thenReturn(Optional.of(assignee)); -// -// //when then -// assertThrows(CustomException.class, -// ()->issueService.setAssignee(issue.getId(),user.getId(),assignee.getId())); -// } -// -// @Test -// @DisplayName("개발자 추천") -// void candidateDev() { -// //given -// Project project = new Project(); -// -// Dev user1 = new Dev(); -// ReflectionTestUtils.setField(user1,"username","User1"); -// Dev user2 = new Dev(); -// ReflectionTestUtils.setField(user2,"username","User2"); -// List users = Arrays.asList(user1,user2); -// -// ProjectContributor projectContributor1 = new ProjectContributor(); -// ReflectionTestUtils.setField(projectContributor1,"project",project); -// ReflectionTestUtils.setField(projectContributor1,"contributor",user1); -// ProjectContributor projectContributor2 = new ProjectContributor(); -// ReflectionTestUtils.setField(projectContributor2,"project",project); -// ReflectionTestUtils.setField(projectContributor2,"contributor",user2); -// List projectContributors = Arrays.asList(projectContributor1, projectContributor2); -// -// Issue issue1 = new Issue(); -// issue1.setId(1L); -// issue1.setTitle("A A B"); -// issue1.setDescription("B B"); -// issue1.setFixer(user1); -// issue1.setStatus(Issue.Status.FIXED); -// -// Issue issue2 = new Issue(); -// issue1.setId(2L); -// issue2.setTitle("B A"); -// issue2.setDescription("A A A B B B"); -// issue2.setFixer(user2); -// issue2.setStatus(Issue.Status.FIXED); -// -// Issue testIssue1 = new Issue(); -// testIssue1.setId(3L); -// testIssue1.setTitle("A"); -// testIssue1.setDescription("A"); -// -// Issue testIssue2 = new Issue(); -// testIssue2.setId(4L); -// testIssue2.setTitle("B"); -// testIssue2.setDescription("B"); -// -// when(userRepository.findAll()).thenReturn(users); -// when(issueRepository.findById(testIssue1.getId())).thenReturn(Optional.of(testIssue1)); -// when(issueRepository.findById(testIssue2.getId())).thenReturn(Optional.of(testIssue2)); -// when(projectContributorRepository.findAllByContributor(user1)).thenReturn(Arrays.asList(projectContributor1)); -// when(projectContributorRepository.findAllByContributor(user2)).thenReturn(Arrays.asList(projectContributor2)); -// when(issueRepository.findAllByFixer(user1)).thenReturn(Arrays.asList(issue1)); -// when(issueRepository.findAllByFixer(user2)).thenReturn(Arrays.asList(issue2)); -// -// //when -// User result1 = issueService.candidateUser(testIssue1.getId()).get(); -// User result2 = issueService.candidateUser(testIssue2.getId()).get(); -// -// //then -// assertEquals(result1.getUsername(),user1.getUsername()); -// assertEquals(result2.getUsername(),user2.getUsername()); -// } -// -//} \ No newline at end of file +package se.issuetrackingsystem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.util.ReflectionTestUtils; +import se.issuetrackingsystem.common.exception.CustomException; +import se.issuetrackingsystem.issue.domain.Issue; +import se.issuetrackingsystem.issue.repository.IssueRepository; +import se.issuetrackingsystem.issue.service.IssueService; +import se.issuetrackingsystem.project.domain.Project; +import se.issuetrackingsystem.project.repository.ProjectRepository; +import se.issuetrackingsystem.user.domain.*; +import se.issuetrackingsystem.user.repository.ProjectContributorRepository; +import se.issuetrackingsystem.user.repository.UserRepository; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(SpringExtension.class) +public class IssueServiceTests { + + IssueService issueService; + @MockBean + IssueRepository issueRepository; + @MockBean + ProjectRepository projectRepository; + @MockBean + UserRepository userRepository; + @MockBean + ProjectContributorRepository projectContributorRepository; + + @BeforeEach + void setUp() { + issueService = new IssueService(issueRepository, userRepository, projectRepository, projectContributorRepository); + } + + @Test + @DisplayName("이슈 생성 성공") + void issueCreate() { + //given + Project project = mock(Project.class); + User user = mock(User.class); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); + when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); + when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); + + //when + Issue result = issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR); + + //then + assertEquals(result.getTitle(), "Issue1"); + assertEquals(result.getDescription(), "issue"); + assertEquals(result.getReporter(), user); + assertEquals(result.getPriority(), Issue.Priority.MINOR); + assertEquals(result.getProject(), project); + } + + @Test + @DisplayName("이슈 생성 실패-프로젝트_없음") + void issueCreateNoProject() { + //given + Project project = mock(Project.class); + User user = mock(User.class); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.empty()); + when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); + when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); + + //when then + assertThrows(CustomException.class, + ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); + } + + @Test + @DisplayName("이슈 생성 실패-리포터_없음") + void issueCreateNoReporter() { + //given + Project project = mock(Project.class); + User user = mock(User.class); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); + when(userRepository.findById(user.getId())).thenReturn(Optional.empty()); + when(issueRepository.save(any(Issue.class))).thenAnswer(i -> i.getArgument(0)); + + //when then + assertThrows(CustomException.class, + ()-> issueService.create(project.getId(), "Issue1", "issue", user.getId(), Issue.Priority.MINOR)); + } + + @Test + @DisplayName("이슈 찾기 성공") + void getIssue() { + //given + Issue issue = new Issue(); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + + //when + Issue result = issueService.getIssue(issue.getId()); + + //then + assertEquals(issue, result); + } + + @Test + @DisplayName("프로젝트로 이슈들 찾기") + void getIssues() { + //given + Project project = mock(Project.class); + Issue issue1 = new Issue(); + issue1.setProject(project); + Issue issue2 = new Issue(); + issue2.setProject(project); + List issues = Arrays.asList(issue1, issue2); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); + when(issueRepository.findAllByProject(project)).thenReturn(issues); + + //when + List result = this.issueService.getList(project.getId()); + + //then + assertEquals(result.size(), 2); + assertEquals(result.get(0), issue1); + assertEquals(result.get(1), issue2); + } + + @Test + @DisplayName("Assignee로 이슈들 찾기") + void getIssuesByAssignee() { + //given + User user = mock(User.class); + Issue issue1 = new Issue(); + issue1.setAssignee(user); + Issue issue2 = new Issue(); + issue2.setAssignee(user); + List issues = Arrays.asList(issue1, issue2); + + when(userRepository.findById(user.getId())).thenReturn(Optional.of(user)); + when(issueRepository.findAllByAssignee(user)).thenReturn(issues); + + //when + List result = this.issueService.getListByAssignee(user.getId()); + + //then + assertEquals(result.size(), 2); + assertEquals(result.get(0), issue1); + assertEquals(result.get(1), issue2); + } + + @Test + @DisplayName("프로젝트와 상태로 이슈들 찾기") + void getIssuesWithStatus() { + //given + Project project = mock(Project.class); + Issue issue1 = new Issue(); + issue1.setProject(project); + issue1.setStatus(Issue.Status.FIXED); + Issue issue2 = new Issue(); + issue2.setProject(project); + issue2.setStatus(Issue.Status.FIXED); + List issues = Arrays.asList(issue1, issue2); + + when(projectRepository.findById(project.getId())).thenReturn(Optional.of(project)); + when(issueRepository.findAllByProjectAndStatus(project, Issue.Status.FIXED)).thenReturn(issues); + + //then + List result = this.issueService.getList(project.getId(), Issue.Status.FIXED); + + //then + assertEquals(result.size(), 2); + assertEquals(result.get(0), issue1); + assertEquals(result.get(1), issue2); + } + + @Test + @DisplayName("이슈 수정 성공") + void issueModify() { + //given + Issue issue = new Issue(); + issue.setTitle("Hey"); + issue.setDescription("Apple"); + issue.setPriority(Issue.Priority.MINOR); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + + //when + Issue result = issueService.modify(issue.getId(), "Hello", "Banana", Issue.Priority.MAJOR); + + //then + assertEquals(result.getTitle(), "Hello"); + assertEquals(result.getDescription(), "Banana"); + assertEquals(result.getPriority(), Issue.Priority.MAJOR); + } + + @Test + @DisplayName("이슈 삭제 성공") + void issueDelete() { + //given + Issue issue = mock(Issue.class); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + + //when + Issue result = this.issueService.delete(issue.getId()); + + //then + assertEquals(result, issue); + } + + @Test + @DisplayName("이슈 Assignee 설정 성공") + void setAssignee() { + //given + User assignee = mock(User.class); + PL pl = new PL(); + Issue issue = new Issue(); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + when(userRepository.findById(any())).thenReturn(Optional.of(pl)).thenReturn(Optional.of(assignee)); + + //when + Issue result = issueService.setAssignee(issue.getId(),pl.getId(),assignee.getId()); + + //then + assertEquals(result.getAssignee(),assignee); + } + + @Test + @DisplayName("이슈 Assignee 설정 실패_PL이 아님") + void setAssigneeFail() { + //given + User assignee = mock(User.class); + User user = mock(User.class); + Issue issue = new Issue(); + + when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue)); + when(userRepository.findById(any())).thenReturn(Optional.of(user)).thenReturn(Optional.of(assignee)); + + //when then + assertThrows(CustomException.class, + ()->issueService.setAssignee(issue.getId(),user.getId(),assignee.getId())); + } + + @Test + @DisplayName("개발자 추천") + void candidateDev() { + //given + Project project = new Project(); + + Dev user1 = new Dev(); + ReflectionTestUtils.setField(user1,"username","User1"); + Dev user2 = new Dev(); + ReflectionTestUtils.setField(user2,"username","User2"); + List users = Arrays.asList(user1,user2); + + ProjectContributor projectContributor1 = new ProjectContributor(); + ReflectionTestUtils.setField(projectContributor1,"project",project); + ReflectionTestUtils.setField(projectContributor1,"contributor",user1); + ProjectContributor projectContributor2 = new ProjectContributor(); + ReflectionTestUtils.setField(projectContributor2,"project",project); + ReflectionTestUtils.setField(projectContributor2,"contributor",user2); + List projectContributors = Arrays.asList(projectContributor1, projectContributor2); + + Issue issue1 = new Issue(); + issue1.setId(1L); + issue1.setTitle("A A B"); + issue1.setDescription("B B"); + issue1.setFixer(user1); + issue1.setStatus(Issue.Status.FIXED); + + Issue issue2 = new Issue(); + issue1.setId(2L); + issue2.setTitle("B A"); + issue2.setDescription("A A A B B B"); + issue2.setFixer(user2); + issue2.setStatus(Issue.Status.FIXED); + + Issue testIssue1 = new Issue(); + testIssue1.setId(3L); + testIssue1.setTitle("A"); + testIssue1.setDescription("A"); + + Issue testIssue2 = new Issue(); + testIssue2.setId(4L); + testIssue2.setTitle("B"); + testIssue2.setDescription("B"); + + when(userRepository.findAll()).thenReturn(users); + when(issueRepository.findById(testIssue1.getId())).thenReturn(Optional.of(testIssue1)); + when(issueRepository.findById(testIssue2.getId())).thenReturn(Optional.of(testIssue2)); + when(projectContributorRepository.findAllByContributor(user1)).thenReturn(Arrays.asList(projectContributor1)); + when(projectContributorRepository.findAllByContributor(user2)).thenReturn(Arrays.asList(projectContributor2)); + when(issueRepository.findAllByFixer(user1)).thenReturn(Arrays.asList(issue1)); + when(issueRepository.findAllByFixer(user2)).thenReturn(Arrays.asList(issue2)); + + //when + User result1 = issueService.candidateUser(testIssue1.getId()).get(); + User result2 = issueService.candidateUser(testIssue2.getId()).get(); + + //then + assertEquals(result1.getUsername(),user1.getUsername()); + assertEquals(result2.getUsername(),user2.getUsername()); + } + +} \ No newline at end of file