Browse Source

SONAR-7968 Create client for list_definitions WS

tags/6.1-RC1
Julien Lancelot 7 years ago
parent
commit
4beb2af88d

+ 54
- 22
server/sonar-server/src/main/java/org/sonar/server/settings/ws/ListDefinitionsAction.java View File

@@ -20,7 +20,7 @@
package org.sonar.server.settings.ws;

import java.util.List;
import javax.annotation.CheckForNull;
import java.util.Optional;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
@@ -28,33 +28,42 @@ import org.sonar.api.config.PropertyFieldDefinition;
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.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Settings;
import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
import org.sonarqube.ws.client.setting.ListDefinitionsRequest;

import static org.elasticsearch.common.Strings.isNullOrEmpty;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY;
import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;

public class ListDefinitionsAction implements SettingsWsAction {

private final DbClient dbClient;
private final SettingsWsComponentParameters settingsWsComponentParameters;
private final ComponentFinder componentFinder;
private final UserSession userSession;
private final PropertyDefinitions propertyDefinitions;

public ListDefinitionsAction(DbClient dbClient, SettingsWsComponentParameters settingsWsComponentParameters, PropertyDefinitions propertyDefinitions) {
public ListDefinitionsAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, PropertyDefinitions propertyDefinitions) {
this.dbClient = dbClient;
this.settingsWsComponentParameters = settingsWsComponentParameters;
this.componentFinder = componentFinder;
this.userSession = userSession;
this.propertyDefinitions = propertyDefinitions;
}

@Override
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("list_definitions")
WebService.NewAction action = context.createAction(ACTION_LIST_DEFINITIONS)
.setDescription(String.format("List settings definitions.<br>" +
"Either '%s' or '%s' can be provided, not both.<br> " +
"Requires one of the following permissions: " +
@@ -65,7 +74,6 @@ public class ListDefinitionsAction implements SettingsWsAction {
.setResponseExample(getClass().getResource("list_definitions-example.json"))
.setSince("6.1")
.setHandler(this);

addComponentParameters(action);
}

@@ -75,16 +83,52 @@ public class ListDefinitionsAction implements SettingsWsAction {
}

private ListDefinitionsWsResponse doHandle(Request request) {
String qualifier = getQualifier(request);
ListDefinitionsRequest wsRequest = toWsRequest(request);
Optional<String> qualifier = getQualifier(wsRequest);
ListDefinitionsWsResponse.Builder wsResponse = ListDefinitionsWsResponse.newBuilder();

propertyDefinitions.getAll().stream()
.filter(definition -> qualifier == null ? definition.global() : definition.qualifiers().contains(qualifier))
.filter(definition -> qualifier.isPresent() ? definition.qualifiers().contains(qualifier.get()) : definition.global())
.filter(definition -> !definition.type().equals(PropertyType.LICENSE))
.forEach(definition -> addDefinition(definition, wsResponse));
return wsResponse.build();
}

private static ListDefinitionsRequest toWsRequest(Request request) {
return ListDefinitionsRequest.builder()
.setComponentId(request.param(PARAM_COMPONENT_ID))
.setComponentKey(request.param(PARAM_COMPONENT_KEY))
.build();
}

private Optional<String> getQualifier(ListDefinitionsRequest wsRequest) {
DbSession dbSession = dbClient.openSession(false);
try {
Optional<ComponentDto> component = getComponent(dbSession, wsRequest);
checkAdminPermission(component);
return component.isPresent() ? Optional.of(component.get().qualifier()) : Optional.empty();
} finally {
dbClient.closeSession(dbSession);
}
}

private Optional<ComponentDto> getComponent(DbSession dbSession, ListDefinitionsRequest wsRequest) {
String componentId = wsRequest.getComponentId();
String componentKey = wsRequest.getComponentKey();
if (componentId != null || componentKey != null) {
return Optional.of(componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, ID_AND_KEY));
}
return Optional.empty();
}

private void checkAdminPermission(Optional<ComponentDto> component) {
if (component.isPresent()) {
userSession.checkComponentUuidPermission(UserRole.ADMIN, component.get().uuid());
} else {
userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
}
}

private void addDefinition(PropertyDefinition definition, ListDefinitionsWsResponse.Builder wsResponse) {
String key = definition.key();
Settings.Definition.Builder builder = wsResponse.addDefinitionsBuilder()
@@ -133,16 +177,4 @@ public class ListDefinitionsAction implements SettingsWsAction {
.build();
}

@CheckForNull
private String getQualifier(Request request) {
DbSession dbSession = dbClient.openSession(false);
try {
ComponentDto component = settingsWsComponentParameters.getComponent(dbSession, request);
settingsWsComponentParameters.checkAdminPermission(component);
return component == null ? null : component.qualifier();
} finally {
dbClient.closeSession(dbSession);
}
}

}

+ 3
- 1
server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWs.java View File

@@ -21,6 +21,8 @@ package org.sonar.server.settings.ws;

import org.sonar.api.server.ws.WebService;

import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;

public class SettingsWs implements WebService {

private final SettingsWsAction[] actions;
@@ -31,7 +33,7 @@ public class SettingsWs implements WebService {

@Override
public void define(Context context) {
NewController controller = context.createController("api/settings")
NewController controller = context.createController(CONTROLLER_SETTINGS)
.setDescription("Manage settings.")
.setSince("6.1");
for (SettingsWsAction action : actions) {

+ 4
- 35
server/sonar-server/src/main/java/org/sonar/server/settings/ws/SettingsWsComponentParameters.java View File

@@ -19,32 +19,17 @@
*/
package org.sonar.server.settings.ws;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.web.UserRole;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;

import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;

public class SettingsWsComponentParameters {

static final String PARAM_COMPONENT_ID = "componentId";
static final String PARAM_COMPONENT_KEY = "componentKey";

private final ComponentFinder componentFinder;
private final UserSession userSession;

public SettingsWsComponentParameters(ComponentFinder componentFinder, UserSession userSession) {
this.componentFinder = componentFinder;
this.userSession = userSession;
private SettingsWsComponentParameters() {
// Only static methods
}

static void addComponentParameters(WebService.NewAction action) {
@@ -57,20 +42,4 @@ public class SettingsWsComponentParameters {
.setExampleValue(KEY_PROJECT_EXAMPLE_001);
}

@CheckForNull
ComponentDto getComponent(DbSession dbSession, Request request) {
if (request.hasParam(PARAM_COMPONENT_ID) || request.hasParam(PARAM_COMPONENT_KEY)) {
return componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_COMPONENT_ID), request.param(PARAM_COMPONENT_KEY), ID_AND_KEY);
}
return null;
}

void checkAdminPermission(@Nullable ComponentDto component) {
if (component == null) {
userSession.checkPermission(GlobalPermissions.SYSTEM_ADMIN);
} else {
userSession.checkComponentUuidPermission(UserRole.ADMIN, component.uuid());
}
}

}

+ 2
- 2
server/sonar-server/src/main/java/org/sonar/server/settings/ws/ValuesAction.java View File

@@ -43,10 +43,10 @@ import org.sonarqube.ws.Settings.ValuesWsResponse;

import static org.elasticsearch.common.Strings.isNullOrEmpty;
import static org.sonar.api.PropertyType.PROPERTY_SET;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_ID;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.PARAM_COMPONENT_KEY;
import static org.sonar.server.settings.ws.SettingsWsComponentParameters.addComponentParameters;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;

public class ValuesAction implements SettingsWsAction {


+ 1
- 2
server/sonar-server/src/test/java/org/sonar/server/settings/ws/ListDefinitionsActionTest.java View File

@@ -79,9 +79,8 @@ public class ListDefinitionsActionTest {
ComponentDto project;

PropertyDefinitions propertyDefinitions = new PropertyDefinitions();
SettingsWsComponentParameters settingsWsComponentParameters = new SettingsWsComponentParameters(new ComponentFinder(dbClient), userSession);

WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, settingsWsComponentParameters, propertyDefinitions));
WsActionTester ws = new WsActionTester(new ListDefinitionsAction(dbClient, new ComponentFinder(dbClient), userSession, propertyDefinitions));

@Before
public void setUp() throws Exception {

+ 8
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java View File

@@ -29,6 +29,7 @@ import org.sonarqube.ws.client.projectlinks.ProjectLinksService;
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
import org.sonarqube.ws.client.rule.RulesService;
import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
import org.sonarqube.ws.client.usertoken.UserTokensService;

@@ -53,6 +54,7 @@ class DefaultWsClient implements WsClient {
private final RulesService rulesService;
private final ProjectsService projectsService;
private final ProjectLinksService projectLinksService;
private final SettingsService settingsService;

DefaultWsClient(WsConnector wsConnector) {
this.wsConnector = wsConnector;
@@ -68,6 +70,7 @@ class DefaultWsClient implements WsClient {
this.rulesService = new RulesService(wsConnector);
this.projectsService = new ProjectsService(wsConnector);
this.projectLinksService = new ProjectLinksService(wsConnector);
this.settingsService = new SettingsService(wsConnector);
}

@Override
@@ -134,4 +137,9 @@ class DefaultWsClient implements WsClient {
public ProjectLinksService projectLinks() {
return projectLinksService;
}

@Override
public SettingsService settingsService() {
return settingsService;
}
}

+ 6
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java View File

@@ -29,6 +29,7 @@ import org.sonarqube.ws.client.projectlinks.ProjectLinksService;
import org.sonarqube.ws.client.qualitygate.QualityGatesService;
import org.sonarqube.ws.client.qualityprofile.QualityProfilesService;
import org.sonarqube.ws.client.rule.RulesService;
import org.sonarqube.ws.client.setting.SettingsService;
import org.sonarqube.ws.client.system.SystemService;
import org.sonarqube.ws.client.usertoken.UserTokensService;

@@ -82,4 +83,9 @@ public interface WsClient {
* @since 6.1
*/
ProjectLinksService projectLinks();

/**
* @since 6.1
*/
SettingsService settingsService();
}

+ 73
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ListDefinitionsRequest.java View File

@@ -0,0 +1,73 @@
/*
* 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.sonarqube.ws.client.setting;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class ListDefinitionsRequest {

private final String componentId;
private final String componentKey;

private ListDefinitionsRequest(Builder builder) {
this.componentId = builder.componentId;
this.componentKey = builder.componentKey;
}

@CheckForNull
public String getComponentId() {
return componentId;
}

@CheckForNull
public String getComponentKey() {
return componentKey;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
private String componentId;
private String componentKey;

private Builder() {
// enforce factory method use
}

public Builder setComponentId(@Nullable String componentId) {
this.componentId = componentId;
return this;
}

public Builder setComponentKey(@Nullable String componentKey) {
this.componentKey = componentKey;
return this;
}

public ListDefinitionsRequest build() {
return new ListDefinitionsRequest(this);
}
}

}

+ 44
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java View File

@@ -0,0 +1,44 @@
/*
* 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.sonarqube.ws.client.setting;

import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
import org.sonarqube.ws.client.BaseService;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.WsConnector;

import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_KEY;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;

public class SettingsService extends BaseService {
public SettingsService(WsConnector wsConnector) {
super(wsConnector, CONTROLLER_SETTINGS);
}

public ListDefinitionsWsResponse listDefinitions(ListDefinitionsRequest request) {
GetRequest getRequest = new GetRequest(path(ACTION_LIST_DEFINITIONS))
.setParam(PARAM_COMPONENT_ID, request.getComponentId())
.setParam(PARAM_COMPONENT_KEY, request.getComponentKey());
return call(getRequest, ListDefinitionsWsResponse.parser());
}

}

+ 31
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsWsParameters.java View File

@@ -0,0 +1,31 @@
/*
* 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.sonarqube.ws.client.setting;

public class SettingsWsParameters {
public static final String CONTROLLER_SETTINGS = "api/settings";

public static final String ACTION_LIST_DEFINITIONS = "list_definitions";

public static final String PARAM_COMPONENT_ID = "componentId";
public static final String PARAM_COMPONENT_KEY = "componentKey";

}

+ 27
- 0
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/package-info.java View File

@@ -0,0 +1,27 @@
/*
* 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.
*/



@ParametersAreNonnullByDefault
package org.sonarqube.ws.client.setting;

import javax.annotation.ParametersAreNonnullByDefault;


+ 60
- 0
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ListDefinitionsRequestTest.java View File

@@ -0,0 +1,60 @@
/*
* 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.sonarqube.ws.client.setting;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Assertions.assertThat;

public class ListDefinitionsRequestTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

ListDefinitionsRequest.Builder underTest = ListDefinitionsRequest.builder();

@Test
public void create_request_with_no_component() {
ListDefinitionsRequest result = underTest.build();

assertThat(result.getComponentId()).isNull();
assertThat(result.getComponentKey()).isNull();
}

@Test
public void create_request_with_component_id() {
ListDefinitionsRequest result = underTest.setComponentId("projectId").build();

assertThat(result.getComponentId()).isEqualTo("projectId");
assertThat(result.getComponentKey()).isNull();
}

@Test
public void create_request_with_component_key() {
ListDefinitionsRequest result = underTest.setComponentKey("projectKey").build();

assertThat(result.getComponentId()).isNull();
assertThat(result.getComponentKey()).isEqualTo("projectKey");
}

}

+ 56
- 0
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java View File

@@ -0,0 +1,56 @@
/*
* 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.sonarqube.ws.client.setting;

import org.junit.Rule;
import org.junit.Test;
import org.sonarqube.ws.Settings;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.ServiceTester;
import org.sonarqube.ws.client.WsConnector;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;

public class SettingsServiceTest {

@Rule
public ServiceTester<SettingsService> serviceTester = new ServiceTester<>(new SettingsService(mock(WsConnector.class)));

private SettingsService underTest = serviceTester.getInstanceUnderTest();

@Test
public void list_definitions() {
ListDefinitionsRequest request = ListDefinitionsRequest.builder()
.setComponentKey("KEY")
.build();

underTest.listDefinitions(request);
GetRequest getRequest = serviceTester.getGetRequest();

assertThat(serviceTester.getGetParser()).isSameAs(Settings.ListDefinitionsWsResponse.parser());
serviceTester.assertThat(getRequest)
.hasParam(PARAM_COMPONENT_KEY, "KEY")
.andNoOtherParam();
}

}

Loading…
Cancel
Save