]> source.dussan.org Git - sonarqube.git/blob
f90d6a095df806067a1d0dd0591bdb267a220444
[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.newcodeperiod.NewCodePeriodDto;
31
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;
40
41 public class NewCodeDefinitionResolverTest {
42
43   @Rule
44   public DbTester db = DbTester.create(System2.INSTANCE);
45
46   private static final String DEFAULT_PROJECT_ID = "12345";
47
48   private static final String MAIN_BRANCH = "main";
49
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);
54
55   @Test
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");
60   }
61
62   @Test
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]");
67   }
68
69   @Test
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");
74   }
75
76   @Test
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");
81   }
82
83   @Test
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'");
88   }
89
90   @Test
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'");
95   }
96
97   @Test
98   public void createNewCodeDefinition_persist_previous_version_type() {
99     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
100
101     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
102     assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
103   }
104
105   @Test
106   public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
107     String numberOfDays = "30";
108
109     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
110
111     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
112
113     assertThat(newCodePeriodDto)
114       .isPresent()
115       .get()
116       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
117       .containsExactly(NUMBER_OF_DAYS, numberOfDays);
118   }
119
120   @Test
121   public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
122     newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
123
124     Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
125
126     assertThat(newCodePeriodDto)
127       .isPresent()
128       .get()
129       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
130       .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH);
131   }
132
133   @Test
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");
138   }
139
140   @Test
141   public void checkNewCodeDefinitionParam_do_not_throw_when_both_value_and_type_are_provided() {
142     assertThatNoException()
143       .isThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam("PREVIOUS_VERSION", "anyvalue"));
144   }
145
146 }