From 7b73ceaff46a212b53c22e038d684dd108f157be Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 3 Feb 2014 17:22:47 +0100 Subject: [PATCH] Fix quality flaws and coverage --- .../batch/index/DefaultResourcePersister.java | 17 ++----- .../qualityprofile/ESActiveRuleTest.java | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index f387bebe1b6..61315db2d63 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -19,24 +19,15 @@ */ 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); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java index af285737fd5..ce350047990 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java @@ -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. of()); + } + + @Test + public void bulk_index_active_rules_checking_into_db() throws IOException { + List 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. 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()); } -- 2.39.5