3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.measure.custom.ws;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.component.ComponentTesting;
34 import org.sonar.db.measure.custom.CustomMeasureDto;
35 import org.sonar.db.metric.MetricDto;
36 import org.sonar.server.component.TestComponentFinder;
37 import org.sonar.server.es.EsTester;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.tester.UserSessionRule;
40 import org.sonar.server.ws.TestResponse;
41 import org.sonar.server.ws.WsActionTester;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto;
45 import static org.sonar.db.metric.MetricTesting.newMetricDto;
47 public class MetricsActionTest {
48 private static final String DEFAULT_PROJECT_UUID = "project-uuid";
49 private static final String DEFAULT_PROJECT_KEY = "project-key";
52 public EsTester es = EsTester.create();
54 public ExpectedException expectedException = ExpectedException.none();
56 public UserSessionRule userSession = UserSessionRule.standalone();
58 public DbTester db = DbTester.create(System2.INSTANCE);
60 private final DbClient dbClient = db.getDbClient();
61 private final DbSession dbSession = db.getSession();
62 private ComponentDto defaultProject;
63 private MetricsAction underTest = new MetricsAction(dbClient, userSession, TestComponentFinder.from(db));
64 private WsActionTester tester = new WsActionTester(underTest);
68 defaultProject = insertDefaultProject();
69 userSession.logIn().addProjectPermission(UserRole.ADMIN, defaultProject);
73 public void list_metrics() {
74 insertCustomMetric("metric-key-1");
75 insertCustomMetric("metric-key-2");
76 insertCustomMetric("metric-key-3");
78 String response = newRequest().getInput();
80 assertThat(response).contains("metric-key-1", "metric-key-2", "metric-key-3");
84 public void list_metrics_active_and_custom_only() {
85 insertCustomMetric("metric-key-1");
86 dbClient.metricDao().insert(dbSession, newMetricDto().setEnabled(true).setUserManaged(false).setKey("metric-key-2"));
87 dbClient.metricDao().insert(dbSession, newMetricDto().setEnabled(false).setUserManaged(true).setKey("metric-key-3"));
90 String response = newRequest().getInput();
92 assertThat(response).contains("metric-key-1")
93 .doesNotContain("metric-key-2")
94 .doesNotContain("metric-key-3");
98 public void list_metrics_where_no_existing_custom_measure() {
99 MetricDto metric = insertCustomMetric("metric-key-1");
100 insertCustomMetric("metric-key-2");
101 insertProject("project-uuid-2", "project-key-2");
103 CustomMeasureDto customMeasure = newCustomMeasureDto()
104 .setComponentUuid(defaultProject.uuid())
105 .setMetricUuid(metric.getUuid());
106 dbClient.customMeasureDao().insert(dbSession, customMeasure);
109 String response = newRequest().getInput();
111 assertThat(response).contains("metric-key-2")
112 .doesNotContain("metric-key-1");
116 public void list_metrics_based_on_project_key() {
117 MetricDto metric = insertCustomMetric("metric-key-1");
118 insertCustomMetric("metric-key-2");
119 insertProject("project-uuid-2", "project-key-2");
121 CustomMeasureDto customMeasure = newCustomMeasureDto()
122 .setComponentUuid(defaultProject.uuid())
123 .setMetricUuid(metric.getUuid());
124 dbClient.customMeasureDao().insert(dbSession, customMeasure);
127 String response = tester.newRequest()
128 .setParam(MetricsAction.PARAM_PROJECT_KEY, DEFAULT_PROJECT_KEY)
129 .execute().getInput();
131 assertThat(response).contains("metric-key-2")
132 .doesNotContain("metric-key-1");
136 public void list_metrics_as_a_project_admin() {
137 insertCustomMetric("metric-key-1");
138 userSession.logIn("login").addProjectPermission(UserRole.ADMIN, defaultProject);
140 String response = newRequest().getInput();
142 assertThat(response).contains("metric-key-1");
146 public void response_with_correct_formatting() {
147 dbClient.metricDao().insert(dbSession, newCustomMetric("custom-key-1")
148 .setShortName("custom-name-1")
149 .setDescription("custom-description-1")
150 .setDomain("custom-domain-1")
151 .setValueType(Metric.ValueType.INT.name())
153 .setQualitative(false)
155 dbClient.metricDao().insert(dbSession, newCustomMetric("custom-key-2")
156 .setShortName("custom-name-2")
157 .setDescription("custom-description-2")
158 .setDomain("custom-domain-2")
159 .setValueType(Metric.ValueType.INT.name())
161 .setQualitative(true)
163 dbClient.metricDao().insert(dbSession, newCustomMetric("custom-key-3")
164 .setShortName("custom-name-3")
165 .setDescription("custom-description-3")
166 .setDomain("custom-domain-3")
167 .setValueType(Metric.ValueType.INT.name())
169 .setQualitative(false)
173 TestResponse response = tester.newRequest()
174 .setParam(MetricsAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
177 response.assertJson(getClass(), "metrics.json");
181 public void fail_if_project_id_nor_project_key_provided() {
182 expectedException.expect(IllegalArgumentException.class);
184 tester.newRequest().execute();
188 public void fail_if_insufficient_privilege() {
189 expectedException.expect(ForbiddenException.class);
190 userSession.logIn("login");
192 insertCustomMetric("metric-key-1");
197 private TestResponse newRequest() {
198 return tester.newRequest()
199 .setParam(MetricsAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
203 private MetricDto insertCustomMetric(String metricKey) {
204 MetricDto metric = newCustomMetric(metricKey);
205 dbClient.metricDao().insert(dbSession, metric);
211 private static MetricDto newCustomMetric(String metricKey) {
212 return newMetricDto().setEnabled(true).setUserManaged(true).setKey(metricKey);
215 private ComponentDto insertDefaultProject() {
216 return insertProject(DEFAULT_PROJECT_UUID, DEFAULT_PROJECT_KEY);
219 private ComponentDto insertProject(String projectUuid, String projectKey) {
220 ComponentDto project = ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization(), projectUuid)
221 .setDbKey(projectKey);
222 dbClient.componentDao().insert(dbSession, project);