From b5c9fcb1cb02a7f574f74277c6491fcbfbdc9c8d Mon Sep 17 00:00:00 2001 From: Michal Duda Date: Tue, 23 Feb 2021 14:23:31 +0100 Subject: [PATCH] SONAR-13848 remove deprecations from api/ce/activity_status --- .../server/ce/ws/ActivityStatusAction.java | 22 +++--- .../sonar/server/ce/ws/CeWsParameters.java | 1 - .../ce/ws/ActivityStatusActionTest.java | 67 +++++-------------- 3 files changed, 30 insertions(+), 60 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java index 275a445e918..6828b1d9ad2 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java @@ -38,7 +38,7 @@ import org.sonar.server.user.UserSession; import org.sonar.server.ws.KeyExamples; import org.sonarqube.ws.Ce.ActivityStatusWsResponse; -import static org.sonar.server.ce.ws.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; +import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT; import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID; import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -67,15 +67,17 @@ public class ActivityStatusAction implements CeWsAction { .setHandler(this); action.createParam(PARAM_COMPONENT_ID) + .setDeprecatedSince("8.8") .setDescription("Id of the component (project) to filter on") .setExampleValue(Uuids.UUID_EXAMPLE_03); - action.createParam(DEPRECATED_PARAM_COMPONENT_KEY) - .setDeprecatedSince("6.6") + action.createParam(PARAM_COMPONENT) .setDescription("Key of the component (project) to filter on") .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001); action.setChangelog(new Change("6.6", "New field 'inProgress' in response")); action.setChangelog(new Change("7.8", "New field 'pendingTime' in response, only included when there are pending tasks")); + action.setChangelog(new Change("8.8", "Parameter 'componentId' is now deprecated.")); + action.setChangelog(new Change("8.8", "Parameter 'componentKey' is now removed. Please use parameter 'component' instead.")); } @Override @@ -88,7 +90,7 @@ public class ActivityStatusAction implements CeWsAction { try (DbSession dbSession = dbClient.openSession(false)) { Optional component = searchComponent(dbSession, request); String componentUuid = component.map(ComponentDto::uuid).orElse(null); - checkPermissions(component); + checkPermissions(component.orElse(null)); int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid); int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid); int failingCount = dbClient.ceActivityDao().countLastByStatusAndMainComponentUuid(dbSession, CeActivityDto.Status.FAILED, componentUuid); @@ -117,9 +119,9 @@ public class ActivityStatusAction implements CeWsAction { return Optional.ofNullable(component); } - private void checkPermissions(Optional component) { - if (component.isPresent()) { - userSession.checkComponentPermission(UserRole.ADMIN, component.get()); + private void checkPermissions(@Nullable ComponentDto component) { + if (component != null) { + userSession.checkComponentPermission(UserRole.ADMIN, component); } else { userSession.checkIsSystemAdministrator(); } @@ -130,12 +132,12 @@ public class ActivityStatusAction implements CeWsAction { } private static Request toWsRequest(org.sonar.api.server.ws.Request request) { - return new Request(request.param(PARAM_COMPONENT_ID), request.param(DEPRECATED_PARAM_COMPONENT_KEY)); + return new Request(request.param(PARAM_COMPONENT_ID), request.param(PARAM_COMPONENT)); } private static class Request { - private String componentId; - private String componentKey; + private final String componentId; + private final String componentKey; Request(@Nullable String componentId, @Nullable String componentKey) { this.componentId = componentId; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CeWsParameters.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CeWsParameters.java index 4f8a21ddcaf..46bd654b9d1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CeWsParameters.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CeWsParameters.java @@ -24,7 +24,6 @@ public class CeWsParameters { public static final String ACTION_WORKER_COUNT = "worker_count"; public static final String PARAM_COMPONENT_ID = "componentId"; - public static final String DEPRECATED_PARAM_COMPONENT_KEY = "componentKey"; public static final String PARAM_COMPONENT = "component"; public static final String PARAM_TYPE = "type"; public static final String PARAM_STATUS = "status"; diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java index 4f69dd13368..ce1560fffeb 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java @@ -32,7 +32,6 @@ import org.sonar.db.DbTester; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -47,8 +46,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.db.ce.CeQueueTesting.newCeQueueDto; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; -import static org.sonar.server.ce.ws.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; -import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID; +import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT; import static org.sonar.test.JsonAssert.assertJson; public class ActivityStatusActionTest { @@ -58,11 +56,10 @@ public class ActivityStatusActionTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - private System2 system2 = mock(System2.class); - - private DbClient dbClient = db.getDbClient(); - private DbSession dbSession = db.getSession(); - private WsActionTester ws = new WsActionTester(new ActivityStatusAction(userSession, dbClient, TestComponentFinder.from(db), system2)); + private final System2 system2 = mock(System2.class); + private final DbClient dbClient = db.getDbClient(); + private final DbSession dbSession = db.getSession(); + private final WsActionTester ws = new WsActionTester(new ActivityStatusAction(userSession, dbClient, TestComponentFinder.from(db), system2)); @Test public void test_definition() { @@ -70,7 +67,7 @@ public class ActivityStatusActionTest { assertThat(def.key()).isEqualTo("activity_status"); assertThat(def.isInternal()).isFalse(); assertThat(def.isPost()).isFalse(); - assertThat(def.params()).extracting(WebService.Param::key).containsOnly("componentId", "componentKey"); + assertThat(def.params()).extracting(WebService.Param::key).containsOnly("componentId", "component"); } @Test @@ -92,12 +89,12 @@ public class ActivityStatusActionTest { @Test public void status_for_a_project_as_project_admin() { - String projectUuid = "project-uuid"; - String anotherProjectUuid = "another-project-uuid"; - ComponentDto project = newPrivateProjectDto(projectUuid); - ComponentDto anotherProject = newPrivateProjectDto(anotherProjectUuid); + String projectKey = "project-key"; + String anotherProjectKey = "another-project-key"; + ComponentDto project = newPrivateProjectDto().setDbKey(projectKey); + ComponentDto anotherProject = newPrivateProjectDto().setDbKey(anotherProjectKey); db.components().insertComponent(project); - db.components().insertComponent(newPrivateProjectDto(anotherProjectUuid)); + db.components().insertComponent(newPrivateProjectDto().setDbKey(anotherProjectKey)); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); // pending tasks returned insertInQueue(CeQueueDto.Status.PENDING, project); @@ -113,7 +110,7 @@ public class ActivityStatusActionTest { insertActivity(CeActivityDto.Status.FAILED, project); insertActivity(CeActivityDto.Status.FAILED, anotherProject); - Ce.ActivityStatusWsResponse result = call(projectUuid); + Ce.ActivityStatusWsResponse result = callByComponentKey(projectKey); assertThat(result.getPending()).isEqualTo(2); assertThat(result.getFailing()).isEqualTo(1); @@ -121,14 +118,14 @@ public class ActivityStatusActionTest { @Test public void add_pending_time() { - String projectUuid = "project-uuid"; - ComponentDto project = newPrivateProjectDto(projectUuid); + String projectKey = "project-key"; + ComponentDto project = newPrivateProjectDto().setDbKey(projectKey); db.components().insertComponent(project); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); when(system2.now()).thenReturn(2000L); insertInQueue(CeQueueDto.Status.PENDING, project, 1000L); - Ce.ActivityStatusWsResponse result = call(projectUuid); + Ce.ActivityStatusWsResponse result = callByComponentKey(projectKey); assertThat(result).extracting(Ce.ActivityStatusWsResponse::getPending, Ce.ActivityStatusWsResponse::getFailing, Ce.ActivityStatusWsResponse::getInProgress, Ce.ActivityStatusWsResponse::getPendingTime) @@ -143,23 +140,6 @@ public class ActivityStatusActionTest { assertThat(result.getFailing()).isZero(); } - @Test - public void fail_if_component_uuid_and_key_are_provided() { - ComponentDto project = ComponentTesting.newPrivateProjectDto(); - db.components().insertComponent(project); - - String uuid = project.uuid(); - String dbKey = project.getDbKey(); - assertThatThrownBy(() -> callByComponentUuidOrComponentKey(uuid, dbKey)) - .isInstanceOf(IllegalArgumentException.class); - } - - @Test - public void fail_if_component_uuid_is_unknown() { - assertThatThrownBy(() -> call("unknown-uuid")) - .isInstanceOf(NotFoundException.class); - } - @Test public void fail_if_component_key_is_unknown() { assertThatThrownBy(() -> callByComponentKey("unknown-key")) @@ -210,24 +190,13 @@ public class ActivityStatusActionTest { } private Ce.ActivityStatusWsResponse call() { - return callByComponentUuidOrComponentKey(null, null); - } - - private Ce.ActivityStatusWsResponse call(String componentUuid) { - return callByComponentUuidOrComponentKey(componentUuid, null); - } - - private Ce.ActivityStatusWsResponse callByComponentKey(String componentKey) { - return callByComponentUuidOrComponentKey(null, componentKey); + return callByComponentKey(null); } - private Ce.ActivityStatusWsResponse callByComponentUuidOrComponentKey(@Nullable String componentUuid, @Nullable String componentKey) { + private Ce.ActivityStatusWsResponse callByComponentKey(@Nullable String componentKey) { TestRequest request = ws.newRequest(); - if (componentUuid != null) { - request.setParam(PARAM_COMPONENT_ID, componentUuid); - } if (componentKey != null) { - request.setParam(DEPRECATED_PARAM_COMPONENT_KEY, componentKey); + request.setParam(PARAM_COMPONENT, componentKey); } return request.executeProtobuf(Ce.ActivityStatusWsResponse.class); } -- 2.39.5