]> source.dussan.org Git - sonarqube.git/blob
6631d1720525b7e15223d4b4e61cf47e946c361b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.index;
21
22 import com.google.common.base.Function;
23 import com.google.common.collect.Maps;
24 import java.util.Map;
25 import javax.annotation.Nonnull;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.qualityprofile.ActiveRuleKey;
34 import org.sonar.server.qualityprofile.ActiveRule;
35 import org.sonar.test.DbTests;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.rule.Severity.BLOCKER;
39 import static org.sonar.api.rule.Severity.CRITICAL;
40 import static org.sonar.api.rule.Severity.INFO;
41 import static org.sonar.server.qualityprofile.ActiveRule.Inheritance.INHERITED;
42
43 @Category(DbTests.class)
44 public class ActiveRuleResultSetIteratorTest {
45
46   @Rule
47   public DbTester dbTester = DbTester.create(System2.INSTANCE);
48
49   @Before
50   public void setUp() {
51     dbTester.truncateTables();
52   }
53
54   @Test
55   public void iterator_over_one_active_rule() {
56     dbTester.prepareDbUnit(getClass(), "one_active_rule.xml");
57     ActiveRuleResultSetIterator it = ActiveRuleResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L);
58     Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey = activeRulesByKey(it);
59     it.close();
60
61     assertThat(activeRulesByKey).hasSize(1);
62
63     ActiveRuleKey key = ActiveRuleKey.of("sonar-way", RuleKey.of("xoo", "S001"));
64     ActiveRuleDoc activeRule = activeRulesByKey.get(key);
65     assertThat(activeRule.key()).isEqualTo(key);
66     assertThat(activeRule.severity()).isEqualTo(CRITICAL);
67     assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
68     assertThat(activeRule.parentKey()).isNull();
69     assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L);
70     assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L);
71   }
72
73   @Test
74   public void iterator_over_active_rules() {
75     dbTester.prepareDbUnit(getClass(), "shared.xml");
76     ActiveRuleResultSetIterator it = ActiveRuleResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L);
77     Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey = activeRulesByKey(it);
78     it.close();
79
80     assertThat(activeRulesByKey).hasSize(3);
81
82     ActiveRuleKey key = ActiveRuleKey.of("sonar-way", RuleKey.of("xoo", "S002"));
83     ActiveRuleDoc activeRule = activeRulesByKey.get(key);
84     assertThat(activeRule.key()).isEqualTo(key);
85     assertThat(activeRule.severity()).isEqualTo(CRITICAL);
86     assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
87     assertThat(activeRule.parentKey()).isNull();
88     assertThat(activeRule.createdAtAsLong()).isEqualTo(2000000000000L);
89     assertThat(activeRule.updatedAtAsLong()).isEqualTo(2100000000000L);
90
91     key = ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001"));
92     activeRule = activeRulesByKey.get(key);
93     assertThat(activeRule.key()).isEqualTo(key);
94     assertThat(activeRule.severity()).isEqualTo(INFO);
95     assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.NONE);
96     assertThat(activeRule.parentKey()).isNull();
97     assertThat(activeRule.createdAtAsLong()).isEqualTo(1700000000000L);
98     assertThat(activeRule.updatedAtAsLong()).isEqualTo(1800000000000L);
99
100     key = ActiveRuleKey.of("child", RuleKey.of("xoo", "S001"));
101     activeRule = activeRulesByKey.get(key);
102     assertThat(activeRule.key()).isEqualTo(key);
103     assertThat(activeRule.severity()).isEqualTo(BLOCKER);
104     assertThat(activeRule.inheritance()).isEqualTo(INHERITED);
105     assertThat(activeRule.parentKey()).isEqualTo(ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001")));
106     assertThat(activeRule.createdAtAsLong()).isEqualTo(1500000000000L);
107     assertThat(activeRule.updatedAtAsLong()).isEqualTo(1600000000000L);
108   }
109
110   @Test
111   public void active_rule_with_inherited_inheritance() {
112     dbTester.prepareDbUnit(getClass(), "active_rule_with_inherited_inheritance.xml");
113     ActiveRuleResultSetIterator it = ActiveRuleResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L);
114     Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey = activeRulesByKey(it);
115     it.close();
116
117     assertThat(activeRulesByKey).hasSize(2);
118
119     ActiveRuleKey key = ActiveRuleKey.of("child", RuleKey.of("xoo", "S001"));
120     ActiveRuleDoc activeRule = activeRulesByKey.get(key);
121     assertThat(activeRule.inheritance()).isEqualTo(INHERITED);
122     assertThat(activeRule.parentKey()).isEqualTo(ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001")));
123   }
124
125   @Test
126   public void active_rule_with_overrides_inheritance() {
127     dbTester.prepareDbUnit(getClass(), "active_rule_with_overrides_inheritance.xml");
128     ActiveRuleResultSetIterator it = ActiveRuleResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 0L);
129     Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey = activeRulesByKey(it);
130     it.close();
131
132     assertThat(activeRulesByKey).hasSize(2);
133
134     ActiveRuleKey key = ActiveRuleKey.of("child", RuleKey.of("xoo", "S001"));
135     ActiveRuleDoc activeRule = activeRulesByKey.get(key);
136     assertThat(activeRule.inheritance()).isEqualTo(ActiveRule.Inheritance.OVERRIDES);
137     assertThat(activeRule.parentKey()).isEqualTo(ActiveRuleKey.of("parent", RuleKey.of("xoo", "S001")));
138   }
139
140   @Test
141   public void select_after_date() {
142     dbTester.prepareDbUnit(getClass(), "shared.xml");
143     ActiveRuleResultSetIterator it = ActiveRuleResultSetIterator.create(dbTester.getDbClient(), dbTester.getSession(), 1_900_000_000_000L);
144
145     assertThat(it.hasNext()).isTrue();
146     ActiveRuleDoc doc = it.next();
147     assertThat(doc.key()).isEqualTo(ActiveRuleKey.of("sonar-way", RuleKey.of("xoo", "S002")));
148
149     assertThat(it.hasNext()).isFalse();
150     it.close();
151   }
152
153   private static Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey(ActiveRuleResultSetIterator it) {
154     return Maps.uniqueIndex(it, DocToKey.INSTANCE);
155   }
156
157   private enum DocToKey implements Function<ActiveRuleDoc, ActiveRuleKey> {
158     INSTANCE;
159
160     @Override
161     public ActiveRuleKey apply(@Nonnull ActiveRuleDoc doc) {
162       return doc.key();
163     }
164   }
165
166 }