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;
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;
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);
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),
}
@Test
- public void create_shouldReturnRule() throws Exception {
+ void create_shouldReturnRule() throws Exception {
userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
RuleInformation ruleInformation = generateRuleInformation();
}
@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)
}
@Test
- public void create_whenNoPermission_shouldFailWithForbidden() throws Exception {
+ void create_whenNoPermission_shouldFailWithForbidden() throws Exception {
userSession.logIn();
mockMvc.perform(
}
@Test
- public void create_whenReactivationExceptionThrown_shouldFailWithConflict() throws Exception {
+ void create_whenReactivationExceptionThrown_shouldFailWithConflict() throws Exception {
userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
String errorMessage = "reactivation_exception";
}
@Test
- public void create_whenMissingBodyField_shouldFailWithBadRequest() throws Exception {
+ void create_whenMissingBodyField_shouldFailWithBadRequest() throws Exception {
userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
RuleCreateRestRequest request = newRequest(null);
}
@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));
"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() {