IntegrationService { public void commentIssue(String repositoryName, User user, Collection<Commit> commits, String issue) { if(jiraService.isExistingIssue(issue)) { commits.forEach(commit -> { if(!jiraService.isIssueAlreadyCommented(issue, commit.getId())) { try { log.info("Comment issue <{}> for commit <{}>", issue, commit); if(!jiraService.performTransition(commit.getMessage(), issue, buildCommentForTransition(user, repositoryName, commit))) { jiraService.commentIssue(issue, new Comment(buildComment(user, repositoryName, commit))); } } catch (IOException e) { log.error("Unable to comment issue <{}>", issue, e); } } else { log.warn("Issue <{}> has already been commented for commit <{}>", issue, commit.getId()); } }); } else { log.warn("Issue <{}> has been mentioned, but does not exists", issue); } } @Inject  IntegrationService(GitLabService gitLabService, JiraService jiraService); String getDateAsString(Date date); String buildComment(User user, String repositoryName, Commit commit); String buildCommentForTransition(User user, String repositoryName, Commit commit); void commentIssue(String repositoryName, User user, Collection<Commit> commits, String issue); void performPushEvent(Event event); User getUser(Event event); static final DateFormat DATE_FORMAT; }
@Test public void commentExistingIssueWithoutUsername() throws Exception { String issue = "TESTGITLAB-1"; String repository = "test-repo"; String commitId = "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327"; ArgumentCaptor<Comment> commentArgumentCaptor = ArgumentCaptor.forClass(Comment.class); when(jiraService.isExistingIssue(issue)).thenReturn(true); when(jiraService.isIssueAlreadyCommented(issue, commitId)).thenReturn(false); String message = "Very nice commit !"; String url = "http: Date date = new Date(); service.commentIssue(repository, new User(1L, null, "john.smith@mocked.com"), Collections.singletonList(Commit.builder() .id(commitId) .message(message) .url(url) .timestamp(date) .build()), issue); verify(jiraService, times(1)).commentIssue(eq(issue), commentArgumentCaptor.capture()); Comment comment = commentArgumentCaptor.getValue(); assertThat(comment.getBody()) .contains("john.smith@mocked.com", repository, message, url, IntegrationService.DATE_FORMAT.format(date), message); }
@Test public void commentNonExistingIssue() throws Exception { String issue = "TESTGITLAB-1"; String repository = "test-repo"; String commitId = "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327"; when(jiraService.isExistingIssue(issue)).thenReturn(false); when(jiraService.isIssueAlreadyCommented(issue, commitId)).thenReturn(false); service.commentIssue(repository, new User(1L, "John Smith", "john.smith@mocked.com"), Collections.singletonList(Commit.builder().id(commitId).build()), issue); verify(jiraService, times(0)).commentIssue(eq(issue), any(Comment.class)); }
@Test public void commentExistingIssueWithUnavailableJira() throws Exception { String issue = "TESTGITLAB-1"; String repository = "test-repo"; String commitId = "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327"; when(jiraService.isExistingIssue(issue)).thenReturn(true); when(jiraService.isIssueAlreadyCommented(issue, commitId)).thenReturn(false); when(jiraService.commentIssue(eq(issue), any(Comment.class))) .thenThrow(new IOException()); service.commentIssue(repository, new User(1L, "John Smith", "john.smith@mocked.com"), Collections.singletonList(Commit.builder().id(commitId).build()), issue); verify(jiraService, times(1)).commentIssue(eq(issue), any(Comment.class)); }
@Test public void issueShouldNotBeCommentedWhenItsAlreadyIs() throws IOException { String issue = "TESTGITLAB-1"; String repository = "test-repo"; String commitId = "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327"; when(jiraService.isExistingIssue(issue)).thenReturn(true); when(jiraService.isIssueAlreadyCommented(issue, commitId)).thenReturn(true); service.commentIssue(repository, new User(1L, "John Smith", "john.smith@mocked.com"), Collections.singletonList(Commit.builder().id(commitId).build()), issue); verify(jiraService, times(0)).commentIssue(eq(issue), any(Comment.class)); }
@Test public void commentExistingIssue() throws Exception { String issue = "TESTGITLAB-1"; String repository = "test-repo"; String commitId = "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327"; ArgumentCaptor<Comment> commentArgumentCaptor = ArgumentCaptor.forClass(Comment.class); when(jiraService.isExistingIssue(issue)).thenReturn(true); when(jiraService.isIssueAlreadyCommented(issue, commitId)).thenReturn(false); service.commentIssue(repository, new User(1L, "John Smith", "john.smith@mocked.com"), Collections.singletonList(Commit.builder().id(commitId).build()), issue); verify(jiraService, times(1)).commentIssue(eq(issue), commentArgumentCaptor.capture()); Comment comment = commentArgumentCaptor.getValue(); assertThat(comment.getBody()) .contains("unknown date of commit"); }