]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8865 remove old documents from rules/extension index when updating
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Thu, 27 Apr 2017 16:31:04 +0000 (18:31 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Wed, 3 May 2017 17:49:09 +0000 (19:49 +0200)
it/it-tests/src/test/java/it/rule/RulesPerOrganizationTest.java [new file with mode: 0644]
it/it-tests/src/test/java/it/rule/ToDoTest.java [deleted file]
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleExtensionDoc.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java
sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java

diff --git a/it/it-tests/src/test/java/it/rule/RulesPerOrganizationTest.java b/it/it-tests/src/test/java/it/rule/RulesPerOrganizationTest.java
new file mode 100644 (file)
index 0000000..f405fc0
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package it.rule;
+
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.http.HttpMethod;
+import it.Category6Suite;
+import java.util.List;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.organization.CreateWsRequest;
+import util.ItUtils;
+
+import static it.Category6Suite.enableOrganizationsSupport;
+import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.deleteOrganizationsIfExists;
+import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.newWsClient;
+
+public class RulesPerOrganizationTest {
+
+  private static WsClient adminWsClient;
+  private static final String ORGANIZATION_FOO = "foo-org";
+  private static final String ORGANIZATION_BAR = "bar-org";
+
+  @ClassRule
+  public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
+
+  @BeforeClass
+  public static void setUp() {
+    adminWsClient = newAdminWsClient(orchestrator);
+    enableOrganizationsSupport();
+    createOrganization(ORGANIZATION_FOO);
+    createOrganization(ORGANIZATION_BAR);
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    deleteOrganizationsIfExists(orchestrator, ORGANIZATION_FOO);
+    deleteOrganizationsIfExists(orchestrator, ORGANIZATION_BAR);
+  }
+
+  private static void createOrganization(String organization) {
+    adminWsClient.organizations().create(new CreateWsRequest.Builder().setKey(organization).setName(organization).build());
+  }
+
+  @Test
+  public void should_not_show_tags_of_other_org() {
+    updateTag("foo-tag", ORGANIZATION_FOO);
+    updateTag("bar-tag", ORGANIZATION_BAR);
+    assertThat(showRuleTags(ORGANIZATION_FOO)).containsExactly("foo-tag");
+    assertThat(showRuleTags(ORGANIZATION_BAR)).containsExactly("bar-tag");
+  }
+
+  @Test
+  public void should_not_list_tags_of_other_org() {
+    updateTag("foo-tag", ORGANIZATION_FOO);
+    updateTag("bar-tag", ORGANIZATION_BAR);
+    assertThat(listTags(ORGANIZATION_FOO))
+      .contains("foo-tag")
+      .doesNotContain("bar-tag");
+  }
+
+  @Test
+  public void should_not_show_removed_tags() {
+    updateTag("foo-tag", ORGANIZATION_FOO);
+    assertThat(showRuleTags(ORGANIZATION_FOO)).contains("foo-tag");
+
+    updateTag("", ORGANIZATION_FOO);
+    assertThat(showRuleTags(ORGANIZATION_FOO)).isEmpty();
+  }
+
+  @Test
+  public void should_not_list_removed_tags() {
+    updateTag("foo-tag", ORGANIZATION_FOO);
+    assertThat(listTags(ORGANIZATION_FOO)).contains("foo-tag");
+
+    updateTag("", ORGANIZATION_FOO);
+    assertThat(listTags(ORGANIZATION_FOO)).doesNotContain("foo-tag");
+  }
+
+  private List<String> listTags(String organization) {
+    String json = orchestrator.getServer().newHttpCall("/api/rules/tags")
+      .setParam("organization", organization)
+      .execute()
+      .getBodyAsString();
+    return (List<String>) ItUtils.jsonToMap(json).get("tags");
+  }
+
+  private List<String> showRuleTags(String organization) {
+    return newWsClient(orchestrator).rules().show(organization, "xoo:OneIssuePerFile")
+      .getRule().getTags().getTagsList();
+  }
+
+  private void updateTag(String tag, String organization) {
+    orchestrator.getServer().newHttpCall("/api/rules/update")
+      .setMethod(HttpMethod.POST)
+      .setAdminCredentials()
+      .setParam("organization", organization)
+      .setParam("key", "xoo:OneIssuePerFile")
+      .setParam("tags", tag)
+      .execute();
+  }
+}
diff --git a/it/it-tests/src/test/java/it/rule/ToDoTest.java b/it/it-tests/src/test/java/it/rule/ToDoTest.java
deleted file mode 100644 (file)
index 1314ebb..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package it.rule;
-
-public class ToDoTest {
-}
index 67a50fed5991f1d54a9927b7365ee16b14655f82..86c659636b817db6585d9637d6bf5ac87f201ec9 100644 (file)
@@ -35,6 +35,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.ImmutableSet.copyOf;
 import static com.google.common.collect.Sets.newHashSet;
 import static java.util.Objects.requireNonNull;
+import static org.apache.commons.lang.RandomStringUtils.random;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
 import static org.apache.commons.lang.math.RandomUtils.nextInt;
@@ -250,6 +251,10 @@ public class RuleTesting {
     return RuleKey.of("repo_" + randomAlphanumeric(3), "rule_" + randomAlphanumeric(3));
   }
 
+  public static RuleKey randomRuleKeyOfMaximumLength() {
+    return RuleKey.of(random(255), random(200));
+  }
+
   public static Consumer<RuleDefinitionDto> setRepositoryKey(String repositoryKey) {
     return rule -> rule.setRepositoryKey(repositoryKey);
   }
index db8fe2539c7070394eab40eb5eea1b5b4e60077e..c205a8af62942032234324236ae53ec7b8d80f51 100644 (file)
@@ -39,8 +39,7 @@ public class RuleExtensionDoc extends BaseDoc {
 
   @Override
   public String getId() {
-    // the Id is generated by elasticsearch
-    return null;
+    return getRuleKey() + "|" + getScope().getScope();
   }
 
   @Override
index 2e53f7bf60e5ead0f5cfafa63d9a22d7cfb382d7..b5032644f38fcb6ee417d22e149e6667399c962d 100644 (file)
@@ -129,7 +129,7 @@ public class RuleIndexer implements StartupIndexer {
   }
 
   private static IndexRequest newIndexRequest(RuleExtensionDoc ruleExtension) {
-    return new IndexRequest(INDEX_TYPE_RULE_EXTENSION.getIndex(), INDEX_TYPE_RULE_EXTENSION.getType())
+    return new IndexRequest(INDEX_TYPE_RULE_EXTENSION.getIndex(), INDEX_TYPE_RULE_EXTENSION.getType(), ruleExtension.getId())
       .source(ruleExtension.getFields())
       .parent(ruleExtension.getParent());
   }
index 43803aa0fea819cd263b75275c1c2e98ffc70fc4..6ba22ff90c745ffff86ffac8b8f53874708b7da5 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.rule.index;
 
+import com.google.common.collect.ImmutableSet;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.config.MapSettings;
@@ -28,13 +29,15 @@ import org.sonar.api.rules.RuleType;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.db.rule.RuleDto;
+import org.sonar.db.rule.RuleTesting;
 import org.sonar.server.es.EsTester;
-import org.sonar.server.organization.TestDefaultOrganizationProvider;
 
 import static com.google.common.collect.Sets.newHashSet;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
 
 public class RuleIndexerTest {
 
@@ -94,4 +97,25 @@ public class RuleIndexerTest {
 
     assertThat(esTester.countDocuments(RuleIndexDefinition.INDEX_TYPE_RULE)).isEqualTo(1);
   }
+
+  @Test
+  public void index_rule_extension_with_long_id() {
+    RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setRuleKey(RuleTesting.randomRuleKeyOfMaximumLength()));
+    underTest.indexRuleDefinition(rule.getKey());
+    OrganizationDto organization = dbTester.organizations().insert();
+    dbTester.rules().insertOrUpdateMetadata(rule, organization, m -> m.setTags(ImmutableSet.of("bla")));
+    underTest.indexRuleExtension(organization, rule.getKey());
+
+    RuleExtensionDoc doc = new RuleExtensionDoc()
+      .setRuleKey(rule.getKey())
+      .setScope(RuleExtensionScope.organization(organization.getUuid()));
+    assertThat(
+      esTester.client()
+        .prepareSearch(RuleIndexDefinition.INDEX_TYPE_RULE_EXTENSION)
+        .setQuery(termQuery("_id", doc.getId()))
+        .get()
+        .getHits()
+        .getHits()[0]
+          .getId()).isEqualTo(doc.getId());
+  }
 }
index 2e35abbb99b713bfee62bbe440699c4d7b52ac25..668e6ed69dc888580a83c4d9f8e0a97b0c00b7d4 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonarqube.ws.client.rule;
 
+import javax.annotation.Nullable;
+import org.sonarqube.ws.Rules;
 import org.sonarqube.ws.Rules.SearchResponse;
 import org.sonarqube.ws.client.BaseService;
 import org.sonarqube.ws.client.GetRequest;
@@ -71,4 +73,11 @@ public class RulesService extends BaseService {
         .setParam(PARAM_TYPES, inlineMultipleParamValue(request.getTypes())),
       SearchResponse.parser());
   }
+
+  public Rules.ShowResponse show(@Nullable String organization, String key) {
+    GetRequest request = new GetRequest(path("show"))
+      .setParam("organization", organization)
+      .setParam("key", key);
+    return call(request, Rules.ShowResponse.parser());
+  }
 }
index a42a7a1e81467d07fe9a778e72733644e3e9e748..96d5eb7e5a374ef3b2d3de6e6b95366e4edcbe57 100644 (file)
@@ -23,6 +23,7 @@ import com.google.common.collect.Lists;
 import java.util.List;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonarqube.ws.Rules;
 import org.sonarqube.ws.Rules.SearchResponse;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.ServiceTester;
@@ -85,7 +86,7 @@ public class RulesServiceTest {
   private RulesService underTest = serviceTester.getInstanceUnderTest();
 
   @Test
-  public void search() {
+  public void test_search() {
     underTest.search(new SearchWsRequest()
       .setActivation(ACTIVATION_VALUE)
       .setActiveSeverities(ACTIVE_SEVERITIES_VALUE)
@@ -136,4 +137,17 @@ public class RulesServiceTest {
       .hasParam(PARAM_TYPES, TYPES_VALUE_INLINED)
       .andNoOtherParam();
   }
+
+  @Test
+  public void test_show() {
+    underTest.show("the-org", "the-rule/key");
+
+    assertThat(serviceTester.getGetParser()).isSameAs(Rules.ShowResponse.parser());
+    GetRequest getRequest = serviceTester.getGetRequest();
+    serviceTester.assertThat(getRequest)
+      .hasPath("show")
+      .hasParam("organization", "the-org")
+      .hasParam("key", "the-rule/key")
+      .andNoOtherParam();
+  }
 }