@Test public void shouldGiveDefaultPriority() throws Exception { assertThat(jiraIssueCreator.sonarSeverityToJiraPriorityId(RulePriority.BLOCKER, settings)) .isEqualTo("1"); assertThat(jiraIssueCreator.sonarSeverityToJiraPriorityId(RulePriority.CRITICAL, settings)) .isEqualTo("2"); assertThat(jiraIssueCreator.sonarSeverityToJiraPriorityId(RulePriority.MAJOR, settings)) .isEqualTo("3"); assertThat(jiraIssueCreator.sonarSeverityToJiraPriorityId(RulePriority.MINOR, settings)) .isEqualTo("4"); assertThat(jiraIssueCreator.sonarSeverityToJiraPriorityId(RulePriority.INFO, settings)) .isEqualTo("5"); }
@Test public void shouldFailToCreateSoapSessionWithIncorrectUrl() throws Exception { settings.removeProperty(JiraConstants.SERVER_URL_PROPERTY); settings.appendProperty(JiraConstants.SERVER_URL_PROPERTY, "my.server"); thrown.expect(IllegalStateException.class); thrown.expectMessage( "The JIRA server URL is not a valid one: my.server/rpc/soap/jirasoapservice-v2"); jiraIssueCreator.createSoapSession(settings); }
@Test public void shouldFailToCreateIssueIfCantConnect() throws Exception { // Given that JiraSoapSession soapSession = mock(JiraSoapSession.class); doThrow(RemoteException.class).when(soapSession).connect(anyString(), anyString()); // Verify thrown.expect(IllegalStateException.class); thrown.expectMessage("Impossible to connect to the JIRA server"); jiraIssueCreator.doCreateIssue(sonarIssue, soapSession, settings, ""); }
@Test public void shouldFailToCreateIssueIfRemoteError() throws Exception { // Given that JiraSoapService jiraSoapService = mock(JiraSoapService.class); doThrow(RemoteException.class) .when(jiraSoapService) .createIssue(anyString(), any(RemoteIssue.class)); // Verify thrown.expect(IllegalStateException.class); thrown.expectMessage("Impossible to create the issue on the JIRA server (my.jira)"); jiraIssueCreator.sendRequest(jiraSoapService, "", null, "my.jira", "foo"); }
@Test public void shouldFailExecuteIfRemoteProblem() throws Exception { when(jiraIssueCreator.createIssue(review, settings, parameters)) .thenThrow( new IllegalStateException( "Impossible to " + "create an issue on JIRA. A problem occured with the remote server: Server Error")); thrown.expect(IllegalStateException.class); thrown.expectMessage( "Impossible to create an issue on JIRA. A problem occured with the remote server: Server Error"); action.doExecute(mutableReview, review, workflowContext, parameters); }
@Test public void shouldFailToCreateIssueIfCantAuthenticate() throws Exception { // Given that JiraSoapService jiraSoapService = mock(JiraSoapService.class); doThrow(RemoteAuthenticationException.class) .when(jiraSoapService) .createIssue(anyString(), any(RemoteIssue.class)); // Verify thrown.expect(IllegalStateException.class); thrown.expectMessage( "Impossible to connect to the JIRA server (my.jira) because of invalid credentials for user foo"); jiraIssueCreator.sendRequest(jiraSoapService, "", null, "my.jira", "foo"); }
@Test public void shouldInitRemoteIssueWithComponent() throws Exception { // Given that settings.setProperty(JiraConstants.JIRA_ISSUE_COMPONENT_ID, "123"); RemoteIssue expectedIssue = new RemoteIssue(); expectedIssue.setProject("TEST"); expectedIssue.setType("3"); expectedIssue.setPriority("4"); expectedIssue.setSummary("Sonar Issue #ABCD - Avoid cycle between java packages"); expectedIssue.setDescription( "Issue detail:\n{quote}\nThe Cyclomatic Complexity of this method is 14 which is greater than 10 authorized.\n" + "{quote}\n\n\nCheck it on Sonar: http://my.sonar.com/issue/show/ABCD"); expectedIssue.setComponents(new RemoteComponent[] {new RemoteComponent("123", null)}); // Verify RemoteIssue returnedIssue = jiraIssueCreator.initRemoteIssue(sonarIssue, settings, ""); assertThat(returnedIssue).isEqualTo(expectedIssue); }
@Test public void shouldCreateIssue() throws Exception { // Given that RemoteIssue issue = new RemoteIssue(); JiraSoapService jiraSoapService = mock(JiraSoapService.class); when(jiraSoapService.createIssue(anyString(), any(RemoteIssue.class))).thenReturn(issue); JiraSoapSession soapSession = mock(JiraSoapSession.class); when(soapSession.getJiraSoapService()).thenReturn(jiraSoapService); // Verify RemoteIssue returnedIssue = jiraIssueCreator.doCreateIssue(sonarIssue, soapSession, settings, ""); verify(soapSession).connect("foo", "bar"); verify(soapSession).getJiraSoapService(); verify(soapSession).getAuthenticationToken(); assertThat(returnedIssue).isEqualTo(issue); }
@Test public void shouldInitRemoteIssueWithoutName() throws Exception { // Given that when(ruleFinder.findByKey(RuleKey.of("squid", "CycleBetweenPackages"))) .thenReturn(org.sonar.api.rules.Rule.create().setName(null)); RemoteIssue expectedIssue = new RemoteIssue(); expectedIssue.setProject("TEST"); expectedIssue.setType("3"); expectedIssue.setPriority("4"); expectedIssue.setSummary("Sonar Issue #ABCD"); expectedIssue.setDescription( "Issue detail:\n{quote}\nThe Cyclomatic Complexity of this method is 14 which is greater than 10 authorized.\n" + "{quote}\n\n\nCheck it on Sonar: http://my.sonar.com/issue/show/ABCD"); // Verify RemoteIssue returnedIssue = jiraIssueCreator.initRemoteIssue(sonarIssue, settings, ""); assertThat(returnedIssue.getSummary()).isEqualTo(expectedIssue.getSummary()); assertThat(returnedIssue.getDescription()).isEqualTo(expectedIssue.getDescription()); assertThat(returnedIssue).isEqualTo(expectedIssue); }
@Before public void init() throws Exception { mutableReview = mock(MutableReview.class); comment = mock(Comment.class); when(mutableReview.createComment()).thenReturn(comment); review = mock(Review.class); workflowContext = mock(WorkflowContext.class); settings = new Settings(); when(workflowContext.getProjectSettings()).thenReturn(settings); assigneeLogin = "******"; jiraIssueCreator = mock(JiraIssueCreator.class); reviewDao = mock(ReviewDao.class); userFinder = mock(UserFinder.class); remoteIssue = new Issue( null, null, "FOO-15", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); parameters = new HashMap<String, String>(); parameters.put(JiraConstants.JIRA_ISSUE_REPORTER_PROPERTY, workflowContext.getUserLogin()); parameters.put(JiraConstants.JIRA_ISSUE_ASSIGNEE_PROPERTY, assigneeLogin); ReviewDto reviewDto = new ReviewDto(); reviewDto.setAssigneeId(40L); when(reviewDao.findById(Mockito.anyLong())).thenReturn(reviewDto); User assignee = new User(); assignee.setLogin(assigneeLogin); when(userFinder.findById(Mockito.anyInt())).thenReturn(assignee); when(jiraIssueCreator.createIssue(review, settings, parameters)).thenReturn(remoteIssue); action = new LinkFunction(jiraIssueCreator, reviewDao, userFinder); }
@Test public void shouldCreateSoapSession() throws Exception { JiraSoapSession soapSession = jiraIssueCreator.createSoapSession(settings); assertThat(soapSession.getWebServiceUrl().toString()) .isEqualTo("http://my.jira.com/rpc/soap/jirasoapservice-v2"); }