3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.qualityprofile.index;
22 import com.google.common.base.Function;
23 import com.google.common.collect.Maps;
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;
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;
43 @Category(DbTests.class)
44 public class ActiveRuleResultSetIteratorTest {
47 public DbTester dbTester = DbTester.create(System2.INSTANCE);
51 dbTester.truncateTables();
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);
61 assertThat(activeRulesByKey).hasSize(1);
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);
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);
80 assertThat(activeRulesByKey).hasSize(3);
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);
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);
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);
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);
117 assertThat(activeRulesByKey).hasSize(2);
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")));
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);
132 assertThat(activeRulesByKey).hasSize(2);
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")));
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);
145 assertThat(it.hasNext()).isTrue();
146 ActiveRuleDoc doc = it.next();
147 assertThat(doc.key()).isEqualTo(ActiveRuleKey.of("sonar-way", RuleKey.of("xoo", "S002")));
149 assertThat(it.hasNext()).isFalse();
153 private static Map<ActiveRuleKey, ActiveRuleDoc> activeRulesByKey(ActiveRuleResultSetIterator it) {
154 return Maps.uniqueIndex(it, DocToKey.INSTANCE);
157 private enum DocToKey implements Function<ActiveRuleDoc, ActiveRuleKey> {
161 public ActiveRuleKey apply(@Nonnull ActiveRuleDoc doc) {