import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.junit.runner.RunWith;
import org.mockito.Matchers;
-import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
-import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.rule.RuleKey;
import static org.mockito.Matchers.anyCollection;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-@RunWith(MockitoJUnitRunner.class)
public class IssueQueryServiceTest {
@Rule
@Rule
public ExpectedException expectedException = ExpectedException.none();
- @Mock
- DbClient dbClient;
+ DbClient dbClient = mock(DbClient.class);
+ DbSession session = mock(DbSession.class);
+ ComponentDao componentDao = mock(ComponentDao.class);
+ AuthorDao authorDao = mock(AuthorDao.class);
+ ComponentService componentService = mock(ComponentService.class);
+ System2 system = mock(System2.class);
- @Mock
- DbSession session;
-
- @Mock
- ComponentDao componentDao;
-
- @Mock
- AuthorDao authorDao;
-
- @Mock
- ComponentService componentService;
-
- @Mock
- System2 system;
-
- IssueQueryService issueQueryService;
+ IssueQueryService underTest;
@Before
public void setUp() {
}
});
- issueQueryService = new IssueQueryService(dbClient, componentService, system, userSessionRule);
+ underTest = new IssueQueryService(dbClient, componentService, system, userSessionRule);
}
@Test
when(componentService.getDistinctQualifiers(eq(session), Matchers.anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.PROJECT));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.issueKeys()).containsOnly("ABCDE1234");
assertThat(query.severities()).containsOnly("MAJOR", "MINOR");
assertThat(query.statuses()).containsOnly("CLOSED");
}
});
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.componentUuids()).containsOnly("<UNKNOWN>");
}
map.put("componentUuids", newArrayList("ABCD"));
try {
- issueQueryService.createFromMap(map);
+ underTest.createFromMap(map);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("components and componentUuids cannot be set simultaneously");
map.put("projectUuids", newArrayList("ABCD"));
try {
- issueQueryService.createFromMap(map);
+ underTest.createFromMap(map);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("projects and projectUuids cannot be set simultaneously");
map.put("componentRootUuids", newArrayList("ABCD"));
try {
- issueQueryService.createFromMap(map);
+ underTest.createFromMap(map);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("componentRoots and componentRootUuids cannot be set simultaneously");
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.<String>newHashSet());
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.onComponentOnly()).isFalse();
assertThat(query.componentUuids()).contains("ABCD");
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newTreeSet(Arrays.asList("TRK", "BRC")));
try {
- issueQueryService.createFromMap(map);
+ underTest.createFromMap(map);
Fail.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch (IllegalArgumentException exception) {
assertThat(exception).hasMessage("All components must have the same qualifier, found BRC,TRK");
userSessionRule.addProjectUuidPermissions(UserRole.USER, viewUuid);
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.viewUuids()).containsExactly(viewUuid);
assertThat(query.onComponentOnly()).isFalse();
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.VIEW));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.viewUuids()).isNotEmpty().doesNotContain(subViewUuid);
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.PROJECT));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.projectUuids()).containsExactly(projectUuid);
assertThat(query.onComponentOnly()).isFalse();
}
when(componentService.componentUuids(isA(DbSession.class), anyCollection(), eq(true))).thenReturn(Sets.newHashSet(projectUuid));
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.PROJECT));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.projectUuids()).isEmpty();
assertThat(query.componentUuids()).containsExactly(projectUuid);
assertThat(query.onComponentOnly()).isTrue();
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet("DEV"));
when(authorDao.selectScmAccountsByDeveloperUuids(isA(DbSession.class), anyCollection())).thenReturn(Lists.newArrayList(login1, login2));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.authors()).containsExactly(login1, login2);
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet("DEV"));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.authors()).containsExactly(login);
}
Map<String, Object> map = newHashMap();
map.put("componentUuids", newArrayList(copyProjectUuid));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.authors()).containsExactly(login1, login2);
assertThat(query.projectUuids()).containsExactly(projectUuid);
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.MODULE));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.moduleRootUuids()).containsExactly(moduleUuid);
assertThat(query.onComponentOnly()).isFalse();
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.DIRECTORY));
when(componentService.getByUuids(isA(DbSession.class), anyCollection())).thenReturn(Arrays.asList(new ComponentDto().setModuleUuid(moduleUuid).setPath(directoryPath)));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.moduleUuids()).contains(moduleUuid);
assertThat(query.directories()).containsExactly(directoryPath);
assertThat(query.onComponentOnly()).isFalse();
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.FILE));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.fileUuids()).containsExactly(fileUuid);
}
when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet(Qualifiers.UNIT_TEST_FILE));
- IssueQuery query = issueQueryService.createFromMap(map);
+ IssueQuery query = underTest.createFromMap(map);
assertThat(query.fileUuids()).containsExactly(fileUuid);
}
map.put("createdInLast", "palap");
try {
- issueQueryService.createFromMap(map);
+ underTest.createFromMap(map);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("createdAfter and createdInLast cannot be set simultaneously");
Map<String, Object> map = newHashMap();
map.put("createdInLast", "1y2m3w4d");
- assertThat(issueQueryService.createFromMap(map).createdAfter()).isEqualTo(DateUtils.parseDateTime("2012-04-30T07:35:00+0100"));
+ assertThat(underTest.createFromMap(map).createdAfter()).isEqualTo(DateUtils.parseDateTime("2012-04-30T07:35:00+0100"));
}
@Test
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("'createdAfter' and 'sinceLeakPeriod' cannot be set simultaneously");
- issueQueryService.createFromRequest(new SearchWsRequest()
+ underTest.createFromRequest(new SearchWsRequest()
.setSinceLeakPeriod(true)
.setCreatedAfter("2013-07-25T07:35:00+0100"));
}
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("One and only one component must be provided when searching since leak period");
- issueQueryService.createFromRequest(new SearchWsRequest().setSinceLeakPeriod(true));
+ underTest.createFromRequest(new SearchWsRequest().setSinceLeakPeriod(true));
}
@Test
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("One and only one component must be provided when searching since leak period");
- issueQueryService.createFromRequest(new SearchWsRequest()
+ underTest.createFromRequest(new SearchWsRequest()
.setSinceLeakPeriod(true)
.setComponentUuids(Collections.singletonList("component-uuid"))
- .setProjectUuids(Collections.singletonList("project-uuid"))
- );
+ .setProjectUuids(Collections.singletonList("project-uuid")));
+ }
+
+ @Test
+ public void fail_if_date_is_not_formatted_correctly() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("'unknown-date' cannot be parsed as either a date or date+time");
+
+ underTest.createFromRequest(new SearchWsRequest()
+ .setCreatedAfter("unknown-date"));
+
}
}