]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7375 WS api/rules/search add to WS client
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 18 Mar 2016 15:18:51 +0000 (16:18 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 18 Mar 2016 17:09:35 +0000 (18:09 +0100)
sonar-ws/src/main/java/org/sonarqube/ws/client/HttpWsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/RulesService.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java [new file with mode: 0644]
sonar-ws/src/test/java/org/sonarqube/ws/client/rule/RulesServiceTest.java [new file with mode: 0644]

index abec779c8c55deecde0318337b102df8502ca27f..5ceb0b6f27f157b9ac0584104dba04ad312f9b18 100644 (file)
@@ -26,6 +26,7 @@ import org.sonarqube.ws.client.measure.MeasuresService;
 import org.sonarqube.ws.client.permission.PermissionsService;
 import org.sonarqube.ws.client.qualitygate.QualityGatesService;
 import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
+import org.sonarqube.ws.client.rule.RulesService;
 import org.sonarqube.ws.client.system.SystemService;
 import org.sonarqube.ws.client.usertoken.UserTokensService;
 
@@ -46,6 +47,7 @@ public class HttpWsClient implements WsClient {
   private final MeasuresService measuresService;
   private final SystemService systemService;
   private final CeService ceService;
+  private final RulesService rulesService;
 
   public HttpWsClient(WsConnector wsConnector) {
     this.wsConnector = wsConnector;
@@ -58,6 +60,7 @@ public class HttpWsClient implements WsClient {
     this.measuresService = new MeasuresService(wsConnector);
     this.systemService = new SystemService(wsConnector);
     this.ceService = new CeService(wsConnector);
+    this.rulesService = new RulesService(wsConnector);
   }
 
   @Override
@@ -109,4 +112,9 @@ public class HttpWsClient implements WsClient {
   public CeService ce() {
     return ceService;
   }
+
+  @Override
+  public RulesService rules() {
+    return rulesService;
+  }
 }
index 7f865292fc7575cfe2d862d0105b5294c3eb0701..522e6054a464f805467fc0f8964da2645237dc28 100644 (file)
@@ -26,6 +26,7 @@ import org.sonarqube.ws.client.measure.MeasuresService;
 import org.sonarqube.ws.client.permission.PermissionsService;
 import org.sonarqube.ws.client.qualitygate.QualityGatesService;
 import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
+import org.sonarqube.ws.client.rule.RulesService;
 import org.sonarqube.ws.client.system.SystemService;
 import org.sonarqube.ws.client.usertoken.UserTokensService;
 
@@ -51,5 +52,7 @@ public interface WsClient {
 
   CeService ce();
 
+  RulesService rules();
+
   WsConnector wsConnector();
 }
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
new file mode 100644 (file)
index 0000000..4dcbdf5
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonarqube.ws.client.rule;
+
+import org.sonarqube.ws.Rules.SearchResponse;
+import org.sonarqube.ws.client.BaseService;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.WsConnector;
+
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVATION;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_AVAILABLE_SINCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_INHERITANCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_IS_TEMPLATE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_KEY;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_LANGUAGES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_QPROFILE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_REPOSITORIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_SEVERITIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_STATUSES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TAGS;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TEMPLATE_KEY;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TYPES;
+
+public class RulesService extends BaseService {
+
+  public RulesService(WsConnector wsConnector) {
+    super(wsConnector, "api/rules");
+  }
+
+  public SearchResponse search(SearchWsRequest request) {
+    return call(
+      new GetRequest(path("search"))
+        .setParam(PARAM_ACTIVATION, request.getActivation())
+        .setParam(PARAM_ACTIVE_SEVERITIES, inlineMultipleParamValue(request.getActiveSeverities()))
+        .setParam("asc", request.getAsc())
+        .setParam(PARAM_AVAILABLE_SINCE, request.getAvailableSince())
+        .setParam("f", inlineMultipleParamValue(request.getFields()))
+        .setParam("facets", inlineMultipleParamValue(request.getFacets()))
+        .setParam(PARAM_INHERITANCE, inlineMultipleParamValue(request.getInheritance()))
+        .setParam(PARAM_IS_TEMPLATE, request.getIsTemplate())
+        .setParam(PARAM_LANGUAGES, inlineMultipleParamValue(request.getLanguages()))
+        .setParam("p", request.getPage())
+        .setParam("ps", request.getPageSize())
+        .setParam("q", request.getQuery())
+        .setParam(PARAM_QPROFILE, request.getQProfile())
+        .setParam(PARAM_REPOSITORIES, inlineMultipleParamValue(request.getRepositories()))
+        .setParam(PARAM_KEY, request.getRuleKey())
+        .setParam("s", request.getSort())
+        .setParam(PARAM_SEVERITIES, inlineMultipleParamValue(request.getSeverities()))
+        .setParam(PARAM_STATUSES, inlineMultipleParamValue(request.getStatuses()))
+        .setParam(PARAM_TAGS, inlineMultipleParamValue(request.getTags()))
+        .setParam(PARAM_TEMPLATE_KEY, request.getTemplateKey())
+        .setParam(PARAM_TYPES, inlineMultipleParamValue(request.getTypes())),
+      SearchResponse.parser());
+  }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/rule/SearchWsRequest.java
new file mode 100644 (file)
index 0000000..74f117c
--- /dev/null
@@ -0,0 +1,259 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonarqube.ws.client.rule;
+
+import java.util.List;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class SearchWsRequest {
+  private Boolean activation;
+  private List<String> activeSeverities;
+  private Boolean asc;
+  private String availableSince;
+  private List<String> fields;
+  private List<String> facets;
+  private List<String> inheritance;
+  private Boolean isTemplate;
+  private List<String> languages;
+  private Integer page;
+  private Integer pageSize;
+  private String query;
+  private String qProfile;
+  private List<String> repositories;
+  private String ruleKey;
+  private String sort;
+  private List<String> severities;
+  private List<String> statuses;
+  private List<String> tags;
+  private String templateKey;
+  private List<String> types;
+
+  @CheckForNull
+  public Boolean getActivation() {
+    return activation;
+  }
+
+  public SearchWsRequest setActivation(@Nullable Boolean activation) {
+    this.activation = activation;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getActiveSeverities() {
+    return activeSeverities;
+  }
+
+  public SearchWsRequest setActiveSeverities(@Nullable List<String> activeSeverities) {
+    this.activeSeverities = activeSeverities;
+    return this;
+  }
+
+  @CheckForNull
+  public Boolean getAsc() {
+    return asc;
+  }
+
+  public SearchWsRequest setAsc(Boolean asc) {
+    this.asc = asc;
+    return this;
+  }
+
+  @CheckForNull
+  public String getAvailableSince() {
+    return availableSince;
+  }
+
+  public SearchWsRequest setAvailableSince(@Nullable String availableSince) {
+    this.availableSince = availableSince;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getFields() {
+    return fields;
+  }
+
+  public SearchWsRequest setFields(@Nullable List<String> fields) {
+    this.fields = fields;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getFacets() {
+    return facets;
+  }
+
+  public SearchWsRequest setFacets(@Nullable List<String> facets) {
+    this.facets = facets;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getInheritance() {
+    return inheritance;
+  }
+
+  public SearchWsRequest setInheritance(@Nullable List<String> inheritance) {
+    this.inheritance = inheritance;
+    return this;
+  }
+
+  @CheckForNull
+  public Boolean getIsTemplate() {
+    return isTemplate;
+  }
+
+  public SearchWsRequest setIsTemplate(@Nullable Boolean isTemplate) {
+    this.isTemplate = isTemplate;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getLanguages() {
+    return languages;
+  }
+
+  public SearchWsRequest setLanguages(@Nullable List<String> languages) {
+    this.languages = languages;
+    return this;
+  }
+
+  @CheckForNull
+  public Integer getPage() {
+    return page;
+  }
+
+  public SearchWsRequest setPage(@Nullable Integer page) {
+    this.page = page;
+    return this;
+  }
+
+  @CheckForNull
+  public Integer getPageSize() {
+    return pageSize;
+  }
+
+  public SearchWsRequest setPageSize(@Nullable Integer pageSize) {
+    this.pageSize = pageSize;
+    return this;
+  }
+
+  @CheckForNull
+  public String getQuery() {
+    return query;
+  }
+
+  public SearchWsRequest setQuery(@Nullable String query) {
+    this.query = query;
+    return this;
+  }
+
+  @CheckForNull
+  public String getQProfile() {
+    return qProfile;
+  }
+
+  public SearchWsRequest setQProfile(@Nullable String qProfile) {
+    this.qProfile = qProfile;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getRepositories() {
+    return repositories;
+  }
+
+  public SearchWsRequest setRepositories(@Nullable List<String> repositories) {
+    this.repositories = repositories;
+    return this;
+  }
+
+  @CheckForNull
+  public String getRuleKey() {
+    return ruleKey;
+  }
+
+  public SearchWsRequest setRuleKey(@Nullable String ruleKey) {
+    this.ruleKey = ruleKey;
+    return this;
+  }
+
+  @CheckForNull
+  public String getSort() {
+    return sort;
+  }
+
+  public SearchWsRequest setSort(@Nullable String sort) {
+    this.sort = sort;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getSeverities() {
+    return severities;
+  }
+
+  public SearchWsRequest setSeverities(@Nullable List<String> severities) {
+    this.severities = severities;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getStatuses() {
+    return statuses;
+  }
+
+  public SearchWsRequest setStatuses(@Nullable List<String> statuses) {
+    this.statuses = statuses;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getTags() {
+    return tags;
+  }
+
+  public SearchWsRequest setTags(@Nullable List<String> tags) {
+    this.tags = tags;
+    return this;
+  }
+
+  @CheckForNull
+  public String getTemplateKey() {
+    return templateKey;
+  }
+
+  public SearchWsRequest setTemplateKey(@Nullable String templateKey) {
+    this.templateKey = templateKey;
+    return this;
+  }
+
+  @CheckForNull
+  public List<String> getTypes() {
+    return types;
+  }
+
+  public SearchWsRequest setTypes(@Nullable List<String> types) {
+    this.types = types;
+    return this;
+  }
+}
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
new file mode 100644 (file)
index 0000000..d07af4d
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.sonarqube.ws.client.rule;
+
+import com.google.common.collect.Lists;
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonarqube.ws.Rules.SearchResponse;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.ServiceTester;
+import org.sonarqube.ws.client.WsConnector;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVATION;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_ACTIVE_SEVERITIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_AVAILABLE_SINCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_INHERITANCE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_IS_TEMPLATE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_KEY;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_LANGUAGES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_QPROFILE;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_REPOSITORIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_SEVERITIES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_STATUSES;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TAGS;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TEMPLATE_KEY;
+import static org.sonarqube.ws.client.rule.RulesWsParameters.PARAM_TYPES;
+
+public class RulesServiceTest {
+  static final boolean ACTIVATION_VALUE = true;
+  static final List<String> ACTIVE_SEVERITIES_VALUE = Lists.newArrayList("CRITICAL", "BLOCKER");
+  static final String ACTIVE_SEVERITIES_VALUE_INLINED = "CRITICAL,BLOCKER";
+  static final boolean ASC_VALUE = false;
+  static final String AVAILABLE_SINCE_VALUE = "2015-06-22";
+  static final List<String> FIELDS_VALUE = newArrayList("repo", "name");
+  static final String FIELDS_VALUE_INLINED = "repo,name";
+  static final List<String> FACETS_VALUE = newArrayList("languages", "repositories");
+  static final String FACETS_VALUE_INLINED = "languages,repositories";
+  static final List<String> INHERITANCE_VALUE = newArrayList("INHERITED", "OVERRIDES");
+  static final String INHERITANCE_VALUE_INLINED = "INHERITED,OVERRIDES";
+  static final boolean IS_TEMPLATE_VALUE = true;
+  static final List<String> LANGUAGES_VALUE = newArrayList("java", "js");
+  static final String LANGUAGES_VALUE_INLINED = "java,js";
+  static final int PAGE_VALUE = 12;
+  static final int PAGE_SIZE_VALUE = 42;
+  static final String QUERY_VALUE = "query-value";
+  static final String QPROFILE_VALUE = "qprofile-key";
+  static final List<String> REPOSITORIES_VALUE = newArrayList("findbugs", "checkstyle");
+  static final String REPOSITORIES_VALUE_INLINED = "findbugs,checkstyle";
+  static final String RULE_KEY_VALUE = "rule-key-value";
+  static final String SORT_VALUE = "name";
+  static final List<String> SEVERITIES_VALUE = newArrayList("INFO", "MINOR");
+  static final String SEVERITIES_VALUE_INLINED = "INFO,MINOR";
+  static final List<String> STATUSES_VALUE = newArrayList("BETA", "DEPRECATED");
+  static final String STATUSES_VALUE_INLINED = "BETA,DEPRECATED";
+  static final List<String> TAGS_VALUE = newArrayList("clumsy", "java8");
+  static final String TAGS_VALUE_INLINED = "clumsy,java8";
+  static final String TEMPLATE_KEY_VALUE = "template-key-value";
+  static final List<String> TYPES_VALUE = newArrayList("CODE_SMELL", "BUG");
+  static final String TYPES_VALUE_INLINED = "CODE_SMELL,BUG";
+
+  @Rule
+  public ServiceTester<RulesService> serviceTester = new ServiceTester<>(new RulesService(mock(WsConnector.class)));
+
+  private RulesService underTest = serviceTester.getInstanceUnderTest();
+
+  @Test
+  public void search() {
+    underTest.search(new SearchWsRequest()
+      .setActivation(ACTIVATION_VALUE)
+      .setActiveSeverities(ACTIVE_SEVERITIES_VALUE)
+      .setAsc(ASC_VALUE)
+      .setAvailableSince(AVAILABLE_SINCE_VALUE)
+      .setFields(FIELDS_VALUE)
+      .setFacets(FACETS_VALUE)
+      .setInheritance(INHERITANCE_VALUE)
+      .setIsTemplate(IS_TEMPLATE_VALUE)
+      .setLanguages(LANGUAGES_VALUE)
+      .setPage(PAGE_VALUE)
+      .setPageSize(PAGE_SIZE_VALUE)
+      .setQuery(QUERY_VALUE)
+      .setQProfile(QPROFILE_VALUE)
+      .setRepositories(REPOSITORIES_VALUE)
+      .setRuleKey(RULE_KEY_VALUE)
+      .setSort(SORT_VALUE)
+      .setSeverities(SEVERITIES_VALUE)
+      .setStatuses(STATUSES_VALUE)
+      .setTags(TAGS_VALUE)
+      .setTemplateKey(TEMPLATE_KEY_VALUE)
+      .setTypes(TYPES_VALUE));
+
+    assertThat(serviceTester.getGetParser()).isSameAs(SearchResponse.parser());
+    GetRequest getRequest = serviceTester.getGetRequest();
+    serviceTester.assertThat(getRequest)
+      .hasPath("search")
+      .hasParam(PARAM_ACTIVATION, ACTIVATION_VALUE)
+      .hasParam(PARAM_ACTIVE_SEVERITIES, ACTIVE_SEVERITIES_VALUE_INLINED)
+      .hasParam("asc", ASC_VALUE)
+      .hasParam(PARAM_AVAILABLE_SINCE, AVAILABLE_SINCE_VALUE)
+      .hasParam("f", FIELDS_VALUE_INLINED)
+      .hasParam("facets", FACETS_VALUE_INLINED)
+      .hasParam(PARAM_INHERITANCE, INHERITANCE_VALUE_INLINED)
+      .hasParam(PARAM_IS_TEMPLATE, IS_TEMPLATE_VALUE)
+      .hasParam("p", PAGE_VALUE)
+      .hasParam("ps", PAGE_SIZE_VALUE)
+      .hasParam("q", QUERY_VALUE)
+      .hasParam(PARAM_QPROFILE, QPROFILE_VALUE)
+      .hasParam(PARAM_REPOSITORIES, REPOSITORIES_VALUE_INLINED)
+      .hasParam(PARAM_KEY, RULE_KEY_VALUE)
+      .hasParam(PARAM_LANGUAGES, LANGUAGES_VALUE_INLINED)
+      .hasParam("s", SORT_VALUE)
+      .hasParam(PARAM_SEVERITIES, SEVERITIES_VALUE_INLINED)
+      .hasParam(PARAM_STATUSES, STATUSES_VALUE_INLINED)
+      .hasParam(PARAM_TAGS, TAGS_VALUE_INLINED)
+      .hasParam(PARAM_TEMPLATE_KEY, TEMPLATE_KEY_VALUE)
+      .hasParam(PARAM_TYPES, TYPES_VALUE_INLINED)
+      .andNoOtherParam();
+  }
+}