]> source.dussan.org Git - sonarqube.git/blob
003501437d0c9ffcca380a8d02b94419666ffb59
[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.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;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42
43 public class ChangelogActionDatabaseTest {
44
45   @Rule
46   public DbTester dbTester = DbTester.create(System2.INSTANCE);
47   @Rule
48   public UserSessionRule userSession = UserSessionRule.standalone();
49   @Rule
50   public ExpectedException thrown = ExpectedException.none();
51
52   private WsActionTester wsTester;
53   private ChangelogLoader changelogLoader;
54   private QProfileWsSupport wsSupport;
55   private OrganizationDto organization;
56   private DefaultOrganizationProvider defaultOrganizationProvider;
57
58   @Before
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();
66   }
67
68   @Test
69   public void find_changelog_by_profile_key() throws Exception {
70     QProfileDto profile = dbTester.qualityProfiles().insert(organization);
71
72     String response = wsTester.newRequest()
73       .setMethod("GET")
74       .setParam("profileKey", profile.getKee())
75       .execute()
76       .getInput();
77
78     assertThat(response).isNotEmpty();
79   }
80
81   @Test
82   public void find_changelog_by_language_and_name() throws Exception {
83     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(dbTester.getDefaultOrganization());
84
85     String response = wsTester.newRequest()
86       .setMethod("GET")
87       .setParam("language", qualityProfile.getLanguage())
88       .setParam("profileName", qualityProfile.getName())
89       .execute()
90       .getInput();
91
92     assertThat(response).isNotEmpty();
93   }
94
95   @Test
96   public void find_changelog_by_organization_and_language_and_name() throws Exception {
97     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
98
99     String response = wsTester.newRequest()
100       .setMethod("GET")
101       .setParam("language", qualityProfile.getLanguage())
102       .setParam("profileName", qualityProfile.getName())
103       .setParam("organization", organization.getKey())
104       .execute()
105       .getInput();
106
107     assertThat(response).isNotEmpty();
108   }
109
110   @Test
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();
114
115     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization1);
116
117     TestRequest request = wsTester.newRequest()
118       .setMethod("GET")
119       .setParam("language", qualityProfile.getLanguage())
120       .setParam("profileName", qualityProfile.getName())
121       .setParam("organization", organization2.getKey());
122
123     thrown.expect(NotFoundException.class);
124
125     request.execute();
126   }
127
128   @Test
129   public void changelog_empty() throws Exception {
130     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
131
132     String response = wsTester.newRequest()
133       .setMethod("GET")
134       .setParam("profileKey", qualityProfile.getKee())
135       .execute()
136       .getInput();
137
138     assertThat(response).contains("\"total\":0");
139     assertThat(response).contains("\"events\":[]");
140   }
141
142   @Test
143   public void changelog_not_empty() throws Exception {
144     QProfileDto qualityProfile = dbTester.qualityProfiles().insert(organization);
145     QProfileChangeDto change = QualityProfileTesting.newQProfileChangeDto()
146       .setUuid(null)
147       .setCreatedAt(0)
148       .setRulesProfileUuid(qualityProfile.getRulesProfileUuid());
149     DbSession session = dbTester.getSession();
150     dbTester.getDbClient().qProfileChangeDao().insert(session, change);
151     session.commit();
152
153     String response = wsTester.newRequest()
154       .setMethod("GET")
155       .setParam("profileKey", qualityProfile.getKee())
156       .execute()
157       .getInput();
158
159     assertThat(response).contains("\"total\":1");
160   }
161 }