@Override
protected void configureModule() {
add(
+ ProjectsWsSupport.class,
ProjectsWs.class,
CreateAction.class,
IndexAction.class,
--- /dev/null
+/*
+ * 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.project.ws;
+
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+
+import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+
+public class ProjectsWsSupport {
+ static final String PARAM_ORGANIZATION = "organization";
+
+ private final DbClient dbClient;
+
+ public ProjectsWsSupport(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ void addOrganizationParam(WebService.NewAction action) {
+ action.createParam(PARAM_ORGANIZATION)
+ .setDescription("The key of the organization")
+ .setRequired(false)
+ .setInternal(true)
+ .setSince("6.3");
+ }
+
+ OrganizationDto getOrganization(DbSession dbSession, String organizationKey) {
+ return checkFoundWithOptional(
+ dbClient.organizationDao().selectByKey(dbSession, organizationKey),
+ "No organization for key '%s'", organizationKey);
+ }
+}
import static com.google.common.collect.Sets.newHashSet;
import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
-import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+import static org.sonar.server.project.ws.ProjectsWsSupport.PARAM_ORGANIZATION;
public class ProvisionedAction implements ProjectsWsAction {
- private static final String PARAM_ORGANIZATION = "organization";
private static final Set<String> QUALIFIERS_FILTER = newHashSet(Qualifiers.PROJECT);
private static final Set<String> POSSIBLE_FIELDS = newHashSet("uuid", "key", "name", "creationDate");
+ private final ProjectsWsSupport support;
private final DbClient dbClient;
private final UserSession userSession;
private final DefaultOrganizationProvider defaultOrganizationProvider;
- public ProvisionedAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+ public ProvisionedAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, DefaultOrganizationProvider defaultOrganizationProvider) {
+ this.support = support;
this.dbClient = dbClient;
this.userSession = userSession;
this.defaultOrganizationProvider = defaultOrganizationProvider;
.addSearchQuery("sonar", "names", "keys")
.addFieldsParam(POSSIBLE_FIELDS);
- action.createParam(PARAM_ORGANIZATION)
- .setDescription("The key of the organization")
- .setRequired(false)
- .setInternal(true)
- .setSince("6.3");
+ support.addOrganizationParam(action);
}
@Override
String query = request.param(Param.TEXT_QUERY);
try (DbSession dbSession = dbClient.openSession(false)) {
- OrganizationDto organization = getOrganization(dbSession, request);
+ OrganizationDto organization = support.getOrganization(dbSession,
+ request.getParam(PARAM_ORGANIZATION).or(defaultOrganizationProvider.get()::getKey));
userSession.checkOrganizationPermission(organization.getUuid(), GlobalPermissions.PROVISIONING);
RowBounds rowBounds = new RowBounds(options.getOffset(), options.getLimit());
}
}
- private OrganizationDto getOrganization(DbSession dbSession, Request request) {
- String organizationKey = request.getParam(PARAM_ORGANIZATION)
- .or(defaultOrganizationProvider.get()::getKey);
- return checkFoundWithOptional(
- dbClient.organizationDao().selectByKey(dbSession, organizationKey),
- "No organization for key '%s'", organizationKey);
- }
-
private static void writeProjects(List<ComponentDto> projects, JsonWriter json, Set<String> desiredFields) {
json.name("projects");
json.beginArray();
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
-import org.sonar.db.component.SnapshotDto;
import org.sonar.db.component.SnapshotTesting;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.exceptions.ForbiddenException;
return project;
}
- private void insertNewActiveProject(String id) {
- ComponentDto project = ComponentTesting
- .newProjectDto(db.organizations().insert(), "analyzed-uuid-" + id)
- .setName("analyzed-name-" + id)
- .setKey("analyzed-key-" + id);
- dbClient.componentDao().insert(db.getSession(), project);
- SnapshotDto snapshot = SnapshotTesting.newAnalysis(project);
- dbClient.snapshotDao().insert(db.getSession(), snapshot);
- db.getSession().commit();
- }
}
import org.sonar.core.platform.ComponentContainer;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER;
public class ProjectsWsModuleTest {
@Test
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new ProjectsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(2 + 9);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 10);
}
}
private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private DbClient dbClient = db.getDbClient();
- private WsActionTester underTest = new WsActionTester(new ProvisionedAction(dbClient, userSessionRule, defaultOrganizationProvider));
+ private WsActionTester underTest = new WsActionTester(new ProvisionedAction(new ProjectsWsSupport(dbClient), dbClient, userSessionRule, defaultOrganizationProvider));
@Test
public void verify_definition() {