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.utils.System2;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.qualityprofile.QProfileChangeDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.db.qualityprofile.QualityProfileTesting;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.organization.DefaultOrganizationProvider;
36 import org.sonar.server.organization.TestDefaultOrganizationProvider;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.WsActionTester;
41 import static org.assertj.core.api.Assertions.assertThat;
43 public class ChangelogActionDatabaseTest {
46 public DbTester dbTester = DbTester.create(System2.INSTANCE);
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 public ExpectedException thrown = ExpectedException.none();
52 private WsActionTester wsTester;
53 private ChangelogLoader changelogLoader;
54 private QProfileWsSupport wsSupport;
55 private OrganizationDto organization;
56 private DefaultOrganizationProvider defaultOrganizationProvider;
59 public void before() {
60 defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
61 wsSupport = new QProfileWsSupport(dbTester.getDbClient(), userSession, defaultOrganizationProvider);
62 changelogLoader = new ChangelogLoader(dbTester.getDbClient());
63 wsTester = new WsActionTester(
64 new ChangelogAction(changelogLoader, wsSupport, new Languages(), dbTester.getDbClient()));
65 organization = dbTester.organizations().insert();
69 public void find_changelog_by_profile_key() throws Exception {
70 QProfileDto profile = dbTester.qualityProfiles().insert(organization);
72 String response = wsTester.newRequest()
74 .setParam("profileKey", profile.getKee())
78 assertThat(response).isNotEmpty();
82 public void find_changelog_by_language_and_name() throws Exception {
83 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization());
85 String response = wsTester.newRequest()
87 .setParam("language", qualityProfile.getLanguage())
88 .setParam("profileName", qualityProfile.getName())
92 assertThat(response).isNotEmpty();
96 public void find_changelog_by_organization_and_language_and_name() throws Exception {
97 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
99 String response = wsTester.newRequest()
101 .setParam("language", qualityProfile.getLanguage())
102 .setParam("profileName", qualityProfile.getName())
103 .setParam("organization", organization.getKey())
107 assertThat(response).isNotEmpty();
111 public void do_not_find_changelog_by_wrong_organization_and_language_and_name() throws Exception {
112 OrganizationDto organization1 = dbTester.organizations().insert();
113 OrganizationDto organization2 = dbTester.organizations().insert();
115 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization1);
117 TestRequest request = wsTester.newRequest()
119 .setParam("language", qualityProfile.getLanguage())
120 .setParam("profileName", qualityProfile.getName())
121 .setParam("organization", organization2.getKey());
123 thrown.expect(NotFoundException.class);
129 public void changelog_empty() throws Exception {
130 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
132 String response = wsTester.newRequest()
134 .setParam("profileKey", qualityProfile.getKee())
138 assertThat(response).contains("\"total\":0");
139 assertThat(response).contains("\"events\":[]");
143 public void changelog_not_empty() throws Exception {
144 QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
145 QProfileChangeDto change = QualityProfileTesting.newQProfileChangeDto()
148 .setRulesProfileUuid(qualityProfile.getRulesProfileUuid());
149 DbSession session = dbTester.getSession();
150 dbTester.getDbClient().qProfileChangeDao().insert(session, change);
153 String response = wsTester.newRequest()
155 .setParam("profileKey", qualityProfile.getKee())
159 assertThat(response).contains("\"total\":1");