aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-09-26 12:11:12 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-09-30 14:16:55 +0200
commit2bbfd0c6ca169d20ae2d51e7cbb029ad3f9ea73b (patch)
treeb942ab1833e9f3bf51234cd24fbcfdaccd7e93c5 /sonar-ws
parent8492c97d913d9aa4330975017b68200977a02123 (diff)
downloadsonarqube-2bbfd0c6ca169d20ae2d51e7cbb029ad3f9ea73b.tar.gz
sonarqube-2bbfd0c6ca169d20ae2d51e7cbb029ad3f9ea73b.zip
SONAR-8120 Create WS measures/search to search for measures
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/measure/SearchRequest.java96
-rw-r--r--sonar-ws/src/main/protobuf/ws-measures.proto5
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/measure/SearchRequestTest.java123
3 files changed, 224 insertions, 0 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/measure/SearchRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/measure/SearchRequest.java
new file mode 100644
index 00000000000..ad16a8c3a5b
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/measure/SearchRequest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.measure;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+public class SearchRequest {
+ private final List<String> metricKeys;
+ private final List<String> componentIds;
+ private final List<String> componentKeys;
+
+ public SearchRequest(Builder builder) {
+ metricKeys = builder.metricKeys;
+ componentIds = builder.componentIds;
+ componentKeys = builder.componentKeys;
+ }
+
+ public List<String> getMetricKeys() {
+ return metricKeys;
+ }
+
+ public boolean hasComponentIds() {
+ return componentIds != null;
+ }
+
+ public List<String> getComponentIds() {
+ return requireNonNull(componentIds, "No component id in request");
+ }
+
+ public boolean hasComponentKeys() {
+ return componentKeys != null;
+ }
+
+ public List<String> getComponentKeys() {
+ return requireNonNull(componentKeys, "No component key in request");
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private List<String> metricKeys;
+ private List<String> componentIds;
+ private List<String> componentKeys;
+
+ private Builder() {
+ // enforce method constructor
+ }
+
+ public Builder setMetricKeys(List<String> metricKeys) {
+ this.metricKeys = metricKeys;
+ return this;
+ }
+
+ public Builder setComponentIds(List<String> componentIds) {
+ this.componentIds = componentIds;
+ return this;
+ }
+
+ public Builder setComponentKeys(List<String> componentKeys) {
+ this.componentKeys = componentKeys;
+ return this;
+ }
+
+ public SearchRequest build() {
+ checkArgument(metricKeys != null && !metricKeys.isEmpty(), "Metric keys must be provided");
+ checkArgument(
+ (componentIds != null && !componentIds.isEmpty())
+ ^ (componentKeys != null && !componentKeys.isEmpty()),
+ "Either component ids or component keys must be provided, not both.");
+ return new SearchRequest(this);
+ }
+ }
+}
diff --git a/sonar-ws/src/main/protobuf/ws-measures.proto b/sonar-ws/src/main/protobuf/ws-measures.proto
index 2ade7c75b56..b29ffdf8c12 100644
--- a/sonar-ws/src/main/protobuf/ws-measures.proto
+++ b/sonar-ws/src/main/protobuf/ws-measures.proto
@@ -42,6 +42,11 @@ message ComponentWsResponse {
optional Periods periods = 3;
}
+// WS api/measures/search
+message SearchWsResponse {
+ repeated Component components = 1;
+}
+
message Component {
optional string id = 1;
optional string key = 2;
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/measure/SearchRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/measure/SearchRequestTest.java
new file mode 100644
index 00000000000..7cffedb9471
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/measure/SearchRequestTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.measure;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SearchRequestTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ SearchRequest.Builder underTest = SearchRequest.builder();
+
+ @Test
+ public void with_component_ids() {
+ SearchRequest result = underTest
+ .setMetricKeys(singletonList("metric"))
+ .setComponentIds(singletonList("uuid"))
+ .build();
+
+ assertThat(result.getMetricKeys()).containsExactly("metric");
+ assertThat(result.getComponentIds()).containsExactly("uuid");
+ assertThat(result.hasComponentKeys()).isFalse();
+ }
+
+ @Test
+ public void with_component_keys() {
+ SearchRequest result = underTest
+ .setMetricKeys(singletonList("metric"))
+ .setComponentKeys(singletonList("key"))
+ .build();
+
+ assertThat(result.getMetricKeys()).containsExactly("metric");
+ assertThat(result.getComponentKeys()).containsExactly("key");
+ assertThat(result.hasComponentIds()).isFalse();
+ }
+
+ @Test
+ public void fail_when_non_null_metric_keys() {
+ expectExceptionOnMetricKeys();
+
+ underTest.setMetricKeys(null).build();
+ }
+
+ @Test
+ public void fail_when_non_empty_metric_keys() {
+ expectExceptionOnMetricKeys();
+
+ underTest.setMetricKeys(emptyList()).build();
+ }
+
+ @Test
+ public void fail_when_unset_metric_keys() {
+ expectExceptionOnMetricKeys();
+
+ underTest.build();
+ }
+
+ @Test
+ public void fail_when_component_ids_and_keys_provided() {
+ expectExceptionOnComponents();
+
+ underTest
+ .setMetricKeys(singletonList("metric"))
+ .setComponentIds(singletonList("uuid"))
+ .setComponentKeys(singletonList("key"))
+ .build();
+ }
+
+ @Test
+ public void fail_when_component_ids_is_empty() {
+ expectExceptionOnComponents();
+
+ underTest
+ .setMetricKeys(singletonList("metric"))
+ .setComponentIds(emptyList())
+ .build();
+ }
+
+ @Test
+ public void fail_when_component_keys_is_empty() {
+ expectExceptionOnComponents();
+
+ underTest
+ .setMetricKeys(singletonList("metric"))
+ .setComponentKeys(emptyList())
+ .build();
+ }
+
+ private void expectExceptionOnMetricKeys() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Metric keys must be provided");
+ }
+
+ private void expectExceptionOnComponents() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Either component ids or component keys must be provided, not both.");
+ }
+}