From c6a3cea9ae3c68a4b5c2ea48886c771c571fd5e3 Mon Sep 17 00:00:00 2001 From: OrlovAlexander Date: Wed, 13 Nov 2024 16:03:18 +0100 Subject: [PATCH] SONAR-23593 /clean-code-policy/rules accepts type and severity --- .../controller/DefaultRuleController.java | 4 +- .../rule/request/RuleCreateRestRequest.java | 16 +++- .../controller/DefaultRuleControllerTest.java | 79 ++++++++++++++----- 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java index 5d519325e3f..d625791c865 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/controller/DefaultRuleController.java @@ -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 params = new HashMap<>(); diff --git a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java index 022a2fcba99..6552f28df0d 100644 --- a/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java +++ b/server/sonar-webserver-webapi-v2/src/main/java/org/sonar/server/v2/api/rule/request/RuleCreateRestRequest.java @@ -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 parameters, - @NotNull + @Nullable @Schema(description = "Clean code attribute") CleanCodeAttributeRestEnum cleanCodeAttribute, @Valid - @NotEmpty + @NotNull @Schema(description = "Impacts") - List impacts + List impacts, + + @Nullable + @Schema(description = "Severity") + String severity, + + @Nullable + @Schema(description = "Rule type") + RuleType type ) { } diff --git a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/rule/controller/DefaultRuleControllerTest.java b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/rule/controller/DefaultRuleControllerTest.java index 843e0b091d4..e1d1c60c401 100644 --- a/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/rule/controller/DefaultRuleControllerTest.java +++ b/server/sonar-webserver-webapi-v2/src/test/java/org/sonar/server/v2/api/rule/controller/DefaultRuleControllerTest.java @@ -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 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() { -- 2.39.5