]> source.dussan.org Git - sonarqube.git/blob
cdbc9cad7aa7c3fc27ceccadb50fa68aa13e13d8
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
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
21 package org.sonar.server.qualityprofile;
22
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;
44
45 import java.io.Reader;
46 import java.util.List;
47
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;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class QProfileRepositoryExporterTest {
61
62   @Mock
63   SqlSession session;
64
65   @Mock
66   DatabaseSession hibernateSession;
67
68   @Mock
69   ActiveRuleDao activeRuleDao;
70
71   List<ProfileImporter> importers = newArrayList();
72
73   Integer currentId = 1;
74
75   QProfileRepositoryExporter operations;
76
77   @Before
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++);
85         return null;
86       }
87     }).when(activeRuleDao).insert(any(ActiveRuleDto.class), any(SqlSession.class));
88
89     operations = new QProfileRepositoryExporter(activeRuleDao, importers);
90   }
91
92   @Test
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");
97     rule.setId(10);
98     ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
99     activeRule.setParameter("max", "10");
100
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);
105
106     operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
107
108     verify(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
109
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);
114
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");
119   }
120
121   @Test
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");
126     rule.setId(10);
127     ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
128     activeRule.setParameter("max", "10");
129
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");
138         return profile;
139       }
140     }).when(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
141     importers.add(importer);
142
143     QProfileResult result = operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
144     ;
145     assertThat(result.infos()).hasSize(1);
146     assertThat(result.warnings()).hasSize(1);
147   }
148
149   @Test
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");
154     rule.setId(10);
155     ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
156     activeRule.setParameter("max", "10");
157
158     ProfileImporter importer = mock(ProfileImporter.class);
159     when(importer.getKey()).thenReturn("pmd");
160     importers.add(importer);
161
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!");
167         return profile;
168       }
169     }).when(importer).importProfile(any(Reader.class), any(ValidationMessages.class));
170
171     try {
172       operations.importXml(new QProfile().setId(1), "pmd", "<xml/>", session);
173       fail();
174     } catch (Exception e) {
175       assertThat(e).isInstanceOf(BadRequestException.class);
176     }
177   }
178
179   @Test
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");
184     rule.setId(10);
185     ActiveRule activeRule = profile.activateRule(rule, RulePriority.BLOCKER);
186     activeRule.setParameter("max", "10");
187
188     ProfileImporter importer = mock(ProfileImporter.class);
189     when(importer.getKey()).thenReturn("pmd");
190     importers.add(importer);
191
192     when(importer.importProfile(any(Reader.class), any(ValidationMessages.class))).thenReturn(profile);
193
194     try {
195       operations.importXml(new QProfile().setId(1), "unknown", "<xml/>", session);
196       fail();
197     } catch (Exception e) {
198       assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("No such importer : unknown");
199     }
200     verify(importer, never()).importProfile(any(Reader.class), any(ValidationMessages.class));
201   }
202
203   @Test
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));
210
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);
215
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);
220
221     assertThat(operations.getProfileImportersForLanguage("java")).hasSize(3);
222   }
223
224 }