]> source.dussan.org Git - sonarqube.git/blob
d080694a2ea7645c68f102d7c7f0b1182aad34f2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.ws.pull;
21
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.util.Arrays;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.utils.System2;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.issue.IssueDto;
32 import org.sonar.db.protobuf.DbCommons;
33 import org.sonar.db.protobuf.DbIssues;
34 import org.sonar.server.tester.UserSessionRule;
35
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.ArgumentMatchers.anyInt;
38 import static org.mockito.Mockito.atLeastOnce;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42
43 public class PullTaintActionResponseWriterIT {
44   @Rule
45   public DbTester db = DbTester.create(System2.INSTANCE);
46
47   @Rule
48   public UserSessionRule userSession = UserSessionRule.standalone();
49
50   private final System2 system2 = mock(System2.class);
51   private final PullTaintActionProtobufObjectGenerator protobufObjectGenerator = new PullTaintActionProtobufObjectGenerator(db.getDbClient(),
52     userSession);
53
54   private final PullActionResponseWriter underTest = new PullActionResponseWriter(system2, protobufObjectGenerator);
55
56   @Before
57   public void before() {
58     when(system2.now()).thenReturn(1_000_000L);
59   }
60
61   @Test
62   public void appendIssuesToResponse_outputStreamIsCalledAtLeastOnce() throws IOException {
63     OutputStream outputStream = mock(OutputStream.class);
64     IssueDto issueDto = new IssueDto();
65     issueDto.setFilePath("filePath");
66     issueDto.setKee("key");
67     issueDto.setStatus("OPEN");
68     issueDto.setRuleKey("repo", "rule");
69     DbIssues.Locations locations = DbIssues.Locations.newBuilder()
70       .setTextRange(range(2, 3))
71       .addFlow(newFlow(newLocation(4, 5)))
72       .addFlow(newFlow(newLocation(6, 7, "another-component")))
73       .build();
74
75     issueDto.setLocations(locations);
76
77     underTest.appendIssuesToResponse(List.of(issueDto), outputStream);
78
79     verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
80   }
81
82   @Test
83   public void appendClosedIssuesToResponse_outputStreamIsCalledAtLeastOnce() throws IOException {
84     OutputStream outputStream = mock(OutputStream.class);
85
86     underTest.appendClosedIssuesUuidsToResponse(List.of("uuid", "uuid2"), outputStream);
87
88     verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
89   }
90
91   @Test
92   public void appendTimestampToResponse_outputStreamIsCalledAtLeastOnce() throws IOException {
93     OutputStream outputStream = mock(OutputStream.class);
94
95     underTest.appendTimestampToResponse(outputStream);
96
97     verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
98   }
99
100   private static DbIssues.Location newLocation(int startLine, int endLine) {
101     return DbIssues.Location.newBuilder().setTextRange(range(startLine, endLine)).build();
102   }
103
104   private static DbIssues.Location newLocation(int startLine, int endLine, String componentUuid) {
105     return DbIssues.Location.newBuilder().setTextRange(range(startLine, endLine)).setComponentId(componentUuid).build();
106   }
107
108
109   private static org.sonar.db.protobuf.DbCommons.TextRange range(int startLine, int endLine) {
110     return DbCommons.TextRange.newBuilder().setStartLine(startLine).setEndLine(endLine).build();
111   }
112
113   private static DbIssues.Flow newFlow(DbIssues.Location... locations) {
114     DbIssues.Flow.Builder builder = DbIssues.Flow.newBuilder();
115     Arrays.stream(locations).forEach(builder::addLocation);
116     return builder.build();
117   }
118 }