Skip to content

Commit

Permalink
feat: Comment,Issue에 대한 Repository,Service JUnit Test코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
KEEKE132 committed May 26, 2024
1 parent 6a68ff0 commit 397b430
Show file tree
Hide file tree
Showing 8 changed files with 691 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public Comment create(Long issueid, String content, Long authorid){
return comment;
}

public void modify(Long commentid,String content){
public Comment modify(Long commentid,String content){
Comment comment = this.commentRepository.findById(commentid).orElseThrow(()->new CustomException(ErrorCode.COMMENT_NOT_FOUND));
comment.setMessage(content);
this.commentRepository.save(comment);
return comment;
}

public void delete(Long commentid){
Expand All @@ -50,8 +51,4 @@ public List<Comment> getList(Long issueid){
List<Comment> comments = this.commentRepository.findAllByIssue(issue);
return comments;
}

public Comment getComment(Long commentid){
return this.commentRepository.findById(commentid).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void issueDelete(@RequestParam("issueId") Long issueid){

@PatchMapping
public void issueModify(@RequestBody IssueRequest issueRequest,@RequestParam("issueId") Long issueid){
this.issueService.modify(issueid,issueRequest.getDescription(),issueRequest.getPriority());
this.issueService.modify(issueid,issueRequest.getTitle(),issueRequest.getDescription(),issueRequest.getPriority());
}

@PostMapping("/assignees")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import se.issuetrackingsystem.project.domain.Project;
import se.issuetrackingsystem.project.repository.ProjectRepository;
import se.issuetrackingsystem.user.domain.Dev;
import se.issuetrackingsystem.user.domain.PL;
import se.issuetrackingsystem.user.domain.ProjectContributor;
import se.issuetrackingsystem.user.domain.User;
import se.issuetrackingsystem.user.repository.ProjectContributorRepository;
Expand Down Expand Up @@ -71,28 +72,32 @@ public List<Issue> getList(Long projectid, Issue.Status status){
return issues;
}

public void modify(Long issueid, String description, Issue.Priority priority){
public Issue modify(Long issueid,String title ,String description, Issue.Priority priority){
Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND));
issue.setTitle(title);
issue.setDescription(description);
issue.setUpdated_at(LocalDateTime.now());
issue.setPriority(priority);
this.issueRepository.save(issue);
return issue;
}

public void delete(Long issueid){
public Issue delete(Long issueid){
Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND));
this.issueRepository.delete(issue);
return issue;
}

public void setAssignee(Long issueid,Long userid ,Long assigneeid){
public Issue setAssignee(Long issueid,Long userid ,Long assigneeid){
Issue issue = this.issueRepository.findById(issueid).orElseThrow(()->new CustomException(ErrorCode.ISSUE_NOT_FOUND));
User user = this.userRepository.findById(userid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
if (user.getRole() != "PL") {
if (!"PL".equals(user.getRole())) {
throw new CustomException(ErrorCode.ROLE_FORBIDDEN);
}
issue.setAssignee(userRepository.findById(assigneeid).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)));
issue.setStatus(Issue.Status.ASSIGNED);
this.issueRepository.save(issue);
return issue;
}

public void changeStatus(Long userid, Long issueid){
Expand Down
77 changes: 77 additions & 0 deletions src/test/java/se/issuetrackingsystem/CommentRepositoryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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<Comment> 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());
}
}
112 changes: 112 additions & 0 deletions src/test/java/se/issuetrackingsystem/CommentServiceTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +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<Comment> comments = Arrays.asList(
comment1,comment2
);

when(issueRepository.findById(issue.getId())).thenReturn(Optional.of(issue));
when(commentRepository.findAllByIssue(issue)).thenReturn(comments);

//when
List<Comment> result = commentService.getList(issue.getId());

//then
assertEquals(result.size(),2);
assertEquals("1M",result.get(0).getMessage());
assertEquals("2M",result.get(1).getMessage());
}
}
81 changes: 81 additions & 0 deletions src/test/java/se/issuetrackingsystem/IssueRepositoryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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<Issue> 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());
}
}
Loading

0 comments on commit 397b430

Please sign in to comment.