package org.sonar.plugins.core.issue;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.HashMultiset;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Multiset;
-import com.google.common.collect.Sets;
+import com.google.common.collect.*;
import org.apache.commons.lang.time.DateUtils;
-import org.sonar.api.batch.Decorator;
-import org.sonar.api.batch.DecoratorBarriers;
-import org.sonar.api.batch.DecoratorContext;
-import org.sonar.api.batch.DependedUpon;
-import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.batch.*;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issue;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.measures.MeasureUtils;
-import org.sonar.api.measures.MeasuresFilters;
-import org.sonar.api.measures.Metric;
-import org.sonar.api.measures.RuleMeasure;
+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.api.rule.RuleKey;
-import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RulePriority;
import org.sonar.batch.components.Period;
import org.sonar.batch.components.TimeMachineConfiguration;
import javax.annotation.Nullable;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
/**
* Computes metrics related to number of issues.
public class CountUnresolvedIssuesDecorator implements Decorator {
private final ResourcePerspectives perspectives;
- private final RuleFinder rulefinder;
private final TimeMachineConfiguration timeMachineConfiguration;
- public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, RuleFinder rulefinder, TimeMachineConfiguration timeMachineConfiguration) {
+ public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, TimeMachineConfiguration timeMachineConfiguration) {
this.perspectives = perspectives;
- this.rulefinder = rulefinder;
this.timeMachineConfiguration = timeMachineConfiguration;
}
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.internal.DefaultIssue;
-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.measures.RuleMeasure;
+import org.sonar.api.measures.*;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.Scopes;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.test.IsRuleMeasure;
import org.sonar.batch.components.Period;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
public class CountUnresolvedIssuesDecoratorTest {
CountUnresolvedIssuesDecorator decorator;
TimeMachineConfiguration timeMachineConfiguration;
- RuleFinder ruleFinder;
Issuable issuable;
DecoratorContext context;
Resource resource;
ruleA2 = Rule.create().setRepositoryKey("ruleA2").setKey("ruleA2").setName("nameA2");
ruleB1 = Rule.create().setRepositoryKey("ruleB1").setKey("ruleB1").setName("nameB1");
- ruleFinder = mock(RuleFinder.class);
- when(ruleFinder.findByKey(ruleA1.ruleKey())).thenReturn(ruleA1);
- when(ruleFinder.findByKey(ruleA2.ruleKey())).thenReturn(ruleA2);
- when(ruleFinder.findByKey(ruleB1.ruleKey())).thenReturn(ruleB1);
-
rightNow = new Date();
tenDaysAgo = DateUtils.addDays(rightNow, -10);
afterTenDaysAgo = DateUtils.addDays(tenDaysAgo, 1);
issuable = mock(Issuable.class);
ResourcePerspectives perspectives = mock(ResourcePerspectives.class);
when(perspectives.as(Issuable.class, resource)).thenReturn(issuable);
- decorator = new CountUnresolvedIssuesDecorator(perspectives, ruleFinder, timeMachineConfiguration);
+ decorator = new CountUnresolvedIssuesDecorator(perspectives, timeMachineConfiguration);
}
@Test
public void should_do_nothing_when_issuable_is_null() {
ResourcePerspectives perspectives = mock(ResourcePerspectives.class);
when(perspectives.as(Issuable.class, resource)).thenReturn(null);
- CountUnresolvedIssuesDecorator decorator = new CountUnresolvedIssuesDecorator(perspectives, ruleFinder, timeMachineConfiguration);
+ CountUnresolvedIssuesDecorator decorator = new CountUnresolvedIssuesDecorator(perspectives, timeMachineConfiguration);
decorator.decorate(resource, context);
package org.sonar.server.rule;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
-import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.persistence.MyBatis;
public class RuleOperations implements ServerComponent {
private final DbClient dbClient;
- private final System2 system;
public RuleOperations(DbClient dbClient) {
- this(dbClient, System2.INSTANCE);
- }
-
- @VisibleForTesting
- RuleOperations(DbClient dbClient, System2 system) {
this.dbClient = dbClient;
- this.system = system;
}
-
public void updateRule(RuleChange ruleChange, UserSession userSession) {
checkPermission(userSession);
DbSession session = dbClient.openSession(false);
}
}
- boolean needUpdate = updateRule(ruleDto, subCharacteristic, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset(),
+ boolean needUpdate = updateRule(ruleDto, subCharacteristic, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(),
+ ruleChange.debtRemediationOffset(),
session);
if (needUpdate) {
session.commit();
}
public boolean updateRule(RuleDto ruleDto, @Nullable CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
- @Nullable String newCoefficient, @Nullable String newOffset, DbSession session) {
+ @Nullable String newCoefficient, @Nullable String newOffset, DbSession session) {
boolean needUpdate = false;
// A sub-characteristic and a remediation function is given -> update rule debt
} else {
// Rule characteristic is not already disabled -> update it
if (ruleDto.getSubCharacteristicId() == null || !RuleDto.DISABLED_CHARACTERISTIC_ID.equals(ruleDto.getSubCharacteristicId())) {
- // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin update
+ // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin
+ // update
ruleDto.setSubCharacteristicId(ruleDto.getDefaultSubCharacteristicId() != null ? RuleDto.DISABLED_CHARACTERISTIC_ID : null);
ruleDto.setRemediationFunction(null);
ruleDto.setRemediationCoefficient(null);
}
private static boolean isRuleDebtSameAsDefaultValues(RuleDto ruleDto, CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
- @Nullable String newCoefficient, @Nullable String newOffset) {
+ @Nullable String newCoefficient, @Nullable String newOffset) {
return newSubCharacteristic.getId().equals(ruleDto.getDefaultSubCharacteristicId()) &&
isSameRemediationFunction(newFunction, newCoefficient, newOffset, ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationCoefficient(),
ruleDto.getDefaultRemediationOffset());
}
private static boolean isRuleDebtSameAsOverriddenValues(RuleDto ruleDto, CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
- @Nullable String newCoefficient, @Nullable String newOffset) {
+ @Nullable String newCoefficient, @Nullable String newOffset) {
return newSubCharacteristic.getId().equals(ruleDto.getSubCharacteristicId())
&& isSameRemediationFunction(newFunction, newCoefficient, newOffset, ruleDto.getRemediationFunction(), ruleDto.getRemediationCoefficient(), ruleDto.getRemediationOffset());
}
private static boolean isSameRemediationFunction(@Nullable String newFunction, @Nullable String newCoefficient, @Nullable String newOffset,
- String oldFunction, @Nullable String oldCoefficient, @Nullable String oldOffset) {
+ String oldFunction, @Nullable String oldCoefficient, @Nullable String oldOffset) {
return new EqualsBuilder()
.append(oldFunction, newFunction)
.append(oldCoefficient, newCoefficient)
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.rule.RuleKey;
-import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.rule.RuleDto;
when(dbClient.openSession(false)).thenReturn(session);
when(dbClient.ruleDao()).thenReturn(ruleDao);
when(dbClient.debtCharacteristicDao()).thenReturn(characteristicDao);
- operations = new RuleOperations(dbClient, System2.INSTANCE);
+ operations = new RuleOperations(dbClient);
}
@Test