Browse Source

SONAR-7326 WS api/ce/queue removed

tags/5.5-M2
Teryk Bellahsene 8 years ago
parent
commit
8f75baf20d

+ 0
- 1
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeWsModule.java View File

@@ -29,7 +29,6 @@ public class CeWsModule extends Module {
ActivityAction.class,
CancelAction.class,
CancelAllAction.class,
QueueAction.class,
IsQueueEmptyWs.class,
LogsAction.class,
ComponentAction.class,

+ 0
- 94
server/sonar-server/src/main/java/org/sonar/server/computation/ws/QueueAction.java View File

@@ -1,94 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.ws;

import java.util.List;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsUtils;
import org.sonarqube.ws.WsCe;

public class QueueAction implements CeWsAction {

public static final String PARAM_COMPONENT_UUID = "componentId";

private final UserSession userSession;
private final DbClient dbClient;
private final TaskFormatter formatter;

public QueueAction(UserSession userSession, DbClient dbClient, TaskFormatter formatter) {
this.userSession = userSession;
this.dbClient = dbClient;
this.formatter = formatter;
}

@Override
public void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction("queue")
.setDescription("Gets the pending and in-progress tasks. Requires system administration permission.")
.setInternal(true)
.setSince("5.2")
.setResponseExample(getClass().getResource("queue-example.json"))
.setHandler(this);

action
.createParam(PARAM_COMPONENT_UUID)
.setDescription("Optional filter on component. Requires administration permission of the component.")
.setExampleValue(Uuids.UUID_EXAMPLE_01);
}

@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
String componentUuid = wsRequest.param(PARAM_COMPONENT_UUID);

DbSession dbSession = dbClient.openSession(false);
try {
List<CeQueueDto> dtos;
if (componentUuid == null) {
// no filters
userSession.checkPermission(UserRole.ADMIN);
dtos = dbClient.ceQueueDao().selectAllInAscOrder(dbSession);
} else {
// filter by component
if (userSession.hasPermission(GlobalPermissions.SYSTEM_ADMIN) || userSession.hasComponentUuidPermission(UserRole.ADMIN, componentUuid)) {
dtos = dbClient.ceQueueDao().selectByComponentUuid(dbSession, componentUuid);
} else {
throw new ForbiddenException("Requires system administration permission");
}
}

WsCe.QueueResponse.Builder wsResponseBuilder = WsCe.QueueResponse.newBuilder();
wsResponseBuilder.addAllTasks(formatter.formatQueue(dbSession, dtos));
WsUtils.writeProtobuf(wsResponseBuilder.build(), wsRequest, wsResponse);

} finally {
dbClient.closeSession(dbSession);
}
}
}

+ 0
- 28
server/sonar-server/src/main/resources/org/sonar/server/computation/ws/queue-example.json View File

@@ -1,28 +0,0 @@
{
"tasks": [
{
"id": "BU_dO1vsORa8_beWCwsP",
"type": "REPORT",
"componentId": "AU-Tpxb--iU5OvuD2FLy",
"componentKey": "project_1",
"componentName": "Project One",
"componentQualifier": "TRK",
"status": "IN_PROGRESS",
"submittedAt": "2015-08-13T23:34:59+0200",
"submitterLogin": "john",
"logs": true
},
{
"id": "AU_dO1vsORa8_beWCwmP",
"taskType": "REPORT",
"componentId": "AU_dO1vlORa8_beWCwmO",
"componentKey": "project_2",
"componentName": "Project Two",
"componentQualifier": "TRK",
"status": "PENDING",
"submittedAt": "2015-09-17T23:34:59+0200",
"startedAt": "2015-09-17T23:35:00+0200",
"logs": true
}
]
}

+ 1
- 1
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeWsModuleTest.java View File

@@ -30,6 +30,6 @@ public class CeWsModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new CeWsModule().configure(container);
assertThat(container.size()).isEqualTo(12 + 2 /* injected by ComponentContainer */);
assertThat(container.size()).isEqualTo(11 + 2 /* injected by ComponentContainer */);
}
}

+ 0
- 121
server/sonar-server/src/test/java/org/sonar/server/computation/ws/QueueActionTest.java View File

@@ -1,121 +0,0 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.ws;

import com.google.common.base.Optional;
import java.io.File;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.core.util.Protobuf;
import org.sonar.db.DbTester;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.server.computation.log.CeLogging;
import org.sonar.server.computation.log.LogFileRef;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonarqube.ws.MediaTypes;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.WsCe;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class QueueActionTest {

@Rule
public UserSessionRule userSession = UserSessionRule.standalone();

@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);

CeLogging ceLogging = mock(CeLogging.class);
TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), ceLogging, System2.INSTANCE);
QueueAction underTest = new QueueAction(userSession, dbTester.getDbClient(), formatter);
WsActionTester tester = new WsActionTester(underTest);

@Before
public void setUp() {
when(ceLogging.getFile(any(LogFileRef.class))).thenReturn(Optional.<File>absent());
}

@Test
public void get_all_queue() {
userSession.setGlobalPermissions(UserRole.ADMIN);
insert("T1", "PROJECT_1", CeQueueDto.Status.PENDING);
insert("T2", "PROJECT_2", CeQueueDto.Status.IN_PROGRESS);

TestResponse wsResponse = tester.newRequest()
.setMediaType(MediaTypes.PROTOBUF)
.execute();

// verify the protobuf response
WsCe.QueueResponse queueResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.QueueResponse.PARSER);
assertThat(queueResponse.getTasksCount()).isEqualTo(2);
assertThat(queueResponse.getTasks(0).getId()).isEqualTo("T1");
assertThat(queueResponse.getTasks(0).getStatus()).isEqualTo(WsCe.TaskStatus.PENDING);
assertThat(queueResponse.getTasks(0).getComponentId()).isEqualTo("PROJECT_1");
assertThat(queueResponse.getTasks(1).getId()).isEqualTo("T2");
assertThat(queueResponse.getTasks(1).getStatus()).isEqualTo(WsCe.TaskStatus.IN_PROGRESS);
assertThat(queueResponse.getTasks(1).getComponentId()).isEqualTo("PROJECT_2");
}

@Test
public void get_queue_of_project() {
userSession.addComponentUuidPermission(UserRole.ADMIN, "PROJECT_1", "PROJECT_1");
insert("T1", "PROJECT_1", CeQueueDto.Status.PENDING);
insert("T2", "PROJECT_2", CeQueueDto.Status.PENDING);
insert("T3", "PROJECT_2", CeQueueDto.Status.IN_PROGRESS);

TestResponse wsResponse = tester.newRequest()
.setParam("componentId", "PROJECT_1")
.setMediaType(MediaTypes.PROTOBUF)
.execute();

// verify the protobuf response
WsCe.QueueResponse queueResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.QueueResponse.PARSER);
assertThat(queueResponse.getTasksCount()).isEqualTo(1);
assertThat(queueResponse.getTasks(0).getId()).isEqualTo("T1");
}

@Test(expected = ForbiddenException.class)
public void requires_admin_permission() {
tester.newRequest()
.setMediaType(MediaTypes.PROTOBUF)
.execute();
}

private CeQueueDto insert(String taskUuid, String componentUuid, CeQueueDto.Status status) {
CeQueueDto queueDto = new CeQueueDto();
queueDto.setTaskType(CeTaskTypes.REPORT);
queueDto.setComponentUuid(componentUuid);
queueDto.setUuid(taskUuid);
queueDto.setStatus(status);
dbTester.getDbClient().ceQueueDao().insert(dbTester.getSession(), queueDto);
dbTester.getSession().commit();
return queueDto;
}
}

+ 0
- 5
sonar-ws/src/main/protobuf/ws-ce.proto View File

@@ -37,11 +37,6 @@ message TaskResponse {
optional Task task = 1;
}

// GET api/ce/queue
message QueueResponse {
repeated Task tasks = 1;
}

// GET api/ce/activity
message ActivityResponse {
optional sonarqube.ws.commons.Paging paging = 1;

Loading…
Cancel
Save