if (IssueQuery.SORT_BY_CLOSE_DATE.equals(sort)) {
return new CloseDateSortIssueProcessor();
}
- throw new IllegalArgumentException("Cannot sort issues on field : " + sort);
+ throw new IllegalArgumentException("Cannot sort on field : " + sort);
}
abstract static class IssueProcessor {
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class ComponentsFinderSortTest {
@Test
- public void should_sort_by_name() {
+ public void should_sort_by_asc_name() {
List<? extends Component> dtoList = newArrayList(
new ComponentDto().setKey("org.codehaus.sonar").setName("Sonar"),
new ComponentDto().setKey("org.apache.tika:tika").setName("Apache Tika"),
);
ComponentQuery query = ComponentQuery.builder().sort(ComponentQuery.SORT_BY_NAME).asc(true).build();
- ComponentsFinderSort issuesFinderSort = new ComponentsFinderSort(dtoList, query);
+ ComponentsFinderSort finderSort = new ComponentsFinderSort(dtoList, query);
- List<Component> result = newArrayList(issuesFinderSort.sort());
+ List<Component> result = newArrayList(finderSort.sort());
assertThat(result).hasSize(4);
assertThat(result.get(0).name()).isEqualTo("Apache Tika");
assertThat(result.get(3).name()).isNull();
}
+ @Test
+ public void should_sort_by_desc_name() {
+ List<? extends Component> dtoList = newArrayList(
+ new ComponentDto().setKey("org.codehaus.sonar").setName("Sonar"),
+ new ComponentDto().setKey("org.apache.tika:tika").setName("Apache Tika"),
+ new ComponentDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent"),
+ new ComponentDto().setKey("org.codehaus.sample")
+ );
+
+ ComponentQuery query = ComponentQuery.builder().sort(ComponentQuery.SORT_BY_NAME).asc(false).build();
+ ComponentsFinderSort finderSort = new ComponentsFinderSort(dtoList, query);
+
+ List<Component> result = newArrayList(finderSort.sort());
+
+ assertThat(result).hasSize(4);
+ assertThat(result.get(0).name()).isNull();
+ assertThat(result.get(1).name()).isEqualTo("Sonar");
+ assertThat(result.get(2).name()).isEqualTo("PicoContainer Parent");
+ assertThat(result.get(3).name()).isEqualTo("Apache Tika");
+ }
+
+ @Test
+ public void should_not_sort_with_null_sort() {
+ List<? extends Component> dtoList = newArrayList(
+ new ComponentDto().setKey("org.codehaus.sonar").setName("Sonar"),
+ new ComponentDto().setKey("org.apache.tika:tika").setName("Apache Tika"),
+ new ComponentDto().setKey("org.picocontainer:picocontainer-parent").setName("PicoContainer Parent"),
+ new ComponentDto().setKey("org.codehaus.sample")
+ );
+
+ ComponentQuery query = ComponentQuery.builder().sort(null).build();
+ ComponentsFinderSort finderSort = new ComponentsFinderSort(dtoList, query);
+
+ List<Component> result = newArrayList(finderSort.sort());
+
+ assertThat(result).hasSize(4);
+ assertThat(result.get(0).name()).isEqualTo("Sonar");
+ assertThat(result.get(1).name()).isEqualTo("Apache Tika");
+ assertThat(result.get(2).name()).isEqualTo("PicoContainer Parent");
+ assertThat(result.get(3).name()).isNull();
+ }
+
+ @Test
+ public void should_fail_to_sort_with_unknown_sort() {
+ ComponentQuery query = mock(ComponentQuery.class);
+ when(query.sort()).thenReturn("unknown");
+ ComponentsFinderSort finderSort = new ComponentsFinderSort(null, query);
+ try {
+ finderSort.sort();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Cannot sort on field : unknown");
+ }
+ }
+
}
assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME);
assertThat(query.asc()).isTrue();
}
+
+ @Test
+ public void should_create_query_with_default_paging_from_parameters() {
+ Map<String, Object> map = newHashMap();
+ map.put("keys", newArrayList("org.codehaus.sonar"));
+ map.put("names", newArrayList("Sonar"));
+ map.put("qualifiers", newArrayList("TRK"));
+
+ ComponentQuery query = DefaultRubyComponentService.toQuery(map);
+ assertThat(query.keys()).containsOnly("org.codehaus.sonar");
+ assertThat(query.names()).containsOnly("Sonar");
+ assertThat(query.qualifiers()).containsOnly("TRK");
+ assertThat(query.pageSize()).isEqualTo(100);
+ assertThat(query.pageIndex()).isEqualTo(1);
+ assertThat(query.sort()).isEqualTo(ComponentQuery.SORT_BY_NAME);
+ assertThat(query.asc()).isTrue();
+ }
}
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class IssuesFinderSortTest {
Assertions.assertThat(result.get(2).getSeverity()).isEqualTo("BLOCKER");
}
+ @Test
+ public void should_sort_by_desc_severity() {
+ IssueDto issue1 = new IssueDto().setId(1L).setSeverity("INFO");
+ IssueDto issue2 = new IssueDto().setId(2L).setSeverity("BLOCKER");
+ IssueDto issue3 = new IssueDto().setId(3L).setSeverity("MAJOR");
+ List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3);
+
+ IssueQuery query = IssueQuery.builder().sort(IssueQuery.SORT_BY_SEVERITY).asc(false).build();
+ IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
+
+ List<IssueDto> result = newArrayList(issuesFinderSort.sort());
+
+ Assertions.assertThat(result).hasSize(3);
+ Assertions.assertThat(result.get(0).getSeverity()).isEqualTo("BLOCKER");
+ Assertions.assertThat(result.get(1).getSeverity()).isEqualTo("MAJOR");
+ Assertions.assertThat(result.get(2).getSeverity()).isEqualTo("INFO");
+ }
+
@Test
public void should_sort_by_creation_date() {
Date date = new Date();
Assertions.assertThat(result.get(2).getIssueCloseDate()).isEqualTo(date1);
}
+ @Test
+ public void should_not_sort_with_null_sort() {
+ IssueDto issue1 = new IssueDto().setId(1L).setAssignee("perceval");
+ IssueDto issue2 = new IssueDto().setId(2L).setAssignee("arthur");
+ IssueDto issue3 = new IssueDto().setId(3L).setAssignee("vincent");
+ IssueDto issue4 = new IssueDto().setId(4L).setAssignee(null);
+ List<IssueDto> dtoList = newArrayList(issue1, issue2, issue3, issue4);
+
+ IssueQuery query = IssueQuery.builder().sort(null).build();
+ IssuesFinderSort issuesFinderSort = new IssuesFinderSort(dtoList, query);
+
+ List<IssueDto> result = newArrayList(issuesFinderSort.sort());
+
+ assertThat(result).hasSize(4);
+ assertThat(result.get(0).getAssignee()).isEqualTo("perceval");
+ assertThat(result.get(1).getAssignee()).isEqualTo("arthur");
+ assertThat(result.get(2).getAssignee()).isEqualTo("vincent");
+ assertThat(result.get(3).getAssignee()).isNull();
+ }
+
+ @Test
+ public void should_fail_to_sort_with_unknown_sort() {
+ IssueQuery query = mock(IssueQuery.class);
+ when(query.sort()).thenReturn("unknown");
+ IssuesFinderSort issuesFinderSort = new IssuesFinderSort(null, query);
+ try {
+ issuesFinderSort.sort();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Cannot sort on field : unknown");
+ }
+ }
+
}