1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.measure;
import java.util.Collections;
import org.assertj.core.api.Java6Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
public class MeasureQueryTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void create_query_from_projects() throws Exception {
MeasureQuery query = MeasureQuery.builder().setProjectUuids(asList("PROJECT_1", "PROJECT_2")).build();
assertThat(query.getProjectUuids()).containsOnly("PROJECT_1", "PROJECT_2");
assertThat(query.isOnProjects()).isTrue();
assertThat(query.isOnComponents()).isFalse();
assertThat(query.isOnSingleComponent()).isFalse();
}
@Test
public void create_query_from_project_and_components() throws Exception {
MeasureQuery query = MeasureQuery.builder().setComponentUuids("PROJECT_1", asList("FILE_1", "FILE_2")).build();
assertThat(query.getProjectUuids()).containsOnly("PROJECT_1");
assertThat(query.getProjectUuid()).isEqualTo("PROJECT_1");
assertThat(query.getComponentUuids()).containsOnly("FILE_1", "FILE_2");
assertThat(query.isOnProjects()).isFalse();
assertThat(query.isOnComponents()).isTrue();
assertThat(query.isOnSingleComponent()).isFalse();
}
@Test
public void create_query_from_single_component_uuid() throws Exception {
MeasureQuery query = MeasureQuery.builder().setComponentUuid("FILE_1").build();
assertThat(query.getComponentUuids()).containsOnly("FILE_1");
assertThat(query.getComponentUuid()).isEqualTo("FILE_1");
assertThat(query.isOnProjects()).isFalse();
assertThat(query.isOnComponents()).isFalse();
assertThat(query.isOnSingleComponent()).isTrue();
}
@Test
public void create_query_from_metric_ids() throws Exception {
MeasureQuery query = MeasureQuery.builder().setProjectUuids(asList("PROJECT_1", "PROJECT_2")).setMetricIds(asList(10, 11)).build();
assertThat(query.getMetricIds()).containsOnly(10, 11);
assertThat(query.getMetricKeys()).isNull();
}
@Test
public void create_query_from_metric_keys() throws Exception {
MeasureQuery query = MeasureQuery.builder().setProjectUuids(asList("PROJECT_1", "PROJECT_2")).setMetricKeys(asList("M1", "M2")).build();
assertThat(query.getMetricKeys()).containsOnly("M1", "M2");
assertThat(query.getMetricIds()).isNull();
}
@Test
public void create_query_from_person_id() throws Exception {
MeasureQuery query = MeasureQuery.builder().setProjectUuids(asList("PROJECT_1", "PROJECT_2")).setPersonId(100L).build();
assertThat(query.getPersonId()).isEqualTo(100L);
}
@Test
public void return_empty_when_metrics_are_empty() throws Exception {
Java6Assertions.assertThat(MeasureQuery.builder()
.setProjectUuids(asList("PROJECT_1", "PROJECT_2"))
.setMetricKeys(Collections.emptyList())
.build().returnsEmpty()).isTrue();
Java6Assertions.assertThat(MeasureQuery.builder()
.setProjectUuids(asList("PROJECT_1", "PROJECT_2"))
.setMetricIds(Collections.emptyList())
.build().returnsEmpty()).isTrue();
}
@Test
public void return_empty_when_projects_are_empty() throws Exception {
Java6Assertions.assertThat(MeasureQuery.builder()
.setProjectUuids(Collections.emptyList())
.build().returnsEmpty()).isTrue();
}
@Test
public void return_empty_when_components_are_empty() throws Exception {
Java6Assertions.assertThat(MeasureQuery.builder()
.setComponentUuids("PROJECT", Collections.emptyList())
.build().returnsEmpty()).isTrue();
}
@Test
public void fail_when_no_component_uuid_filter() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("At least one filter on component UUID is expected");
MeasureQuery.builder().build();
}
@Test
public void fail_when_component_uuids_without_project_uuid() throws Exception {
expectedException.expect(NullPointerException.class);
MeasureQuery.builder().setComponentUuids(null, asList("FILE_1", "FILE_2")).build();
}
@Test
public void fail_when_using_metric_ids_and_metric_keys() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Metric IDs and keys must not be set both");
MeasureQuery.builder().setMetricIds(asList(10, 11)).setMetricKeys(asList("M1", "M2")).setProjectUuids(asList("PROJECT_1", "PROJECT_2")).build();
}
}
|