]> source.dussan.org Git - sonarqube.git/commitdiff
Add some unit tests
authorJulien Lancelot <julien.lancelot@gmail.com>
Tue, 16 Jul 2013 07:55:38 +0000 (09:55 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Tue, 16 Jul 2013 07:55:38 +0000 (09:55 +0200)
sonar-server/src/main/java/org/sonar/server/issue/IssuesFinderSort.java
sonar-server/src/test/java/org/sonar/server/component/ComponentsFinderSortTest.java
sonar-server/src/test/java/org/sonar/server/component/DefaultRubyComponentServiceTest.java
sonar-server/src/test/java/org/sonar/server/issue/IssuesFinderSortTest.java

index 786b5f25b68706abb60269822404165dfa29a823..ff8256c61174676b7b35789d8fa32d601fa9b8cd 100644 (file)
@@ -70,7 +70,7 @@ class IssuesFinderSort {
     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 {
index 688009ccbd21a3e0ae664ac85b03eb6a12a25396..2d629afe37df997174f6776a2fb1a4018ecaef22 100644 (file)
@@ -28,11 +28,13 @@ import java.util.List;
 
 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"),
@@ -41,9 +43,9 @@ public class ComponentsFinderSortTest {
     );
 
     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");
@@ -52,4 +54,58 @@ public class ComponentsFinderSortTest {
     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");
+    }
+  }
+
 }
index 2a5cdb3051b09e63c575d1a9a11f19a972264c1f..af5de1e4429121e3c46ce6a9f6131f306bedcb04 100644 (file)
@@ -89,4 +89,21 @@ public class DefaultRubyComponentServiceTest {
     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();
+  }
 }
index 23601d03ed77a785fc353d7fa3be685e5dd75c98..7cf32f96e7f0e66b08aae4e4fc2ce27b8bb54870 100644 (file)
@@ -31,6 +31,8 @@ import java.util.List;
 
 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 {
 
@@ -90,6 +92,24 @@ 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();
@@ -156,4 +176,36 @@ public class IssuesFinderSortTest {
     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");
+    }
+  }
+
 }