org.sonar.api.server.debt.DebtRemediationFunction Java Examples

The following examples show how to use org.sonar.api.server.debt.DebtRemediationFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TsLintRule.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
public TsLintRule(
    String key,
    String severity,
    String name,
    String htmlDescription
) {
    this.key = key;
    this.severity = severity;
    this.name = name;
    this.htmlDescription = htmlDescription;
    
    this.hasDebtRemediation = false;
    this.debtRemediationFunction = DebtRemediationFunction.Type.CONSTANT_ISSUE;
    this.debtRemediationScalar = "0min";
    this.debtRemediationOffset = "0min";
    this.debtType = null;
}
 
Example #2
Source File: TsLintRule.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
public TsLintRule(
    String key,
    String severity,
    String name,
    String htmlDescription,
    DebtRemediationFunction.Type debtRemediationFunction,
    String debtRemediationScalar,
    String debtRemediationOffset,
    String debtType
) {
    this.key = key;
    this.severity = severity;
    this.name = name;
    this.htmlDescription = htmlDescription;

    this.hasDebtRemediation = true;
    this.debtRemediationFunction = debtRemediationFunction;
    this.debtRemediationScalar = debtRemediationScalar;
    this.debtRemediationOffset = debtRemediationOffset;
    this.debtType = debtType;
}
 
Example #3
Source File: TsLintRuleTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void ruleWithDebtRemediation() {
    TsLintRule rule = new TsLintRule(
        "key",
        Severity.MAJOR,
        "name",
        "<html></html>",
        DebtRemediationFunction.Type.LINEAR_OFFSET,
        "1min",
        "2min",
        RuleType.CODE_SMELL.name()
    );

    assertEquals("key", rule.key);
    assertEquals(Severity.MAJOR, rule.severity);
    assertEquals("name", rule.name);
    assertEquals("<html></html>", rule.htmlDescription);
    assertEquals(true, rule.hasDebtRemediation);
    assertEquals(DebtRemediationFunction.Type.LINEAR_OFFSET, rule.debtRemediationFunction);
    assertEquals("1min", rule.debtRemediationScalar);
    assertEquals("2min", rule.debtRemediationOffset);
    assertEquals(RuleType.CODE_SMELL.name(), rule.debtType);
}
 
Example #4
Source File: TsLintRuleTest.java    From SonarTsPlugin with MIT License 6 votes vote down vote up
@Test
public void ruleWithoutDebtRemediation() {
    TsLintRule rule = new TsLintRule(
        "key",
        Severity.MAJOR,
        "name",
        "<html></html>"
    );

    assertEquals("key", rule.key);
    assertEquals(Severity.MAJOR, rule.severity);
    assertEquals("name", rule.name);
    assertEquals("<html></html>", rule.htmlDescription);
    assertEquals(false, rule.hasDebtRemediation);
    assertEquals(DebtRemediationFunction.Type.CONSTANT_ISSUE, rule.debtRemediationFunction);
    assertEquals("0min", rule.debtRemediationScalar);
    assertEquals("0min", rule.debtRemediationOffset);
    assertEquals(null, rule.debtType);
}
 
Example #5
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
    if (func.startsWith("Constant")) {
        return drf.constantPerIssue(constantCost.replace("mn", "min"));
    }
    if ("Linear".equals(func)) {
        return drf.linear(linearFactor.replace("mn", "min"));
    }
    return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));
}
 
Example #6
Source File: SonarDefinition.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
    if (func.startsWith("Constant")) {
        return drf.constantPerIssue(constantCost.replace("mn", "min"));
    }
    if ("Linear".equals(func)) {
        return drf.linear(linearFactor.replace("mn", "min"));
    }
    return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));
}
 
Example #7
Source File: Remediation.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
public DebtRemediationFunction remediationFunction(DebtRemediationFunctions debt) {
    if (func.startsWith("Constant")) {
        return debt.constantPerIssue(constantCost.replace("mn", "min"));
    }
    if ("Linear".equals(func)) {
        return debt.linear(linearFactor.replace("mn", "min"));
    }
    return debt.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));
}
 
Example #8
Source File: RemediationTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Test
public void testRemediationFunctionConstantPerIssue() {
    remediation.func = "Constant issue";
    remediation.constantCost = "3 mn";
    DebtRemediationFunctions functions = mock(DebtRemediationFunctions.class);
    DebtRemediationFunction function = mock(DebtRemediationFunction.class);
    when(functions.constantPerIssue(eq(remediation.constantCost))).thenReturn(function);
    when(function.coefficient()).thenReturn("3 min");
    remediation.remediationFunction(functions);
    verify(functions).constantPerIssue("3 min");
    assertEquals("3 min",function.coefficient());
}
 
Example #9
Source File: RemediationTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Test
public void testRemediationFunctionLinear() {
    remediation.func = "Linear";
    remediation.linearFactor = "2 mn";
    DebtRemediationFunctions functions = mock(DebtRemediationFunctions.class);
    DebtRemediationFunction function = mock(DebtRemediationFunction.class);
    when(functions.linear(eq(remediation.linearFactor))).thenReturn(function);
    when(function.coefficient()).thenReturn("2 min");
    remediation.remediationFunction(functions);
    verify(functions).linear("2 min");
    assertEquals("2 min",function.coefficient());
}
 
Example #10
Source File: RemediationTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Test
public void testRemediationFunctionlinearWithOffset() {
    remediation.func = "somethingElse";
    remediation.linearFactor = "2 mn";
    remediation.linearOffset = "2 mn";
    DebtRemediationFunctions functions = mock(DebtRemediationFunctions.class);
    DebtRemediationFunction function = mock(DebtRemediationFunction.class);
    when(functions.linearWithOffset(eq(remediation.linearFactor), eq(remediation.linearOffset))).thenReturn(function);
    when(function.coefficient()).thenReturn("2 min");
    remediation.remediationFunction(functions);
    verify(functions).linearWithOffset("2 min", "2 min");
    assertEquals("2 min",function.coefficient());
}
 
Example #11
Source File: SmellRulesDefinition.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 5 votes vote down vote up
private DebtRemediationFunction remediationFunction(final DebtRemediationFunctions drf) {
    if (this.func.startsWith("Constant")) {
        return drf.constantPerIssue(this.constantCost.replace("mn", "min"));
    }
    if ("Linear".equals(this.func)) {
        return drf.linear(this.coeff.replace("mn", "min"));
    }
    return drf.linearWithOffset(this.coeff.replace("mn", "min"), this.linearOffset.replace("mn", "min"));
}
 
Example #12
Source File: WebDriverRulesDefinition.java    From sonar-webdriver-plugin with MIT License 5 votes vote down vote up
public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
  if (func.startsWith("Constant")) {
    return drf.constantPerIssue(constantCost.replace("mn", "min"));
  }
  if ("Linear".equals(func)) {
    return drf.linear(linearFactor.replace("mn", "min"));
  }
  return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));
}
 
Example #13
Source File: TsRulesDefinitionTest.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
@Test
public void ConfiguresAdditionalRules() {
    // cfg1
    Rule rule1 = getRule("custom-rule-1");
    assertNull(rule1);

    // cfg2
    Rule rule2 = getRule("custom-rule-2");
    assertNotNull(rule2);
    assertEquals("test rule #2", rule2.name());
    assertEquals(Severity.MINOR, rule2.severity());
    assertEquals("#2 description", rule2.htmlDescription());
    assertEquals(null, rule2.debtRemediationFunction());
    assertEquals(RuleType.CODE_SMELL, rule2.type());

    // cfg3
    Rule rule3 = getRule("custom-rule-3");
    assertNotNull(rule3);
    assertEquals("test rule #3", rule3.name());
    assertEquals(Severity.INFO, rule3.severity());
    assertEquals("#3 description", rule3.htmlDescription());
    assertEquals(
        DebtRemediationFunction.Type.CONSTANT_ISSUE,
        rule3.debtRemediationFunction().type()
    );
    assertEquals(null, rule3.debtRemediationFunction().gapMultiplier());
    assertEquals("15min", rule3.debtRemediationFunction().baseEffort());
    assertEquals(RuleType.CODE_SMELL, rule3.type());

    // cfg4
    Rule rule4 = getRule("custom-rule-4");
    assertNotNull(rule4);
    assertEquals("test rule #4", rule4.name());
    assertEquals(Severity.MINOR, rule4.severity());
    assertEquals("#4 description", rule4.htmlDescription());
    assertEquals(
        DebtRemediationFunction.Type.LINEAR,
        rule4.debtRemediationFunction().type()
    );
    assertEquals("5min", rule4.debtRemediationFunction().gapMultiplier());
    assertEquals(null, rule4.debtRemediationFunction().baseEffort());
    assertEquals(RuleType.BUG, rule4.type());

    // cfg5
    Rule rule5 = getRule("custom-rule-5");
    assertNotNull(rule5);
    assertEquals("test rule #5", rule5.name());
    assertEquals(Severity.MAJOR, rule5.severity());
    assertEquals("#5 description", rule5.htmlDescription());
    assertEquals(RuleType.VULNERABILITY, rule5.type());

    assertEquals("30min", rule5.debtRemediationFunction().gapMultiplier());
    assertEquals("15min", rule5.debtRemediationFunction().baseEffort());
}
 
Example #14
Source File: TsRulesDefinitionTest.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {

    this.settings = mock(Settings.class);

    when(this.settings.getKeysStartingWith(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS))
        .thenReturn(new ArrayList<String>() {{
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg1.name");
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg1.config");
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg2.name");
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg2.config");
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg3.name");
            add(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg3.config");
        }});

    // config with one disabled rule
    when(this.settings.getString(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg1.config"))
        .thenReturn(
            "custom-rule-1=false\n" +
            "custom-rule-1.name=test rule #1\n" +
            "custom-rule-1.severity=MAJOR\n" +
            "custom-rule-1.description=#1 description\n" +
            "\n"
        );

    // config with a basic rule (no debt settings)
    when(this.settings.getString(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg2.config"))
        .thenReturn(
            "custom-rule-2=true\n" +
            "custom-rule-2.name=test rule #2\n" +
            "custom-rule-2.severity=MINOR\n" +
            "custom-rule-2.description=#2 description\n" +
            "\n"
        );

    // config with a advanced rules (including debt settings)
    when(this.settings.getString(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS + ".cfg3.config"))
        .thenReturn(
            "custom-rule-3=true\n" +
            "custom-rule-3.name=test rule #3\n" +
            "custom-rule-3.severity=INFO\n" +
            "custom-rule-3.description=#3 description\n" +
            "custom-rule-3.debtFunc=" + DebtRemediationFunction.Type.CONSTANT_ISSUE + "\n" +
            "custom-rule-3.debtScalar=15min\n" +
            "custom-rule-3.debtOffset=1min\n" +
            "custom-rule-3.debtType=INVALID_TYPE_GOES_HERE\n" +
            "\n" +
            "custom-rule-4=true\n" +
            "custom-rule-4.name=test rule #4\n" +
            "custom-rule-4.severity=MINOR\n" +
            "custom-rule-4.description=#4 description\n" +
            "custom-rule-4.debtFunc=" + DebtRemediationFunction.Type.LINEAR + "\n" +
            "custom-rule-4.debtScalar=5min\n" +
            "custom-rule-4.debtOffset=2h\n" +
            "custom-rule-4.debtType=" + RuleType.BUG.name() + "\n" +
            "\n" +
            "custom-rule-5=true\n" +
            "custom-rule-5.name=test rule #5\n" +
            "custom-rule-5.severity=MAJOR\n" +
            "custom-rule-5.description=#5 description\n" +
            "custom-rule-5.debtFunc=" + DebtRemediationFunction.Type.LINEAR_OFFSET + "\n" +
            "custom-rule-5.debtScalar=30min\n" +
            "custom-rule-5.debtOffset=15min\n" +
            "custom-rule-5.debtType=" + RuleType.VULNERABILITY.name() + "\n" +
            "\n"
        );

    this.definition = new TsRulesDefinition(this.settings);
    this.context = new Context();
    this.definition.define(context);
}
 
Example #15
Source File: TsRulesDefinition.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
private void createRule(NewRepository repository, TsLintRule tsRule) {
    NewRule sonarRule =
                repository
                .createRule(tsRule.key)
                .setName(tsRule.name)
                .setSeverity(tsRule.severity)
                .setHtmlDescription(tsRule.htmlDescription)
                .setStatus(RuleStatus.READY);

    if (tsRule.hasDebtRemediation) {
        DebtRemediationFunction debtRemediationFn = null;
        DebtRemediationFunctions funcs = sonarRule.debtRemediationFunctions();

        switch (tsRule.debtRemediationFunction)
        {
            case LINEAR:
                debtRemediationFn = funcs.linear(tsRule.debtRemediationScalar);
                break;

            case LINEAR_OFFSET:
                debtRemediationFn = funcs.linearWithOffset(tsRule.debtRemediationScalar, tsRule.debtRemediationOffset);
                break;

            case CONSTANT_ISSUE:
                debtRemediationFn = funcs.constantPerIssue(tsRule.debtRemediationScalar);
                break;
        }

        sonarRule.setDebtRemediationFunction(debtRemediationFn);
    }

    RuleType type = null;

    if (tsRule.debtType != null && RuleType.names().contains(tsRule.debtType)) {
        // Try and parse it as a new-style rule type (since 5.5 SQALE's been replaced
        // with something simpler, and there's really only three buckets)
        type = RuleType.valueOf(tsRule.debtType);
    }

    if (type == null) {
        type = RuleType.CODE_SMELL;
    }

    sonarRule.setType(type);
}
 
Example #16
Source File: TsRulesDefinition.java    From SonarTsPlugin with MIT License 4 votes vote down vote up
public static void loadRules(InputStream stream, List<TsLintRule> rulesCollection) {
    Properties properties = new Properties();

    try {
        properties.load(stream);
    } catch (IOException e) {
        LOG.error("Error while loading TsLint rules", e);
    }

    for(String propKey : properties.stringPropertyNames()) {
        if (propKey.contains(".")) {
            continue;
        }

        String ruleEnabled = properties.getProperty(propKey);

        if (!"true".equals(ruleEnabled)) {
            continue;
        }

        String ruleId = propKey;
        String ruleName = properties.getProperty(propKey + ".name", ruleId.replace("-", " "));
        String ruleSeverity = properties.getProperty(propKey + ".severity", DEFAULT_RULE_SEVERITY);
        String ruleDescription = properties.getProperty(propKey + ".description", DEFAULT_RULE_DESCRIPTION);

        String debtRemediationFunction = properties.getProperty(propKey + ".debtFunc", null);
        String debtRemediationScalar = properties.getProperty(propKey + ".debtScalar", DEFAULT_RULE_DEBT_SCALAR);
        String debtRemediationOffset = properties.getProperty(propKey + ".debtOffset", DEFAULT_RULE_DEBT_OFFSET);
        String debtType = properties.getProperty(propKey + ".debtType", DEFAULT_RULE_DEBT_TYPE);

        TsLintRule tsRule = null;

        // try to apply the specified debt remediation function
        if (debtRemediationFunction != null) {
            DebtRemediationFunction.Type debtRemediationFunctionEnum = DebtRemediationFunction.Type.valueOf(debtRemediationFunction);

            tsRule = new TsLintRule(
                ruleId,
                ruleSeverity,
                ruleName,
                ruleDescription,
                debtRemediationFunctionEnum,
                debtRemediationScalar,
                debtRemediationOffset,
                debtType
            );
        }

        // no debt remediation function specified
        if (tsRule == null) {
            tsRule = new TsLintRule(
                ruleId,
                ruleSeverity,
                ruleName,
                ruleDescription
            );
        }

        rulesCollection.add(tsRule);
    }

    Collections.sort(rulesCollection, (TsLintRule r1, TsLintRule r2) -> r1.key.compareTo(r2.key));
}
 
Example #17
Source File: SmellRulesDefinitionTest.java    From qualinsight-plugins-sonarqube-smell with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void define_should_createRepositoryAndRegisterAllSmellPluginChecks() {
    Mockito.when(this.repository.key())
        .thenReturn(EXPECTED_REPOSITORY_KEY);
    Mockito.when(this.rule.key())
        .thenReturn(LAST_RULE_KEY);
    // Some plumbering...
    Mockito.when(this.context.createRepository(Matchers.anyString(), Matchers.anyString()))
        .thenReturn(this.repository);
    Mockito.when(this.repository.setName(Matchers.anyString()))
        .thenReturn(this.repository);
    Mockito.when(this.repository.rules())
        .thenReturn(ImmutableList.<NewRule> of(this.rule));
    Mockito.when(this.repository.createRule(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.repository.rule(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setName(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setHtmlDescription(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setSeverity(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setTemplate(Matchers.anyBoolean()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setStatus(Matchers.any(RuleStatus.class)))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setTags(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setDebtSubCharacteristic(Matchers.anyString()))
        .thenReturn(this.rule);
    Mockito.when(this.rule.setDebtRemediationFunction(Matchers.any(DebtRemediationFunction.class)))
        .thenReturn(this.rule);
    Mockito.when(this.rule.debtRemediationFunctions())
        .thenReturn(this.functions);
    Mockito.when(this.rule.setEffortToFixDescription(Matchers.anyString()))
        .thenReturn(this.rule);
    // Execute
    final SmellRulesDefinition sut = new SmellRulesDefinition();
    sut.define(this.context);
    // Verify behavior
    Mockito.verify(this.context, Mockito.times(1))
        .createRepository(Matchers.eq(EXPECTED_REPOSITORY_KEY), Matchers.eq(Java.KEY));
    Mockito.verify(this.repository, Mockito.times(1))
        .setName(Matchers.eq(EXPECTED_REPOSITORY_NAME));
    Mockito.verify(this.repository, Mockito.times(27))
        .createRule(this.captor.capture());
    Assertions.assertThat(this.captor.getValue())
        .isEqualTo(LAST_RULE_KEY);
    Mockito.verify(this.rule, Mockito.times(28))
        .key();
    Mockito.verify(this.rule, Mockito.times(1))
        .setInternalKey(Matchers.eq(LAST_RULE_KEY));
    Mockito.verify(this.repository, Mockito.times(1))
        .done();
}