3 * Copyright (C) 2009-2017 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.qualityprofile.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.resources.Languages;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.api.utils.System2;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.qualityprofile.QProfileChangeDto;
33 import org.sonar.db.qualityprofile.QProfileDto;
34 import org.sonar.db.qualityprofile.QualityProfileTesting;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.organization.DefaultOrganizationProvider;
37 import org.sonar.server.organization.TestDefaultOrganizationProvider;
38 import org.sonar.server.tester.UserSessionRule;
39 import org.sonar.server.ws.TestRequest;
40 import org.sonar.server.ws.WsActionTester;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
44 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
45 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
46 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME;
48 public class ChangelogActionDatabaseTest {
51 public DbTester dbTester = DbTester.create(System2.INSTANCE);
53 public UserSessionRule userSession = UserSessionRule.standalone();
55 public ExpectedException thrown = ExpectedException.none();
57 private WsActionTester ws;
58 private ChangelogLoader changelogLoader;
59 private QProfileWsSupport wsSupport;
60 private OrganizationDto organization;
61 private DefaultOrganizationProvider defaultOrganizationProvider;
64 public void before() {
65 defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
66 wsSupport = new QProfileWsSupport(dbTester.getDbClient(), userSession, defaultOrganizationProvider);
67 changelogLoader = new ChangelogLoader(dbTester.getDbClient());
68 ws = new WsActionTester(
69 new ChangelogAction(changelogLoader, wsSupport, new Languages(), dbTester.getDbClient()));
70 organization = dbTester.organizations().insert();
74 public void definition() {
75 WebService.Action definition = ws.getDef();
77 assertThat(definition.responseExampleAsString()).isNotEmpty();
78 assertThat(definition.params()).extracting(WebService.Param::key)
79 .containsExactlyInAnyOrder("profile", "profileName", "language", "organization", "since", "to", "p", "ps");
80 WebService.Param profile = definition.param("profile");
81 assertThat(profile.deprecatedKey()).isEqualTo("profileKey");
82 WebService.Param profileName = definition.param("profileName");
83 assertThat(profileName.deprecatedSince()).isEqualTo("6.5");
84 WebService.Param language = definition.param("language");
85 assertThat(language.deprecatedSince()).isEqualTo("6.5");
89 public void find_changelog_by_profile_key() throws Exception {
90 QProfileDto profile = dbTester.qualityProfiles().insert(organization);
92 String response = ws.newRequest()
94 .setParam(PARAM_PROFILE, profile.getKee())
98 assertThat(response).isNotEmpty();
102 public void find_changelog_by_language_and_name() throws Exception {
103 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization());
105 String response = ws.newRequest()
107 .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
108 .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
112 assertThat(response).isNotEmpty();
116 public void find_changelog_by_organization_and_language_and_name() throws Exception {
117 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
119 String response = ws.newRequest()
121 .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
122 .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
123 .setParam(PARAM_ORGANIZATION, organization.getKey())
127 assertThat(response).isNotEmpty();
131 public void do_not_find_changelog_by_wrong_organization_and_language_and_name() throws Exception {
132 OrganizationDto organization1 = dbTester.organizations().insert();
133 OrganizationDto organization2 = dbTester.organizations().insert();
135 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization1);
137 TestRequest request = ws.newRequest()
139 .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
140 .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
141 .setParam(PARAM_ORGANIZATION, organization2.getKey());
143 thrown.expect(NotFoundException.class);
149 public void changelog_empty() throws Exception {
150 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
152 String response = ws.newRequest()
154 .setParam(PARAM_PROFILE, qualityProfile.getKee())
158 assertThat(response).contains("\"total\":0");
159 assertThat(response).contains("\"events\":[]");
163 public void changelog_not_empty() throws Exception {
164 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
165 QProfileChangeDto change = QualityProfileTesting.newQProfileChangeDto()
168 .setRulesProfileUuid(qualityProfile.getRulesProfileUuid());
169 DbSession session = dbTester.getSession();
170 dbTester.getDbClient().qProfileChangeDao().insert(session, change);
173 String response = ws.newRequest()
175 .setParam(PARAM_PROFILE, qualityProfile.getKee())
179 assertThat(response).contains("\"total\":1");