diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-30 14:25:59 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-30 14:25:59 +0100 |
commit | 1bf38eef436b58e21c972522fa05823672cbadc5 (patch) | |
tree | 095e167b3ed0d17ffd700e5251ec2974465e5d8e | |
parent | 8a2d479837ab5080f9dfdf2a19433f1016169bfd (diff) | |
download | sonarqube-1bf38eef436b58e21c972522fa05823672cbadc5.tar.gz sonarqube-1bf38eef436b58e21c972522fa05823672cbadc5.zip |
SoNAR-5292 Return 404 when file does not exists
6 files changed, 76 insertions, 31 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java index 12471fc91b1..8a934738231 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ b/server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java @@ -27,7 +27,6 @@ import org.apache.ibatis.session.ResultContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.utils.System2; -import org.sonar.core.persistence.BatchSession; import org.sonar.core.persistence.DaoComponent; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.Dto; @@ -355,10 +354,6 @@ public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializ @Override public void synchronizeAfter(final DbSession session, @Nullable Date date, Map<String, String> params) { - if (!session.getClass().isAssignableFrom(BatchSession.class)) { - LOGGER.warn("Synchronizer should only be used with BatchSession!"); - } - DbSynchronizationHandler handler = getSynchronizationResultHandler(session, params); session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date, params), handler); handler.enqueueCollected(); 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 61755cac9d7..aeac79a61eb 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 @@ -74,19 +74,14 @@ public class SourceService implements ServerComponent { return deprecatedSourceDecorator.getSourceAsHtml(fileKey, from, to); } - public List<String> getLinesAsTxt(String fileKey) { + public List<String> getLinesAsTxt(DbSession session, String fileKey) { checkPermission(fileKey); - DbSession session = dbClient.openSession(false); - try { - String source = snapshotSourceDao.selectSnapshotSourceByComponentKey(fileKey, session); - if (source != null) { - return newArrayList(Splitter.onPattern("\r?\n|\r").split(source)); - } - return Collections.emptyList(); - } finally { - MyBatis.closeQuietly(session); + String source = snapshotSourceDao.selectSnapshotSourceByComponentKey(fileKey, session); + if (source != null) { + return newArrayList(Splitter.onPattern("\r?\n|\r").split(source)); } + return Collections.emptyList(); } @CheckForNull 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 14b4fdd69df..4c4565e875b 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 @@ -25,6 +25,8 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; +import org.sonar.core.persistence.DbSession; +import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; @@ -32,15 +34,17 @@ import java.util.List; public class RawAction implements RequestHandler { + private final DbClient dbClient; private final SourceService sourceService; - public RawAction(SourceService sourceService) { + public RawAction(DbClient dbClient, SourceService sourceService) { + this.dbClient = dbClient; this.sourceService = sourceService; } void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("raw") - .setDescription("Get source code as plain text. Require Browse permission on file's project") + .setDescription("Get source code as plain text. Require Code viewer permission on file") .setSince("5.0") .setResponseExample(Resources.getResource(getClass(), "example-raw.txt")) .setHandler(this); @@ -55,10 +59,16 @@ public class RawAction implements RequestHandler { @Override public void handle(Request request, Response response) { String fileKey = request.mandatoryParam("key"); - List<String> lines = sourceService.getLinesAsTxt(fileKey); - if (lines.isEmpty()) { - throw new NotFoundException("File '" + fileKey + "' has no sources"); + DbSession session = dbClient.openSession(false); + try { + dbClient.componentDao().getByKey(session, fileKey); + List<String> lines = sourceService.getLinesAsTxt(session, fileKey); + if (lines.isEmpty()) { + throw new NotFoundException("File '" + fileKey + "' has no sources"); + } + response.newTxtWriter().values(lines).close(); + } finally { + session.close(); } - response.newTxtWriter().values(lines).close(); } } 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 e501ede7802..ef632c3200f 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 @@ -145,7 +145,7 @@ public class SourceServiceTest { when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn("line1\nline2"); - List<String> result = service.getLinesAsTxt(COMPONENT_KEY); + List<String> result = service.getLinesAsTxt(session, COMPONENT_KEY); assertThat(result).contains("line1", "line2"); } @@ -155,7 +155,7 @@ public class SourceServiceTest { when(snapshotSourceDao.selectSnapshotSourceByComponentKey(COMPONENT_KEY, session)).thenReturn(null); - List<String> result = service.getLinesAsTxt(COMPONENT_KEY); + List<String> result = service.getLinesAsTxt(session, COMPONENT_KEY); assertThat(result).isEmpty(); } 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 07794a4bb10..fcac29ee4d4 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 @@ -20,7 +20,16 @@ package org.sonar.server.source.ws; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.persistence.DbSession; +import org.sonar.server.component.ComponentTesting; +import org.sonar.server.component.db.ComponentDao; +import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.source.SourceService; import org.sonar.server.ws.WsTester; @@ -29,21 +38,46 @@ import java.util.Collections; import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@RunWith(MockitoJUnitRunner.class) public class RawActionTest { - SourceService sourceService = mock(SourceService.class); - WsTester tester = new WsTester(new SourcesWs(mock(ShowAction.class), new RawAction(sourceService), mock(ScmAction.class))); + @Mock + DbClient dbClient; + + @Mock + DbSession session; + + @Mock + ComponentDao componentDao; + + @Mock + SourceService sourceService; + + WsTester tester; + + ComponentDto project = ComponentTesting.newProjectDto(); + ComponentDto file = ComponentTesting.newFileDto(project); + + @Before + public void setUp() throws Exception { + when(dbClient.componentDao()).thenReturn(componentDao); + when(dbClient.openSession(false)).thenReturn(session); + tester = new WsTester(new SourcesWs(mock(ShowAction.class), new RawAction(dbClient, sourceService), mock(ScmAction.class))); + } @Test public void get_txt() throws Exception { String fileKey = "src/Foo.java"; - when(sourceService.getLinesAsTxt(fileKey)).thenReturn(newArrayList( + when(componentDao.getByKey(session, fileKey)).thenReturn(file); + + when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(newArrayList( "public class HelloWorld {", "}" - )); + )); WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey); String result = request.execute().outputAsString(); @@ -51,12 +85,22 @@ public class RawActionTest { } @Test(expected = NotFoundException.class) + public void fail_to_get_txt_when_file_does_not_exists() throws Exception { + WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", "unknown"); + request.execute(); + } + public void fail_to_get_txt_when_no_source() throws Exception { String fileKey = "src/Foo.java"; - when(sourceService.getLinesAsTxt(fileKey)).thenReturn(Collections.<String>emptyList()); + when(componentDao.getByKey(session, fileKey)).thenReturn(file); + when(sourceService.getLinesAsTxt(session, fileKey)).thenReturn(Collections.<String>emptyList()); WsTester.TestRequest request = tester.newGetRequest("api/sources", "raw").setParam("key", fileKey); - request.execute(); + try { + request.execute(); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(NotFoundException.class).hasMessage(""); + } } - } diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java index c1754676916..512c771618c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java @@ -22,6 +22,7 @@ package org.sonar.server.source.ws; import org.junit.Test; import org.sonar.api.server.ws.WebService; +import org.sonar.server.db.DbClient; import org.sonar.server.source.SourceService; import org.sonar.server.ws.WsTester; @@ -31,7 +32,7 @@ import static org.mockito.Mockito.mock; public class SourcesWsTest { ShowAction showAction = new ShowAction(mock(SourceService.class)); - RawAction rawAction = new RawAction(mock(SourceService.class)); + RawAction rawAction = new RawAction(mock(DbClient.class), mock(SourceService.class)); ScmAction scmAction = new ScmAction(mock(SourceService.class), new ScmWriter()); WsTester tester = new WsTester(new SourcesWs(showAction, rawAction, scmAction)); |