3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.branch.ws;
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.resources.ResourceTypes;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.BranchDto;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.ResourceTypesRule;
33 import org.sonar.server.component.ComponentFinder;
34 import org.sonar.server.exceptions.ForbiddenException;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.WsActionTester;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.sonar.api.resources.Qualifiers.PROJECT;
43 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
45 public class SetAutomaticDeletionProtectionActionTest {
48 public DbTester db = DbTester.create(System2.INSTANCE);
50 public UserSessionRule userSession = UserSessionRule.standalone();
52 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT);
53 private ComponentFinder componentFinder = new ComponentFinder(db.getDbClient(), resourceTypes);
54 private WsActionTester tester = new WsActionTester(new SetAutomaticDeletionProtectionAction(db.getDbClient(), userSession, componentFinder));
57 public void test_definition() {
58 WebService.Action definition = tester.getDef();
59 assertThat(definition.key()).isEqualTo("set_automatic_deletion_protection");
60 assertThat(definition.isPost()).isTrue();
61 assertThat(definition.isInternal()).isFalse();
62 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "branch", "value");
63 assertThat(definition.since()).isEqualTo("8.1");
67 public void fail_if_missing_project_parameter() {
70 assertThatThrownBy(() -> tester.newRequest().execute())
71 .isInstanceOf(IllegalArgumentException.class)
72 .hasMessageContaining("The 'project' parameter is missing");
76 public void fail_if_missing_branch_parameter() {
79 assertThatThrownBy(() -> tester.newRequest()
80 .setParam("project", "projectName")
82 .isInstanceOf(IllegalArgumentException.class)
83 .hasMessageContaining("The 'branch' parameter is missing");
87 public void fail_if_missing_value_parameter() {
90 assertThatThrownBy(() -> tester.newRequest()
91 .setParam("project", "projectName")
92 .setParam("branch", "foobar")
94 .isInstanceOf(IllegalArgumentException.class)
95 .hasMessageContaining("The 'value' parameter is missing");
99 public void fail_if_not_logged_in() {
100 assertThatThrownBy(() -> tester.newRequest().execute())
101 .isInstanceOf(UnauthorizedException.class)
102 .hasMessageContaining("Authentication is required");
106 public void fail_if_no_administer_permission() {
108 ComponentDto project = db.components().insertPublicProject();
110 assertThatThrownBy(() -> tester.newRequest()
111 .setParam("project", project.getKey())
112 .setParam("branch", "branch1")
113 .setParam("value", "true")
115 .isInstanceOf(ForbiddenException.class)
116 .hasMessageContaining("Insufficient privileges");
120 public void fail_when_attempting_to_set_main_branch_as_included_in_purge() {
122 ComponentDto project = db.components().insertPublicProject();
123 ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
124 userSession.addProjectPermission(UserRole.ADMIN, project);
126 assertThatThrownBy(() -> tester.newRequest()
127 .setParam("project", project.getKey())
128 .setParam("branch", DEFAULT_PROJECT_MAIN_BRANCH_NAME)
129 .setParam("value", "false")
131 .isInstanceOf(IllegalArgumentException.class)
132 .hasMessageContaining("Main branch of the project is always excluded from automatic deletion.");
136 public void set_purge_exclusion() {
138 ComponentDto project = db.components().insertPublicProject();
139 ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1").setExcludeFromPurge(false));
140 userSession.addProjectPermission(UserRole.ADMIN, project);
143 .setParam("project", project.getKey())
144 .setParam("branch", "branch1")
145 .setParam("value", "true")
148 assertThat(db.countRowsOfTable("project_branches")).isEqualTo(2);
149 Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.uuid());
150 assertThat(mainBranch.get().getKey()).isEqualTo(DEFAULT_PROJECT_MAIN_BRANCH_NAME);
151 assertThat(mainBranch.get().isExcludeFromPurge()).isTrue();
153 Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid());
154 assertThat(branchDto.get().getKey()).isEqualTo("branch1");
155 assertThat(branchDto.get().isExcludeFromPurge()).isTrue();
159 public void fail_on_non_boolean_value_parameter() {
161 ComponentDto project = db.components().insertPublicProject();
163 assertThatThrownBy(() -> tester.newRequest()
164 .setParam("project", project.getKey())
165 .setParam("branch", "branch1")
166 .setParam("value", "foobar")
168 .isInstanceOf(IllegalArgumentException.class)
169 .hasMessageContaining("Value of parameter 'value' (foobar) must be one of: [true, false, yes, no]");
173 public void fail_if_project_does_not_exist() {
176 assertThatThrownBy(() -> tester.newRequest()
177 .setParam("project", "foo")
178 .setParam("branch", "branch1")
179 .setParam("value", "true")
181 .isInstanceOf(NotFoundException.class)
182 .hasMessageContaining("Project 'foo' not found");
186 public void fail_if_branch_does_not_exist() {
188 ComponentDto project = db.components().insertPublicProject();
189 userSession.addProjectPermission(UserRole.ADMIN, project);
191 assertThatThrownBy(() -> tester.newRequest()
192 .setParam("project", project.getKey())
193 .setParam("branch", "branch1")
194 .setParam("value", "true")
196 .isInstanceOf(NotFoundException.class)
197 .hasMessageContaining("Branch 'branch1' not found for project '" + project.getKey() + "'");