private WsActionTester ws;
@Before
- public void setup(){
+ public void setup() {
when(documentationLinkGenerator.getDocumentationLink(any())).thenReturn("https://docs.sonarqube.org/9.9/project-administration/defining-new-code/");
ws = new WsActionTester(new SetAction(dbClient, userSession, componentFinder, editionProvider, dao, documentationLinkGenerator));
}
.hasMessageContaining("Failed to parse number of days: unknown");
}
+ @Test
+ public void throw_IAE_if_setting_is_not_cayc_compliant() {
+ ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
+ logInAsProjectAdministrator(project);
+
+ TestRequest request = ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("type", "number_of_days")
+ .setParam("branch", DEFAULT_MAIN_BRANCH_NAME)
+ .setParam("value", "92");
+ assertThatThrownBy(() -> request
+ .execute())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. "
+ + "Please refer to the documentation for compliant options.");
+ }
+
+ @Test
+ public void no_error_if_setting_is_cayc_compliant() {
+ ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
+ logInAsProjectAdministrator(project);
+
+ ws.newRequest()
+ .setParam("project", project.getKey())
+ .setParam("type", "number_of_days")
+ .setParam("value", "90")
+ .execute();
+
+ assertTableContainsOnly(project.uuid(), null, NewCodePeriodType.NUMBER_OF_DAYS, "90");
+ }
+
@Test
public void throw_IAE_if_analysis_is_not_found() {
ComponentDto project = componentDb.insertPublicProject().getMainBranchComponent();
@DataProvider
public static Object[][] provideNewCodePeriodTypeAndValue() {
- return new Object[][]{
+ return new Object[][] {
{NewCodePeriodType.NUMBER_OF_DAYS, "5"},
{NewCodePeriodType.SPECIFIC_ANALYSIS, "analysis-uuid"},
{NewCodePeriodType.PREVIOUS_VERSION, null},
import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.newcodeperiod.CaycUtils;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
"Existing projects or branches having a specific new code definition will not be impacted" + END_ITEM_LIST +
BEGIN_ITEM_LIST + "Project key must be provided to update the value for a project" + END_ITEM_LIST +
BEGIN_ITEM_LIST + "Both project and branch keys must be provided to update the value for a branch" + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "New setting must be compliant with the Clean as You Code methodology" + END_ITEM_LIST +
END_LIST +
"Requires one of the following permissions: " +
BEGIN_LIST +
BEGIN_LIST +
BEGIN_ITEM_LIST + "the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + END_ITEM_LIST +
BEGIN_ITEM_LIST + "no value, when type is " + PREVIOUS_VERSION.name() + END_ITEM_LIST +
- BEGIN_ITEM_LIST + "a number, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST +
+ BEGIN_ITEM_LIST + "a number between 1 and 90, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST +
BEGIN_ITEM_LIST + "a string, when type is " + REFERENCE_BRANCH.name() + END_ITEM_LIST +
END_LIST
);
setValue(dbSession, dto, type, project, branch, valueStr);
+ if (!CaycUtils.isNewCodePeriodCompliant(dto.getType(), dto.getValue())) {
+ throw new IllegalArgumentException("Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. "
+ + "Please refer to the documentation for compliant options.");
+ }
+
newCodePeriodDao.upsert(dbSession, dto);
dbSession.commit();
}