3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.issue;
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;
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;
48 public class RuleRepositoryImplTest {
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";
56 public ExpectedException expectedException = ExpectedException.none();
58 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
59 .setOrganizationUuid(ORGANIZATION_UUID);
61 private DbClient dbClient = mock(DbClient.class);
62 private DbSession dbSession = mock(DbSession.class);
63 private RuleDao ruleDao = mock(RuleDao.class);
65 RuleRepositoryImpl underTest = new RuleRepositoryImpl(dbClient, analysisMetadataHolder);
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));
75 public void constructor_does_not_query_DB_to_retrieve_rules() {
76 verifyNoMoreInteractions(dbClient);
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());
83 verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
85 verifyNoMethodCallTriggersCallToDB();
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());
92 verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
94 verifyNoMethodCallTriggersCallToDB();
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());
101 verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
103 verifyNoMethodCallTriggersCallToDB();
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());
110 verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));
112 verifyNoMethodCallTriggersCallToDB();
116 public void getByKey_throws_NPE_if_key_argument_is_null() {
117 expectNullRuleKeyNPE();
119 underTest.getByKey(null);
123 public void getByKey_does_not_call_DB_if_key_argument_is_null() {
125 underTest.getByKey(null);
126 } catch (NullPointerException e) {
132 public void getByKey_returns_Rule_if_it_exists_in_DB() {
133 Rule rule = underTest.getByKey(AB_RULE.getKey());
135 assertIsABRule(rule);
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");
143 underTest.getByKey(AC_RULE_KEY);
147 public void findByKey_throws_NPE_if_key_argument_is_null() {
148 expectNullRuleKeyNPE();
150 underTest.findByKey(null);
154 public void findByKey_does_not_call_DB_if_key_argument_is_null() {
156 underTest.findByKey(null);
157 } catch (NullPointerException e) {
163 public void findByKey_returns_absent_if_rule_does_not_exist_in_DB() {
164 Optional<Rule> rule = underTest.findByKey(AC_RULE_KEY);
166 assertThat(rule).isAbsent();
170 public void findByKey_returns_Rule_if_it_exists_in_DB() {
171 Optional<Rule> rule = underTest.findByKey(AB_RULE.getKey());
173 assertIsABRule(rule.get());
177 public void getById_returns_Rule_if_it_exists_in_DB() {
178 Rule rule = underTest.getById(AB_RULE.getId());
180 assertIsABRule(rule);
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");
188 underTest.getById(AC_RULE_ID);
192 public void findById_returns_absent_if_rule_does_not_exist_in_DB() {
193 Optional<Rule> rule = underTest.findById(AC_RULE_ID);
195 assertThat(rule).isAbsent();
199 public void findById_returns_Rule_if_it_exists_in_DB() {
200 Optional<Rule> rule = underTest.findById(AB_RULE.getId());
202 assertIsABRule(rule.get());
205 private void expectNullRuleKeyNPE() {
206 expectedException.expect(NullPointerException.class);
207 expectedException.expectMessage("RuleKey can not be null");
210 private void verifyNoMethodCallTriggersCallToDB() {
212 underTest.getByKey(AB_RULE.getKey());
215 underTest.findByKey(AB_RULE.getKey());
218 underTest.getById(AB_RULE.getId());
221 underTest.findById(AB_RULE.getId());
225 private void assertNoCallToDb() {
226 verifyNoMoreInteractions(ruleDao);
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);
237 private static RuleDto createABRuleDto() {
238 RuleKey ruleKey = RuleKey.of("a", "b");
240 .setId(ruleKey.hashCode())
241 .setRepositoryKey(ruleKey.repository())
242 .setRuleKey(ruleKey.rule())
243 .setStatus(RuleStatus.REMOVED)
244 .setType(RuleType.CODE_SMELL);