3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info 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.issue.ws.pull;
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;
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;
43 public class PullTaintActionResponseWriterIT {
45 public DbTester db = DbTester.create(System2.INSTANCE);
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 private final System2 system2 = mock(System2.class);
51 private final PullTaintActionProtobufObjectGenerator protobufObjectGenerator = new PullTaintActionProtobufObjectGenerator(db.getDbClient(),
54 private final PullActionResponseWriter underTest = new PullActionResponseWriter(system2, protobufObjectGenerator);
57 public void before() {
58 when(system2.now()).thenReturn(1_000_000L);
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")))
75 issueDto.setLocations(locations);
77 underTest.appendIssuesToResponse(List.of(issueDto), outputStream);
79 verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
83 public void appendClosedIssuesToResponse_outputStreamIsCalledAtLeastOnce() throws IOException {
84 OutputStream outputStream = mock(OutputStream.class);
86 underTest.appendClosedIssuesUuidsToResponse(List.of("uuid", "uuid2"), outputStream);
88 verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
92 public void appendTimestampToResponse_outputStreamIsCalledAtLeastOnce() throws IOException {
93 OutputStream outputStream = mock(OutputStream.class);
95 underTest.appendTimestampToResponse(outputStream);
97 verify(outputStream, atLeastOnce()).write(any(byte[].class), anyInt(), anyInt());
100 private static DbIssues.Location newLocation(int startLine, int endLine) {
101 return DbIssues.Location.newBuilder().setTextRange(range(startLine, endLine)).build();
104 private static DbIssues.Location newLocation(int startLine, int endLine, String componentUuid) {
105 return DbIssues.Location.newBuilder().setTextRange(range(startLine, endLine)).setComponentId(componentUuid).build();
109 private static org.sonar.db.protobuf.DbCommons.TextRange range(int startLine, int endLine) {
110 return DbCommons.TextRange.newBuilder().setStartLine(startLine).setEndLine(endLine).build();
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();