]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4326 Fix case when no tag is defined (i.e current state for most installs)
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 17 Jan 2014 21:03:27 +0000 (22:03 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 17 Jan 2014 21:03:27 +0000 (22:03 +0100)
sonar-server/src/main/java/org/sonar/server/rule/ESRuleTags.java
sonar-server/src/test/java/org/sonar/server/rule/ESRuleTagsTest.java

index 661238573acd5170c39e41755ae448ab846c5f06..fb2f911fe2b515941781617fc39c4e769fb62e8e 100644 (file)
@@ -45,25 +45,27 @@ public class ESRuleTags {
 
   public void putAllTags(Collection<RuleTagDto> tags) {
     String[] ids = new String[tags.size()];
-    BytesStream[] sources = new BytesStream[tags.size()];
-    int tagCounter = 0;
     try {
-      for (RuleTagDto tag: tags) {
-        ids[tagCounter] = tag.getTag();
-        sources[tagCounter] = XContentFactory.jsonBuilder()
-          .startObject()
-          .field(RuleTagDocument.FIELD_VALUE, tag.getTag())
-          .endObject();
-        tagCounter ++;
+      if (!tags.isEmpty()) {
+        BytesStream[] sources = new BytesStream[tags.size()];
+        int tagCounter = 0;
+        for (RuleTagDto tag: tags) {
+          ids[tagCounter] = tag.getTag();
+          sources[tagCounter] = XContentFactory.jsonBuilder()
+            .startObject()
+            .field(RuleTagDocument.FIELD_VALUE, tag.getTag())
+            .endObject();
+          tagCounter ++;
+        }
+        index.bulkIndex(RuleRegistry.INDEX_RULES, TYPE_TAG, ids, sources);
       }
-      index.bulkIndex(RuleRegistry.INDEX_RULES, TYPE_TAG, ids, sources);
       index.client().prepareDeleteByQuery(RuleRegistry.INDEX_RULES).setTypes(TYPE_TAG)
       .setQuery(
-        QueryBuilders.filteredQuery(
-          QueryBuilders.matchAllQuery(),
-          FilterBuilders.notFilter(
-            FilterBuilders.termsFilter(RuleTagDocument.FIELD_VALUE, ids))))
-            .execute().actionGet();
+          QueryBuilders.filteredQuery(
+              QueryBuilders.matchAllQuery(),
+              FilterBuilders.notFilter(
+                  FilterBuilders.termsFilter(RuleTagDocument.FIELD_VALUE, ids))))
+                  .execute().actionGet();
     } catch(IOException ioException) {
       throw new IllegalStateException("Unable to index tags", ioException);
     }
index 8f180f82be4a49d4d3095c8df334b5f880cc532c..c0d967f4307869907d213c7685a06d1cbf5d3fdf 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.rule;
 
 import com.github.tlrx.elasticsearch.test.EsSetup;
+import com.github.tlrx.elasticsearch.test.request.Index;
 import com.google.common.collect.ImmutableList;
 import org.elasticsearch.common.settings.ImmutableSettings;
 import org.junit.After;
@@ -76,35 +77,60 @@ public class ESRuleTagsTest {
     assertThat(esSetup.client().admin().indices().prepareTypesExists("rules").setTypes("tag").execute().actionGet().isExists()).isTrue();
   }
 
+  @Test
+  public void should_skip_bulk_if_no_tag() {
+    esSetup.execute(
+      indexTagDocument("tag5"),
+      indexTagDocument("tag6"),
+      indexTagDocument("tag7"));
+    ruleTags.putAllTags(ImmutableList.<RuleTagDto>of());
+    checkTagCount(0L);
+  }
+
   @Test
   public void should_register_new_tags() throws Exception {
     ruleTags.putAllTags(ImmutableList.of(
-      new RuleTagDto().setTag("tag1"),
-      new RuleTagDto().setTag("tag2"),
-      new RuleTagDto().setTag("tag3"),
-      new RuleTagDto().setTag("tag4"),
-      new RuleTagDto().setTag("tag5")
+      newRuleTagDto("tag1"),
+      newRuleTagDto("tag2"),
+      newRuleTagDto("tag3"),
+      newRuleTagDto("tag4"),
+      newRuleTagDto("tag5")
     ));
 
-    assertThat(esSetup.client().prepareCount("rules").setTypes(ESRuleTags.TYPE_TAG).execute().actionGet().getCount()).isEqualTo(5L);
+    checkTagCount(5L);
   }
 
+
   @Test
   public void should_remove_unused_tags() throws Exception {
 
     esSetup.execute(
-      EsSetup.index(RuleRegistry.INDEX_RULES, ESRuleTags.TYPE_TAG).withSource("{\"value\":\"tag5\"}"),
-      EsSetup.index(RuleRegistry.INDEX_RULES, ESRuleTags.TYPE_TAG).withSource("{\"value\":\"tag6\"}"),
-      EsSetup.index(RuleRegistry.INDEX_RULES, ESRuleTags.TYPE_TAG).withSource("{\"value\":\"tag7\"}"));
+      indexTagDocument("tag4"),
+      indexTagDocument("tag5"),
+      indexTagDocument("tag6"),
+      indexTagDocument("tag7"));
 
     ruleTags.putAllTags(ImmutableList.of(
-      new RuleTagDto().setTag("tag1"),
-      new RuleTagDto().setTag("tag2"),
-      new RuleTagDto().setTag("tag3"),
-      new RuleTagDto().setTag("tag4"),
-      new RuleTagDto().setTag("tag5")
+      newRuleTagDto("tag1"),
+      newRuleTagDto("tag2"),
+      newRuleTagDto("tag3"),
+      newRuleTagDto("tag4"),
+      newRuleTagDto("tag5")
     ));
 
-    assertThat(esSetup.client().prepareCount("rules").setTypes(ESRuleTags.TYPE_TAG).execute().actionGet().getCount()).isEqualTo(5L);
+    checkTagCount(5L);
+  }
+
+  private void checkTagCount(long count) {
+    assertThat(esSetup.client().prepareCount("rules").setTypes(ESRuleTags.TYPE_TAG)
+        .execute().actionGet().getCount()).isEqualTo(count);
+  }
+
+  private RuleTagDto newRuleTagDto(String tagValue) {
+    return new RuleTagDto().setTag(tagValue);
+  }
+
+  private Index indexTagDocument(String tagValue) {
+    return EsSetup.index(RuleRegistry.INDEX_RULES, ESRuleTags.TYPE_TAG).withSource(String.format("{\"value\":\"%s\"}", tagValue));
   }
 }