aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2019-08-21 11:03:51 -0500
committerSonarTech <sonartech@sonarsource.com>2019-09-24 20:21:14 +0200
commit01108373a26f91011a11f3917f619af71b28bedb (patch)
tree6ebd82e509f6b5e0c07f5b0f14d590b0d9d2bb61 /server/sonar-server/src
parentdd1fcb210381b1f72f9519ca39c427b64f1b67f9 (diff)
downloadsonarqube-01108373a26f91011a11f3917f619af71b28bedb.tar.gz
sonarqube-01108373a26f91011a11f3917f619af71b28bedb.zip
SONAR-12366 New Code Period WebServices
Diffstat (limited to 'server/sonar-server/src')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java152
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWs.java42
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsAction.java26
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModule.java34
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java222
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java181
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java121
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/package-info.java23
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/setting/ws/list_new_code_period-example.json0
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/setting/ws/show_new_code_period-example.json0
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ListActionTest.java319
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java36
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java378
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java247
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java229
15 files changed, 0 insertions, 2010 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java
deleted file mode 100644
index 5e0c15df75c..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import java.util.Collection;
-import java.util.Map;
-import java.util.stream.Collectors;
-import javax.annotation.Nullable;
-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.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.user.UserSession;
-import org.sonarqube.ws.NewCodePeriods;
-import org.sonarqube.ws.NewCodePeriods.ListWSResponse;
-
-import static org.sonar.core.util.stream.MoreCollectors.toList;
-import static org.sonar.db.component.BranchType.LONG;
-import static org.sonar.server.ws.WsUtils.writeProtobuf;
-import static org.sonarqube.ws.NewCodePeriods.ShowWSResponse.newBuilder;
-
-public class ListAction implements NewCodePeriodsWsAction {
- private static final String PARAM_PROJECT = "project";
-
- private final DbClient dbClient;
- private final UserSession userSession;
- private final ComponentFinder componentFinder;
- private final NewCodePeriodDao newCodePeriodDao;
-
- public ListAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- this.componentFinder = componentFinder;
- this.newCodePeriodDao = newCodePeriodDao;
- }
-
- @Override
- public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("list")
- .setDescription("List the New Code Periods for all long lived branches in a project.<br>" +
- "Requires the permission to browse the project")
- .setSince("8.0")
- .setResponseExample(getClass().getResource("list-example.json"))
- .setHandler(this);
-
- action.createParam(PARAM_PROJECT)
- .setRequired(true)
- .setDescription("Project key");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- String projectKey = request.mandatoryParam(PARAM_PROJECT);
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
- userSession.checkComponentPermission(UserRole.ADMIN, project);
- Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project).stream()
- .filter(b -> b.getBranchType() == LONG)
- .collect(toList());
- Map<String, InheritedNewCodePeriod> newCodePeriodByBranchUuid = newCodePeriodDao
- .selectAllByProject(dbSession, project.uuid())
- .stream()
- .collect(Collectors.toMap(NewCodePeriodDto::getBranchUuid, dto -> new InheritedNewCodePeriod(dto, dto.getBranchUuid() == null)));
-
- InheritedNewCodePeriod projectDefault = newCodePeriodByBranchUuid.getOrDefault(null,
- newCodePeriodDao.selectGlobal(dbSession)
- .map(dto -> new InheritedNewCodePeriod(dto, true))
- .orElse(new InheritedNewCodePeriod(NewCodePeriodDto.defaultInstance(), true))
- );
-
- ListWSResponse.Builder builder = ListWSResponse.newBuilder();
- for (BranchDto branch : branches) {
- InheritedNewCodePeriod inherited = newCodePeriodByBranchUuid.getOrDefault(branch.getUuid(), projectDefault);
- builder.addNewCodePeriods(
- build(projectKey, branch.getKey(), inherited.getType(), inherited.getValue(), inherited.inherited));
- }
-
- writeProtobuf(builder.build(), request, response);
- }
- }
-
- private NewCodePeriods.ShowWSResponse build(String projectKey, String branchKey, NewCodePeriodType newCodePeriodType, @Nullable String value, boolean inherited) {
- NewCodePeriods.ShowWSResponse.Builder builder = newBuilder()
- .setType(convertType(newCodePeriodType))
- .setInherited(inherited)
- .setBranchKey(branchKey)
- .setProjectKey(projectKey);
-
- if (value != null) {
- builder.setValue(value);
- }
-
- return builder.build();
- }
-
- private static NewCodePeriods.NewCodePeriodType convertType(NewCodePeriodType type) {
- switch (type) {
- case NUMBER_OF_DAYS:
- return NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS;
- case PREVIOUS_VERSION:
- return NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION;
- case SPECIFIC_ANALYSIS:
- return NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS;
- default:
- throw new IllegalStateException("Unexpected type: " + type);
- }
- }
-
- private static class InheritedNewCodePeriod {
- NewCodePeriodDto newCodePeriod;
- boolean inherited;
-
- InheritedNewCodePeriod(NewCodePeriodDto newCodePeriod, boolean inherited) {
- this.newCodePeriod = newCodePeriod;
- this.inherited = inherited;
- }
-
- NewCodePeriodType getType() {
- return newCodePeriod.getType();
- }
-
- String getValue() {
- return newCodePeriod.getValue();
- }
- }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWs.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWs.java
deleted file mode 100644
index 3cb959ade82..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWs.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import org.sonar.api.server.ws.WebService;
-
-public class NewCodePeriodsWs implements WebService {
-
- private final NewCodePeriodsWsAction[] actions;
-
- public NewCodePeriodsWs(NewCodePeriodsWsAction... actions) {
- this.actions = actions;
- }
-
- @Override
- public void define(Context context) {
- NewController controller = context.createController("api/new_code_periods")
- .setDescription("Manage new code periods.")
- .setSince("8.0");
- for (NewCodePeriodsWsAction action : actions) {
- action.define(controller);
- }
- controller.done();
- }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsAction.java
deleted file mode 100644
index 09b614abf72..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsAction.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import org.sonar.server.ws.WsAction;
-
-public interface NewCodePeriodsWsAction extends WsAction {
- // marker interface
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModule.java
deleted file mode 100644
index 6913a4ec81e..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModule.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import org.sonar.core.platform.Module;
-
-public class NewCodePeriodsWsModule extends Module {
- @Override
- protected void configureModule() {
- add(
- NewCodePeriodsWs.class,
- ShowAction.class,
- SetAction.class,
- UnsetAction.class,
- ListAction.class);
- }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
deleted file mode 100644
index 796c929fd23..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java
+++ /dev/null
@@ -1,222 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import com.google.common.base.Preconditions;
-import java.util.EnumSet;
-import java.util.Locale;
-import java.util.Set;
-import javax.annotation.Nullable;
-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.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.SnapshotDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodParser;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
-import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
-import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
-import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
-import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
-
-public class SetAction implements NewCodePeriodsWsAction {
- private static final String PARAM_BRANCH = "branch";
- private static final String PARAM_PROJECT = "project";
- private static final String PARAM_TYPE = "type";
- private static final String PARAM_VALUE = "value";
- private static final Set<NewCodePeriodType> OVERALL_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS);
- private static final Set<NewCodePeriodType> PROJECT_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS);
- private static final Set<NewCodePeriodType> BRANCH_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, SPECIFIC_ANALYSIS);
-
- private final DbClient dbClient;
- private final UserSession userSession;
- private final ComponentFinder componentFinder;
- private final NewCodePeriodDao newCodePeriodDao;
-
- public SetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- this.componentFinder = componentFinder;
- this.newCodePeriodDao = newCodePeriodDao;
- }
-
- @Override
- public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("set")
- .setPost(true)
- .setDescription("Sets the setting for the New Code Period.<br>" +
- "Requires one of the following permissions: " +
- "<ul>" +
- "<li>'Administer System' to change the global setting</li>" +
- "<li>'Administer' rights for a specified component</li>" +
- "</ul>")
- .setSince("8.0")
- .setHandler(this);
-
- action.createParam(PARAM_PROJECT)
- .setDescription("Project key");
- action.createParam(PARAM_BRANCH)
- .setDescription("Branch key");
- action.createParam(PARAM_TYPE)
- .setRequired(true)
- .setDescription("Type");
- action.createParam(PARAM_VALUE)
- .setDescription("Value");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- String projectStr = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);
- String branchStr = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);
-
- if (projectStr == null && branchStr != null) {
- throw new IllegalArgumentException("If branch key is specified, project key needs to be specified too");
- }
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- String typeStr = request.mandatoryParam(PARAM_TYPE);
- String valueStr = request.getParam(PARAM_VALUE).emptyAsNull().or(() -> null);
- NewCodePeriodType type = validateType(typeStr, projectStr == null, branchStr != null);
-
- NewCodePeriodDto dto = new NewCodePeriodDto();
- dto.setType(type);
-
- ComponentDto projectBranch = null;
- if (projectStr != null) {
- projectBranch = getProject(dbSession, projectStr, branchStr);
- userSession.checkComponentPermission(UserRole.ADMIN, projectBranch);
- if (branchStr != null) {
- dto.setBranchUuid(projectBranch.uuid());
- }
- // depending whether it's the main branch or not
- dto.setProjectUuid(projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid());
- } else {
- userSession.checkIsSystemAdministrator();
- }
-
- setValue(dbSession, dto, type, projectBranch, branchStr, valueStr);
-
- newCodePeriodDao.upsert(dbSession, dto);
- dbSession.commit();
- }
- }
-
- private void setValue(DbSession dbSession, NewCodePeriodDto dto, NewCodePeriodType type, @Nullable ComponentDto projectBranch,
- @Nullable String branch, @Nullable String value) {
- switch (type) {
- case PREVIOUS_VERSION:
- Preconditions.checkArgument(value == null, "Unexpected value for type '%s'", type);
- break;
- case NUMBER_OF_DAYS:
- requireValue(type, value);
- try {
- dto.setValue(Integer.toString(NewCodePeriodParser.parseDays(value)));
- } catch (Exception e) {
- throw new IllegalArgumentException("Failed to parse number of days: " + value);
- }
- break;
- case SPECIFIC_ANALYSIS:
- requireValue(type, value);
- SnapshotDto analysis = getAnalysis(dbSession, value, projectBranch, branch);
- dto.setValue(analysis.getUuid());
- break;
- default:
- throw new IllegalStateException("Unexpected type: " + type);
- }
- }
-
- private void requireValue(NewCodePeriodType type, @Nullable String value) {
- Preconditions.checkArgument(value != null, "New Code Period type '%s' requires a value", type);
- }
-
- private ComponentDto getProject(DbSession dbSession, String projectKey, @Nullable String branchKey) {
- if (branchKey == null) {
- return componentFinder.getByUuidOrKey(dbSession, null, projectKey, PROJECT_ID_AND_KEY);
- }
- ComponentDto project = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey);
-
- BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, project.uuid())
- .orElseThrow(() -> new NotFoundException(format("Branch '%s' is not found", branchKey)));
-
- checkArgument(branchDto.getBranchType() == BranchType.LONG,
- "Not a long-living branch: '%s'", branchKey);
-
- return project;
- }
-
- private NewCodePeriodType validateType(String typeStr, boolean isOverall, boolean isBranch) {
- NewCodePeriodType type;
- try {
- type = NewCodePeriodType.valueOf(typeStr.toUpperCase(Locale.US));
- } catch (IllegalArgumentException e) {
- throw new IllegalArgumentException("Invalid type: " + typeStr);
- }
-
- if (isOverall) {
- checkType("Overall setting", OVERALL_TYPES, type);
- } else if (isBranch) {
- checkType("Branches", BRANCH_TYPES, type);
- } else {
- checkType("Projects", PROJECT_TYPES, type);
- }
- return type;
- }
-
- private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid, ComponentDto projectBranch, @Nullable String branchKey) {
- SnapshotDto snapshotDto = dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid)
- .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", analysisUuid)));
- checkAnalysis(dbSession, projectBranch, branchKey, snapshotDto);
- return snapshotDto;
- }
-
- private void checkAnalysis(DbSession dbSession, ComponentDto projectBranch, @Nullable String branchKey, SnapshotDto analysis) {
- ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, analysis.getComponentUuid()).orElse(null);
-
- boolean analysisMatchesProjectBranch = project != null && projectBranch.uuid().equals(project.uuid());
- if (branchKey != null) {
- checkArgument(analysisMatchesProjectBranch,
- "Analysis '%s' does not belong to branch '%s' of project '%s'",
- analysis.getUuid(), branchKey, projectBranch.getKey());
- } else {
- checkArgument(analysisMatchesProjectBranch,
- "Analysis '%s' does not belong to project '%s'",
- analysis.getUuid(), projectBranch.getKey());
- }
- }
-
- private static void checkType(String name, Set<NewCodePeriodType> validTypes, NewCodePeriodType type) {
- if (!validTypes.contains(type)) {
- throw new IllegalArgumentException(String.format("Invalid type '%s'. %s can only be set with types: %s", type, name, validTypes));
- }
- }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java
deleted file mode 100644
index 6dff716e39d..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java
+++ /dev/null
@@ -1,181 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import java.util.Optional;
-import javax.annotation.Nullable;
-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.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.user.UserSession;
-import org.sonarqube.ws.NewCodePeriods;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
-import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
-import static org.sonar.server.ws.WsUtils.writeProtobuf;
-import static org.sonarqube.ws.NewCodePeriods.*;
-
-public class ShowAction implements NewCodePeriodsWsAction {
- private static final String PARAM_BRANCH = "branch";
- private static final String PARAM_PROJECT = "project";
-
- private final DbClient dbClient;
- private final UserSession userSession;
- private final ComponentFinder componentFinder;
- private final NewCodePeriodDao newCodePeriodDao;
-
- public ShowAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- this.componentFinder = componentFinder;
- this.newCodePeriodDao = newCodePeriodDao;
- }
-
- @Override
- public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("show")
- .setDescription("Shows a setting for the New Code Period.<br>" +
- "Requires one of the following permissions: " +
- "<ul>" +
- "<li>'Administer System'</li>" +
- "<li>'Administer' rights on the specified component</li>" +
- "</ul>")
- .setSince("8.0")
- .setResponseExample(getClass().getResource("show_new_code_period-example.json"))
- .setHandler(this);
-
- action.createParam(PARAM_PROJECT)
- .setDescription("Project key");
- action.createParam(PARAM_BRANCH)
- .setDescription("Branch key");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- String projectStr = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);
- String branchStr = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);
-
- if (projectStr == null && branchStr != null) {
- throw new IllegalArgumentException("If branch key is specified, project key needs to be specified too");
- }
-
- try (DbSession dbSession = dbClient.openSession(false)) {
-
- ComponentDto projectBranch = null;
- String projectUuid = null;
- String branchUuid = null;
-
- if (projectStr != null) {
- projectBranch = getProject(dbSession, projectStr, branchStr);
- userSession.checkComponentPermission(UserRole.ADMIN, projectBranch);
- if (branchStr != null) {
- branchUuid = projectBranch.uuid();
- }
- // depending whether it's the main branch or not
- projectUuid = projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid();
- }
-
- ShowWSResponse.Builder builder = get(dbSession, projectUuid, branchUuid, false);
-
- if (projectStr != null) {
- builder.setProjectKey(projectStr);
- }
- if (branchStr != null) {
- builder.setBranchKey(branchStr);
- }
- writeProtobuf(builder.build(), request, response);
- }
- }
-
- private ShowWSResponse.Builder get(DbSession dbSession, @Nullable String projectUuid, @Nullable String branchUuid, boolean inherited) {
- if (projectUuid == null) {
- Optional<NewCodePeriodDto> dto = newCodePeriodDao.selectGlobal(dbSession);
- return dto.map(d -> build(d, inherited))
- .orElseGet(() -> buildDefault(inherited));
- }
- if (branchUuid == null) {
- Optional<NewCodePeriodDto> dto = newCodePeriodDao.selectByProject(dbSession, projectUuid);
- return dto.map(d -> build(d, inherited))
- .orElseGet(() -> get(dbSession, null, null, true));
- }
-
- Optional<NewCodePeriodDto> dto = newCodePeriodDao.selectByBranch(dbSession, projectUuid, branchUuid);
- return dto.map(d -> build(d, inherited))
- .orElseGet(() -> get(dbSession, projectUuid, null, true));
- }
-
- private ShowWSResponse.Builder build(NewCodePeriodDto dto, boolean inherited) {
- ShowWSResponse.Builder builder = ShowWSResponse.newBuilder()
- .setType(convertType(dto.getType()))
- .setInherited(inherited);
-
- if (dto.getValue() != null) {
- builder.setValue(dto.getValue());
- }
- return builder;
- }
-
- private ShowWSResponse.Builder buildDefault(boolean inherited) {
- return ShowWSResponse.newBuilder()
- .setType(convertType(NewCodePeriodType.PREVIOUS_VERSION))
- .setInherited(inherited);
- }
-
- private NewCodePeriods.NewCodePeriodType convertType(NewCodePeriodType type) {
- switch (type) {
- case NUMBER_OF_DAYS:
- return NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS;
- case PREVIOUS_VERSION:
- return NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION;
- case SPECIFIC_ANALYSIS:
- return NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS;
- default:
- throw new IllegalStateException("Unexpected type: " + type);
- }
- }
-
- private ComponentDto getProject(DbSession dbSession, String projectKey, @Nullable String branchKey) {
- if (branchKey == null) {
- return componentFinder.getByUuidOrKey(dbSession, null, projectKey, PROJECT_ID_AND_KEY);
- }
- ComponentDto project = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey);
-
- BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, project.uuid())
- .orElseThrow(() -> new NotFoundException(format("Branch '%s' is not found", branchKey)));
-
- checkArgument(branchDto.getBranchType() == BranchType.LONG,
- "Not a long-living branch: '%s'", branchKey);
-
- return project;
- }
-
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java
deleted file mode 100644
index 2ef7d0ca728..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/UnsetAction.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import javax.annotation.Nullable;
-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.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
-import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
-
-public class UnsetAction implements NewCodePeriodsWsAction {
- private static final String PARAM_BRANCH = "branch";
- private static final String PARAM_PROJECT = "project";
-
- private final DbClient dbClient;
- private final UserSession userSession;
- private final ComponentFinder componentFinder;
- private final NewCodePeriodDao newCodePeriodDao;
-
- public UnsetAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, NewCodePeriodDao newCodePeriodDao) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- this.componentFinder = componentFinder;
- this.newCodePeriodDao = newCodePeriodDao;
- }
-
- @Override
- public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("unset")
- .setPost(true)
- .setDescription("Unset the New Code Period setting for a branch, project or global.<br>" +
- "Requires one of the following permissions: " +
- "<ul>" +
- "<li>'Administer System' to change the global setting</li>" +
- "<li>'Administer' rights for a specified component</li>" +
- "</ul>")
- .setSince("8.0")
- .setHandler(this);
-
- action.createParam(PARAM_PROJECT)
- .setDescription("Project key");
- action.createParam(PARAM_BRANCH)
- .setDescription("Branch key");
- }
-
- @Override
- public void handle(Request request, Response response) throws Exception {
- String projectStr = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);
- String branchStr = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);
-
- if (projectStr == null && branchStr != null) {
- throw new IllegalArgumentException("If branch key is specified, project key needs to be specified too");
- }
-
- try (DbSession dbSession = dbClient.openSession(false)) {
- String projectUuid = null;
- String branchUuid = null;
- ComponentDto projectBranch = null;
-
- if (projectStr != null) {
- projectBranch = getProject(dbSession, projectStr, branchStr);
- userSession.checkComponentPermission(UserRole.ADMIN, projectBranch);
- if (branchStr != null) {
- branchUuid = projectBranch.uuid();
- }
- // depending whether it's the main branch or not
- projectUuid = projectBranch.getMainBranchProjectUuid() != null ? projectBranch.getMainBranchProjectUuid() : projectBranch.uuid();
- } else {
- userSession.checkIsSystemAdministrator();
- }
-
- newCodePeriodDao.delete(dbSession, projectUuid, branchUuid);
- dbSession.commit();
- }
- }
-
- private ComponentDto getProject(DbSession dbSession, String projectKey, @Nullable String branchKey) {
- if (branchKey == null) {
- return componentFinder.getByUuidOrKey(dbSession, null, projectKey, PROJECT_ID_AND_KEY);
- }
- ComponentDto project = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey);
-
- BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, project.uuid())
- .orElseThrow(() -> new NotFoundException(format("Branch '%s' is not found", branchKey)));
-
- checkArgument(branchDto.getBranchType() == BranchType.LONG,
- "Not a long-living branch: '%s'", branchKey);
-
- return project;
- }
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/package-info.java
deleted file mode 100644
index 4162912abbb..00000000000
--- a/server/sonar-server/src/main/java/org/sonar/server/newcodeperiod/ws/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.sonar.server.newcodeperiod.ws;
-
-import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/list_new_code_period-example.json b/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/list_new_code_period-example.json
deleted file mode 100644
index e69de29bb2d..00000000000
--- a/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/list_new_code_period-example.json
+++ /dev/null
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/show_new_code_period-example.json b/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/show_new_code_period-example.json
deleted file mode 100644
index e69de29bb2d..00000000000
--- a/server/sonar-server/src/main/resources/org/sonar/server/setting/ws/show_new_code_period-example.json
+++ /dev/null
diff --git a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ListActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ListActionTest.java
deleted file mode 100644
index 24e15d68b00..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ListActionTest.java
+++ /dev/null
@@ -1,319 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import java.util.Optional;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodDbTester;
-import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.NewCodePeriods;
-import org.sonarqube.ws.NewCodePeriods.ListWSResponse;
-import org.sonarqube.ws.NewCodePeriods.ShowWSResponse;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ListActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
- private NewCodePeriodDbTester tester = new NewCodePeriodDbTester(db);
- private ListAction underTest = new ListAction(dbClient, userSession, componentFinder, dao);
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("list");
- assertThat(definition.isInternal()).isFalse();
- assertThat(definition.since()).isEqualTo("8.0");
- assertThat(definition.isPost()).isFalse();
-
- assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("project");
- assertThat(definition.param("project").isRequired()).isTrue();
- }
-
- @Test
- public void throw_NFE_if_project_not_found() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'unknown' not found");
-
- ws.newRequest()
- .setParam("project", "unknown")
- .execute();
- }
-
- @Test
- public void throw_FE_if_no_project_permission() {
- ComponentDto project = componentDb.insertMainBranch();
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .execute();
- }
-
- @Test
- public void list_only_LLB() {
- ComponentDto project = componentDb.insertMainBranch();
-
- createBranches(project, 5, BranchType.LONG);
- createBranches(project, 3, BranchType.SHORT);
-
- logInAsProjectAdministrator(project);
-
- ListWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(6);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .contains("master", "LONG_0", "LONG_1", "LONG_2", "LONG_3", "LONG_4");
-
- //check if global default is set
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION);
- }
-
- @Test
- public void list_inherited_global_settings() {
- ComponentDto project = componentDb.insertMainBranch();
- tester.insert(new NewCodePeriodDto().setType(NewCodePeriodType.SPECIFIC_ANALYSIS).setValue("uuid"));
-
- createBranches(project, 5, BranchType.LONG);
-
- logInAsProjectAdministrator(project);
-
- ListWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(6);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .contains("master", "LONG_0", "LONG_1", "LONG_2", "LONG_3", "LONG_4");
-
- //check if global default is set
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getValue)
- .contains("uuid");
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getInherited)
- .contains(true);
- }
-
- @Test
- public void list_inherited_project_settings() {
- ComponentDto projectWithOwnSettings = componentDb.insertMainBranch();
- ComponentDto projectWithGlobalSettings = componentDb.insertMainBranch();
- tester.insert(new NewCodePeriodDto()
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("global_uuid"));
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(projectWithOwnSettings.uuid())
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("project_uuid"));
-
- createBranches(projectWithOwnSettings, 5, BranchType.LONG);
-
- logInAsProjectAdministrator(projectWithOwnSettings, projectWithGlobalSettings);
-
- ListWSResponse response = ws.newRequest()
- .setParam("project", projectWithOwnSettings.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- //verify project with project level settings
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(6);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .contains("master", "LONG_0", "LONG_1", "LONG_2", "LONG_3", "LONG_4");
-
- //check if project setting is set
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getValue)
- .containsOnly("project_uuid");
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getInherited)
- .containsOnly(true);
-
- //verify project with global level settings
- response = ws.newRequest()
- .setParam("project", projectWithGlobalSettings.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(1);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .containsOnly("master");
-
- //check if global setting is set
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getValue)
- .contains("global_uuid");
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getInherited)
- .containsOnly(true);
- }
-
- @Test
- public void list_branch_and_inherited_global_settings() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branchWithOwnSettings = componentDb.insertProjectBranch(project, branchDto -> branchDto.setKey("OWN_SETTINGS"));
- componentDb.insertProjectBranch(project, branchDto -> branchDto.setKey("GLOBAL_SETTINGS"));
-
- tester.insert(new NewCodePeriodDto()
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("global_uuid"));
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setBranchUuid(branchWithOwnSettings.uuid())
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("branch_uuid"));
-
- logInAsProjectAdministrator(project);
-
- ListWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(3);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .contains("master", "OWN_SETTINGS", "GLOBAL_SETTINGS");
-
- Optional<ShowWSResponse> ownSettings = response.getNewCodePeriodsList().stream()
- .filter(s -> !s.getInherited())
- .findFirst();
-
- assertThat(ownSettings).isNotNull();
- assertThat(ownSettings).isNotEmpty();
- assertThat(ownSettings.get().getProjectKey()).isEqualTo(project.getKey());
- assertThat(ownSettings.get().getBranchKey()).isEqualTo("OWN_SETTINGS");
- assertThat(ownSettings.get().getType()).isEqualTo(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(ownSettings.get().getValue()).isEqualTo("branch_uuid");
- assertThat(ownSettings.get().getInherited()).isFalse();
-
- //check if global default is set
- assertThat(response.getNewCodePeriodsList())
- .filteredOn(ShowWSResponse::getInherited)
- .extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(response.getNewCodePeriodsList())
- .filteredOn(ShowWSResponse::getInherited)
- .extracting(ShowWSResponse::getValue)
- .contains("global_uuid");
- }
-
- @Test
- public void list_branch_and_inherited_project_settings() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branchWithOwnSettings = componentDb.insertProjectBranch(project, branchDto -> branchDto.setKey("OWN_SETTINGS"));
- componentDb.insertProjectBranch(project, branchDto -> branchDto.setKey("PROJECT_SETTINGS"));
-
- tester.insert(new NewCodePeriodDto()
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("global_uuid"));
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("project_uuid"));
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setBranchUuid(branchWithOwnSettings.uuid())
- .setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
- .setValue("branch_uuid"));
-
- logInAsProjectAdministrator(project);
-
- ListWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ListWSResponse.class);
-
- assertThat(response).isNotNull();
- assertThat(response.getNewCodePeriodsCount()).isEqualTo(3);
- assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey)
- .contains("master", "OWN_SETTINGS", "PROJECT_SETTINGS");
-
- Optional<ShowWSResponse> ownSettings = response.getNewCodePeriodsList().stream()
- .filter(s -> !s.getInherited())
- .findFirst();
-
- assertThat(ownSettings).isNotNull();
- assertThat(ownSettings).isNotEmpty();
- assertThat(ownSettings.get().getProjectKey()).isEqualTo(project.getKey());
- assertThat(ownSettings.get().getBranchKey()).isEqualTo("OWN_SETTINGS");
- assertThat(ownSettings.get().getType()).isEqualTo(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(ownSettings.get().getValue()).isEqualTo("branch_uuid");
- assertThat(ownSettings.get().getInherited()).isFalse();
-
- //check if global default is set
- assertThat(response.getNewCodePeriodsList())
- .filteredOn(ShowWSResponse::getInherited)
- .extracting(ShowWSResponse::getType)
- .contains(NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS);
- assertThat(response.getNewCodePeriodsList())
- .filteredOn(ShowWSResponse::getInherited)
- .extracting(ShowWSResponse::getValue)
- .contains("project_uuid");
- }
-
- private void createBranches(ComponentDto project, int numberOfBranches, BranchType branchType) {
- for (int branchCount = 0; branchCount < numberOfBranches; branchCount++) {
- String branchKey = String.format("%s_%d", branchType.name(), branchCount);
- componentDb.insertProjectBranch(project, branchDto -> branchDto.setKey(branchKey).setBranchType(branchType));
- }
- }
-
- private void logInAsProjectAdministrator(ComponentDto... project) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java
deleted file mode 100644
index 60166ae4756..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/NewCodePeriodsWsModuleTest.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import org.junit.Test;
-import org.sonar.core.platform.ComponentContainer;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class NewCodePeriodsWsModuleTest {
-
- @Test
- public void verify_count_of_added_components() {
- ComponentContainer container = new ComponentContainer();
- new NewCodePeriodsWsModule().configure(container);
- assertThat(container.size()).isEqualTo(7);
- }
-
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java
deleted file mode 100644
index e18763e72eb..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java
+++ /dev/null
@@ -1,378 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.SnapshotDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsActionTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.entry;
-
-public class SetActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
-
- private SetAction underTest = new SetAction(dbClient, userSession, componentFinder, dao);
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("set");
- assertThat(definition.isInternal()).isFalse();
- assertThat(definition.since()).isEqualTo("8.0");
- assertThat(definition.isPost()).isTrue();
-
- assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("value", "type", "project", "branch");
- assertThat(definition.param("value").isRequired()).isFalse();
- assertThat(definition.param("type").isRequired()).isTrue();
- assertThat(definition.param("project").isRequired()).isFalse();
- assertThat(definition.param("branch").isRequired()).isFalse();
- }
-
- // validation of type
- @Test
- public void throw_IAE_if_no_type_specified() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("The 'type' parameter is missing");
-
- ws.newRequest()
- .execute();
- }
-
- @Test
- public void throw_IAE_if_type_is_invalid() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Invalid type: unknown");
-
- ws.newRequest()
- .setParam("type", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_type_is_invalid_for_global() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Invalid type 'SPECIFIC_ANALYSIS'. Overall setting can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS]");
-
- ws.newRequest()
- .setParam("type", "specific_analysis")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_type_is_invalid_for_project() {
- ComponentDto project = componentDb.insertPublicProject();
- logInAsProjectAdministrator(project);
-
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Invalid type 'SPECIFIC_ANALYSIS'. Projects can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS]");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_no_value_for_days() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("New Code Period type 'NUMBER_OF_DAYS' requires a value");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "master")
- .setParam("type", "number_of_days")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_no_value_for_analysis() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("New Code Period type 'SPECIFIC_ANALYSIS' requires a value");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .setParam("branch", "master")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_days_is_invalid() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Failed to parse number of days: unknown");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "number_of_days")
- .setParam("branch", "master")
- .setParam("value", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_analysis_is_not_found() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Analysis 'unknown' is not found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .setParam("branch", "master")
- .setParam("value", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_analysis_doesnt_belong_to_branch() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- SnapshotDto analysisMaster = db.components().insertSnapshot(project);
- SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
-
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Analysis '" + analysisBranch.getUuid() + "' does not belong to branch 'master' of project '" + project.getKey() + "'");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .setParam("branch", "master")
- .setParam("value", analysisBranch.getUuid())
- .execute();
- }
-
- // validation of project/branch
- @Test
- public void throw_IAE_if_branch_is_specified_without_project() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("If branch key is specified, project key needs to be specified too");
-
- ws.newRequest()
- .setParam("branch", "branch")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_project_not_found() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'unknown' not found");
-
- ws.newRequest()
- .setParam("type", "previous_version")
- .setParam("project", "unknown")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_branch_not_found() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component '" + project.getKey() + "' on branch 'unknown' not found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .setParam("branch", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_branch_is_a_SLB() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch").setBranchType(BranchType.SHORT));
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Not a long-living branch: 'branch'");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .setParam("branch", "branch")
- .execute();
- }
-
- // permission
- @Test
- public void throw_NFE_if_no_project_permission() {
- ComponentDto project = componentDb.insertMainBranch();
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_no_system_permission() {
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("type", "previous_version")
- .execute();
- }
-
- // success cases
- @Test
- public void set_global_period_to_previous_version() {
- logInAsSystemAdministrator();
- ws.newRequest()
- .setParam("type", "previous_version")
- .execute();
-
- assertTableContainsOnly(null, null, NewCodePeriodType.PREVIOUS_VERSION, null);
- }
-
- @Test
- public void set_project_period_to_number_of_days() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "number_of_days")
- .setParam("value", "5")
- .execute();
- assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
- }
-
- @Test
- public void set_project_twice_period_to_number_of_days() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .execute();
- assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.PREVIOUS_VERSION, null);
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "number_of_days")
- .setParam("value", "5")
- .execute();
- assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5");
- }
-
- @Test
- public void set_branch_period_to_analysis() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- SnapshotDto analysisMaster = db.components().insertSnapshot(project);
- SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
-
- logInAsProjectAdministrator(project);
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .setParam("branch", "branch")
- .setParam("value", analysisBranch.getUuid())
- .execute();
-
- assertTableContainsOnly(project.uuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, analysisBranch.getUuid());
- }
-
- @Test
- public void set_branch_period_twice_to_analysis() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- SnapshotDto analysisMaster = db.components().insertSnapshot(project);
- SnapshotDto analysisBranch = db.components().insertSnapshot(branch);
-
- logInAsProjectAdministrator(project);
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "specific_analysis")
- .setParam("branch", "branch")
- .setParam("value", analysisBranch.getUuid())
- .execute();
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .setParam("branch", "branch")
- .execute();
-
- assertTableContainsOnly(project.uuid(), branch.uuid(), NewCodePeriodType.PREVIOUS_VERSION, null);
- }
-
- private void assertTableContainsOnly(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
- assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isEqualTo(1);
- assertThat(db.selectFirst(dbSession, "select project_uuid, branch_uuid, type, value from new_code_periods"))
- .containsOnly(entry("PROJECT_UUID", projectUuid), entry("BRANCH_UUID", branchUuid), entry("TYPE", type.name()), entry("VALUE", value));
- }
-
- private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- }
-
- private void logInAsSystemAdministrator() {
- userSession.logIn().setSystemAdministrator();
- }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java
deleted file mode 100644
index d57733d02da..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java
+++ /dev/null
@@ -1,247 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodDbTester;
-import org.sonar.db.newcodeperiod.NewCodePeriodDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsActionTester;
-import org.sonarqube.ws.NewCodePeriods;
-import org.sonarqube.ws.NewCodePeriods.ShowWSResponse;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ShowActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
- private NewCodePeriodDbTester tester = new NewCodePeriodDbTester(db);
- private ShowAction underTest = new ShowAction(dbClient, userSession, componentFinder, dao);
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("show");
- assertThat(definition.isInternal()).isFalse();
- assertThat(definition.since()).isEqualTo("8.0");
- assertThat(definition.isPost()).isFalse();
-
- assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("project", "branch");
- assertThat(definition.param("project").isRequired()).isFalse();
- assertThat(definition.param("branch").isRequired()).isFalse();
- }
-
- @Test
- public void throw_IAE_if_branch_is_specified_without_project() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("If branch key is specified, project key needs to be specified too");
-
- ws.newRequest()
- .setParam("branch", "branch")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_project_not_found() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'unknown' not found");
-
- ws.newRequest()
- .setParam("project", "unknown")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_branch_not_found() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component '" + project.getKey() + "' on branch 'unknown' not found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_branch_is_a_SLB() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch").setBranchType(BranchType.SHORT));
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Not a long-living branch: 'branch'");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "branch")
- .execute();
- }
-
- @Test
- public void throw_FE_if_no_project_permission() {
- ComponentDto project = componentDb.insertMainBranch();
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .execute();
- }
-
- @Test
- public void show_global_setting() {
- tester.insert(new NewCodePeriodDto().setType(NewCodePeriodType.PREVIOUS_VERSION));
-
- ShowWSResponse response = ws.newRequest()
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, "", "", NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION, "", false);
- }
-
- @Test
- public void show_project_setting() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setType(NewCodePeriodType.NUMBER_OF_DAYS)
- .setValue("4"));
-
- ShowWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "4", false);
- }
-
- @Test
- public void show_branch_setting() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
-
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setBranchUuid(branch.uuid())
- .setType(NewCodePeriodType.NUMBER_OF_DAYS)
- .setValue("1"));
-
- ShowWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "branch")
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "1", false);
- }
-
- @Test
- public void show_inherited_project_setting() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- tester.insert(new NewCodePeriodDto().setType(NewCodePeriodType.PREVIOUS_VERSION));
-
- ShowWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION, "", true);
- }
-
- @Test
- public void show_inherited_branch_setting_from_project() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
-
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- tester.insert(new NewCodePeriodDto()
- .setProjectUuid(project.uuid())
- .setType(NewCodePeriodType.NUMBER_OF_DAYS)
- .setValue("1"));
-
- ShowWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "branch")
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "1", true);
- }
-
- @Test
- public void show_inherited_branch_setting_from_global() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
- tester.insert(new NewCodePeriodDto().setType(NewCodePeriodType.NUMBER_OF_DAYS).setValue("3"));
-
- ShowWSResponse response = ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "branch")
- .executeProtobuf(ShowWSResponse.class);
-
- assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true);
- }
-
- private void assertResponse(ShowWSResponse response, String projectKey, String branchKey, NewCodePeriods.NewCodePeriodType type, String value, boolean inherited) {
- assertThat(response.getBranchKey()).isEqualTo(branchKey);
- assertThat(response.getProjectKey()).isEqualTo(projectKey);
- assertThat(response.getInherited()).isEqualTo(inherited);
- assertThat(response.getValue()).isEqualTo(value);
- assertThat(response.getType()).isEqualTo(type);
- }
-
- private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- }
-
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java
deleted file mode 100644
index 7475a38d2a5..00000000000
--- a/server/sonar-server/src/test/java/org/sonar/server/newcodeperiod/ws/UnsetActionTest.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info 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.newcodeperiod.ws;
-
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.util.UuidFactoryFast;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDbTester;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.newcodeperiod.NewCodePeriodDao;
-import org.sonar.db.newcodeperiod.NewCodePeriodType;
-import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.component.TestComponentFinder;
-import org.sonar.server.exceptions.ForbiddenException;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsActionTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.entry;
-
-public class UnsetActionTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone();
- @Rule
- public DbTester db = DbTester.create(System2.INSTANCE);
-
- private ComponentDbTester componentDb = new ComponentDbTester(db);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
- private ComponentFinder componentFinder = TestComponentFinder.from(db);
- private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE, UuidFactoryFast.getInstance());
-
- private UnsetAction underTest = new UnsetAction(dbClient, userSession, componentFinder, dao);
- private WsActionTester ws = new WsActionTester(underTest);
-
- @Test
- public void test_definition() {
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("unset");
- assertThat(definition.isInternal()).isFalse();
- assertThat(definition.since()).isEqualTo("8.0");
- assertThat(definition.isPost()).isTrue();
-
- assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("project", "branch");
- assertThat(definition.param("project").isRequired()).isFalse();
- assertThat(definition.param("branch").isRequired()).isFalse();
- }
-
- // validation of project/branch
- @Test
- public void throw_IAE_if_branch_is_specified_without_project() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("If branch key is specified, project key needs to be specified too");
-
- ws.newRequest()
- .setParam("branch", "branch")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_project_not_found() {
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component key 'unknown' not found");
-
- ws.newRequest()
- .setParam("type", "previous_version")
- .setParam("project", "unknown")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_branch_not_found() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- expectedException.expect(NotFoundException.class);
- expectedException.expectMessage("Component '" + project.getKey() + "' on branch 'unknown' not found");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .setParam("branch", "unknown")
- .execute();
- }
-
- @Test
- public void throw_IAE_if_branch_is_a_SLB() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch").setBranchType(BranchType.SHORT));
- logInAsProjectAdministrator(project);
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Not a long-living branch: 'branch'");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .setParam("branch", "branch")
- .execute();
- }
-
- // permission
- @Test
- public void throw_NFE_if_no_project_permission() {
- ComponentDto project = componentDb.insertMainBranch();
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("type", "previous_version")
- .execute();
- }
-
- @Test
- public void throw_NFE_if_no_system_permission() {
- expectedException.expect(ForbiddenException.class);
- expectedException.expectMessage("Insufficient privileges");
-
- ws.newRequest()
- .setParam("type", "previous_version")
- .execute();
- }
-
- // success cases
- @Test
- public void delete_global_period() {
- logInAsSystemAdministrator();
- ws.newRequest()
- .execute();
-
- assertTableEmpty();
- }
-
- @Test
- public void delete_project_period() {
- ComponentDto project = componentDb.insertMainBranch();
- logInAsProjectAdministrator(project);
- ws.newRequest()
- .setParam("project", project.getKey())
- .execute();
-
- assertTableEmpty();
- }
-
- @Test
- public void delete_project_period_twice() {
- ComponentDto project1 = componentDb.insertMainBranch();
- ComponentDto project2 = componentDb.insertMainBranch();
- db.newCodePeriods().insert(project1.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1");
- db.newCodePeriods().insert(project2.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
-
- logInAsProjectAdministrator(project1);
- ws.newRequest()
- .setParam("project", project1.getKey())
- .execute();
- assertTableContainsOnly(project2.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
-
- ws.newRequest()
- .setParam("project", project1.getKey())
- .execute();
-
- assertTableContainsOnly(project2.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
- }
-
- @Test
- public void delete_branch_period() {
- ComponentDto project = componentDb.insertMainBranch();
- ComponentDto branch = componentDb.insertProjectBranch(project, b -> b.setKey("branch"));
-
- db.newCodePeriods().insert(project.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1");
- db.newCodePeriods().insert(project.uuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid2");
-
- logInAsProjectAdministrator(project);
-
- ws.newRequest()
- .setParam("project", project.getKey())
- .setParam("branch", "branch")
- .execute();
-
- assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.SPECIFIC_ANALYSIS, "uuid1");
- }
-
- private void assertTableEmpty() {
- assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isEqualTo(0);
- }
-
- private void assertTableContainsOnly(@Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type, @Nullable String value) {
- assertThat(db.countRowsOfTable(dbSession, "new_code_periods")).isEqualTo(1);
- assertThat(db.selectFirst(dbSession, "select project_uuid, branch_uuid, type, value from new_code_periods"))
- .containsOnly(entry("PROJECT_UUID", projectUuid), entry("BRANCH_UUID", branchUuid), entry("TYPE", type.name()), entry("VALUE", value));
- }
-
- private void logInAsProjectAdministrator(ComponentDto project) {
- userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
- }
-
- private void logInAsSystemAdministrator() {
- userSession.logIn().setSystemAdministrator();
- }
-}