123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 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.badge.ws;
-
- import com.tngtech.java.junit.dataprovider.DataProvider;
- import com.tngtech.java.junit.dataprovider.DataProviderRunner;
- import com.tngtech.java.junit.dataprovider.UseDataProvider;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
- import org.junit.runner.RunWith;
- import org.sonar.api.config.internal.MapSettings;
- import org.sonar.api.measures.CoreMetrics;
- import org.sonar.api.measures.Metric.Level;
- import org.sonar.api.server.ws.WebService;
- import org.sonar.api.server.ws.WebService.Param;
- import org.sonar.db.DbTester;
- import org.sonar.db.component.BranchType;
- import org.sonar.db.component.ComponentDto;
- import org.sonar.db.component.ComponentTesting;
- import org.sonar.db.metric.MetricDto;
- import org.sonar.db.user.UserDto;
- import org.sonar.server.badge.ws.SvgGenerator.Color;
- import org.sonar.server.component.ComponentFinder;
- import org.sonar.server.measure.Rating;
- import org.sonar.server.tester.UserSessionRule;
- import org.sonar.server.ws.TestResponse;
- import org.sonar.server.ws.WsActionTester;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.assertj.core.api.Assertions.tuple;
- import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
- import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
- import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
- import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
- import static org.sonar.api.measures.Metric.Level.ERROR;
- import static org.sonar.api.measures.Metric.Level.OK;
- import static org.sonar.api.measures.Metric.Level.WARN;
- import static org.sonar.api.measures.Metric.ValueType.INT;
- import static org.sonar.api.measures.Metric.ValueType.LEVEL;
- import static org.sonar.api.measures.Metric.ValueType.PERCENT;
- import static org.sonar.api.measures.Metric.ValueType.RATING;
- import static org.sonar.api.measures.Metric.ValueType.WORK_DUR;
- import static org.sonar.api.web.UserRole.USER;
- import static org.sonar.db.component.BranchType.BRANCH;
- import static org.sonar.server.badge.ws.SvgGenerator.Color.DEFAULT;
- import static org.sonar.server.badge.ws.SvgGenerator.Color.QUALITY_GATE_ERROR;
- import static org.sonar.server.badge.ws.SvgGenerator.Color.QUALITY_GATE_OK;
- import static org.sonar.server.badge.ws.SvgGenerator.Color.QUALITY_GATE_WARN;
-
- @RunWith(DataProviderRunner.class)
- public class MeasureActionTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create();
-
- private MapSettings mapSettings = new MapSettings().setProperty("sonar.sonarcloud.enabled", false);
-
- private WsActionTester ws = new WsActionTester(
- new MeasureAction(
- db.getDbClient(),
- new ProjectBadgesSupport(userSession, new ComponentFinder(db.getDbClient(), null)),
- new SvgGenerator(mapSettings.asConfig())));
-
- @Test
- public void int_measure() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(10_000d));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "bugs", "10k", DEFAULT);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(project, metric, response);
- }
-
- @Test
- public void percent_measure() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY).setValueType(PERCENT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(12.345d));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "coverage", "12.3%", DEFAULT);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(project, metric, response);
- }
-
- @Test
- public void duration_measure() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(TECHNICAL_DEBT_KEY).setValueType(WORK_DUR.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(10_000d));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "technical debt", "21d", DEFAULT);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(project, metric, response);
- }
-
- @DataProvider
- public static Object[][] ratings() {
- return new Object[][] {
- {Rating.A, Color.RATING_A},
- {Rating.B, Color.RATING_B},
- {Rating.C, Color.RATING_C},
- {Rating.D, Color.RATING_D},
- {Rating.E, Color.RATING_E}
- };
- }
-
- @Test
- @UseDataProvider("ratings")
- public void rating_measure(Rating rating, Color color) {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(SQALE_RATING_KEY).setValueType(RATING.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue((double) rating.getIndex()).setData(rating.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "maintainability", rating.name(), color);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(project, metric, response);
- }
-
- @DataProvider
- public static Object[][] qualityGates() {
- return new Object[][] {
- {OK, "passed", QUALITY_GATE_OK},
- {ERROR, "failed", QUALITY_GATE_ERROR}
- };
- }
-
- @Test
- @UseDataProvider("qualityGates")
- public void quality_gate(Level status, String expectedValue, Color expectedColor) {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = createQualityGateMetric();
- db.measures().insertLiveMeasure(project, metric, m -> m.setData(status.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "quality gate", expectedValue, expectedColor);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(project, metric, response);
- }
-
- @Test
- public void display_deprecated_warning_quality_gate() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = createQualityGateMetric();
- db.measures().insertLiveMeasure(project, metric, m -> m.setData(WARN.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "quality gate", "warning", QUALITY_GATE_WARN);
- }
-
- @Test
- public void measure_on_non_main_branch() {
- ComponentDto project = db.components().insertPublicProject(p -> p.setPrivate(false));
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(5_000d));
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
- db.measures().insertLiveMeasure(branch, metric, m -> m.setValue(10_000d));
-
- TestResponse response = ws.newRequest()
- .setParam("project", branch.getKey())
- .setParam("branch", branch.getBranch())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "bugs", "10k", DEFAULT);
-
- // Second call with If-None-Match must return 304
- response = ws.newRequest()
- .setHeader("If-None-Match", response.getHeader("ETag"))
- .setParam("project", branch.getKey())
- .setParam("branch", branch.getBranch())
- .setParam("metric", metric.getKey())
- .execute();
-
- assertThat(response.getInput()).isEmpty();
- assertThat(response.getStatus()).isEqualTo(304);
- }
-
- @Test
- public void measure_on_application() {
- ComponentDto application = db.components().insertPublicApplication();
- userSession.registerComponents(application);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(application, metric, m -> m.setValue(10_000d));
-
- TestResponse response = ws.newRequest()
- .setParam("project", application.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkSvg(response, "bugs", "10k", DEFAULT);
-
- // Second call with If-None-Match must return 304
- checkWithIfNoneMatchHeader(application, metric, response);
- }
-
- @Test
- public void return_error_if_project_does_not_exist() throws ParseException {
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY));
-
- TestResponse response = ws.newRequest()
- .setParam("project", "unknown")
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Project has not been found");
- }
-
- @Test
- public void return_error_if_branch_does_not_exist() throws ParseException {
- ComponentDto project = db.components().insertPublicProject();
- ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH));
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "unknown")
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Project has not been found");
- }
-
- @Test
- public void return_error_if_measure_not_found() throws ParseException {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Measure has not been found");
- }
-
- @Test
- public void return_error_on_directory() throws ParseException {
- ComponentDto project = db.components().insertPublicProject();
- ComponentDto directory = db.components().insertComponent(ComponentTesting.newDirectory(project, "path"));
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", directory.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Project has not been found");
- }
-
- @Test
- public void return_error_on_private_project() throws ParseException {
- ComponentDto project = db.components().insertPrivateProject();
- UserDto user = db.users().insertUser();
- userSession.logIn(user).addProjectPermission(USER, project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Project is invalid");
- }
-
- @Test
- public void return_error_on_provisioned_project() throws ParseException {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Measure has not been found");
- }
-
- @Test
- public void return_error_if_unauthorized() throws ParseException {
- ComponentDto project = db.components().insertPublicProject();
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY));
-
- TestResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- checkError(response, "Insufficient privileges");
- }
-
- @Test
- public void fail_on_invalid_quality_gate() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = createQualityGateMetric();
- db.measures().insertLiveMeasure(project, metric, m -> m.setData("UNKNOWN"));
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("No enum constant org.sonar.api.measures.Metric.Level.UNKNOWN");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
- }
-
- @Test
- public void fail_when_measure_value_is_null() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
- MetricDto metric = db.measures().insertMetric(m -> m.setKey(BUGS_KEY).setValueType(INT.name()));
- db.measures().insertLiveMeasure(project, metric, m -> m.setValue(null));
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Measure has not been found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", metric.getKey())
- .execute();
- }
-
- @Test
- public void fail_when_metric_not_found() {
- ComponentDto project = db.components().insertPublicProject();
- userSession.registerComponents(project);
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Metric 'bugs' hasn't been found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("metric", BUGS_KEY)
- .execute();
- }
-
- @Test
- public void test_definition() {
- WebService.Action def = ws.getDef();
- assertThat(def.key()).isEqualTo("measure");
- assertThat(def.isInternal()).isFalse();
- assertThat(def.isPost()).isFalse();
- assertThat(def.since()).isEqualTo("7.1");
- assertThat(def.responseExampleAsString()).isNotEmpty();
-
- assertThat(def.params())
- .extracting(Param::key, Param::isRequired)
- .containsExactlyInAnyOrder(
- tuple("project", true),
- tuple("branch", false),
- tuple("metric", true));
- }
-
- private void checkSvg(TestResponse response, String expectedLabel, String expectedValue, Color expectedColorValue) {
- assertThat(response.getHeader("ETag")).startsWith("W/");
- assertThat(response.getHeader("Cache-Control")).contains("no-cache");
- assertThat(response.getHeader("Expires")).isNull();
-
- assertThat(response.getInput()).contains(
- "<text", expectedLabel + "</text>",
- "<text", expectedValue + "</text>",
- "rect fill=\"" + expectedColorValue.getValue() + "\"");
- }
-
- private void checkError(TestResponse response, String expectedError) throws ParseException {
- SimpleDateFormat expiresDateFormat = new SimpleDateFormat(ETagUtils.RFC1123_DATE, Locale.US);
- assertThat(response.getHeader("Cache-Control")).contains("no-cache");
- assertThat(response.getHeader("Expires")).isNotNull();
- assertThat(response.getHeader("ETag")).isNull();
- assertThat(expiresDateFormat.parse(response.getHeader("Expires"))).isBeforeOrEqualsTo(new Date());
- assertThat(response.getInput()).contains("<text", ">" + expectedError + "</text>");
- }
-
- private void checkWithIfNoneMatchHeader(ComponentDto application, MetricDto metric, TestResponse response) {
- TestResponse newResponse = ws.newRequest()
- .setHeader("If-None-Match", response.getHeader("ETag"))
- .setParam("project", application.getKey())
- .setParam("metric", metric.getKey())
- .execute();
-
- assertThat(newResponse.getInput()).isEmpty();
- assertThat(newResponse.getStatus()).isEqualTo(304);
- }
-
- private MetricDto createQualityGateMetric() {
- return db.measures().insertMetric(m -> m.setKey(CoreMetrics.ALERT_STATUS_KEY).setValueType(LEVEL.name()));
- }
- }
|