From d0a8bb44d4b100fe6aea065eebde398841eb15ad Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 10 Feb 2017 09:55:17 +0100 Subject: [PATCH] SONAR-8748 Add message on every exception thrown by WS --- .../org/sonar/server/ce/ws/TaskAction.java | 21 +++----- .../server/exceptions/NotFoundException.java | 4 -- .../server/exceptions/ServerException.java | 4 -- .../exceptions/UnauthorizedException.java | 4 -- .../sonar/server/source/SourceService.java | 53 ++++++------------- .../sonar/server/source/ws/IndexAction.java | 2 +- .../sonar/server/source/ws/LinesAction.java | 15 ++---- .../org/sonar/server/source/ws/RawAction.java | 2 +- .../org/sonar/server/source/ws/ScmAction.java | 11 ++-- .../sonar/server/source/ws/ShowAction.java | 23 +++----- .../server/user/ThreadLocalUserSession.java | 2 +- .../authentication/ws/LoginActionTest.java | 2 +- .../server/source/SourceServiceTest.java | 2 +- .../server/source/ws/IndexActionTest.java | 14 ++--- .../sonar/server/source/ws/RawActionTest.java | 6 +-- .../server/source/ws/ShowActionTest.java | 16 +++--- 16 files changed, 62 insertions(+), 119 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskAction.java index 48d3b44c1ce..df90da53e67 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskAction.java @@ -38,8 +38,8 @@ import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.UserSession; +import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.WsCe; import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; @@ -96,18 +96,13 @@ public class TaskAction implements CeWsAction { checkPermission(component); wsTaskResponse.setTask(wsTaskFormatter.formatQueue(dbSession, queueDto.get(), component)); } else { - Optional activityDto = dbClient.ceActivityDao().selectByUuid(dbSession, taskUuid); - if (activityDto.isPresent()) { - CeActivityDto ceActivityDto = activityDto.get(); - Optional component = loadComponent(dbSession, ceActivityDto.getComponentUuid()); - checkPermission(component); - Set additionalFields = AdditionalField.getFromRequest(wsRequest); - maskErrorStacktrace(ceActivityDto, additionalFields); - wsTaskResponse.setTask( - wsTaskFormatter.formatActivity(dbSession, ceActivityDto, component, extractScannerContext(dbSession, ceActivityDto, additionalFields))); - } else { - throw new NotFoundException(); - } + CeActivityDto ceActivityDto = WsUtils.checkFoundWithOptional(dbClient.ceActivityDao().selectByUuid(dbSession, taskUuid), "No activity found for task '%s'", taskUuid); + Optional component = loadComponent(dbSession, ceActivityDto.getComponentUuid()); + checkPermission(component); + Set additionalFields = AdditionalField.getFromRequest(wsRequest); + maskErrorStacktrace(ceActivityDto, additionalFields); + wsTaskResponse.setTask( + wsTaskFormatter.formatActivity(dbSession, ceActivityDto, component, extractScannerContext(dbSession, ceActivityDto, additionalFields))); } writeProtobuf(wsTaskResponse.build(), wsRequest, wsResponse); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java b/server/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java index def30a7a26a..d2de7611828 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java +++ b/server/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java @@ -23,10 +23,6 @@ import static java.net.HttpURLConnection.HTTP_NOT_FOUND; public class NotFoundException extends ServerException { - public NotFoundException() { - super(HTTP_NOT_FOUND); - } - public NotFoundException(String message) { super(HTTP_NOT_FOUND, message); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java b/server/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java index 0ea1bbee4ec..e619997e9fe 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java +++ b/server/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java @@ -24,10 +24,6 @@ import static java.util.Objects.requireNonNull; public class ServerException extends RuntimeException { private final int httpCode; - public ServerException(int httpCode) { - this.httpCode = httpCode; - } - public ServerException(int httpCode, String message) { super(requireNonNull(message, "Error message cannot be null")); this.httpCode = httpCode; diff --git a/server/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java b/server/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java index 650a561c176..aacfa9b676e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java +++ b/server/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java @@ -26,10 +26,6 @@ import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; */ public class UnauthorizedException extends ServerException { - public UnauthorizedException() { - super(HTTP_UNAUTHORIZED); - } - public UnauthorizedException(String message) { super(HTTP_UNAUTHORIZED, message); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java index 42d48210560..8a430e1d5e4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java @@ -19,18 +19,16 @@ */ package org.sonar.server.source; -import com.google.common.base.Function; -import com.google.common.base.Functions; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; -import com.google.common.collect.FluentIterable; -import javax.annotation.Nonnull; +import java.util.Optional; +import java.util.function.Function; +import org.sonar.core.util.stream.Collectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.protobuf.DbFileSources; import org.sonar.db.source.FileSourceDto; +import static com.google.common.base.Preconditions.checkArgument; + public class SourceService { private final DbClient dbClient; @@ -47,7 +45,7 @@ public class SourceService { * @param toInclusive starts from 1, must be greater than or equal param {@code from} */ public Optional> getLines(DbSession dbSession, String fileUuid, int from, int toInclusive) { - return getLines(dbSession, fileUuid, from, toInclusive, Functions.identity()); + return getLines(dbSession, fileUuid, from, toInclusive, Function.identity()); } /** @@ -55,7 +53,7 @@ public class SourceService { * @see #getLines(DbSession, String, int, int) */ public Optional> getLinesAsRawText(DbSession dbSession, String fileUuid, int from, int toInclusive) { - return getLines(dbSession, fileUuid, from, toInclusive, LineToRaw.INSTANCE); + return getLines(dbSession, fileUuid, from, toInclusive, DbFileSources.Line::getSource); } public Optional> getLinesAsHtml(DbSession dbSession, String fileUuid, int from, int toInclusive) { @@ -64,45 +62,24 @@ public class SourceService { private Optional> getLines(DbSession dbSession, String fileUuid, int from, int toInclusive, Function function) { verifyLine(from); - Preconditions.checkArgument(toInclusive >= from, String.format("Line number must greater than or equal to %d, got %d", from, toInclusive)); + checkArgument(toInclusive >= from, String.format("Line number must greater than or equal to %d, got %d", from, toInclusive)); FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, fileUuid); if (dto == null) { - return Optional.absent(); + return Optional.empty(); } - DbFileSources.Data data = dto.getSourceData(); - return Optional.of(FluentIterable.from(data.getLinesList()) - .filter(new IsGreaterOrEqualThanLine(from)) - .limit(toInclusive - from + 1) - .transform(function)); + return Optional.of(dto.getSourceData().getLinesList().stream() + .filter(line -> line.hasLine() && line.getLine() >= from) + .limit((toInclusive - from) + 1L) + .map(function) + .collect(Collectors.toList())); } private static void verifyLine(int line) { - Preconditions.checkArgument(line >= 1, String.format("Line number must start at 1, got %d", line)); + checkArgument(line >= 1, String.format("Line number must start at 1, got %d", line)); } private Function lineToHtml() { return line -> htmlDecorator.getDecoratedSourceAsHtml(line.getSource(), line.getHighlighting(), line.getSymbols()); } - private enum LineToRaw implements Function { - INSTANCE; - @Override - public String apply(@Nonnull DbFileSources.Line line) { - return line.getSource(); - } - - } - - private static class IsGreaterOrEqualThanLine implements Predicate { - private final int from; - - IsGreaterOrEqualThanLine(int from) { - this.from = from; - } - - @Override - public boolean apply(@Nonnull DbFileSources.Line line) { - return line.hasLine() && line.getLine() >= from; - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java index 3a1140f65ca..e49d3c9e448 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java @@ -19,8 +19,8 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; import com.google.common.io.Resources; +import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java index fc9fa859095..8efe6a1b919 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java @@ -34,13 +34,13 @@ import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.protobuf.DbFileSources; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.HtmlSourceDecorator; import org.sonar.server.source.SourceService; import org.sonar.server.user.UserSession; import static org.sonar.server.component.ComponentFinder.ParamNames.UUID_AND_KEY; import static org.sonar.server.ws.KeyExamples.KEY_FILE_EXAMPLE_001; +import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; public class LinesAction implements SourcesWsAction { @@ -112,24 +112,17 @@ public class LinesAction implements SourcesWsAction { @Override public void handle(Request request, Response response) { - DbSession dbSession = dbClient.openSession(false); - try { + try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto file = componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_UUID), request.param(PARAM_KEY), UUID_AND_KEY); userSession.checkComponentPermission(UserRole.CODEVIEWER, file); int from = request.mandatoryParamAsInt(PARAM_FROM); int to = MoreObjects.firstNonNull(request.paramAsInt(PARAM_TO), Integer.MAX_VALUE); - com.google.common.base.Optional> lines = sourceService.getLines(dbSession, file.uuid(), from, to); - if (!lines.isPresent()) { - throw new NotFoundException(); - } - + Iterable lines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "No source found for file '%s'", file.key()); JsonWriter json = response.newJsonWriter().beginObject(); - writeSource(lines.get(), json); + writeSource(lines, json); json.endObject().close(); - } finally { - dbClient.closeSession(dbSession); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java index 97b65e84f4b..b30572e49d2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java @@ -19,11 +19,11 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; import com.google.common.io.Resources; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java index e857f2e5857..4dbb7207215 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; import com.google.common.base.Strings; import com.google.common.io.Resources; import java.util.Date; @@ -36,10 +35,11 @@ import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.protobuf.DbFileSources; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; import org.sonar.server.user.UserSession; +import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; + public class ScmAction implements SourcesWsAction { private final DbClient dbClient; @@ -105,12 +105,9 @@ public class ScmAction implements SourcesWsAction { try { ComponentDto file = componentFinder.getByKey(dbSession, fileKey); userSession.checkComponentPermission(UserRole.CODEVIEWER, file); - Optional> sourceLines = sourceService.getLines(dbSession, file.uuid(), from, to); - if (!sourceLines.isPresent()) { - throw new NotFoundException(String.format("File '%s' has no sources", fileKey)); - } + Iterable sourceLines = checkFoundWithOptional(sourceService.getLines(dbSession, file.uuid(), from, to), "File '%s' has no sources", fileKey); JsonWriter json = response.newJsonWriter().beginObject(); - writeSource(sourceLines.get(), commitsByLine, json); + writeSource(sourceLines, commitsByLine, json); json.endObject().close(); } finally { dbClient.closeSession(dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java index 56ba7b260b1..2933ebb6057 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; import com.google.common.io.Resources; import org.apache.commons.lang.ObjectUtils; import org.sonar.api.server.ws.Request; @@ -31,10 +30,11 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; import org.sonar.server.user.UserSession; +import static org.sonar.server.ws.WsUtils.checkFoundWithOptional; + public class ShowAction implements SourcesWsAction { private final SourceService sourceService; @@ -86,26 +86,19 @@ public class ShowAction implements SourcesWsAction { int from = Math.max(request.paramAsInt("from"), 1); int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE); - DbSession dbSession = dbClient.openSession(false); - try { + try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto file = componentFinder.getByKey(dbSession, fileKey); userSession.checkComponentPermission(UserRole.CODEVIEWER, file); - Optional> linesHtml = sourceService.getLinesAsHtml(dbSession, file.uuid(), from, to); - if (linesHtml.isPresent()) { - JsonWriter json = response.newJsonWriter().beginObject(); - writeSource(linesHtml.get(), from, json); - json.endObject().close(); - } else { - throw new NotFoundException(); - } + Iterable linesHtml = checkFoundWithOptional(sourceService.getLinesAsHtml(dbSession, file.uuid(), from, to), "No source found for file '%s'", fileKey); + JsonWriter json = response.newJsonWriter().beginObject(); + writeSource(linesHtml, from, json); + json.endObject().close(); - } finally { - dbClient.closeSession(dbSession); } } - private void writeSource(Iterable lines, int from, JsonWriter json) { + private static void writeSource(Iterable lines, int from, JsonWriter json) { json.name("sources").beginArray(); long index = 0L; for (String line : lines) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java b/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java index 5d22b449804..4a7c2e7059a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java @@ -37,7 +37,7 @@ public class ThreadLocalUserSession implements UserSession { if (session != null) { return session; } - throw new UnauthorizedException(); + throw new UnauthorizedException("User is not authenticate"); } public void set(UserSession session) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/authentication/ws/LoginActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/authentication/ws/LoginActionTest.java index bf38fa25e21..353d21a0088 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/authentication/ws/LoginActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/authentication/ws/LoginActionTest.java @@ -116,7 +116,7 @@ public class LoginActionTest { @Test public void return_authorized_code_when_unauthorized_exception_is_thrown() throws Exception { - doThrow(new UnauthorizedException()).when(credentialsAuthenticator).authenticate(LOGIN, PASSWORD, request, FORM); + doThrow(new UnauthorizedException("error !")).when(credentialsAuthenticator).authenticate(LOGIN, PASSWORD, request, FORM); executeRequest(LOGIN, PASSWORD); diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java index 05c73508927..0426d0e74ea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java @@ -19,10 +19,10 @@ */ package org.sonar.server.source; -import com.google.common.base.Optional; import com.google.common.collect.Lists; import java.io.IOException; import java.util.List; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java index e9e74fee090..adad6106331 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -78,9 +78,9 @@ public class IndexActionTest { public void get_json() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); - when(sourceService.getLinesAsRawText(session, file.uuid(), 1, Integer.MAX_VALUE)).thenReturn(Optional.of((Iterable) newArrayList( + when(sourceService.getLinesAsRawText(session, file.uuid(), 1, Integer.MAX_VALUE)).thenReturn(Optional.of(newArrayList( "public class HelloWorld {", "}"))); @@ -92,9 +92,9 @@ public class IndexActionTest { public void limit_range() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); - when(sourceService.getLinesAsRawText(session, file.uuid(), 1, 2)).thenReturn(Optional.of((Iterable) newArrayList( + when(sourceService.getLinesAsRawText(session, file.uuid(), 1, 2)).thenReturn(Optional.of(newArrayList( "public class HelloWorld {", "}"))); @@ -105,7 +105,7 @@ public class IndexActionTest { @Test(expected = ForbiddenException.class) public void requires_code_viewer_permission() throws Exception { - when(componentDao.selectByKey(session, "foo")).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, "foo")).thenReturn(com.google.common.base.Optional.of(file)); tester.newGetRequest("api/sources", "index").setParam("resource", "foo").execute(); } @@ -113,7 +113,7 @@ public class IndexActionTest { public void close_db_session() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.absent()); + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.absent()); WsTester.TestRequest request = tester.newGetRequest("api/sources", "index").setParam("resource", fileKey); try { diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java index 92b4188d827..9f6401f43fc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -77,7 +77,7 @@ public class RawActionTest { public void get_txt() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); Iterable lines = newArrayList( "public class HelloWorld {", @@ -91,7 +91,7 @@ public class RawActionTest { @Test(expected = ForbiddenException.class) public void requires_code_viewer_permission() throws Exception { - when(componentDao.selectByKey(session, "src/Foo.java")).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, "src/Foo.java")).thenReturn(com.google.common.base.Optional.of(file)); tester.newGetRequest("api/sources", "raw").setParam("key", "src/Foo.java").execute(); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java index 9da0dfe2ab2..d2e2d418854 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.source.ws; -import com.google.common.base.Optional; +import java.util.Optional; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -79,8 +79,8 @@ public class ShowActionTest { public void show_source() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); - when(sourceService.getLinesAsHtml(eq(session), eq(file.uuid()), anyInt(), anyInt())).thenReturn(Optional.of((Iterable) newArrayList( + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); + when(sourceService.getLinesAsHtml(eq(session), eq(file.uuid()), anyInt(), anyInt())).thenReturn(Optional.of(newArrayList( "/*", " * Header", " */", @@ -96,8 +96,8 @@ public class ShowActionTest { public void show_source_with_from_and_to_params() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); - when(sourceService.getLinesAsHtml(session, file.uuid(), 3, 5)).thenReturn(Optional.of((Iterable) newArrayList( + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); + when(sourceService.getLinesAsHtml(session, file.uuid(), 3, 5)).thenReturn(Optional.of(newArrayList( " */", "", "public class HelloWorld {"))); @@ -113,8 +113,8 @@ public class ShowActionTest { public void show_source_accept_from_less_than_one() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, project.uuid()); - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); - when(sourceService.getLinesAsHtml(session, file.uuid(), 1, 5)).thenReturn(Optional.of((Iterable) newArrayList( + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); + when(sourceService.getLinesAsHtml(session, file.uuid(), 1, 5)).thenReturn(Optional.of(newArrayList( " */", "", "public class HelloWorld {"))); @@ -130,7 +130,7 @@ public class ShowActionTest { @Test(expected = ForbiddenException.class) public void require_code_viewer() throws Exception { String fileKey = "src/Foo.java"; - when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); + when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.of(file)); tester.newGetRequest("api/sources", "show").setParam("key", fileKey).execute(); } } -- 2.39.5