Skip to content

Commit

Permalink
Move batch/upload_report to api/analysis_reports/submit_report
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Jan 7, 2015
1 parent 667b768 commit 5e0ae3e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 117 deletions.
Expand Up @@ -35,14 +35,11 @@ public class BatchWs implements WebService {
private final BatchIndex batchIndex; private final BatchIndex batchIndex;
private final GlobalReferentialsAction globalReferentialsAction; private final GlobalReferentialsAction globalReferentialsAction;
private final ProjectReferentialsAction projectReferentialsAction; private final ProjectReferentialsAction projectReferentialsAction;
private final UploadReportAction uploadReportAction;


public BatchWs(BatchIndex batchIndex, GlobalReferentialsAction globalReferentialsAction, ProjectReferentialsAction projectReferentialsAction, public BatchWs(BatchIndex batchIndex, GlobalReferentialsAction globalReferentialsAction, ProjectReferentialsAction projectReferentialsAction) {
UploadReportAction uploadReportAction) {
this.batchIndex = batchIndex; this.batchIndex = batchIndex;
this.globalReferentialsAction = globalReferentialsAction; this.globalReferentialsAction = globalReferentialsAction;
this.projectReferentialsAction = projectReferentialsAction; this.projectReferentialsAction = projectReferentialsAction;
this.uploadReportAction = uploadReportAction;
} }


@Override @Override
Expand All @@ -55,7 +52,6 @@ public void define(Context context) {
defineFileAction(controller); defineFileAction(controller);
globalReferentialsAction.define(controller); globalReferentialsAction.define(controller);
projectReferentialsAction.define(controller); projectReferentialsAction.define(controller);
uploadReportAction.define(controller);


controller.done(); controller.done();
} }
Expand Down
Expand Up @@ -18,9 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */


package org.sonar.server.batch; package org.sonar.server.computation.ws;


import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
Expand All @@ -30,26 +29,25 @@


import java.io.InputStream; import java.io.InputStream;


public class UploadReportAction implements RequestHandler { public class SubmitReportWsAction implements ComputationWsAction, RequestHandler {


public static final String UPLOAD_REPORT_ACTION = "upload_report"; public static final String ACTION = "submit_report";
public static final String PARAM_PROJECT_KEY = "projectKey";
public static final String PARAM_SNAPSHOT = "snapshot";
public static final String PARAM_REPORT_DATA = "report";


static final String PARAM_PROJECT_KEY = "project"; private final AnalysisReportQueue queue;
static final String PARAM_SNAPSHOT = "snapshot"; private final AnalysisReportTaskLauncher taskLauncher;
static final String PARAM_REPORT_DATA = "report";


private final AnalysisReportQueue analysisReportQueue; public SubmitReportWsAction(AnalysisReportQueue queue, AnalysisReportTaskLauncher taskLauncher) {
private final AnalysisReportTaskLauncher analysisTaskLauncher; this.queue = queue;

this.taskLauncher = taskLauncher;
public UploadReportAction(AnalysisReportQueue analysisReportQueue, AnalysisReportTaskLauncher analysisTaskLauncher) {
this.analysisReportQueue = analysisReportQueue;
this.analysisTaskLauncher = analysisTaskLauncher;
} }


void define(WebService.NewController controller) { @Override
WebService.NewAction action = controller.createAction(UPLOAD_REPORT_ACTION) public void define(WebService.NewController controller) {
.setDescription("Update analysis report") WebService.NewAction action = controller.createAction(ACTION)
.setSince("5.0") .setDescription("Submit an analysis report to the queue. Report is integrated asynchronously.")
.setPost(true) .setPost(true)
.setInternal(true) .setInternal(true)
.setHandler(this); .setHandler(this);
Expand All @@ -63,26 +61,22 @@ void define(WebService.NewController controller) {
action action
.createParam(PARAM_SNAPSHOT) .createParam(PARAM_SNAPSHOT)
.setRequired(true) .setRequired(true)
.setDescription("Snapshot id") .setDescription("Snapshot ID")
.setExampleValue("123"); .setExampleValue("123");


action action
.createParam(PARAM_REPORT_DATA) .createParam(PARAM_REPORT_DATA)
.setRequired(false) .setRequired(false)
.setDescription("Report file"); .setDescription("Report file. Format is not an API, it changes among SonarQube versions.");
} }


@Override @Override
public void handle(Request request, Response response) throws Exception { public void handle(Request request, Response response) throws Exception {
String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY); String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY);
String snapshotId = request.mandatoryParam(PARAM_SNAPSHOT); long snapshotId = request.mandatoryParamAsLong(PARAM_SNAPSHOT);
InputStream reportData = request.paramAsInputStream(PARAM_REPORT_DATA); try (InputStream reportData = request.paramAsInputStream(PARAM_REPORT_DATA)) {

queue.add(projectKey, snapshotId, reportData);
try { taskLauncher.startAnalysisTaskNow();
analysisReportQueue.add(projectKey, Long.valueOf(snapshotId), reportData);
analysisTaskLauncher.startAnalysisTaskNow();
} finally {
IOUtils.closeQuietly(reportData);
} }
} }
} }
Expand Up @@ -96,6 +96,7 @@
import org.sonar.server.computation.ws.HistoryWsAction; import org.sonar.server.computation.ws.HistoryWsAction;
import org.sonar.server.computation.ws.ComputationWebService; import org.sonar.server.computation.ws.ComputationWebService;
import org.sonar.server.computation.ws.IsQueueEmptyWsAction; import org.sonar.server.computation.ws.IsQueueEmptyWsAction;
import org.sonar.server.computation.ws.SubmitReportWsAction;
import org.sonar.server.config.ws.PropertiesWs; import org.sonar.server.config.ws.PropertiesWs;
import org.sonar.server.dashboard.db.DashboardDao; import org.sonar.server.dashboard.db.DashboardDao;
import org.sonar.server.dashboard.db.WidgetDao; import org.sonar.server.dashboard.db.WidgetDao;
Expand Down Expand Up @@ -355,7 +356,7 @@ void startLevel4Components(ComponentContainer pico) {
pico.addSingleton(GlobalReferentialsAction.class); pico.addSingleton(GlobalReferentialsAction.class);
pico.addSingleton(ProjectReferentialsAction.class); pico.addSingleton(ProjectReferentialsAction.class);
pico.addSingleton(ProjectReferentialsLoader.class); pico.addSingleton(ProjectReferentialsLoader.class);
pico.addSingleton(UploadReportAction.class); pico.addSingleton(SubmitReportWsAction.class);
pico.addSingleton(BatchWs.class); pico.addSingleton(BatchWs.class);


// update center // update center
Expand Down
Expand Up @@ -29,8 +29,6 @@
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.core.properties.PropertiesDao; import org.sonar.core.properties.PropertiesDao;
import org.sonar.server.computation.AnalysisReportQueue;
import org.sonar.server.computation.AnalysisReportTaskLauncher;
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
import org.sonar.server.ws.WsTester; import org.sonar.server.ws.WsTester;


Expand Down Expand Up @@ -59,8 +57,7 @@ public class BatchWsTest {
public void before() throws IOException { public void before() throws IOException {
tester = new WsTester(new BatchWs(batchIndex, tester = new WsTester(new BatchWs(batchIndex,
new GlobalReferentialsAction(mock(DbClient.class), mock(PropertiesDao.class)), new GlobalReferentialsAction(mock(DbClient.class), mock(PropertiesDao.class)),
new ProjectReferentialsAction(mock(ProjectReferentialsLoader.class)), new ProjectReferentialsAction(mock(ProjectReferentialsLoader.class))));
new UploadReportAction(mock(AnalysisReportQueue.class), mock(AnalysisReportTaskLauncher.class))));
} }


@Test @Test
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void setUp() throws Exception {
when(dbClient.openSession(false)).thenReturn(session); when(dbClient.openSession(false)).thenReturn(session);
when(dbClient.metricDao()).thenReturn(metricDao); when(dbClient.metricDao()).thenReturn(metricDao);


tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao), mock(ProjectReferentialsAction.class), mock(UploadReportAction.class))); tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, propertiesDao), mock(ProjectReferentialsAction.class)));
} }


@Test @Test
Expand Down
Expand Up @@ -44,7 +44,7 @@ public class ProjectReferentialsActionTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
tester = new WsTester(new BatchWs(mock(BatchIndex.class), mock(GlobalReferentialsAction.class), tester = new WsTester(new BatchWs(mock(BatchIndex.class), mock(GlobalReferentialsAction.class),
new ProjectReferentialsAction(projectReferentialsLoader), mock(UploadReportAction.class))); new ProjectReferentialsAction(projectReferentialsLoader)));
} }


@Test @Test
Expand Down

This file was deleted.

Expand Up @@ -18,24 +18,26 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */


package org.sonar.server.batch; package org.sonar.server.computation.ws;


import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.server.computation.AnalysisReportQueue; import org.sonar.server.computation.AnalysisReportQueue;
import org.sonar.server.computation.AnalysisReportTaskLauncher; import org.sonar.server.computation.AnalysisReportTaskLauncher;


import java.io.InputStream; import java.io.InputStream;


import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;


public class UploadReportActionTest { public class SubmitReportWsActionTest {


private static final String DEFAULT_PROJECT_KEY = "123456789-987654321"; private static final String DEFAULT_PROJECT_KEY = "123456789-987654321";
private UploadReportAction sut; private SubmitReportWsAction sut;


private AnalysisReportTaskLauncher analysisTaskLauncher; private AnalysisReportTaskLauncher analysisTaskLauncher;
private AnalysisReportQueue queue; private AnalysisReportQueue queue;
Expand All @@ -45,18 +47,30 @@ public void before() {
analysisTaskLauncher = mock(AnalysisReportTaskLauncher.class); analysisTaskLauncher = mock(AnalysisReportTaskLauncher.class);
queue = mock(AnalysisReportQueue.class); queue = mock(AnalysisReportQueue.class);


sut = new UploadReportAction(queue, analysisTaskLauncher); sut = new SubmitReportWsAction(queue, analysisTaskLauncher);
}

@Test
public void define_metadata() throws Exception {
WebService.Context context = new WebService.Context();
WebService.NewController controller = context.createController("api/computation");
sut.define(controller);
controller.done();

WebService.Action action = context.controller("api/computation").action("submit_report");
assertThat(action).isNotNull();
assertThat(action.params()).hasSize(3);
} }


@Test @Test
public void add_element_to_queue_and_launch_analysis_task() throws Exception { public void add_element_to_queue_and_launch_analysis_task() throws Exception {
Response response = mock(Response.class); Response response = mock(Response.class);
Request request = mock(Request.class); Request request = mock(Request.class);


when(request.mandatoryParam(UploadReportAction.PARAM_PROJECT_KEY)).thenReturn(DEFAULT_PROJECT_KEY); when(request.mandatoryParam(SubmitReportWsAction.PARAM_PROJECT_KEY)).thenReturn(DEFAULT_PROJECT_KEY);
when(request.mandatoryParam(UploadReportAction.PARAM_SNAPSHOT)).thenReturn("123"); when(request.mandatoryParamAsLong(SubmitReportWsAction.PARAM_SNAPSHOT)).thenReturn(123L);
InputStream reportData = IOUtils.toInputStream("report-data"); InputStream reportData = IOUtils.toInputStream("report-data");
when(request.paramAsInputStream(UploadReportAction.PARAM_REPORT_DATA)).thenReturn(reportData); when(request.paramAsInputStream(SubmitReportWsAction.PARAM_REPORT_DATA)).thenReturn(reportData);


sut.handle(request, response); sut.handle(request, response);


Expand Down
Expand Up @@ -102,7 +102,7 @@ void uploadMultiPartReport(File report) {
URL url; URL url;
try { try {
int snapshotId = resourceCache.get(project.getEffectiveKey()).snapshotId(); int snapshotId = resourceCache.get(project.getEffectiveKey()).snapshotId();
url = new URL(serverClient.getURL() + "/batch/upload_report?project=" + project.getEffectiveKey() + "&snapshot=" + snapshotId); url = new URL(serverClient.getURL() + "/api/analysis_reports/submit_report?projectKey=" + project.getEffectiveKey() + "&snapshot=" + snapshotId);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid URL", e); throw new IllegalArgumentException("Invalid URL", e);
} }
Expand Down

0 comments on commit 5e0ae3e

Please sign in to comment.