]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23593 /clean-code-policy/rules accepts type and severity
authorOrlovAlexander <alexander.orlov@sonarsource.com>
Wed, 13 Nov 2024 15:03:18 +0000 (16:03 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 15 Nov 2024 20:02:42 +0000 (20:02 +0000)
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java
server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java
server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/rule/controller/DefaultRuleControllerTest.java

index 5d519325e3f122e0fa72018b1fa1baf6ede17c82..d625791c865f0402a577b366231fbb84cbdc0a3a 100644 (file)
@@ -67,8 +67,10 @@ public class DefaultRuleController implements RuleController {
       .setName(request.name())
       .setMarkdownDescription(request.markdownDescription())
       .setStatus(ofNullable(request.status()).map(RuleStatusRestEnum::getRuleStatus).orElse(null))
-      .setCleanCodeAttribute(request.cleanCodeAttribute().getCleanCodeAttribute())
+      .setCleanCodeAttribute(request.cleanCodeAttribute() != null ? request.cleanCodeAttribute().getCleanCodeAttribute() : null)
       .setImpacts(request.impacts().stream().map(DefaultRuleController::toNewCustomRuleImpact).toList())
+      .setType(request.type())
+      .setSeverity(request.severity())
       .setPreventReactivation(true);
     if (request.parameters() != null) {
       Map<String, String> params = new HashMap<>();
index 022a2fcba990fbb235b8da06991cef80094b2235..6552f28df0d83b76ba6fb537bb9e0bbf08fdb68a 100644 (file)
@@ -23,9 +23,9 @@ import io.swagger.v3.oas.annotations.media.Schema;
 import java.util.List;
 import javax.annotation.Nullable;
 import javax.validation.Valid;
-import javax.validation.constraints.NotEmpty;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
+import org.sonar.api.rules.RuleType;
 import org.sonar.server.v2.api.rule.enums.CleanCodeAttributeRestEnum;
 import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
 import org.sonar.server.v2.api.rule.resource.Impact;
@@ -60,13 +60,21 @@ public record RuleCreateRestRequest(
   @Schema(description = "Custom rule parameters")
   List<Parameter> parameters,
 
-  @NotNull
+  @Nullable
   @Schema(description = "Clean code attribute")
   CleanCodeAttributeRestEnum cleanCodeAttribute,
 
   @Valid
-  @NotEmpty
+  @NotNull
   @Schema(description = "Impacts")
-  List<Impact> impacts
+  List<Impact> impacts,
+
+  @Nullable
+  @Schema(description = "Severity")
+  String severity,
+
+  @Nullable
+  @Schema(description = "Rule type")
+  RuleType type
 ) {
 }
index 843e0b091d4edce42ad131bcfdd6a4cd5615afc6..e1d1c60c40197cae0f9b607e5aa5f885c18ea97a 100644 (file)
@@ -23,13 +23,12 @@ import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import java.util.List;
 import javax.annotation.Nullable;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleType;
 import org.sonar.db.rule.RuleDescriptionSectionDto;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleTesting;
@@ -43,9 +42,7 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.v2.api.ControllerTester;
 import org.sonar.server.v2.api.rule.converter.RuleRestResponseGenerator;
 import org.sonar.server.v2.api.rule.enums.CleanCodeAttributeRestEnum;
-import org.sonar.server.v2.api.rule.enums.ImpactSeverityRestEnum;
 import org.sonar.server.v2.api.rule.enums.RuleStatusRestEnum;
-import org.sonar.server.v2.api.rule.enums.SoftwareQualityRestEnum;
 import org.sonar.server.v2.api.rule.request.RuleCreateRestRequest;
 import org.sonar.server.v2.api.rule.resource.Impact;
 import org.sonar.server.v2.api.rule.resource.Parameter;
@@ -59,16 +56,21 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.sonar.api.rule.Severity.BLOCKER;
+import static org.sonar.api.rules.RuleType.BUG;
 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
 import static org.sonar.server.v2.WebApiEndpoints.RULES_ENDPOINT;
+import static org.sonar.server.v2.api.rule.enums.CleanCodeAttributeRestEnum.MODULAR;
+import static org.sonar.server.v2.api.rule.enums.ImpactSeverityRestEnum.LOW;
+import static org.sonar.server.v2.api.rule.enums.SoftwareQualityRestEnum.MAINTAINABILITY;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
-@RunWith(MockitoJUnitRunner.class)
-public class DefaultRuleControllerTest {
 
-  @Rule
+class DefaultRuleControllerTest {
+
+  @RegisterExtension
   public UserSessionRule userSession = UserSessionRule.standalone();
 
   private final RuleService ruleService = mock(RuleService.class);
@@ -82,8 +84,8 @@ public class DefaultRuleControllerTest {
     ruleRestResponseGenerator));
   private static final Gson gson = new GsonBuilder().create();
 
-  @Before
-  public void setUp() {
+  @BeforeEach
+  void setUp() {
     when(macroInterpreter.interpret(anyString())).thenAnswer(invocation -> "interpreted" + invocation.getArgument(0));
     when(ruleDescriptionFormatter.toHtml(any(), any())).thenAnswer(invocation -> "html" + ((RuleDescriptionSectionDto) invocation.getArgument(1)).getContent());
     when(languages.get(anyString())).thenAnswer(invocation -> LanguageTesting.newLanguage(invocation.getArgument(0, String.class),
@@ -91,7 +93,7 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_shouldReturnRule() throws Exception {
+  void create_shouldReturnRule() throws Exception {
     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
 
     RuleInformation ruleInformation = generateRuleInformation();
@@ -109,7 +111,26 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_whenNotLoggedIn_shouldFailWithUnauthorized() throws Exception {
+  void create_whenOldSeverityPassed_ShouldReturnRule() throws Exception {
+    userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
+
+    RuleInformation ruleInformation = generateRuleInformation();
+    when(ruleService.createCustomRule(any())).thenReturn(ruleInformation);
+
+    RuleCreateRestRequest request = newRequest("rule1", null, List.of(), BLOCKER, BUG);
+    MvcResult mvcResult = mockMvc.perform(
+        post(RULES_ENDPOINT)
+          .contentType(MediaType.APPLICATION_JSON_VALUE)
+          .content(gson.toJson(request)))
+      .andExpect(status().isOk())
+      .andReturn();
+
+    RuleRestResponse response = gson.fromJson(mvcResult.getResponse().getContentAsString(), RuleRestResponse.class);
+    assertThat(response).isEqualTo(ruleRestResponseGenerator.toRuleRestResponse(ruleInformation));
+  }
+
+  @Test
+  void create_whenNotLoggedIn_shouldFailWithUnauthorized() throws Exception {
     mockMvc.perform(
         post(RULES_ENDPOINT)
           .contentType(MediaType.APPLICATION_JSON_VALUE)
@@ -118,7 +139,7 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_whenNoPermission_shouldFailWithForbidden() throws Exception {
+  void create_whenNoPermission_shouldFailWithForbidden() throws Exception {
     userSession.logIn();
 
     mockMvc.perform(
@@ -129,7 +150,7 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_whenReactivationExceptionThrown_shouldFailWithConflict() throws Exception {
+  void create_whenReactivationExceptionThrown_shouldFailWithConflict() throws Exception {
     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
 
     String errorMessage = "reactivation_exception";
@@ -143,7 +164,7 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_whenMissingBodyField_shouldFailWithBadRequest() throws Exception {
+  void create_whenMissingBodyField_shouldFailWithBadRequest() throws Exception {
     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
 
     RuleCreateRestRequest request = newRequest(null);
@@ -156,7 +177,7 @@ public class DefaultRuleControllerTest {
   }
 
   @Test
-  public void create_whenInvalidParam_shouldFailWithBadRequest() throws Exception {
+  void create_whenInvalidParam_shouldFailWithBadRequest() throws Exception {
     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
 
     RuleCreateRestRequest request = newRequest("a".repeat(201));
@@ -182,8 +203,26 @@ public class DefaultRuleControllerTest {
       "some desc",
       RuleStatusRestEnum.BETA,
       List.of(new Parameter("key1", "desc", "value1", "text")),
-      CleanCodeAttributeRestEnum.MODULAR,
-      List.of(new Impact(SoftwareQualityRestEnum.MAINTAINABILITY, ImpactSeverityRestEnum.LOW)));
+      MODULAR,
+      List.of(new Impact(MAINTAINABILITY, LOW)),
+      null,
+      null);
+  }
+
+  private static RuleCreateRestRequest newRequest(@Nullable String name, @Nullable CleanCodeAttributeRestEnum cleanCodeAttribute,
+    @Nullable List<Impact> impacts, @Nullable String severity, @Nullable RuleType type) {
+    return new RuleCreateRestRequest(
+      "java:custom_rule",
+      "java:template_rule",
+      name,
+      "some desc",
+      RuleStatusRestEnum.BETA,
+      List.of(new Parameter("key1", "desc", "value1", "text")),
+      cleanCodeAttribute,
+      impacts,
+      severity,
+      type
+    );
   }
 
   private RuleInformation generateRuleInformation() {