summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-27 09:23:55 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2013-11-27 09:24:42 +0100
commit56b6fa8c650d96a05734983e874f48c1aff40dae (patch)
tree114c68e9f12664219ff6d04ffdf28223316f8fb5 /plugins
parentac5db8387bfabeb9a78ce172fedc6fa86eb9b1e2 (diff)
downloadsonarqube-56b6fa8c650d96a05734983e874f48c1aff40dae.tar.gz
sonarqube-56b6fa8c650d96a05734983e874f48c1aff40dae.zip
SONAR-4892 SONAR-4917 Update technical debt merge model and technical debt decorator to use mybatis classes
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java2
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java61
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java2
-rw-r--r--plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb7
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecoratorTest.java129
5 files changed, 114 insertions, 87 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
index cd2f6d12bed..906a18d2e37 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
@@ -26,7 +26,7 @@ import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Qualifiers;
import org.sonar.batch.components.PastSnapshotFinder;
-import org.sonar.core.technicaldebt.TechnicalDebtCalculator;
+import org.sonar.batch.technicaldebt.TechnicalDebtCalculator;
import org.sonar.core.technicaldebt.TechnicalDebtConverter;
import org.sonar.core.timemachine.Periods;
import org.sonar.plugins.core.batch.IndexProjectPostJob;
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java
index fa5b1495000..243aac29ca5 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecorator.java
@@ -37,10 +37,10 @@ import org.sonar.api.measures.*;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
-import org.sonar.core.technicaldebt.TechnicalDebtCharacteristic;
+import org.sonar.api.technicaldebt.Characteristic;
+import org.sonar.api.technicaldebt.Requirement;
import org.sonar.core.technicaldebt.TechnicalDebtConverter;
import org.sonar.core.technicaldebt.TechnicalDebtModel;
-import org.sonar.core.technicaldebt.TechnicalDebtRequirement;
import java.util.Arrays;
import java.util.Collection;
@@ -87,19 +87,19 @@ public final class TechnicalDebtDecorator implements Decorator {
private void saveMeasures(DecoratorContext context, List<Issue> issues) {
// group issues by requirement
- ListMultimap<TechnicalDebtRequirement, Issue> issuesByRequirement = issuesByRequirement(issues);
+ ListMultimap<Requirement, Issue> issuesByRequirement = issuesByRequirement(issues);
double total = 0.0;
- Map<TechnicalDebtCharacteristic, Double> characteristicCosts = newHashMap();
- Map<TechnicalDebtRequirement, Double> requirementCosts = newHashMap();
+ Map<Characteristic, Double> characteristicCosts = newHashMap();
+ Map<Requirement, Double> requirementCosts = newHashMap();
- for (TechnicalDebtRequirement requirement : technicalDebtModel.getAllRequirements()) {
+ for (Requirement requirement : technicalDebtModel.requirements()) {
List<Issue> requirementIssues = issuesByRequirement.get(requirement);
double value = computeTechnicalDebt(CoreMetrics.TECHNICAL_DEBT, context, requirement, requirementIssues);
requirementCosts.put(requirement, value);
total += value;
- propagateTechnicalDebtInParents(requirement.getParent(), value, characteristicCosts);
+ propagateTechnicalDebtInParents(requirement.characteristic(), value, characteristicCosts);
}
context.saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT, total, DECIMALS_PRECISION));
@@ -107,23 +107,23 @@ public final class TechnicalDebtDecorator implements Decorator {
saveOnRequirement(context, requirementCosts);
}
- private void saveOnCharacteristic(DecoratorContext context, Map<TechnicalDebtCharacteristic, Double> characteristicCosts) {
- for (Map.Entry<TechnicalDebtCharacteristic, Double> entry : characteristicCosts.entrySet()) {
- saveCost(context, entry.getKey().toCharacteristic(), entry.getValue(), false);
+ private void saveOnCharacteristic(DecoratorContext context, Map<Characteristic, Double> characteristicCosts) {
+ for (Map.Entry<Characteristic, Double> entry : characteristicCosts.entrySet()) {
+ saveTechnicalDebt(context, entry.getKey(), entry.getValue(), false);
}
}
- private void saveOnRequirement(DecoratorContext context, Map<TechnicalDebtRequirement, Double> requirementCosts) {
- for (Map.Entry<TechnicalDebtRequirement, Double> entry : requirementCosts.entrySet()) {
- saveCost(context, entry.getKey().toCharacteristic(), entry.getValue(), ResourceUtils.isEntity(context.getResource()));
+ private void saveOnRequirement(DecoratorContext context, Map<Requirement, Double> requirementCosts) {
+ for (Map.Entry<Requirement, Double> entry : requirementCosts.entrySet()) {
+ saveTechnicalDebt(context, entry.getKey(), entry.getValue(), ResourceUtils.isEntity(context.getResource()));
}
}
@VisibleForTesting
- void saveCost(DecoratorContext context, org.sonar.api.qualitymodel.Characteristic characteristic, Double value, boolean inMemory) {
+ void saveTechnicalDebt(DecoratorContext context, Characteristic characteristic, Double value, boolean inMemory) {
// we need the value on projects (root or module) even if value==0 in order to display correctly the SQALE history chart (see SQALE-122)
// BUT we don't want to save zero-values for non top-characteristics (see SQALE-147)
- if (value > 0.0 || (ResourceUtils.isProject(context.getResource()) && characteristic.getDepth() == org.sonar.api.qualitymodel.Characteristic.ROOT_DEPTH)) {
+ if (value > 0.0 || (ResourceUtils.isProject(context.getResource()) && characteristic.isRoot())) {
Measure measure = new Measure(CoreMetrics.TECHNICAL_DEBT);
measure.setCharacteristic(characteristic);
measure.setValue(value, DECIMALS_PRECISION);
@@ -135,12 +135,27 @@ public final class TechnicalDebtDecorator implements Decorator {
}
@VisibleForTesting
- ListMultimap<TechnicalDebtRequirement, Issue> issuesByRequirement(List<Issue> issues) {
- ListMultimap<TechnicalDebtRequirement, Issue> issuesByRequirement = ArrayListMultimap.create();
+ void saveTechnicalDebt(DecoratorContext context, Requirement requirement, Double value, boolean inMemory) {
+ // we need the value on projects (root or module) even if value==0 in order to display correctly the SQALE history chart (see SQALE-122)
+ // BUT we don't want to save zero-values for non top-characteristics (see SQALE-147)
+ if (value > 0.0) {
+ Measure measure = new Measure(CoreMetrics.TECHNICAL_DEBT);
+ measure.setRequirement(requirement);
+ measure.setValue(value, DECIMALS_PRECISION);
+ if (inMemory) {
+ measure.setPersistenceMode(PersistenceMode.MEMORY);
+ }
+ context.saveMeasure(measure);
+ }
+ }
+
+ @VisibleForTesting
+ ListMultimap<Requirement, Issue> issuesByRequirement(List<Issue> issues) {
+ ListMultimap<Requirement, Issue> issuesByRequirement = ArrayListMultimap.create();
for (Issue issue : issues) {
String repositoryKey = issue.ruleKey().repository();
String key = issue.ruleKey().rule();
- TechnicalDebtRequirement requirement = technicalDebtModel.getRequirementByRule(repositoryKey, key);
+ Requirement requirement = technicalDebtModel.requirementsByRule(issue.ruleKey());
if (requirement == null) {
LoggerFactory.getLogger(getClass()).debug("No technical debt requirement for: " + repositoryKey + "/" + key);
} else {
@@ -150,7 +165,7 @@ public final class TechnicalDebtDecorator implements Decorator {
return issuesByRequirement;
}
- private double computeTechnicalDebt(Metric metric, DecoratorContext context, TechnicalDebtRequirement requirement, Collection<Issue> issues) {
+ private double computeTechnicalDebt(Metric metric, DecoratorContext context, Requirement requirement, Collection<Issue> issues) {
double value = 0.0;
if (issues != null) {
for (Issue issue : issues){
@@ -158,15 +173,15 @@ public final class TechnicalDebtDecorator implements Decorator {
}
}
- for (Measure measure : context.getChildrenMeasures(MeasuresFilters.characteristic(metric, requirement.toCharacteristic()))) {
- if (measure.getCharacteristic() != null && measure.getCharacteristic().equals(requirement.toCharacteristic()) && measure.getValue() != null) {
+ for (Measure measure : context.getChildrenMeasures(MeasuresFilters.requirement(metric, requirement))) {
+ if (measure.getRequirement() != null && measure.getRequirement().equals(requirement) && measure.getValue() != null) {
value += measure.getValue();
}
}
return value;
}
- private void propagateTechnicalDebtInParents(TechnicalDebtCharacteristic characteristic, double value, Map<TechnicalDebtCharacteristic, Double> characteristicCosts) {
+ private void propagateTechnicalDebtInParents(Characteristic characteristic, double value, Map<Characteristic, Double> characteristicCosts) {
if (characteristic != null) {
Double parentCost = characteristicCosts.get(characteristic);
if (parentCost == null) {
@@ -174,7 +189,7 @@ public final class TechnicalDebtDecorator implements Decorator {
} else {
characteristicCosts.put(characteristic, value + parentCost);
}
- propagateTechnicalDebtInParents(characteristic.getParent(), value, characteristicCosts);
+ propagateTechnicalDebtInParents(characteristic.parent(), value, characteristicCosts);
}
}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
index 9b9798af144..faae4da4e26 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java
@@ -94,7 +94,7 @@ public class VariationDecorator implements Decorator {
for (Measure measure : context.getMeasures(MeasuresFilters.all())) {
// compare with past measure
Integer metricId = measure.getMetric().getId() != null ? measure.getMetric().getId() : metricFinder.findByKey(measure.getMetric().getKey()).getId();
- Integer characteristicId = measure.getCharacteristic() != null ? measure.getCharacteristic().getId() : null;
+ Integer characteristicId = measure.getCharacteristic() != null ? measure.getCharacteristic().id() : null;
Integer personId = measure.getPersonId();
Integer ruleId = measure instanceof RuleMeasure ? ((RuleMeasure) measure).getRule().getId() : null;
diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb
index 49907134d96..84dcde947fd 100644
--- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb
+++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/plugins/core/widgets/technical_debt_pyramid.html.erb
@@ -1,9 +1,8 @@
<%
- technical_debt_model = 'SQALE'
technical_debt = measure('sqale_index')
# Load root characteristics only
- characteristics = Characteristic.find(:all, :joins => :quality_model,
- :conditions => ['quality_models.name=? AND characteristics.depth=1 AND characteristics.rule_id IS NULL and characteristics.enabled=?', technical_debt_model, true],
+ characteristics = Characteristic.find(:all,
+ :conditions => ['characteristics.parent_id IS NULL AND characteristics.rule_id IS NULL and characteristics.enabled=?', true],
:order => 'characteristic_order DESC')
should_display_diff_measures = dashboard_configuration.selected_period? && technical_debt.variation(dashboard_configuration.period_index)!=nil
if technical_debt.nil? || characteristics.empty?
@@ -104,7 +103,7 @@
value=(measure && measure.value ? measure.value : 0)
total_size=(total>0 ? (100*cumulated/total).to_i: 0)
value_size=(cumulated>0 ? (100*value/cumulated).to_i: 100)
- drilldown_url=url_for_drilldown('sqale_index', :model => technical_debt_model, :characteristic => characteristic.key)
+ drilldown_url = url_for_drilldown('sqale_index', :characteristic => characteristic.key)
%>
<tr>
<td class="label"><%= h(characteristic.name) -%></td>
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecoratorTest.java
index 6293d120229..dd114e59d23 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecoratorTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/technicaldebt/TechnicalDebtDecoratorTest.java
@@ -38,16 +38,15 @@ import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasuresFilter;
import org.sonar.api.measures.Metric;
-import org.sonar.api.qualitymodel.Characteristic;
import org.sonar.api.resources.File;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.technicaldebt.Characteristic;
+import org.sonar.api.technicaldebt.Requirement;
import org.sonar.api.test.IsMeasure;
-import org.sonar.core.technicaldebt.TechnicalDebtCharacteristic;
import org.sonar.core.technicaldebt.TechnicalDebtConverter;
import org.sonar.core.technicaldebt.TechnicalDebtModel;
-import org.sonar.core.technicaldebt.TechnicalDebtRequirement;
import java.util.List;
@@ -105,8 +104,8 @@ public class TechnicalDebtDecoratorTest {
@Test
public void group_issues_by_requirement() throws Exception {
- TechnicalDebtRequirement requirement1 = mock(TechnicalDebtRequirement.class);
- TechnicalDebtRequirement requirement2 = mock(TechnicalDebtRequirement.class);
+ Requirement requirement1 = mock(Requirement.class);
+ Requirement requirement2 = mock(Requirement.class);
Issue issue1 = createIssue("rule1", "repo1");
Issue issue2 = createIssue("rule1", "repo1");
@@ -115,10 +114,10 @@ public class TechnicalDebtDecoratorTest {
List<Issue> issues = newArrayList(issue1, issue2, issue3, issue4);
- when(technicalDebtModel.getRequirementByRule("repo1", "rule1")).thenReturn(requirement1);
- when(technicalDebtModel.getRequirementByRule("repo2", "rule2")).thenReturn(requirement2);
+ when(technicalDebtModel.requirementsByRule(RuleKey.of("repo1", "rule1"))).thenReturn(requirement1);
+ when(technicalDebtModel.requirementsByRule(RuleKey.of("repo2", "rule2"))).thenReturn(requirement2);
- ListMultimap<TechnicalDebtRequirement, Issue> result = decorator.issuesByRequirement(issues);
+ ListMultimap<Requirement, Issue> result = decorator.issuesByRequirement(issues);
assertThat(result.keySet().size()).isEqualTo(2);
assertThat(result.get(requirement1)).containsExactly(issue1, issue2);
@@ -133,18 +132,14 @@ public class TechnicalDebtDecoratorTest {
Issue issue = createIssue("rule1", "repo1").setTechnicalDebt(technicalDebt);
when(issuable.issues()).thenReturn(newArrayList(issue));
- Characteristic requirement = Characteristic.create();
- TechnicalDebtRequirement technicalDebtRequirement = mock(TechnicalDebtRequirement.class);
- when(technicalDebtRequirement.toCharacteristic()).thenReturn(requirement);
- when(technicalDebtRequirement.getParent()).thenReturn(null);
-
- when(technicalDebtModel.getRequirementByRule("repo1", "rule1")).thenReturn(technicalDebtRequirement);
- when(technicalDebtModel.getAllRequirements()).thenReturn(newArrayList(technicalDebtRequirement));
+ Requirement requirement = mock(Requirement.class);
+ when(technicalDebtModel.requirementsByRule(RuleKey.of("repo1", "rule1"))).thenReturn(requirement);
+ when(technicalDebtModel.requirements()).thenReturn(newArrayList(requirement));
decorator.decorate(resource, context);
- verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, null, 1.0)));
- verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, requirement, 1.0)));
+ verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, 1.0)));
+ verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, null, requirement, 1.0)));
}
@Test
@@ -155,22 +150,17 @@ public class TechnicalDebtDecoratorTest {
Issue issue = createIssue("rule1", "repo1").setTechnicalDebt(technicalDebt);
when(issuable.issues()).thenReturn(newArrayList(issue));
- Characteristic requirement = Characteristic.createByName("requirement");
- Characteristic characteristic = Characteristic.createByName("characteristic");
- Characteristic parentCharacteristic = Characteristic.createByName("parentCharacteristic");
-
- TechnicalDebtCharacteristic parentTechDebtCharacteristic = new TechnicalDebtCharacteristic(parentCharacteristic);
- TechnicalDebtCharacteristic techDebtCharacteristic = new TechnicalDebtCharacteristic(characteristic, parentTechDebtCharacteristic);
- TechnicalDebtRequirement technicalDebtRequirement = mock(TechnicalDebtRequirement.class);
- when(technicalDebtRequirement.toCharacteristic()).thenReturn(requirement);
- when(technicalDebtRequirement.getParent()).thenReturn(techDebtCharacteristic);
+ Characteristic parentCharacteristic = new Characteristic().setKey("parentCharacteristic");
+ Characteristic characteristic = new Characteristic().setKey("characteristic").setParent(parentCharacteristic);
+ RuleKey ruleKey = RuleKey.of("repo1", "rule1");
+ Requirement requirement = new Requirement().setCharacteristic(characteristic).setRuleKey(ruleKey);
- when(technicalDebtModel.getRequirementByRule("repo1", "rule1")).thenReturn(technicalDebtRequirement);
- when(technicalDebtModel.getAllRequirements()).thenReturn(newArrayList(technicalDebtRequirement));
+ when(technicalDebtModel.requirementsByRule(ruleKey)).thenReturn(requirement);
+ when(technicalDebtModel.requirements()).thenReturn(newArrayList(requirement));
decorator.decorate(resource, context);
- verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, null, 1.0)));
+ verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, 1.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, parentCharacteristic, 1.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, characteristic, 1.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, requirement, 1.0)));
@@ -190,22 +180,20 @@ public class TechnicalDebtDecoratorTest {
Issue issue4 = createIssue("rule2", "repo2").setTechnicalDebt(technicalDebt2);
when(issuable.issues()).thenReturn(newArrayList(issue1, issue2, issue3, issue4));
- Characteristic requirement1 = Characteristic.createByName("requirement1");
- Characteristic requirement2 = Characteristic.createByName("requirement2");
-
- TechnicalDebtRequirement technicalDebtRequirement1 = mock(TechnicalDebtRequirement.class);
- when(technicalDebtRequirement1.toCharacteristic()).thenReturn(requirement1);
- TechnicalDebtRequirement technicalDebtRequirement2 = mock(TechnicalDebtRequirement.class);
- when(technicalDebtRequirement2.toCharacteristic()).thenReturn(requirement2);
-
+ Characteristic rootCharacteristic = new Characteristic().setKey("rootCharacteristic");
+ Characteristic characteristic = new Characteristic().setKey("characteristic").setParent(rootCharacteristic);
+ RuleKey ruleKey1 = RuleKey.of("repo1", "rule1");
+ Requirement requirement1 = new Requirement().setRuleKey(ruleKey1).setCharacteristic(characteristic);
+ RuleKey ruleKey2 = RuleKey.of("repo2", "rule2");
+ Requirement requirement2 = new Requirement().setRuleKey(ruleKey2).setCharacteristic(characteristic);
- when(technicalDebtModel.getRequirementByRule("repo1", "rule1")).thenReturn(technicalDebtRequirement1);
- when(technicalDebtModel.getRequirementByRule("repo2", "rule2")).thenReturn(technicalDebtRequirement2);
- when(technicalDebtModel.getAllRequirements()).thenReturn(newArrayList(technicalDebtRequirement1, technicalDebtRequirement2));
+ when(technicalDebtModel.requirementsByRule(ruleKey1)).thenReturn(requirement1);
+ when(technicalDebtModel.requirementsByRule(ruleKey2)).thenReturn(requirement2);
+ when(technicalDebtModel.requirements()).thenReturn(newArrayList(requirement1, requirement2));
decorator.decorate(resource, context);
- verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, null, 6.0)));
+ verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, 6.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, requirement1, 2.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, requirement2, 4.0)));
}
@@ -219,19 +207,20 @@ public class TechnicalDebtDecoratorTest {
Issue issue2 = createIssue("rule1", "repo1").setTechnicalDebt(technicalDebt1);
when(issuable.issues()).thenReturn(newArrayList(issue1, issue2));
- Characteristic requirement = Characteristic.createByName("requirement1");
- TechnicalDebtRequirement technicalDebtRequirement = mock(TechnicalDebtRequirement.class);
- when(technicalDebtRequirement.toCharacteristic()).thenReturn(requirement);
+ Characteristic rootCharacteristic = new Characteristic().setKey("rootCharacteristic");
+ Characteristic characteristic = new Characteristic().setKey("characteristic").setParent(rootCharacteristic);
+ RuleKey ruleKey1 = RuleKey.of("repo1", "rule1");
+ Requirement requirement = new Requirement().setRuleKey(ruleKey1).setCharacteristic(characteristic);
- when(technicalDebtModel.getRequirementByRule("repo1", "rule1")).thenReturn(technicalDebtRequirement);
- when(technicalDebtModel.getAllRequirements()).thenReturn(newArrayList(technicalDebtRequirement));
+ when(technicalDebtModel.requirementsByRule(ruleKey1)).thenReturn(requirement);
+ when(technicalDebtModel.requirements()).thenReturn(newArrayList(requirement));
- Measure measure = new Measure().setCharacteristic(requirement).setValue(5.0);
+ Measure measure = new Measure().setRequirement(requirement).setValue(5.0);
when(context.getChildrenMeasures(any(MeasuresFilter.class))).thenReturn(newArrayList(measure));
decorator.decorate(resource, context);
- verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, null, 7.0)));
+ verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, 7.0)));
verify(context).saveMeasure(argThat(new IsCharacteristicMeasure(CoreMetrics.TECHNICAL_DEBT, requirement, 7.0)));
}
@@ -240,13 +229,13 @@ public class TechnicalDebtDecoratorTest {
// for a project
DecoratorContext context = mock(DecoratorContext.class);
when(context.getResource()).thenReturn(new Project("foo"));
- decorator.saveCost(context, null, 12.0, false);
+ decorator.saveTechnicalDebt(context, (Characteristic) null, 12.0, false);
verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT));
// or for a file
context = mock(DecoratorContext.class);
when(context.getResource()).thenReturn(new File("foo"));
- decorator.saveCost(context, null, 12.0, false);
+ decorator.saveTechnicalDebt(context, (Characteristic) null, 12.0, false);
verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT));
}
@@ -255,10 +244,10 @@ public class TechnicalDebtDecoratorTest {
DecoratorContext context = mock(DecoratorContext.class);
when(context.getResource()).thenReturn(new Project("foo"));
// this is a top characteristic
- Characteristic topCharacteristic = Characteristic.create();
+ Characteristic rootCharacteristic = new Characteristic().setKey("root");
- decorator.saveCost(context, topCharacteristic, 0.0, true);
- verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT).setCharacteristic(topCharacteristic));
+ decorator.saveTechnicalDebt(context, rootCharacteristic, 0.0, true);
+ verify(context, times(1)).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT).setCharacteristic(rootCharacteristic));
}
/**
@@ -268,11 +257,11 @@ public class TechnicalDebtDecoratorTest {
public void never_save_technical_debt_for_project_if_not_top_characteristic() throws Exception {
DecoratorContext context = mock(DecoratorContext.class);
when(context.getResource()).thenReturn(new Project("foo"));
- Characteristic topCharacteristic = Characteristic.create();
- Characteristic childCharacteristic = Characteristic.create();
- topCharacteristic.addChild(childCharacteristic);
- decorator.saveCost(context, childCharacteristic, 0.0, true);
+ Characteristic rootCharacteristic = new Characteristic().setKey("rootCharacteristic");
+ Characteristic characteristic = new Characteristic().setKey("characteristic").setParent(rootCharacteristic);
+
+ decorator.saveTechnicalDebt(context, characteristic, 0.0, true);
verify(context, never()).saveMeasure(any(Measure.class));
}
@@ -281,7 +270,7 @@ public class TechnicalDebtDecoratorTest {
DecoratorContext context = mock(DecoratorContext.class);
when(context.getResource()).thenReturn(new File("foo"));
- decorator.saveCost(context, null, 0.0, true);
+ decorator.saveTechnicalDebt(context, (Characteristic) null, 0.0, true);
verify(context, never()).saveMeasure(new Measure(CoreMetrics.TECHNICAL_DEBT));
}
@@ -297,11 +286,34 @@ public class TechnicalDebtDecoratorTest {
class IsCharacteristicMeasure extends ArgumentMatcher<Measure> {
Metric metric = null;
Characteristic characteristic = null;
+ Requirement requirement = null;
Double value = null;
+ public IsCharacteristicMeasure(Metric metric, Characteristic characteristic, Requirement requirement, Double value) {
+ this.metric = metric;
+ this.characteristic = characteristic;
+ this.requirement = requirement;
+ this.value = value;
+ }
+
public IsCharacteristicMeasure(Metric metric, Characteristic characteristic, Double value) {
this.metric = metric;
this.characteristic = characteristic;
+ this.requirement = null;
+ this.value = value;
+ }
+
+ public IsCharacteristicMeasure(Metric metric, Requirement requirement, Double value) {
+ this.metric = metric;
+ this.characteristic = null;
+ this.requirement = requirement;
+ this.value = value;
+ }
+
+ public IsCharacteristicMeasure(Metric metric, Double value) {
+ this.metric = metric;
+ this.characteristic = null;
+ this.requirement = null;
this.value = value;
}
@@ -312,6 +324,7 @@ public class TechnicalDebtDecoratorTest {
Measure m = (Measure) o;
return ObjectUtils.equals(metric, m.getMetric()) &&
ObjectUtils.equals(characteristic, m.getCharacteristic()) &&
+ ObjectUtils.equals(requirement, m.getRequirement()) &&
ObjectUtils.equals(value, m.getValue());
}
}