call(issueDto.getKey(), "please fix it");
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
verifyContentOfPreloadedSearchResponseData(issueDto);
IssueChangeDto issueComment = dbClient.issueChangeDao().selectByTypeAndIssueKeys(dbTester.getSession(), singletonList(issueDto.getKey()), TYPE_COMMENT).get(0);
call(commentDto.getKey());
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
assertThat(dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey())).isNotPresent();
verifyContentOfPreloadedSearchResponseData(issueDto);
}
call(issue.getKey(), "confirm");
- verify(responseWriter).write(eq(issue.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issue.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
verifyContentOfPreloadedSearchResponseData(issue);
verify(issueChangeEventService).distributeIssueChangeEvent(any(), any(), any(), any(), any(), any());
IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get();
call(commentDto.getKey(), "please have a look");
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
verifyContentOfPreloadedSearchResponseData(issueDto);
IssueChangeDto issueComment = dbClient.issueChangeDao().selectCommentByKey(dbTester.getSession(), commentDto.getKey()).get();
@Test
public void search_by_author() {
+ userSession.logIn();
ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
ComponentDto file = db.components().insertComponent(newFileDto(project));
RuleDto rule = db.rules().insertIssueRule();
.isEmpty();
}
+ @Test
+ public void hide_author_if_not_logged_in() {
+ ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
+ ComponentDto file = db.components().insertComponent(newFileDto(project));
+ RuleDto rule = db.rules().insertIssueRule();
+ db.issues().insertIssue(rule, project, file, i -> i.setAuthorLogin("leia"));
+ db.issues().insertIssue(rule, project, file, i -> i.setAuthorLogin("luke"));
+ db.issues().insertIssue(rule, project, file, i -> i.setAuthorLogin("han, solo"));
+ indexPermissionsAndIssues();
+
+ SearchWsResponse response = ws.newRequest()
+ .setMultiParam("author", asList("leia", "han, solo"))
+ .setParam(FACETS, "author")
+ .executeProtobuf(SearchWsResponse.class);
+
+ assertThat(response.getIssuesList())
+ .extracting(Issue::getAuthor)
+ .containsExactlyInAnyOrder("", "");
+ assertThat(response.getFacets().getFacetsList()).isEmpty();
+ }
+
@Test
public void filter_by_test_scope() {
ProjectData projectData = db.components().insertPublicProject("PROJECT_ID",
call(issueDto.getKey(), MINOR);
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
verifyContentOfPreloadedSearchResponseData(issueDto);
verify(issueChangeEventService).distributeIssueChangeEvent(any(), any(), any(), any(), any(), any());
call(issueDto.getKey(), "bug", "todo");
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
verifyContentOfPreloadedSearchResponseData(issueDto);
IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
assertThat(issueReloaded.getTags()).containsOnly("bug", "todo");
call(issueDto.getKey(), to.name());
- verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
+ verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class), eq(true));
IssueDto issueReloaded = dbClient.issueDao().selectByKey(dbTester.getSession(), issueDto.getKey()).get();
assertThat(issueReloaded.getType()).isEqualTo(to.getDbConstant());
import static java.lang.String.format;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
private final HtmlSourceDecorator htmlSourceDecorator = mock(HtmlSourceDecorator.class);
private final SourceService sourceService = new SourceService(db.getDbClient(), htmlSourceDecorator);
private final LinesJsonWriter linesJsonWriter = new LinesJsonWriter(htmlSourceDecorator);
- private final LinesAction underTest = new LinesAction(TestComponentFinder.from(db), db.getDbClient(), sourceService, linesJsonWriter, userSession);
+ private final LinesAction underTest = new LinesAction(TestComponentFinder.from(db), db.getDbClient(), sourceService, linesJsonWriter,
+ userSession);
private final WsActionTester tester = new WsActionTester(underTest);
@Before
ComponentDto file = insertFileWithData(data, mainBranch);
- tester.newRequest()
+ String response = tester.newRequest()
.setParam("uuid", file.uuid())
.execute()
- .assertJson(getClass(), "hide_scmAuthors.json");
+ .getInput();
+
+ assertThat(response).doesNotContain("isaac@asimov.com");
}
@Test
@Test
public void show_scm() {
+ userSessionRule.logIn();
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
.registerBranches(project.getMainBranchDto());
.assertJson(getClass(), "show_scm.json");
}
+ @Test
+ public void hide_author_if_not_logged_in() {
+ userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
+ .registerBranches(project.getMainBranchDto());
+
+ dbTester.getDbClient().fileSourceDao().insert(dbSession, new FileSourceDto()
+ .setUuid(Uuids.createFast())
+ .setProjectUuid(PROJECT_UUID)
+ .setFileUuid(FILE_UUID)
+ .setSourceData(DbFileSources.Data.newBuilder().addLines(
+ newSourceLine("julien", "123-456-789", DateUtils.parseDateTime("2015-03-30T12:34:56+0000"), 1)).build()));
+ dbSession.commit();
+
+ tester.newRequest()
+ .setParam("key", FILE_KEY)
+ .execute()
+ .assertJson(getClass(), "hide_author.json");
+ }
+
@Test
public void show_scm_from_given_range_lines() {
+ userSessionRule.logIn();
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
.registerBranches(project.getMainBranchDto());
@Test
public void not_group_lines_by_commit() {
+ userSessionRule.logIn();
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
.registerBranches(project.getMainBranchDto());
@Test
public void group_lines_by_commit() {
+ userSessionRule.logIn();
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
.registerBranches(project.getMainBranchDto());
@Test
public void accept_negative_value_in_from_parameter() {
+ userSessionRule.logIn();
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project.getProjectDto())
.registerBranches(project.getMainBranchDto());
+++ /dev/null
-{
- "sources": [
- {
- "line": 1,
- "code": "\u003cp\u003eSOURCE_1\u003c/p\u003e",
- "scmRevision": "REVISION_1",
- "scmDate": "1974-10-03T03:40:00+0100",
- "duplicated": false,
- "isNew": false
- }
- ]
-}
--- /dev/null
+{
+ "scm": [
+ [1, "","2015-03-30T12:34:56+0000", "123-456-789"]
+ ]
+}
DefaultIssue defaultIssue = issueDto.toDefaultIssue();
issueFieldsSetter.addComment(defaultIssue, wsRequest.getText(), context);
SearchResponseData preloadedSearchResponseData = issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, issueDto, defaultIssue, context);
- responseWriter.write(defaultIssue.key(), preloadedSearchResponseData, request, response);
+ responseWriter.write(defaultIssue.key(), preloadedSearchResponseData, request, response, true);
}
}
String assignee = getAssignee(request);
String key = request.mandatoryParam(PARAM_ISSUE);
SearchResponseData preloadedResponseData = assign(key, assignee);
- responseWriter.write(key, preloadedResponseData, request, response);
+ responseWriter.write(key, preloadedResponseData, request, response, true);
}
private SearchResponseData assign(String issueKey, @Nullable String login) {
CommentData commentData = loadCommentData(dbSession, request);
deleteComment(dbSession, commentData);
IssueDto issueDto = commentData.getIssueDto();
- responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response);
+ responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response, true);
}
}
try (DbSession dbSession = dbClient.openSession(false)) {
IssueDto issueDto = issueFinder.getByKey(dbSession, issue);
SearchResponseData preloadedSearchResponseData = doTransition(dbSession, issueDto, request.mandatoryParam(PARAM_TRANSITION));
- responseWriter.write(issue, preloadedSearchResponseData, request, response);
+ responseWriter.write(issue, preloadedSearchResponseData, request, response, true);
}
}
CommentData commentData = loadCommentData(dbSession, toWsRequest(request));
updateComment(dbSession, commentData);
IssueDto issueDto = commentData.getIssueDto();
- responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response);
+ responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response, true);
}
}
Paging paging = forPageIndex(request.page)
.withPageSize(request.pageSize)
.andTotal(request.pageSize);
- return searchResponseFormat.formatList(additionalFields, data, paging);
+ return searchResponseFormat.formatList(additionalFields, data, paging, userSession.isLoggedIn());
}
private void collectLoggedInUser(SearchResponseLoader.Collector collector) {
this.format = format;
}
- public void write(String issueKey, SearchResponseData preloadedResponseData, Request request, Response response) {
+ public void write(String issueKey, SearchResponseData preloadedResponseData, Request request, Response response, boolean showAuthor) {
SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(singletonList(issueKey));
SearchResponseData data = loader.load(preloadedResponseData, collector, ALL_ADDITIONAL_FIELDS, null);
- Issues.Operation responseBody = format.formatOperation(data);
+ Issues.Operation responseBody = format.formatOperation(data, showAuthor);
WsUtils.writeProtobuf(responseBody, request, response);
}
// FIXME allow long in Paging
Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) getTotalHits(result).value);
- return searchResponseFormat.formatSearch(additionalFields, data, paging, facets);
+ return searchResponseFormat.formatSearch(additionalFields, data, paging, facets, userSession.isLoggedIn());
}
private static TotalHits getTotalHits(SearchResponse response) {
"results"));
}
- private static SearchOptions createSearchOptionsFromRequest(SearchRequest request) {
+ private SearchOptions createSearchOptionsFromRequest(SearchRequest request) {
SearchOptions options = new SearchOptions();
options.setPage(request.getPage(), request.getPageSize());
List<String> facets = request.getFacets();
- if (facets != null && !facets.isEmpty()) {
- options.addFacets(facets);
+
+ if (facets == null || facets.isEmpty()) {
+ return options;
}
+ List<String> requestedFacets = new ArrayList<>(facets);
+ if (!userSession.isLoggedIn()) {
+ requestedFacets.remove(PARAM_AUTHOR);
+ }
+ options.addFacets(requestedFacets);
return options;
}
private final TextRangeResponseFormatter textRangeFormatter;
private final UserResponseFormatter userFormatter;
- public SearchResponseFormat(Durations durations, Languages languages, TextRangeResponseFormatter textRangeFormatter, UserResponseFormatter userFormatter) {
+ public SearchResponseFormat(Durations durations, Languages languages, TextRangeResponseFormatter textRangeFormatter,
+ UserResponseFormatter userFormatter) {
this.durations = durations;
this.languages = languages;
this.textRangeFormatter = textRangeFormatter;
this.userFormatter = userFormatter;
}
- SearchWsResponse formatSearch(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging, Facets facets) {
+ SearchWsResponse formatSearch(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging, Facets facets,
+ boolean showAuthor) {
SearchWsResponse.Builder response = SearchWsResponse.newBuilder();
formatPaging(paging, response);
ofNullable(data.getEffortTotal()).ifPresent(response::setEffortTotal);
- response.addAllIssues(createIssues(fields, data));
+ response.addAllIssues(createIssues(fields, data, showAuthor));
response.addAllComponents(formatComponents(data));
formatFacets(data, facets, response);
if (fields.contains(SearchAdditionalField.RULES)) {
return response.build();
}
- Issues.ListWsResponse formatList(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging) {
+ Issues.ListWsResponse formatList(Set<SearchAdditionalField> fields, SearchResponseData data, Paging paging, boolean showAuthor) {
Issues.ListWsResponse.Builder response = Issues.ListWsResponse.newBuilder();
response.setPaging(Common.Paging.newBuilder()
.setPageIndex(paging.pageIndex())
.setPageSize(data.getIssues().size()));
- response.addAllIssues(createIssues(fields, data));
+ response.addAllIssues(createIssues(fields, data, showAuthor));
response.addAllComponents(formatComponents(data));
return response.build();
}
- Operation formatOperation(SearchResponseData data) {
+ Operation formatOperation(SearchResponseData data, boolean showAuthor) {
Operation.Builder response = Operation.newBuilder();
if (data.getIssues().size() == 1) {
IssueDto dto = data.getIssues().get(0);
- response.setIssue(createIssue(ALL_ADDITIONAL_FIELDS, data, dto));
+ response.setIssue(createIssue(ALL_ADDITIONAL_FIELDS, data, dto, showAuthor));
}
response.addAllComponents(formatComponents(data));
response.addAllRules(formatRules(data).getRulesList());
.setTotal(paging.total());
}
- private List<Issues.Issue> createIssues(Collection<SearchAdditionalField> fields, SearchResponseData data) {
+ private List<Issues.Issue> createIssues(Collection<SearchAdditionalField> fields, SearchResponseData data, boolean showAuthor) {
return data.getIssues().stream()
- .map(dto -> createIssue(fields, data, dto))
+ .map(dto -> createIssue(fields, data, dto, showAuthor))
.toList();
}
- private Issue createIssue(Collection<SearchAdditionalField> fields, SearchResponseData data, IssueDto dto) {
+ private Issue createIssue(Collection<SearchAdditionalField> fields, SearchResponseData data, IssueDto dto, boolean showAuthor) {
Issue.Builder issueBuilder = Issue.newBuilder();
- addMandatoryFieldsToIssueBuilder(issueBuilder, dto, data);
+ addMandatoryFieldsToIssueBuilder(issueBuilder, dto, data, showAuthor);
addAdditionalFieldsToIssueBuilder(fields, data, dto, issueBuilder);
return issueBuilder.build();
}
- private void addMandatoryFieldsToIssueBuilder(Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data) {
+ private void addMandatoryFieldsToIssueBuilder(Issue.Builder issueBuilder, IssueDto dto, SearchResponseData data, boolean showAuthor) {
issueBuilder.setKey(dto.getKey());
issueBuilder.setType(Common.RuleType.forNumber(dto.getType()));
issueBuilder.setSeverity(Common.Severity.valueOf(dto.getSeverity()));
}
ofNullable(data.getUserByUuid(dto.getAssigneeUuid())).ifPresent(assignee -> issueBuilder.setAssignee(assignee.getLogin()));
+
ofNullable(emptyToNull(dto.getResolution())).ifPresent(issueBuilder::setResolution);
issueBuilder.setStatus(dto.getStatus());
ofNullable(dto.getIssueStatus()).map(IssueStatus::name).ifPresent(issueBuilder::setIssueStatus);
ofNullable(emptyToNull(dto.getChecksum())).ifPresent(issueBuilder::setHash);
completeIssueLocations(dto, issueBuilder, data);
- issueBuilder.setAuthor(nullToEmpty(dto.getAuthorLogin()));
+ if (showAuthor) {
+ issueBuilder.setAuthor(nullToEmpty(dto.getAuthorLogin()));
+ }
ofNullable(dto.getIssueCreationDate()).map(DateUtils::formatDateTime).ifPresent(issueBuilder::setCreationDate);
ofNullable(dto.getIssueUpdateDate()).map(DateUtils::formatDateTime).ifPresent(issueBuilder::setUpdateDate);
ofNullable(dto.getIssueCloseDate()).map(DateUtils::formatDateTime).ifPresent(issueBuilder::setCloseDate);
Optional.ofNullable(dto.getCveId()).ifPresent(issueBuilder::setCveId);
}
- private static void addAdditionalFieldsToIssueBuilder(Collection<SearchAdditionalField> fields, SearchResponseData data, IssueDto dto, Issue.Builder issueBuilder) {
+ private static void addAdditionalFieldsToIssueBuilder(Collection<SearchAdditionalField> fields, SearchResponseData data, IssueDto dto,
+ Issue.Builder issueBuilder) {
if (fields.contains(ACTIONS)) {
issueBuilder.setActions(createIssueActions(data, dto));
}
String severity = request.mandatoryParam(PARAM_SEVERITY);
try (DbSession session = dbClient.openSession(false)) {
SearchResponseData preloadedSearchResponseData = setType(session, issueKey, severity);
- responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
+ responseWriter.write(issueKey, preloadedSearchResponseData, request, response, true);
}
}
String key = request.mandatoryParam(PARAM_ISSUE);
List<String> tags = MoreObjects.firstNonNull(request.paramAsStrings(PARAM_TAGS), Collections.emptyList());
SearchResponseData preloadedSearchResponseData = setTags(key, tags);
- responseWriter.write(key, preloadedSearchResponseData, request, response);
+ responseWriter.write(key, preloadedSearchResponseData, request, response, true);
}
private SearchResponseData setTags(String issueKey, List<String> tags) {
RuleType ruleType = RuleType.valueOf(request.mandatoryParam(PARAM_TYPE));
try (DbSession session = dbClient.openSession(false)) {
SearchResponseData preloadedSearchResponseData = setType(session, issueKey, ruleType);
- responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
+ responseWriter.write(issueKey, preloadedSearchResponseData, request, response, true);
}
}
componentViewerJsonWriter.writeComponentWithoutFav(writer, projectDto, fileDto, branch, pullRequest);
componentViewerJsonWriter.writeMeasures(writer, fileDto, dbSession);
writer.endObject();
- linesJsonWriter.writeSource(lineSources, writer, periodDateSupplier);
+ linesJsonWriter.writeSource(lineSources, writer, periodDateSupplier, userSession.isLoggedIn());
writer.endObject();
}
"No source found for file '%s' (uuid: %s)", file.getKey(), file.uuid());
try (JsonWriter json = response.newJsonWriter()) {
json.beginObject();
- linesJsonWriter.writeSource(lines, json, periodDateSupplier);
+ linesJsonWriter.writeSource(lines, json, periodDateSupplier, userSession.isLoggedIn());
json.endObject();
}
}
this.htmlSourceDecorator = htmlSourceDecorator;
}
- public void writeSource(Iterable<DbFileSources.Line> lines, JsonWriter json, Supplier<Optional<Long>> periodDateSupplier) {
+ public void writeSource(Iterable<DbFileSources.Line> lines, JsonWriter json, Supplier<Optional<Long>> periodDateSupplier,
+ boolean showAuthor) {
Long periodDate = null;
json.name("sources").beginArray();
.prop("line", line.getLine())
.prop("code", htmlSourceDecorator.getDecoratedSourceAsHtml(line.getSource(), line.getHighlighting(), line.getSymbols()))
.prop("scmRevision", line.getScmRevision());
- json.prop("scmAuthor", line.getScmAuthor());
+ if (showAuthor) {
+ json.prop("scmAuthor", line.getScmAuthor());
+ }
if (line.hasScmDate()) {
json.prop("scmDate", DateUtils.formatDateTime(new Date(line.getScmDate())));
}
try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
- Iterable<DbFileSources.Line> sourceLines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "File '%s' has no sources", fileKey);
+ Iterable<DbFileSources.Line> sourceLines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "File " +
+ "'%s' has no sources", fileKey);
try (JsonWriter json = response.newJsonWriter()) {
json.beginObject();
writeSource(sourceLines, commitsByLine, json);
}
}
- private static void writeSource(Iterable<DbFileSources.Line> lines, boolean showCommitsByLine, JsonWriter json) {
+ private void writeSource(Iterable<DbFileSources.Line> lines, boolean showCommitsByLine, JsonWriter json) {
json.name("scm").beginArray();
DbFileSources.Line previousLine = null;
for (DbFileSources.Line lineDoc : lines) {
if (hasScm(lineDoc) && (!started || showCommitsByLine || !isSameCommit(previousLine, lineDoc))) {
json.beginArray()
- .value(lineDoc.getLine())
- .value(lineDoc.getScmAuthor());
+ .value(lineDoc.getLine());
+ json.value(userSession.isLoggedIn() ? lineDoc.getScmAuthor() : "");
json.value(lineDoc.hasScmDate() ? DateUtils.formatDateTime(new Date(lineDoc.getScmDate())) : null);
json.value(lineDoc.getScmRevision());
json.endArray();
import java.util.Date;
import java.util.List;
import java.util.Set;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.sonar.api.issue.IssueStatus;
import org.sonar.api.resources.Languages;
import org.sonar.api.rules.CleanCodeAttribute;
import static org.sonar.server.issue.index.IssueScope.MAIN;
import static org.sonar.server.issue.index.IssueScope.TEST;
-@RunWith(MockitoJUnitRunner.class)
-public class SearchResponseFormatFormatOperationTest {
- @Rule
- public DbTester db = DbTester.create();
+@ExtendWith(MockitoExtension.class)
+class SearchResponseFormatFormatOperationTest {
+ @RegisterExtension
+ DbTester db = DbTester.create();
private final Durations durations = new Durations();
private final Languages languages = mock(Languages.class);
private final TextRangeResponseFormatter textRangeResponseFormatter = mock(TextRangeResponseFormatter.class);
private final UserResponseFormatter userResponseFormatter = mock(UserResponseFormatter.class);
private final Common.User user = mock(Common.User.class);
- private final SearchResponseFormat searchResponseFormat = new SearchResponseFormat(durations, languages, textRangeResponseFormatter, userResponseFormatter);
+ private final SearchResponseFormat searchResponseFormat = new SearchResponseFormat(durations, languages, textRangeResponseFormatter,
+ userResponseFormatter);
private SearchResponseData searchResponseData;
private IssueDto issueDto;
private ComponentDto componentDto;
private UserDto userDto;
- @Before
- public void setUp() {
+ @BeforeEach
+ void setUp() {
searchResponseData = newSearchResponseDataMainBranch();
}
@Test
- public void formatOperation_should_add_components_to_response() {
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ void formatOperation_should_add_components_to_response() {
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getComponentsList()).hasSize(1);
assertThat(result.getComponentsList().get(0).getKey()).isEqualTo(issueDto.getComponentKey());
}
@Test
- public void formatOperation_should_add_rules_to_response() {
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ void formatOperation_should_add_rules_to_response() {
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getRulesList()).hasSize(1);
assertThat(result.getRulesList().get(0).getKey()).isEqualTo(issueDto.getRuleKey().toString());
}
@Test
- public void formatOperation_should_add_users_to_response() {
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ void formatOperation_should_add_users_to_response() {
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getUsersList()).hasSize(1);
assertThat(result.getUsers(0)).isSameAs(user);
}
@Test
- public void formatOperation_should_add_issue_to_response() {
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ void formatOperation_does_not_add_author_to_response_if_showAuthor_false() {
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, false);
+
+ assertThat(result.getIssue().getAuthor()).isEmpty();
+ }
+
+ @Test
+ void formatOperation_should_add_issue_to_response() {
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertIssueEqualsIssueDto(result.getIssue(), issueDto);
}
}
@Test
- public void formatOperation_should_not_add_issue_when_several_issue() {
+ void formatOperation_should_not_add_issue_when_several_issue() {
searchResponseData = new SearchResponseData(List.of(createIssue(), createIssue()));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue()).isEqualTo(Issue.getDefaultInstance());
}
}
@Test
- public void formatOperation_should_add_branch_on_issue() {
+ void formatOperation_should_add_branch_on_issue() {
String branchName = randomAlphanumeric(5);
searchResponseData = newSearchResponseDataBranch(branchName);
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getBranch()).isEqualTo(branchName);
}
@Test
- public void formatOperation_should_add_pullrequest_on_issue() {
+ void formatOperation_should_add_pullrequest_on_issue() {
searchResponseData = newSearchResponseDataPr("pr1");
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getPullRequest()).isEqualTo("pr1");
}
@Test
- public void formatOperation_should_add_project_on_issue() {
+ void formatOperation_should_add_project_on_issue() {
issueDto.setProjectUuid(componentDto.uuid());
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getProject()).isEqualTo(componentDto.getKey());
}
@Test
- public void formatOperation_should_add_external_rule_engine_on_issue() {
+ void formatOperation_should_add_external_rule_engine_on_issue() {
issueDto.setExternal(true);
String expected = randomAlphanumeric(5);
issueDto.setRuleKey(EXTERNAL_RULE_REPO_PREFIX + expected, randomAlphanumeric(5));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getExternalRuleEngine()).isEqualTo(expected);
}
@Test
- public void formatOperation_should_add_effort_and_debt_on_issue() {
+ void formatOperation_should_add_effort_and_debt_on_issue() {
long effort = 60L;
issueDto.setEffort(effort);
String expected = durations.encode(Duration.create(effort));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getEffort()).isEqualTo(expected);
assertThat(result.getIssue().getDebt()).isEqualTo(expected);
}
@Test
- public void formatOperation_should_add_scope_test_on_issue_when_unit_test_file() {
+ void formatOperation_should_add_scope_test_on_issue_when_unit_test_file() {
componentDto.setQualifier(UNIT_TEST_FILE);
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getScope()).isEqualTo(TEST.name());
}
@Test
- public void formatOperation_should_add_scope_main_on_issue_when_not_unit_test_file() {
+ void formatOperation_should_add_scope_main_on_issue_when_not_unit_test_file() {
componentDto.setQualifier(randomAlphanumeric(5));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getScope()).isEqualTo(MAIN.name());
}
@Test
- public void formatOperation_should_add_actions_on_issues() {
+ void formatOperation_should_add_actions_on_issues() {
Set<String> expectedActions = Set.of("actionA", "actionB");
searchResponseData.addActions(issueDto.getKey(), expectedActions);
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getActions().getActionsList()).containsExactlyInAnyOrderElementsOf(expectedActions);
}
@Test
- public void formatOperation_should_add_transitions_on_issues() {
+ void formatOperation_should_add_transitions_on_issues() {
Set<String> expectedTransitions = Set.of("transitionone", "transitiontwo");
searchResponseData.addTransitions(issueDto.getKey(), createFakeTransitions(expectedTransitions));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getTransitions().getTransitionsList()).containsExactlyInAnyOrderElementsOf(expectedTransitions);
}
}
@Test
- public void formatOperation_should_add_comments_on_issues() {
+ void formatOperation_should_add_comments_on_issues() {
IssueChangeDto issueChangeDto = newIssueChangeDto(issueDto);
searchResponseData.setComments(List.of(issueChangeDto));
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getComments().getCommentsList()).hasSize(1).extracting(Common.Comment::getKey).containsExactly(issueChangeDto.getKey());
}
@Test
- public void formatOperation_should_not_set_severity_for_security_hotspot_issue() {
+ void formatOperation_should_not_set_severity_for_security_hotspot_issue() {
issueDto.setType(SECURITY_HOTSPOT);
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().hasSeverity()).isFalse();
}
@Test
- public void formatOperation_shouldReturnExpectedIssueStatus() {
+ void formatOperation_shouldReturnExpectedIssueStatus() {
issueDto.setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED);
issueDto.setResolution(org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX);
- Operation result = searchResponseFormat.formatOperation(searchResponseData);
+ Operation result = searchResponseFormat.formatOperation(searchResponseData, true);
assertThat(result.getIssue().getIssueStatus()).isEqualTo(IssueStatus.ACCEPTED.name());
}