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.newcodeperiod.NewCodePeriodDto;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatNoException;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.Mockito.mock;
36 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
37 import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
38 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
39 import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
41 public class NewCodeDefinitionResolverTest {
44 public DbTester db = DbTester.create(System2.INSTANCE);
46 private static final String DEFAULT_PROJECT_ID = "12345";
48 private static final String MAIN_BRANCH = "main";
50 private DbSession dbSession = db.getSession();
51 private DbClient dbClient = db.getDbClient();
52 private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
53 private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
56 public void createNewCodeDefinition_throw_IAE_if_no_valid_type() {
57 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, "nonValid", null))
58 .isInstanceOf(IllegalArgumentException.class)
59 .hasMessageContaining("Invalid type: nonValid");
63 public void createNewCodeDefinition_throw_IAE_if_type_is_not_allowed() {
64 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, SPECIFIC_ANALYSIS.name(), null))
65 .isInstanceOf(IllegalArgumentException.class)
66 .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. `newCodeDefinitionType` can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]");
70 public void createNewCodeDefinition_throw_IAE_if_no_value_for_days() {
71 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), null))
72 .isInstanceOf(IllegalArgumentException.class)
73 .hasMessageContaining("New code definition type 'NUMBER_OF_DAYS' requires a newCodeDefinitionValue");
77 public void createNewCodeDefinition_throw_IAE_if_days_is_invalid() {
78 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), "unknown"))
79 .isInstanceOf(IllegalArgumentException.class)
80 .hasMessageContaining("Failed to parse number of days: unknown");
84 public void createNewCodeDefinition_throw_IAE_if_value_is_set_for_reference_branch() {
85 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), "feature/zw"))
86 .isInstanceOf(IllegalArgumentException.class)
87 .hasMessageContaining("Unexpected value for newCodeDefinitionType 'REFERENCE_BRANCH'");
91 public void createNewCodeDefinition_throw_IAE_if_previous_version_type_and_value_provided() {
92 assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), "10.2.3"))
93 .isInstanceOf(IllegalArgumentException.class)
94 .hasMessageContaining("Unexpected value for newCodeDefinitionType 'PREVIOUS_VERSION'");
98 public void createNewCodeDefinition_persist_previous_version_type() {
99 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
101 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
102 assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
106 public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
107 String numberOfDays = "30";
109 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
111 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
113 assertThat(newCodePeriodDto)
116 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
117 .containsExactly(NUMBER_OF_DAYS, numberOfDays);
121 public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
122 newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
124 Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
126 assertThat(newCodePeriodDto)
129 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
130 .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH);
134 public void checkNewCodeDefinitionParam_throw_IAE_if_newCodeDefinitionValue_is_provided_without_newCodeDefinitionType() {
135 assertThatThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam(null, "anyvalue"))
136 .isInstanceOf(IllegalArgumentException.class)
137 .hasMessageContaining("New code definition type is required when new code definition value is provided");
141 public void checkNewCodeDefinitionParam_do_not_throw_when_both_value_and_type_are_provided() {
142 assertThatNoException()
143 .isThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam("PREVIOUS_VERSION", "anyvalue"));