@Test
  public void shouldExecuteOnRootProjectWithAllParams() throws Exception {
    Project project = mock(Project.class);
    when(project.isRoot()).thenReturn(true).thenReturn(false);

    assertThat(sensor.shouldExecuteOnProject(project)).isEqualTo(true);
  }
  @Test
  public void shouldCollectIssuesByPriority() throws Exception {
    Filter filter = new Filter(null, 1l, null, null, null, null, null, null, false);
    JiraSoapService jiraSoapService = mock(JiraSoapService.class);

    JiraSoapSession soapSession = mock(JiraSoapSession.class);
    when(soapSession.getJiraSoapService()).thenReturn(jiraSoapService);

    JiraSoapServiceWrapper wrapper = new JiraSoapServiceWrapper(jiraSoapService, null, settings);
    when(soapSession.getJiraService(Matchers.<RuleFinder>any(), Matchers.<Settings>any()))
        .thenReturn(wrapper);

    RemoteIssue issue1 = new RemoteIssue();
    issue1.setPriority("1");
    issue1.setId("1");
    RemoteIssue issue2 = new RemoteIssue();
    issue2.setPriority("1");
    issue2.setId("2");
    RemoteIssue issue3 = new RemoteIssue();
    issue3.setPriority("2");
    issue3.setId("3");
    when(jiraSoapService.getIssuesFromFilter("token", "1"))
        .thenReturn(new RemoteIssue[] {issue1, issue2, issue3});

    Map<Long, Integer> foundIssues = sensor.collectIssuesByPriority(wrapper, "token", filter);
    assertThat(foundIssues.size()).isEqualTo(2);
    assertThat(foundIssues.get(1l)).isEqualTo(2);
    assertThat(foundIssues.get(2l)).isEqualTo(1);
  }
  @Test
  public void shouldNotExecuteOnRootProjectifOneParamMissing() throws Exception {
    Project project = mock(Project.class);
    when(project.isRoot()).thenReturn(true).thenReturn(false);

    settings.removeProperty(JiraConstants.SERVER_URL_PROPERTY);
    sensor = new JiraSensor(settings);

    assertThat(sensor.shouldExecuteOnProject(project)).isEqualTo(false);
  }
  @Test
  public void testPresenceOfProperties() throws Exception {
    assertThat(sensor.missingMandatoryParameters()).isEqualTo(false);

    settings.removeProperty(JiraConstants.PASSWORD_PROPERTY);
    sensor = new JiraSensor(settings);
    assertThat(sensor.missingMandatoryParameters()).isEqualTo(true);

    settings.removeProperty(JiraConstants.USERNAME_PROPERTY);
    sensor = new JiraSensor(settings);
    assertThat(sensor.missingMandatoryParameters()).isEqualTo(true);

    settings.removeProperty(JiraConstants.FILTER_PROPERTY);
    sensor = new JiraSensor(settings);
    assertThat(sensor.missingMandatoryParameters()).isEqualTo(true);

    settings.removeProperty(JiraConstants.SERVER_URL_PROPERTY);
    sensor = new JiraSensor(settings);
    assertThat(sensor.missingMandatoryParameters()).isEqualTo(true);
  }
  @Test
  public void testSaveMeasures() {
    SensorContext context = mock(SensorContext.class);
    String url = "http://localhost/jira";
    String priorityDistribution = "Critical=1";

    sensor.saveMeasures(context, url, 1, priorityDistribution);

    verify(context)
        .saveMeasure(argThat(new IsMeasure(JiraMetrics.ISSUES, 1.0, priorityDistribution)));
    verifyNoMoreInteractions(context);
  }
  @Test
  public void faillIfNoFilterFound() throws Exception {
    JiraSoapService jiraSoapService = mock(JiraSoapService.class);

    JiraSoapSession soapSession = mock(JiraSoapSession.class);
    when(soapSession.getJiraSoapService()).thenReturn(jiraSoapService);

    JiraSoapServiceWrapper wrapper = new JiraSoapServiceWrapper(jiraSoapService, null, settings);
    when(soapSession.getJiraService(Matchers.<RuleFinder>any(), Matchers.<Settings>any()))
        .thenReturn(wrapper);

    when(jiraSoapService.getFavouriteFilters("token")).thenReturn(new RemoteFilter[0]);

    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("Unable to find filter 'myFilter' in JIRA");

    sensor.findJiraFilter(wrapper, "token");
  }
  @Test
  public void shouldCollectPriorities() throws Exception {
    JiraSoapService jiraSoapService = mock(JiraSoapService.class);
    RemotePriority priority1 = new RemotePriority();
    priority1.setId("1");
    priority1.setName("Minor");
    when(jiraSoapService.getPriorities("token")).thenReturn(new RemotePriority[] {priority1});

    JiraSoapSession soapSession = mock(JiraSoapSession.class);
    when(soapSession.getJiraSoapService()).thenReturn(jiraSoapService);

    JiraSoapServiceWrapper wrapper = new JiraSoapServiceWrapper(jiraSoapService, null, settings);
    when(soapSession.getJiraService(Matchers.<RuleFinder>any(), Matchers.<Settings>any()))
        .thenReturn(wrapper);

    Map<Long, String> foundPriorities = sensor.collectPriorities(wrapper, "token");
    assertThat(foundPriorities.size()).isEqualTo(1);
    assertThat(foundPriorities.get(1l)).isEqualTo("Minor");
  }
  @Test
  public void shouldFindFiltersWithPreviousJiraVersions() throws Exception {
    JiraSoapService jiraSoapService = mock(JiraSoapService.class);

    JiraSoapSession soapSession = mock(JiraSoapSession.class);
    when(soapSession.getJiraSoapService()).thenReturn(jiraSoapService);

    JiraSoapServiceWrapper wrapper = new JiraSoapServiceWrapper(jiraSoapService, null, settings);
    when(soapSession.getJiraService(Matchers.<RuleFinder>any(), Matchers.<Settings>any()))
        .thenReturn(wrapper);

    RemoteFilter myFilter = new RemoteFilter();
    myFilter.setName("myFilter");
    myFilter.setId("1");
    when(jiraSoapService.getSavedFilters("token")).thenReturn(new RemoteFilter[] {myFilter});
    when(jiraSoapService.getFavouriteFilters("token")).thenThrow(RemoteException.class);

    Filter foundFilter = sensor.findJiraFilter(wrapper, "token");
    assertThat(foundFilter.getName()).isEqualTo(myFilter.getName());
  }
 @Test
 public void testToString() throws Exception {
   assertThat(sensor.toString()).isEqualTo("JIRA issues sensor");
 }
 @Test
 public void shouldNotExecuteOnNonRootProject() throws Exception {
   assertThat(sensor.shouldExecuteOnProject(mock(Project.class))).isEqualTo(false);
 }