You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewCodeDefinitionResolverTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.common.newcodeperiod;
  21. import java.util.Optional;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.api.utils.System2;
  25. import org.sonar.core.platform.PlatformEditionProvider;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.newcodeperiod.NewCodePeriodDto;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.assertj.core.api.Assertions.assertThatNoException;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.mockito.Mockito.mock;
  34. import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
  35. import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
  36. import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
  37. import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;
  38. public class NewCodeDefinitionResolverTest {
  39. private static final String MAIN_BRANCH_UUID = "main-branch-uuid";
  40. @Rule
  41. public DbTester db = DbTester.create(System2.INSTANCE);
  42. private static final String DEFAULT_PROJECT_ID = "12345";
  43. private static final String MAIN_BRANCH = "main";
  44. private DbSession dbSession = db.getSession();
  45. private DbClient dbClient = db.getDbClient();
  46. private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
  47. private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
  48. @Test
  49. public void createNewCodeDefinition_throw_IAE_if_no_valid_type() {
  50. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, "nonValid", null))
  51. .isInstanceOf(IllegalArgumentException.class)
  52. .hasMessageContaining("Invalid type: nonValid");
  53. }
  54. @Test
  55. public void createNewCodeDefinition_throw_IAE_if_type_is_not_allowed() {
  56. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, SPECIFIC_ANALYSIS.name(), null))
  57. .isInstanceOf(IllegalArgumentException.class)
  58. .hasMessageContaining("Invalid type 'SPECIFIC_ANALYSIS'. `newCodeDefinitionType` can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]");
  59. }
  60. @Test
  61. public void createNewCodeDefinition_throw_IAE_if_no_value_for_days() {
  62. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), null))
  63. .isInstanceOf(IllegalArgumentException.class)
  64. .hasMessageContaining("New code definition type 'NUMBER_OF_DAYS' requires a newCodeDefinitionValue");
  65. }
  66. @Test
  67. public void createNewCodeDefinition_throw_IAE_if_days_is_invalid() {
  68. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), "unknown"))
  69. .isInstanceOf(IllegalArgumentException.class)
  70. .hasMessageContaining("Failed to parse number of days: unknown");
  71. }
  72. @Test
  73. public void createNewCodeDefinition_throw_IAE_if_value_is_set_for_reference_branch() {
  74. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, REFERENCE_BRANCH.name(), "feature/zw"))
  75. .isInstanceOf(IllegalArgumentException.class)
  76. .hasMessageContaining("Unexpected value for newCodeDefinitionType 'REFERENCE_BRANCH'");
  77. }
  78. @Test
  79. public void createNewCodeDefinition_throw_IAE_if_previous_version_type_and_value_provided() {
  80. assertThatThrownBy(() -> newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, PREVIOUS_VERSION.name(), "10.2.3"))
  81. .isInstanceOf(IllegalArgumentException.class)
  82. .hasMessageContaining("Unexpected value for newCodeDefinitionType 'PREVIOUS_VERSION'");
  83. }
  84. @Test
  85. public void createNewCodeDefinition_persist_previous_version_type() {
  86. newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, PREVIOUS_VERSION.name(), null);
  87. Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
  88. assertThat(newCodePeriodDto).map(NewCodePeriodDto::getType).hasValue(PREVIOUS_VERSION);
  89. }
  90. @Test
  91. public void createNewCodeDefinition_return_days_value_for_number_of_days_type() {
  92. String numberOfDays = "30";
  93. newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, NUMBER_OF_DAYS.name(), numberOfDays);
  94. Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
  95. assertThat(newCodePeriodDto)
  96. .isPresent()
  97. .get()
  98. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
  99. .containsExactly(NUMBER_OF_DAYS, numberOfDays);
  100. }
  101. @Test
  102. public void createNewCodeDefinition_return_branch_value_for_reference_branch_type() {
  103. newCodeDefinitionResolver.createNewCodeDefinition(dbSession, DEFAULT_PROJECT_ID, MAIN_BRANCH_UUID, MAIN_BRANCH, REFERENCE_BRANCH.name(), null);
  104. Optional<NewCodePeriodDto> newCodePeriodDto = dbClient.newCodePeriodDao().selectByProject(dbSession, DEFAULT_PROJECT_ID);
  105. assertThat(newCodePeriodDto)
  106. .isPresent()
  107. .get()
  108. .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid, NewCodePeriodDto::getProjectUuid)
  109. .containsExactly(REFERENCE_BRANCH, MAIN_BRANCH, null, DEFAULT_PROJECT_ID);
  110. }
  111. @Test
  112. public void checkNewCodeDefinitionParam_throw_IAE_if_newCodeDefinitionValue_is_provided_without_newCodeDefinitionType() {
  113. assertThatThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam(null, "anyvalue"))
  114. .isInstanceOf(IllegalArgumentException.class)
  115. .hasMessageContaining("New code definition type is required when new code definition value is provided");
  116. }
  117. @Test
  118. public void checkNewCodeDefinitionParam_do_not_throw_when_both_value_and_type_are_provided() {
  119. assertThatNoException()
  120. .isThrownBy(() -> newCodeDefinitionResolver.checkNewCodeDefinitionParam("PREVIOUS_VERSION", "anyvalue"));
  121. }
  122. }