From: Daniel Schwarz Date: Thu, 27 Apr 2017 16:31:04 +0000 (+0200) Subject: SONAR-8865 remove old documents from rules/extension index when updating X-Git-Tag: 6.4-RC1~127 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b6c626d517f2a9783688b80a8dac0d5ba8a1f794;p=sonarqube.git SONAR-8865 remove old documents from rules/extension index when updating --- 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 index 00000000000..f405fc0d181 --- /dev/null +++ b/it/it-tests/src/test/java/it/rule/RulesPerOrganizationTest.java @@ -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 listTags(String organization) { + String json = orchestrator.getServer().newHttpCall("/api/rules/tags") + .setParam("organization", organization) + .execute() + .getBodyAsString(); + return (List) ItUtils.jsonToMap(json).get("tags"); + } + + private List 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 index 1314ebbb4bb..00000000000 --- a/it/it-tests/src/test/java/it/rule/ToDoTest.java +++ /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 { -} diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java index 67a50fed599..86c659636b8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleTesting.java @@ -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 setRepositoryKey(String repositoryKey) { return rule -> rule.setRepositoryKey(repositoryKey); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleExtensionDoc.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleExtensionDoc.java index db8fe2539c7..c205a8af629 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleExtensionDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleExtensionDoc.java @@ -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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java index 2e53f7bf60e..b5032644f38 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndexer.java @@ -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()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java index 43803aa0fea..6ba22ff90c7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java @@ -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()); + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java index 2e35abbb99b..668e6ed69dc 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java @@ -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()); + } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java index a42a7a1e814..96d5eb7e5a3 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java @@ -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(); + } }