]> source.dussan.org Git - sonarqube.git/blob
7066358bc6154f5d937d62df9400ae589ec664bb
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.issue.index;
21
22 import java.util.List;
23 import org.junit.Test;
24 import org.sonar.api.issue.Issue;
25 import org.sonar.api.rule.Severity;
26 import org.sonar.api.rules.RuleType;
27 import org.sonar.db.component.ComponentDto;
28
29 import static java.util.Arrays.asList;
30 import static java.util.Arrays.stream;
31 import static java.util.stream.Collectors.toList;
32 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
33 import static org.sonar.server.issue.IssueDocTesting.newDocForProject;
34
35 public class IssueIndexSecurityCategoriesTest extends IssueIndexTestCommon {
36
37   @Test
38   public void searchSinglePciDss32Category() {
39     ComponentDto project = newPrivateProjectDto();
40
41     indexIssues(
42       newDocForProject("openvul1", project).setPciDss32(asList("1.2.0", "3.4.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_OPEN)
43         .setSeverity(Severity.MAJOR),
44       newDocForProject("openvul2", project).setPciDss32(asList("3.3.2", "1.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
45         .setSeverity(Severity.MINOR)
46     );
47
48     assertThatSearchReturnsOnly(queryPciDss32("1"), "openvul1", "openvul2");
49     assertThatSearchReturnsOnly(queryPciDss32("1.2.0"), "openvul1");
50     assertThatSearchReturnsEmpty(queryPciDss32("1.2"));
51   }
52
53   @Test
54   public void searchMultiplePciDss32Categories() {
55     ComponentDto project = newPrivateProjectDto();
56
57     indexIssues(
58       newDocForProject("openvul1", project).setPciDss32(asList("1.2.0", "3.4.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_OPEN)
59         .setSeverity(Severity.MAJOR),
60       newDocForProject("openvul2", project).setPciDss32(asList("3.3.2", "2.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
61         .setSeverity(Severity.MINOR),
62       newDocForProject("openvul3", project).setPciDss32(asList("4.1", "5.4")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
63         .setSeverity(Severity.MINOR)
64     );
65
66     assertThatSearchReturnsOnly(queryPciDss32("1", "4"), "openvul1", "openvul3");
67     assertThatSearchReturnsOnly(queryPciDss32("1.2.0", "5.4"), "openvul1", "openvul3");
68     assertThatSearchReturnsEmpty(queryPciDss32("6", "7", "8", "9", "10", "11", "12"));
69   }
70
71   @Test
72   public void searchSinglePciDss40Category() {
73     ComponentDto project = newPrivateProjectDto();
74
75     indexIssues(
76       newDocForProject("openvul1", project).setPciDss40(asList("1.2.0", "3.4.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_OPEN)
77         .setSeverity(Severity.MAJOR),
78       newDocForProject("openvul2", project).setPciDss40(asList("3.3.2", "1.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
79         .setSeverity(Severity.MINOR)
80     );
81
82     assertThatSearchReturnsOnly(queryPciDss40("1"), "openvul1", "openvul2");
83     assertThatSearchReturnsOnly(queryPciDss40("1.2.0"), "openvul1");
84     assertThatSearchReturnsEmpty(queryPciDss40("1.2"));
85   }
86
87   @Test
88   public void searchMultiplePciDss40Categories() {
89     ComponentDto project = newPrivateProjectDto();
90
91     indexIssues(
92       newDocForProject("openvul1", project).setPciDss40(asList("1.2.0", "3.4.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_OPEN)
93         .setSeverity(Severity.MAJOR),
94       newDocForProject("openvul2", project).setPciDss40(asList("3.3.2", "2.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
95         .setSeverity(Severity.MINOR),
96       newDocForProject("openvul3", project).setPciDss40(asList("4.1", "5.4")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
97         .setSeverity(Severity.MINOR)
98     );
99
100     assertThatSearchReturnsOnly(queryPciDss40("1", "4"), "openvul1", "openvul3");
101     assertThatSearchReturnsOnly(queryPciDss40("1.2.0", "5.4"), "openvul1", "openvul3");
102     assertThatSearchReturnsEmpty(queryPciDss40("6", "7", "8", "9", "10", "11", "12"));
103   }
104
105   @Test
106   public void searchMixedPciDssCategories() {
107     ComponentDto project = newPrivateProjectDto();
108
109     indexIssues(
110       newDocForProject("openvul1", project).setPciDss40(asList("1.2.0", "3.4.5")).setPciDss32(List.of("2.1")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_OPEN)
111         .setSeverity(Severity.MAJOR),
112       newDocForProject("openvul2", project).setPciDss40(asList("3.3.2", "2.5")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
113         .setSeverity(Severity.MINOR),
114       newDocForProject("openvul3", project).setPciDss32(asList("4.1", "5.4")).setType(RuleType.VULNERABILITY).setStatus(Issue.STATUS_REOPENED)
115         .setSeverity(Severity.MINOR)
116     );
117
118     assertThatSearchReturnsOnly(queryPciDss40("1", "4"), "openvul1");
119     assertThatSearchReturnsOnly(queryPciDss40("1.2.0", "5.4"), "openvul1");
120     assertThatSearchReturnsEmpty(queryPciDss40("6", "7", "8", "9", "10", "11", "12"));
121
122     assertThatSearchReturnsOnly(queryPciDss32("3", "2.1"), "openvul1");
123     assertThatSearchReturnsOnly(queryPciDss32("1", "2"), "openvul1");
124     assertThatSearchReturnsOnly(queryPciDss32("4", "3"), "openvul3");
125     assertThatSearchReturnsEmpty(queryPciDss32("1", "3", "6", "7", "8", "9", "10", "11", "12"));
126
127   }
128
129   private IssueQuery.Builder queryPciDss32(String... values) {
130     return IssueQuery.builder()
131       .pciDss32(stream(values).collect(toList()))
132       .types(List.of("CODE_SMELL", "BUG", "VULNERABILITY"));
133   }
134
135   private IssueQuery.Builder queryPciDss40(String... values) {
136     return IssueQuery.builder()
137       .pciDss40(stream(values).collect(toList()))
138       .types(List.of("CODE_SMELL", "BUG", "VULNERABILITY"));
139   }
140 }