]> source.dussan.org Git - sonarqube.git/blob
eff1c5397fb87542917032b9a9d69ea3574d94f4
[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.computation.task.projectanalysis.issue;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.rule.RuleStatus;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.rule.RuleDao;
33 import org.sonar.db.rule.RuleDto;
34 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.guava.api.Assertions.assertThat;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Matchers.anyBoolean;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.reset;
43 import static org.mockito.Mockito.verify;
44 import static org.mockito.Mockito.verifyNoMoreInteractions;
45 import static org.mockito.Mockito.when;
46 import static org.mockito.internal.verification.VerificationModeFactory.times;
47
48 public class RuleRepositoryImplTest {
49
50   private static final RuleDto AB_RULE = createABRuleDto();
51   private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c");
52   private static final int AC_RULE_ID = 684;
53   private static final String ORGANIZATION_UUID = "org-1";
54
55   @org.junit.Rule
56   public ExpectedException expectedException = ExpectedException.none();
57   @org.junit.Rule
58   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
59       .setOrganizationUuid(ORGANIZATION_UUID);
60
61   private DbClient dbClient = mock(DbClient.class);
62   private DbSession dbSession = mock(DbSession.class);
63   private RuleDao ruleDao = mock(RuleDao.class);
64
65   RuleRepositoryImpl underTest = new RuleRepositoryImpl(dbClient, analysisMetadataHolder);
66
67   @Before
68   public void setUp() throws Exception {
69     when(dbClient.openSession(anyBoolean())).thenReturn(dbSession);
70     when(dbClient.ruleDao()).thenReturn(ruleDao);
71     when(ruleDao.selectAll(any(DbSession.class), eq(ORGANIZATION_UUID))).thenReturn(ImmutableList.of(AB_RULE));
72   }
73
74   @Test
75   public void constructor_does_not_query_DB_to_retrieve_rules() {
76     verifyNoMoreInteractions(dbClient);
77   }
78
79   @Test
80   public void first_call_to_getByKey_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
81     underTest.getByKey(AB_RULE.getKey());
82
83     verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
84
85     verifyNoMethodCallTriggersCallToDB();
86   }
87
88   @Test
89   public void first_call_to_findByKey_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
90     underTest.findByKey(AB_RULE.getKey());
91
92     verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
93
94     verifyNoMethodCallTriggersCallToDB();
95   }
96
97   @Test
98   public void first_call_to_getById_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
99     underTest.getById(AB_RULE.getId());
100
101     verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
102
103     verifyNoMethodCallTriggersCallToDB();
104   }
105
106   @Test
107   public void first_call_to_findById_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
108     underTest.findById(AB_RULE.getId());
109
110     verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
111
112     verifyNoMethodCallTriggersCallToDB();
113   }
114
115   @Test
116   public void getByKey_throws_NPE_if_key_argument_is_null() {
117     expectNullRuleKeyNPE();
118
119     underTest.getByKey(null);
120   }
121
122   @Test
123   public void getByKey_does_not_call_DB_if_key_argument_is_null() {
124     try {
125       underTest.getByKey(null);
126     } catch (NullPointerException e) {
127       assertNoCallToDb();
128     }
129   }
130
131   @Test
132   public void getByKey_returns_Rule_if_it_exists_in_DB() {
133     Rule rule = underTest.getByKey(AB_RULE.getKey());
134
135     assertIsABRule(rule);
136   }
137
138   @Test
139   public void getByKey_throws_IAE_if_rules_does_not_exist_in_DB() {
140     expectedException.expect(IllegalArgumentException.class);
141     expectedException.expectMessage("Can not find rule for key a:c. This rule does not exist in DB");
142
143     underTest.getByKey(AC_RULE_KEY);
144   }
145
146   @Test
147   public void findByKey_throws_NPE_if_key_argument_is_null() {
148     expectNullRuleKeyNPE();
149
150     underTest.findByKey(null);
151   }
152
153   @Test
154   public void findByKey_does_not_call_DB_if_key_argument_is_null() {
155     try {
156       underTest.findByKey(null);
157     } catch (NullPointerException e) {
158       assertNoCallToDb();
159     }
160   }
161
162   @Test
163   public void findByKey_returns_absent_if_rule_does_not_exist_in_DB() {
164     Optional<Rule> rule = underTest.findByKey(AC_RULE_KEY);
165
166     assertThat(rule).isAbsent();
167   }
168
169   @Test
170   public void findByKey_returns_Rule_if_it_exists_in_DB() {
171     Optional<Rule> rule = underTest.findByKey(AB_RULE.getKey());
172
173     assertIsABRule(rule.get());
174   }
175
176   @Test
177   public void getById_returns_Rule_if_it_exists_in_DB() {
178     Rule rule = underTest.getById(AB_RULE.getId());
179
180     assertIsABRule(rule);
181   }
182
183   @Test
184   public void getById_throws_IAE_if_rules_does_not_exist_in_DB() {
185     expectedException.expect(IllegalArgumentException.class);
186     expectedException.expectMessage("Can not find rule for id " + AC_RULE_ID + ". This rule does not exist in DB");
187
188     underTest.getById(AC_RULE_ID);
189   }
190
191   @Test
192   public void findById_returns_absent_if_rule_does_not_exist_in_DB() {
193     Optional<Rule> rule = underTest.findById(AC_RULE_ID);
194
195     assertThat(rule).isAbsent();
196   }
197
198   @Test
199   public void findById_returns_Rule_if_it_exists_in_DB() {
200     Optional<Rule> rule = underTest.findById(AB_RULE.getId());
201
202     assertIsABRule(rule.get());
203   }
204
205   private void expectNullRuleKeyNPE() {
206     expectedException.expect(NullPointerException.class);
207     expectedException.expectMessage("RuleKey can not be null");
208   }
209
210   private void verifyNoMethodCallTriggersCallToDB() {
211     reset(ruleDao);
212     underTest.getByKey(AB_RULE.getKey());
213     assertNoCallToDb();
214     reset(ruleDao);
215     underTest.findByKey(AB_RULE.getKey());
216     assertNoCallToDb();
217     reset(ruleDao);
218     underTest.getById(AB_RULE.getId());
219     assertNoCallToDb();
220     reset(ruleDao);
221     underTest.findById(AB_RULE.getId());
222     assertNoCallToDb();
223   }
224
225   private void assertNoCallToDb() {
226     verifyNoMoreInteractions(ruleDao);
227   }
228
229   private void assertIsABRule(Rule rule) {
230     assertThat(rule).isNotNull();
231     assertThat(rule.getId()).isEqualTo(AB_RULE.getId());
232     assertThat(rule.getKey()).isEqualTo(AB_RULE.getKey());
233     assertThat(rule.getRemediationFunction()).isNull();
234     assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED);
235   }
236
237   private static RuleDto createABRuleDto() {
238     RuleKey ruleKey = RuleKey.of("a", "b");
239     return new RuleDto()
240       .setId(ruleKey.hashCode())
241       .setRepositoryKey(ruleKey.repository())
242       .setRuleKey(ruleKey.rule())
243       .setStatus(RuleStatus.REMOVED)
244       .setType(RuleType.CODE_SMELL);
245   }
246
247 }