3 * Copyright (C) 2009-2023 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.newcodeperiod;
22 import java.util.Optional;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.core.platform.PlatformEditionProvider;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.component.ComponentDbTester;
31 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatNoException;
35 import static org.assertj.core.api.Assertions.assertThatThrownBy;
36 import static org.mockito.Mockito.mock;
37 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
38 import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
39 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
40 import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
42 public class NewCodeDefinitionResolverTest {
45 public DbTester db = DbTester.create(System2.INSTANCE, true);
47 private static final String DEFAULT_PROJECT_ID = "12345";
49 private static final String MAIN_BRANCH = "main";
50 private ComponentDbTester componentDb = new ComponentDbTester(db);
52 private DbSession dbSession = db.getSession();
53 private DbClient dbClient = db.getDbClient();
54 private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
55 private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
58 public void createNewCodeDefinition_throw_IAE_if_no_valid_type() {
59 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, "nonValid", null))
60 .isInstanceOf(IllegalArgumentException.class)
61 .hasMessageContaining("Invalid type: nonValid");
65 public void createNewCodeDefinition_throw_IAE_if_type_is_not_allowed() {
66 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, SPECIFIC_ANALYSIS.name(), null))
67 .isInstanceOf(IllegalArgumentException.class)
68 .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. `newCodeDefinitionType` can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]");
72 public void createNewCodeDefinition_throw_IAE_if_no_value_for_days() {
73 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), null))
74 .isInstanceOf(IllegalArgumentException.class)
75 .hasMessageContaining("New code definition type 'NUMBER_OF_DAYS' requires a newCodeDefinitionValue");
79 public void createNewCodeDefinition_throw_IAE_if_days_is_invalid() {
80 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), "unknown"))
81 .isInstanceOf(IllegalArgumentException.class)
82 .hasMessageContaining("Failed to parse number of days: unknown");
86 public void createNewCodeDefinition_throw_IAE_if_value_is_set_for_reference_branch() {
87 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), "feature/zw"))
88 .isInstanceOf(IllegalArgumentException.class)
89 .hasMessageContaining("Unexpected value for newCodeDefinitionType 'REFERENCE_BRANCH'");
93 public void createNewCodeDefinition_throw_IAE_if_previous_version_type_and_value_provided() {
94 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), "10.2.3"))
95 .isInstanceOf(IllegalArgumentException.class)
96 .hasMessageContaining("Unexpected value for newCodeDefinitionType 'PREVIOUS_VERSION'");
100 public void createNewCodeDefinition_persist_previous_version_type() {
101 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
103 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
104 assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
108 public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
109 String numberOfDays = "30";
111 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
113 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
115 assertThat(newCodePeriodDto)
118 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
119 .containsExactly(NUMBER_OF_DAYS, numberOfDays);
123 public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
124 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
126 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
128 assertThat(newCodePeriodDto)
131 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
132 .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH);
136 public void checkNewCodeDefinitionParam_throw_IAE_if_newCodeDefinitionValue_is_provided_without_newCodeDefinitionType() {
137 assertThatThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam(null, "anyvalue"))
138 .isInstanceOf(IllegalArgumentException.class)
139 .hasMessageContaining("New code definition type is required when new code definition value is provided");
143 public void checkNewCodeDefinitionParam_do_not_throw_when_both_value_and_type_are_provided() {
144 assertThatNoException()
145 .isThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam("PREVIOUS_VERSION", "anyvalue"));