]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws and coverage
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 3 Feb 2014 16:22:47 +0000 (17:22 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 3 Feb 2014 16:22:47 +0000 (17:22 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java

index f387bebe1b61b8bd480c096eb6884ee91828739b..61315db2d63a7b9814a65e9bfc33a2cfbf8ba8fd 100644 (file)
  */
 package org.sonar.batch.index;
 
-import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
-
-import org.sonar.api.scan.filesystem.InputFile;
 import com.google.common.collect.Maps;
 import org.apache.commons.lang.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.ResourceModel;
 import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.JavaFile;
-import org.sonar.api.resources.Library;
-import org.sonar.api.resources.Project;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.resources.ResourceUtils;
-import org.sonar.api.resources.Scopes;
+import org.sonar.api.resources.*;
+import org.sonar.api.scan.filesystem.InputFile;
+import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 import org.sonar.api.security.ResourcePermissions;
 import org.sonar.api.utils.SonarException;
 
@@ -141,7 +132,7 @@ public final class DefaultResourcePersister implements ResourcePersister {
   @Override
   public Snapshot getSnapshotOrFail(InputFile inputFile) {
     return getSnapshotOrFail(fromInputFile(inputFile));
-  };
+  }
 
   private Resource fromInputFile(InputFile inputFile) {
     String languageKey = inputFile.attribute(InputFile.ATTRIBUTE_LANGUAGE);
index af285737fd5fb657cee848b8dd7dc70478bb819b..ce3500479906185aa45dd16fd933070ff50dbadd 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.server.qualityprofile;
 
 import com.github.tlrx.elasticsearch.test.EsSetup;
 import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import org.apache.commons.io.IOUtils;
 import org.apache.ibatis.session.SqlSession;
@@ -47,6 +49,7 @@ import org.sonar.test.TestUtils;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -202,6 +205,47 @@ public class ESActiveRuleTest {
     assertThat(esSetup.exists("rules", "active_rule", "523"));
   }
 
+  @Test
+  public void should_delete_from_integer_ids() throws Exception {
+    esSetup.client().prepareBulk()
+      .add(Requests.indexRequest().index("rules").type("active_rule").parent("1").source(testFileAsString("delete_from_profile/active_rule25.json")))
+      .add(Requests.indexRequest().index("rules").type("active_rule").parent("3").source(testFileAsString("delete_from_profile/active_rule2702.json")))
+      .setRefresh(true)
+      .execute().actionGet();
+    esActiveRule.deleteActiveRules(ImmutableList.of(25, 2702));
+  }
+
+  @Test
+  public void should_not_fail_on_empty_delete_list() {
+    esActiveRule.deleteActiveRules(ImmutableList.<Integer> of());
+  }
+
+  @Test
+  public void bulk_index_active_rules_checking_into_db() throws IOException {
+    List<ActiveRuleDto> activeRules = newArrayList(new ActiveRuleDto().setId(1).setProfileId(10).setRuleId(1).setSeverity(Severity.MAJOR).setParentId(5)
+      .setNoteData("polop").setNoteCreatedAt(new Date()).setNoteUserLogin("godin"));
+
+    SqlSession session = mock(SqlSession.class);
+    when(myBatis.openSession()).thenReturn(session);
+    when(activeRuleDao.selectAll(session)).thenReturn(activeRules);
+    when(activeRuleDao.selectAllParams(session)).thenReturn(Lists.<ActiveRuleParamDto> newArrayList());
+
+    esActiveRule.bulkRegisterActiveRules();
+    assertThat(esSetup.exists("rules", "active_rule", "1"));
+
+    SearchHit[] parentHit = esSetup.client().prepareSearch("rules").setPostFilter(
+      hasChildFilter("active_rule", termFilter("profileId", 10))
+    ).execute().actionGet().getHits().getHits();
+    assertThat(parentHit).hasSize(1);
+    assertThat(parentHit[0].getId()).isEqualTo("1");
+
+    SearchHit[] childHit = esSetup.client().prepareSearch("rules").setPostFilter(
+      hasParentFilter("rule", termFilter("key", "RuleWithParameters"))
+    ).execute().actionGet().getHits().getHits();
+    assertThat(childHit).hasSize(1);
+    assertThat(childHit[0].getId()).isEqualTo("1");
+  }
+
   private String testFileAsString(String testFile) throws Exception {
     return IOUtils.toString(TestUtils.getResource(getClass(), testFile).toURI());
   }