Browse Source

SONAR-8952 remove redundant RuleService class

tags/6.4-RC1
Daniel Schwarz 7 years ago
parent
commit
b91aecc0fb

+ 0
- 2
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java View File

@@ -163,7 +163,6 @@ import org.sonar.server.rule.DeprecatedRulesDefinitionLoader;
import org.sonar.server.rule.RuleCreator;
import org.sonar.server.rule.RuleDefinitionsLoader;
import org.sonar.server.rule.RuleDeleter;
import org.sonar.server.rule.RuleService;
import org.sonar.server.rule.RuleUpdater;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;
@@ -291,7 +290,6 @@ public class PlatformLevel4 extends PlatformLevel {
RuleDefinitionsLoader.class,
CommonRuleDefinitionsImpl.class,
RulesDefinitionXmlLoader.class,
RuleService.class,
RuleUpdater.class,
RuleCreator.class,
RuleDeleter.class,

+ 0
- 63
server/sonar-server/src/main/java/org/sonar/server/rule/RuleService.java View File

@@ -1,63 +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 org.sonar.server.rule;

import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.server.es.SearchIdResult;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleQuery;

/**
* @since 4.4
*/
@ServerSide
public class RuleService {

private final RuleIndex index;

public RuleService(RuleIndex index) {
this.index = index;
}

/**
* List all tags, including system tags, defined on rules
*/
public Set<String> listTags() {
/** using combined ALL_TAGS field of ES until ES update that has multiTerms aggregation */
return index.terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS);
}

/**
* List tags matching a given criterion
*/
public Set<String> listTags(@Nullable String query, int size) {
/** using combined ALL_TAGS field of ES until ES update that has multiTerms aggregation */
return index.terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, query, size);
}

public SearchIdResult<RuleKey> search(RuleQuery query, SearchOptions options) {
return index.search(query, options);
}
}

+ 7
- 7
server/sonar-server/src/main/java/org/sonar/server/rule/ws/TagsAction.java View File

@@ -20,22 +20,22 @@
package org.sonar.server.rule.ws;

import com.google.common.io.Resources;
import java.util.Set;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.server.rule.RuleService;

import java.util.Set;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;

public class TagsAction implements RulesWsAction {

private final RuleService service;
private final RuleIndex ruleIndex;

public TagsAction(RuleService service) {
this.service = service;
public TagsAction(RuleIndex ruleIndex) {
this.ruleIndex = ruleIndex;
}

@Override
@@ -60,7 +60,7 @@ public class TagsAction implements RulesWsAction {
public void handle(Request request, Response response) {
String query = request.param(Param.TEXT_QUERY);
int pageSize = request.mandatoryParamAsInt("ps");
Set<String> tags = service.listTags(query, pageSize);
Set<String> tags = ruleIndex.terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS, query, pageSize);
JsonWriter json = response.newJsonWriter().beginObject();
json.name("tags").beginArray();
for (String tag : tags) {

+ 0
- 123
server/sonar-server/src/test/java/org/sonar/server/rule/RuleServiceMediumTest.java View File

@@ -1,123 +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 org.sonar.server.rule;

import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;

import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;

public class RuleServiceMediumTest {

@ClassRule
public static ServerTester tester = new ServerTester().withEsIndexes();

@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);

@Rule
public ExpectedException expectedException = ExpectedException.none();

private RuleDao dao = tester.get(RuleDao.class);
private RuleService service = tester.get(RuleService.class);
private DbSession dbSession;
private RuleIndexer ruleIndexer;
private OrganizationDto defaultOrganization;

@Before
public void before() {
tester.clearDbAndIndexes();
dbSession = tester.get(DbClient.class).openSession(false);
ruleIndexer = tester.get(RuleIndexer.class);
defaultOrganization = tester.get(DbClient.class).organizationDao().selectByUuid(dbSession, tester.get(DefaultOrganizationProvider.class).get().getUuid()).get();
}

@After
public void after() {
dbSession.close();
}

@Test
public void listTags_returns_all_tags() {
// insert db
insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1"), newHashSet("sys1", "sys2"));
insertRule(RuleKey.of("java", "S001"), newHashSet("tag2"), newHashSet());

// all tags, including system
Set<String> tags = service.listTags();
assertThat(tags).containsOnly("tag1", "tag2", "sys1", "sys2");
}

@Test
public void listTags_returns_tags_filtered_by_name() {
insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1", "misra++"), newHashSet("sys1", "sys2"));
insertRule(RuleKey.of("java", "S001"), newHashSet("tag2"), newHashSet());

assertThat(service.listTags("missing", 10)).isEmpty();
assertThat(service.listTags("", 10)).containsOnly("tag1", "misra++", "tag2", "sys1", "sys2");
assertThat(service.listTags("tag", 10)).containsOnly("tag1", "tag2");
assertThat(service.listTags("sys", 10)).containsOnly("sys1", "sys2");
assertThat(service.listTags("misra", 10)).containsOnly("misra++");
assertThat(service.listTags("misra+", 10)).containsOnly("misra++");
assertThat(service.listTags("++", 10)).containsOnly("misra++");

// LIMITATION: case sensitive
assertThat(service.listTags("TAG", 10)).isEmpty();
assertThat(service.listTags("TAg", 10)).isEmpty();
assertThat(service.listTags("MISSing", 10)).isEmpty();

assertThat(service.listTags("misra-", 10)).isEmpty();
}

@Test
public void listTags_returns_empty_results_if_filter_contains_regexp_special_characters() {
insertRule(RuleKey.of("javascript", "S001"), newHashSet("misra++"), newHashSet("sys1", "sys2"));

assertThat(service.listTags("mis[", 10)).isEmpty();
assertThat(service.listTags("mis\\d", 10)).isEmpty();
assertThat(service.listTags(".*", 10)).isEmpty();
assertThat(service.listTags("<foo>", 10)).isEmpty();
}

private void insertRule(RuleKey key, Set<String> tags, Set<String> systemTags) {
RuleDto ruleDto = RuleTesting.newDto(key, defaultOrganization).setTags(tags).setSystemTags(systemTags);
dao.insert(dbSession, ruleDto.getDefinition());
dao.insertOrUpdate(dbSession, ruleDto.getMetadata().setRuleId(ruleDto.getId()));
dbSession.commit();
ruleIndexer.index(defaultOrganization, ruleDto.getKey());
}
}

+ 3
- 2
server/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java View File

@@ -55,6 +55,7 @@ import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
@@ -207,7 +208,7 @@ public class RuleUpdaterMediumTest {
assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");

// verify that tags are indexed in index
Set<String> tags = tester.get(RuleService.class).listTags();
Set<String> tags = tester.get(RuleIndex.class).terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS);
assertThat(tags).containsOnly("bug", "java8", "javadoc");
}

@@ -229,7 +230,7 @@ public class RuleUpdaterMediumTest {
assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc");

// verify that tags are indexed in index
Set<String> tags = tester.get(RuleService.class).listTags();
Set<String> tags = tester.get(RuleIndex.class).terms(RuleIndexDefinition.FIELD_RULE_ALL_TAGS);
assertThat(tags).containsOnly("java8", "javadoc");
}


Loading…
Cancel
Save