2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.qualityprofile;
23 import org.apache.ibatis.session.SqlSession;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.runners.MockitoJUnitRunner;
31 import org.mockito.stubbing.Answer;
32 import org.sonar.api.database.DatabaseSession;
33 import org.sonar.api.profiles.ProfileImporter;
34 import org.sonar.api.profiles.RulesProfile;
35 import org.sonar.api.rule.Severity;
36 import org.sonar.api.rules.ActiveRule;
37 import org.sonar.api.rules.Rule;
38 import org.sonar.api.rules.RulePriority;
39 import org.sonar.api.utils.ValidationMessages;
40 import org.sonar.core.qualityprofile.db.ActiveRuleDao;
41 import org.sonar.core.qualityprofile.db.ActiveRuleDto;
42 import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
43 import org.sonar.server.exceptions.BadRequestException;
45 import java.io.Reader;
46 import java.util.List;
48 import static com.google.common.collect.Lists.newArrayList;
49 import static org.fest.assertions.Assertions.assertThat;
50 import static org.fest.assertions.Fail.fail;
51 import static org.mockito.Matchers.any;
52 import static org.mockito.Matchers.eq;
53 import static org.mockito.Mockito.doAnswer;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.never;
56 import static org.mockito.Mockito.verify;
57 import static org.mockito.Mockito.when;
59 @RunWith(MockitoJUnitRunner.class)
60 public class QProfileRepositoryExporterTest {
66 DatabaseSession hibernateSession;
69 ActiveRuleDao activeRuleDao;
71 List<ProfileImporter> importers = newArrayList();
73 Integer currentId = 1;
75 QProfileRepositoryExporter operations;
78 public void setUp() throws Exception {
79 // Associate an id when inserting an object to simulate the db id generator
80 doAnswer(new Answer() {
81 public Object answer(InvocationOnMock invocation) {
82 Object[] args = invocation.getArguments();
83 ActiveRuleDto dto = (ActiveRuleDto) args[0];
84 dto.setId(currentId++);
87 }).when(activeRuleDao).insert(any(ActiveRuleDto.class), any(SqlSession.class));
89 operations = new QProfileRepositoryExporter(activeRuleDao, importers);
93 public void import_from_xml_plugin() throws Exception {
94 RulesProfile profile = RulesProfile.create("Default", "java");
95 Rule rule = Rule.create("pmd", "rule1");
96 rule.createParameter("max");
98 ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
99 activeRule.setParameter("max", "10");
101 ProfileImporter importer = mock(ProfileImporter.class);
102 when(importer.getKey()).thenReturn("pmd");
103 when(importer.importProfile(any(Reader.class), any(ValidationMessages.class))).thenReturn(profile);
104 importers.add(importer);
106 operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
108 verify(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
110 ArgumentCaptor<ActiveRuleDto> activeRuleArgument = ArgumentCaptor.forClass(ActiveRuleDto.class);
111 verify(activeRuleDao).insert(activeRuleArgument.capture(), eq(session));
112 assertThat(activeRuleArgument.getValue().getRuleId()).isEqualTo(10);
113 assertThat(activeRuleArgument.getValue().getSeverityString()).isEqualTo(Severity.BLOCKER);
115 ArgumentCaptor<ActiveRuleParamDto> activeRuleParamArgument = ArgumentCaptor.forClass(ActiveRuleParamDto.class);
116 verify(activeRuleDao).insert(activeRuleParamArgument.capture(), eq(session));
117 assertThat(activeRuleParamArgument.getValue().getKey()).isEqualTo("max");
118 assertThat(activeRuleParamArgument.getValue().getValue()).isEqualTo("10");
122 public void import_from_xml_plugin_add_infos_and_warnings() throws Exception {
123 final RulesProfile profile = RulesProfile.create("Default", "java");
124 Rule rule = Rule.create("pmd", "rule1");
125 rule.createParameter("max");
127 ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
128 activeRule.setParameter("max", "10");
130 ProfileImporter importer = mock(ProfileImporter.class);
131 when(importer.getKey()).thenReturn("pmd");
132 doAnswer(new Answer() {
133 public Object answer(InvocationOnMock invocation) {
134 Object[] args = invocation.getArguments();
135 ValidationMessages validationMessages = (ValidationMessages) args[1];
136 validationMessages.addInfoText("an info message");
137 validationMessages.addWarningText("a warning message");
140 }).when(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
141 importers.add(importer);
143 QProfileResult result = operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
145 assertThat(result.infos()).hasSize(1);
146 assertThat(result.warnings()).hasSize(1);
150 public void fail_to_import_profile_from_xml_plugin_if_error() throws Exception {
151 final RulesProfile profile = RulesProfile.create("Default", "java");
152 Rule rule = Rule.create("pmd", "rule1");
153 rule.createParameter("max");
155 ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
156 activeRule.setParameter("max", "10");
158 ProfileImporter importer = mock(ProfileImporter.class);
159 when(importer.getKey()).thenReturn("pmd");
160 importers.add(importer);
162 doAnswer(new Answer() {
163 public Object answer(InvocationOnMock invocation) {
164 Object[] args = invocation.getArguments();
165 ValidationMessages validationMessages = (ValidationMessages) args[1];
166 validationMessages.addErrorText("error!");
169 }).when(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
172 operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
174 } catch (Exception e) {
175 assertThat(e).isInstanceOf(BadRequestException.class);
180 public void fail_to_import_profile_when_missing_importer() throws Exception {
181 final RulesProfile profile = RulesProfile.create("Default", "java");
182 Rule rule = Rule.create("pmd", "rule1");
183 rule.createParameter("max");
185 ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
186 activeRule.setParameter("max", "10");
188 ProfileImporter importer = mock(ProfileImporter.class);
189 when(importer.getKey()).thenReturn("pmd");
190 importers.add(importer);
192 when(importer.importProfile(any(Reader.class), any(ValidationMessages.class))).thenReturn(profile);
195 operations.importXml(new QProfile().setId(1), "unknown", "<xml/>", session);
197 } catch (Exception e) {
198 assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("No such importer : unknown");
200 verify(importer, never()).importProfile(any(Reader.class), any(ValidationMessages.class));
204 public void get_profile_importers_for_language() throws Exception {
205 // 2 importers not declaring supported languages -> match all languages -> to be include in result
206 ProfileImporter importersWithEmptySupportedLanguagesList = mock(ProfileImporter.class);
207 when(importersWithEmptySupportedLanguagesList.getSupportedLanguages()).thenReturn(new String[] {});
208 importers.add(importersWithEmptySupportedLanguagesList);
209 importers.add(mock(ProfileImporter.class));
211 // 1 importers supporting the java language -> to be include in result
212 ProfileImporter importerSupportingJava = mock(ProfileImporter.class);
213 when(importerSupportingJava.getSupportedLanguages()).thenReturn(new String[] {"java"});
214 importers.add(importerSupportingJava);
216 // 1 importers supporting another language -> not to be include in result
217 ProfileImporter importerSupportingAnotherLanguage = mock(ProfileImporter.class);
218 when(importerSupportingAnotherLanguage.getSupportedLanguages()).thenReturn(new String[] {"js"});
219 importers.add(importerSupportingAnotherLanguage);
221 assertThat(operations.getProfileImportersForLanguage("java")).hasSize(3);