diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-07-11 15:45:07 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-07-12 20:21:15 +0200 |
commit | 5e13f199d644db04bb7668751203743d629690fe (patch) | |
tree | db2193805d9d7a2ed248a957e60d5e9e7ffe1863 /sonar-plugin-api-impl | |
parent | 75e8f9867e908f71218d88f8c97f94648c353426 (diff) | |
download | sonarqube-5e13f199d644db04bb7668751203743d629690fe.tar.gz sonarqube-5e13f199d644db04bb7668751203743d629690fe.zip |
Improve test coverage
Diffstat (limited to 'sonar-plugin-api-impl')
3 files changed, 173 insertions, 0 deletions
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultNewRuleTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultNewRuleTest.java index 361785b49f7..eb6ef4086fc 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultNewRuleTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultNewRuleTest.java @@ -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,9 +78,45 @@ 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); rule.setSeverity("invalid"); 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 index 00000000000..96ef99af464 --- /dev/null +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRepositoryTest.java @@ -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 index 00000000000..9be301cd9ca --- /dev/null +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/impl/server/DefaultRuleTest.java @@ -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]"); + } +} |