]> source.dussan.org Git - sonarqube.git/blob
798a30f2206461824dc0f938137afebb240864b6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile.ws;
21
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;
41
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;
47
48 public class ChangelogActionDatabaseTest {
49
50   @Rule
51   public DbTester dbTester = DbTester.create(System2.INSTANCE);
52   @Rule
53   public UserSessionRule userSession = UserSessionRule.standalone();
54   @Rule
55   public ExpectedException thrown = ExpectedException.none();
56
57   private WsActionTester ws;
58   private ChangelogLoader changelogLoader;
59   private QProfileWsSupport wsSupport;
60   private OrganizationDto organization;
61   private DefaultOrganizationProvider defaultOrganizationProvider;
62
63   @Before
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();
71   }
72
73   @Test
74   public void definition() {
75     WebService.Action definition = ws.getDef();
76
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");
86   }
87
88   @Test
89   public void find_changelog_by_profile_key() throws Exception {
90     QProfileDto profile = dbTester.qualityProfiles().insert(organization);
91
92     String response = ws.newRequest()
93       .setMethod("GET")
94       .setParam(PARAM_PROFILE, profile.getKee())
95       .execute()
96       .getInput();
97
98     assertThat(response).isNotEmpty();
99   }
100
101   @Test
102   public void find_changelog_by_language_and_name() throws Exception {
103     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization());
104
105     String response = ws.newRequest()
106       .setMethod("GET")
107       .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
108       .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
109       .execute()
110       .getInput();
111
112     assertThat(response).isNotEmpty();
113   }
114
115   @Test
116   public void find_changelog_by_organization_and_language_and_name() throws Exception {
117     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
118
119     String response = ws.newRequest()
120       .setMethod("GET")
121       .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
122       .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
123       .setParam(PARAM_ORGANIZATION, organization.getKey())
124       .execute()
125       .getInput();
126
127     assertThat(response).isNotEmpty();
128   }
129
130   @Test
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();
134
135     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization1);
136
137     TestRequest request = ws.newRequest()
138       .setMethod("GET")
139       .setParam(PARAM_LANGUAGE, qualityProfile.getLanguage())
140       .setParam(PARAM_PROFILE_NAME, qualityProfile.getName())
141       .setParam(PARAM_ORGANIZATION, organization2.getKey());
142
143     thrown.expect(NotFoundException.class);
144
145     request.execute();
146   }
147
148   @Test
149   public void changelog_empty() throws Exception {
150     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
151
152     String response = ws.newRequest()
153       .setMethod("GET")
154       .setParam(PARAM_PROFILE, qualityProfile.getKee())
155       .execute()
156       .getInput();
157
158     assertThat(response).contains("\"total\":0");
159     assertThat(response).contains("\"events\":[]");
160   }
161
162   @Test
163   public void changelog_not_empty() throws Exception {
164     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
165     QProfileChangeDto change = QualityProfileTesting.newQProfileChangeDto()
166       .setUuid(null)
167       .setCreatedAt(0)
168       .setRulesProfileUuid(qualityProfile.getRulesProfileUuid());
169     DbSession session = dbTester.getSession();
170     dbTester.getDbClient().qProfileChangeDao().insert(session, change);
171     session.commit();
172
173     String response = ws.newRequest()
174       .setMethod("GET")
175       .setParam(PARAM_PROFILE, qualityProfile.getKee())
176       .execute()
177       .getInput();
178
179     assertThat(response).contains("\"total\":1");
180   }
181 }