]> source.dussan.org Git - sonarqube.git/commitdiff
Improve test coverage
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 11 Jul 2019 20:45:07 +0000 (15:45 -0500)
committerSonarTech <sonartech@sonarsource.com>
Fri, 12 Jul 2019 18:21:15 +0000 (20:21 +0200)
sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultNewRuleTest.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRepositoryTest.java [new file with mode: 0644]
sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRuleTest.java [new file with mode: 0644]

index 361785b49f72b80f17a509a218f5bb0b09736946..eb6ef4086fc830439b2fca1bf43dde17191345fb 100644 (file)
@@ -25,9 +25,12 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleScope;
 import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rules.RuleType;
+import org.sonar.api.server.debt.DebtRemediationFunction;
 import org.sonar.api.server.rule.RulesDefinition;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
 
 public class DefaultNewRuleTest {
   @Rule
@@ -75,8 +78,44 @@ public class DefaultNewRuleTest {
 
     rule.addDeprecatedRuleKey("deprecatedrepo", "deprecatedkey");
     assertThat(rule.deprecatedRuleKeys()).containsOnly(RuleKey.of("deprecatedrepo", "deprecatedkey"));
+
+    rule.setStatus(RuleStatus.READY);
+    assertThat(rule.status()).isEqualTo(RuleStatus.READY);
+
+    rule.addCwe(12);
+    rule.addCwe(10);
+    assertThat(rule.securityStandards()).containsOnly("cwe:10", "cwe:12");
+
+    rule.setType(RuleType.SECURITY_HOTSPOT);
+    assertThat(rule.type()).isEqualTo(RuleType.SECURITY_HOTSPOT);
+
+    DebtRemediationFunction f = mock(DebtRemediationFunction.class);
+    rule.setDebtRemediationFunction(f);
+    assertThat(rule.debtRemediationFunction()).isEqualTo(f);
+
+    rule.setSeverity("MAJOR");
+    assertThat(rule.severity()).isEqualTo("MAJOR");
   }
 
+  @Test
+  public void validate_fails() {
+    rule.setHtmlDescription("html");
+    exception.expect(IllegalStateException.class);
+    rule.validate();
+  }
+
+  @Test
+  public void validate_succeeds() {
+    rule.setHtmlDescription("html");
+    rule.setName("name");
+    rule.validate();
+  }
+
+  @Test
+  public void set_markdown_description() {
+    rule.setMarkdownDescription("markdown");
+    assertThat(rule.markdownDescription()).isEqualTo("markdown");
+  }
   @Test
   public void fail_if_severity_is_invalid() {
     exception.expect(IllegalArgumentException.class);
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRepositoryTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRepositoryTest.java
new file mode 100644 (file)
index 0000000..96ef99a
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.api.impl.server;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class DefaultRepositoryTest {
+  @Test
+  public void create_simple_repo() {
+    RulesDefinitionContext ctx = mock(RulesDefinitionContext.class);
+    DefaultNewRepository newRepo = new DefaultNewRepository(ctx, "key", "lang", false);
+    newRepo.createRule("rule1")
+      .setName("rule1")
+      .setHtmlDescription("desc");
+    newRepo.setName("name");
+    DefaultRepository repo = new DefaultRepository(newRepo, null);
+
+    assertThat(repo.isExternal()).isFalse();
+    assertThat(repo.key()).isEqualTo("key");
+    assertThat(repo.language()).isEqualTo("lang");
+    assertThat(repo.isExternal()).isFalse();
+    assertThat(repo.name()).isEqualTo("name");
+    assertThat(repo.rules()).extracting(r -> r.key()).containsOnly("rule1");
+
+  }
+}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRuleTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRuleTest.java
new file mode 100644 (file)
index 0000000..9be301c
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.api.impl.server;
+
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleScope;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rules.RuleType;
+import org.sonar.api.server.debt.DebtRemediationFunction;
+import org.sonar.api.server.rule.RulesDefinition;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class DefaultRuleTest {
+  @Test
+  public void getters() {
+    DefaultRepository repo = mock(DefaultRepository.class);
+    DefaultNewRule rule = new DefaultNewRule("plugin", "repo", "key");
+
+    rule.setScope(RuleScope.MAIN);
+    rule.setName("   name  ");
+    rule.setHtmlDescription("   html  ");
+    rule.setTemplate(true);
+    rule.setActivatedByDefault(true);
+    RulesDefinition.NewParam param1 = rule.createParam("param1");
+    rule.setTags("tag1", "tag2");
+    rule.addTags("tag3");
+    rule.setEffortToFixDescription("effort");
+    rule.setGapDescription("gap");
+    rule.setInternalKey("internal");
+    rule.addDeprecatedRuleKey("deprecatedrepo", "deprecatedkey");
+    rule.setStatus(RuleStatus.READY);
+    rule.addCwe(12);
+    rule.addCwe(10);
+    rule.setType(RuleType.SECURITY_HOTSPOT);
+    DebtRemediationFunction f = mock(DebtRemediationFunction.class);
+    rule.setDebtRemediationFunction(f);
+    rule.setSeverity("MAJOR");
+
+    DefaultRule defaultRule = new DefaultRule(repo, rule);
+    assertThat(defaultRule.scope()).isEqualTo(RuleScope.MAIN);
+    assertThat(defaultRule.name()).isEqualTo("name");
+    assertThat(defaultRule.htmlDescription()).isEqualTo("html");
+    assertThat(defaultRule.template()).isTrue();
+    assertThat(defaultRule.activatedByDefault()).isTrue();
+    assertThat(defaultRule.params()).containsOnly(new DefaultParam(new DefaultNewParam("param1")));
+    assertThat(defaultRule.tags()).containsOnly("tag1", "tag2", "tag3");
+    assertThat(defaultRule.effortToFixDescription()).isEqualTo("gap");
+    assertThat(defaultRule.gapDescription()).isEqualTo("gap");
+    assertThat(defaultRule.internalKey()).isEqualTo("internal");
+    assertThat(defaultRule.deprecatedRuleKeys()).containsOnly(RuleKey.of("deprecatedrepo", "deprecatedkey"));
+    assertThat(defaultRule.status()).isEqualTo(RuleStatus.READY);
+    assertThat(rule.securityStandards()).containsOnly("cwe:10", "cwe:12");
+    assertThat(defaultRule.type()).isEqualTo(RuleType.SECURITY_HOTSPOT);
+    assertThat(defaultRule.debtRemediationFunction()).isEqualTo(f);
+    assertThat(defaultRule.markdownDescription()).isNull();
+    assertThat(defaultRule.severity()).isEqualTo("MAJOR");
+  }
+
+  @Test
+  public void to_string() {
+    DefaultRepository repo = mock(DefaultRepository.class);
+    DefaultNewRule rule = new DefaultNewRule("plugin", "repo", "key");
+    DefaultRule defaultRule = new DefaultRule(repo, rule);
+
+    assertThat(defaultRule.toString()).isEqualTo("[repository=repo, key=key]");
+  }
+}