]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8748 Add message on every exception thrown by WS 1635/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 10 Feb 2017 08:55:17 +0000 (09:55 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 10 Feb 2017 11:56:20 +0000 (12:56 +0100)
16 files changed:
server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskAction.java
server/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java
server/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java
server/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java
server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/IndexAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/ScmAction.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/ShowAction.java
server/sonar-server/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
server/sonar-server/src/test/java/org/sonar/server/authentication/ws/LoginActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/SourceServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/IndexActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/ShowActionTest.java

index 48d3b44c1ce040f2487824525a3f6a4de7720467..df90da53e677535c070acd3a61a99607735bce5c 100644 (file)
@@ -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<CeActivityDto> activityDto = dbClient.ceActivityDao().selectByUuid(dbSession, taskUuid);
-        if (activityDto.isPresent()) {
-          CeActivityDto ceActivityDto = activityDto.get();
-          Optional<ComponentDto> component = loadComponent(dbSession, ceActivityDto.getComponentUuid());
-          checkPermission(component);
-          Set<AdditionalField> 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<ComponentDto> component = loadComponent(dbSession, ceActivityDto.getComponentUuid());
+        checkPermission(component);
+        Set<AdditionalField> additionalFields = AdditionalField.getFromRequest(wsRequest);
+        maskErrorStacktrace(ceActivityDto, additionalFields);
+        wsTaskResponse.setTask(
+          wsTaskFormatter.formatActivity(dbSession, ceActivityDto, component, extractScannerContext(dbSession, ceActivityDto, additionalFields)));
       }
       writeProtobuf(wsTaskResponse.build(), wsRequest, wsResponse);
     }
index def30a7a26a578e258c37698ff3617d273ee7293..d2de7611828723a3105788eeb22118145b921006 100644 (file)
@@ -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);
   }
index 0ea1bbee4ec7aaf84f2da3ded24f73a738335c22..e619997e9fe9e61bdb93833a8ed8d166f882698b 100644 (file)
@@ -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;
index 650a561c176b14ba1447215301655e2744f902b2..aacfa9b676e8fe24e4685eea929ad640a6012d14 100644 (file)
@@ -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);
   }
index 42d482105608277addf3aa56ae9a242d69c3a273..8a430e1d5e4fc5054c52e8addcd09eab85c30c44 100644 (file)
  */
 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<Iterable<DbFileSources.Line>> 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<Iterable<String>> 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<Iterable<String>> getLinesAsHtml(DbSession dbSession, String fileUuid, int from, int toInclusive) {
@@ -64,45 +62,24 @@ public class SourceService {
 
   private <E> Optional<Iterable<E>> getLines(DbSession dbSession, String fileUuid, int from, int toInclusive, Function<DbFileSources.Line, E> 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<DbFileSources.Line, String> lineToHtml() {
     return line -> htmlDecorator.getDecoratedSourceAsHtml(line.getSource(), line.getHighlighting(), line.getSymbols());
   }
 
-  private enum LineToRaw implements Function<DbFileSources.Line, String> {
-    INSTANCE;
-    @Override
-    public String apply(@Nonnull DbFileSources.Line line) {
-      return line.getSource();
-    }
-
-  }
-
-  private static class IsGreaterOrEqualThanLine implements Predicate<DbFileSources.Line> {
-    private final int from;
-
-    IsGreaterOrEqualThanLine(int from) {
-      this.from = from;
-    }
-
-    @Override
-    public boolean apply(@Nonnull DbFileSources.Line line) {
-      return line.hasLine() && line.getLine() >= from;
-    }
-  }
 }
index 3a1140f65ca8083a6c0b81e8e0fd209c15ca21f0..e49d3c9e44827ced1d706d03fe4eb5d367525ab4 100644 (file)
@@ -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;
index fc9fa8590951471d7395dcfaf9c359e458dc17fe..8efe6a1b91941a4d7422d074cfa278b18b9e1af8 100644 (file)
@@ -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<Iterable<DbFileSources.Line>> lines = sourceService.getLines(dbSession, file.uuid(), from, to);
-      if (!lines.isPresent()) {
-        throw new NotFoundException();
-      }
-
+      Iterable<DbFileSources.Line> 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);
     }
   }
 
index 97b65e84f4b0d1605126cd098937428a28feaeb0..b30572e49d2329f7bc2da5016cb577154d0fa334 100644 (file)
  */
 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;
index e857f2e585777b18d8131cab0450c599fd80a357..4dbb720721514af2d9cad000b816e52457a38993 100644 (file)
@@ -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<Iterable<DbFileSources.Line>> sourceLines = sourceService.getLines(dbSession, file.uuid(), from, to);
-      if (!sourceLines.isPresent()) {
-        throw new NotFoundException(String.format("File '%s' has no sources", fileKey));
-      }
+      Iterable<DbFileSources.Line> 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);
index 56ba7b260b11a74d43c0b3007de4932b17d82614..2933ebb6057438db23d2cb6ef515116896876976 100644 (file)
@@ -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<Iterable<String>> 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<String> 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<String> lines, int from, JsonWriter json) {
+  private static void writeSource(Iterable<String> lines, int from, JsonWriter json) {
     json.name("sources").beginArray();
     long index = 0L;
     for (String line : lines) {
index 5d22b449804701453d534f59b04841e43a545432..4a7c2e7059aac6d99d37eacf76a20b532d0ac870 100644 (file)
@@ -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) {
index bf38fa25e21a612057a9f40a1bf00f43ffdf2492..353d21a008893407910a7aaf571c3bbc35f6156a 100644 (file)
@@ -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);
 
index 05c735089273dda798feaf21887ae243ed9ee192..0426d0e74ea5ffc21a73c1854b49fcf6e3aa66a0 100644 (file)
  */
 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;
index e9e74fee090ce567f4e5b7861228dbd692d061b7..adad610633105801716a4fb307d6712da6f077f4 100644 (file)
@@ -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<String>) 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<String>) 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.<ComponentDto>absent());
+    when(componentDao.selectByKey(session, fileKey)).thenReturn(com.google.common.base.Optional.<ComponentDto>absent());
 
     WsTester.TestRequest request = tester.newGetRequest("api/sources", "index").setParam("resource", fileKey);
     try {
index 92b4188d82751ca851b8251586e1ef58aa45580b..9f6401f43fc3e2cfcf9279b8de776d95c8e85265 100644 (file)
@@ -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<String> 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();
   }
 }
index 9da0dfe2ab240d38047c411be150e1750c4bc48b..d2e2d4188547ebc8076c3c22be55ef04c229cdf8 100644 (file)
@@ -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<String>) 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<String>) 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 <span class=\"sym-31 sym\">HelloWorld</span> {")));
@@ -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<String>) 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 <span class=\"sym-31 sym\">HelloWorld</span> {")));
@@ -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();
   }
 }