]> source.dussan.org Git - sonarqube.git/blob
8aa503e2eaa689c0728eef8d62fa576d913fed51
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.newcodeperiod;
21
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;
32
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;
41
42 public class NewCodeDefinitionResolverTest {
43
44   @Rule
45   public DbTester db = DbTester.create(System2.INSTANCE, true);
46
47   private static final String DEFAULT_PROJECT_ID = "12345";
48
49   private static final String MAIN_BRANCH = "main";
50   private ComponentDbTester componentDb = new ComponentDbTester(db);
51
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);
56
57   @Test
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");
62   }
63
64   @Test
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]");
69   }
70
71   @Test
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");
76   }
77
78   @Test
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");
83   }
84
85   @Test
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'");
90   }
91
92   @Test
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'");
97   }
98
99   @Test
100   public void createNewCodeDefinition_persist_previous_version_type() {
101     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
102
103     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
104     assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
105   }
106
107   @Test
108   public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
109     String numberOfDays = "30";
110
111     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
112
113     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
114
115     assertThat(newCodePeriodDto)
116       .isPresent()
117       .get()
118       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
119       .containsExactly(NUMBER_OF_DAYS, numberOfDays);
120   }
121
122   @Test
123   public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
124     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
125
126     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
127
128     assertThat(newCodePeriodDto)
129       .isPresent()
130       .get()
131       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
132       .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH);
133   }
134
135   @Test
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");
140   }
141
142   @Test
143   public void checkNewCodeDefinitionParam_do_not_throw_when_both_value_and_type_are_provided() {
144     assertThatNoException()
145       .isThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam("PREVIOUS_VERSION", "anyvalue"));
146   }
147
148 }