aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-03-20 16:17:04 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-03-20 16:17:13 +0100
commit90431143eb37efe5bc25a5ffb414d0db97bf1ce9 (patch)
tree797ffffd7ff5fbe347124d79b3bd073b84ca6c3b /sonar-server
parent0f87a1eca7108d6ca40320cdf37b5184413feb6c (diff)
downloadsonarqube-90431143eb37efe5bc25a5ffb414d0db97bf1ce9.tar.gz
sonarqube-90431143eb37efe5bc25a5ffb414d0db97bf1ce9.zip
SONAR-5056 Create restore from language action
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java10
-rw-r--r--sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java43
-rw-r--r--sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java16
-rw-r--r--sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java115
-rw-r--r--sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java8
5 files changed, 140 insertions, 52 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java
index a87b0c51c17..871f777a894 100644
--- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java
+++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java
@@ -151,16 +151,16 @@ public class DebtModelOperations implements ServerComponent {
}
/**
- * Disable characteristic and sub characteristic or only sub characteristic.
+ * Disable characteristic and its sub characteristics or only sub characteristic.
* Will also update every rules linked to sub characteristics by setting characteristic id to -1 and remove function, factor and offset.
*/
- public void delete(int characteristicOrSubCharacteristicId) {
+ public void delete(int characteristicId) {
checkPermission();
Date updateDate = new Date(system2.now());
SqlSession session = mybatis.openBatchSession();
try {
- CharacteristicDto characteristicOrSubCharacteristic = findCharacteristic(characteristicOrSubCharacteristicId, session);
+ CharacteristicDto characteristicOrSubCharacteristic = findCharacteristic(characteristicId, session);
disableDebtRules(
ruleDao.selectByCharacteristicOrSubCharacteristicId(characteristicOrSubCharacteristic.getId(), session),
updateDate,
@@ -168,8 +168,8 @@ public class DebtModelOperations implements ServerComponent {
);
if (characteristicOrSubCharacteristic.getParentId() == null) {
- List<CharacteristicDto> subChracteristics = dao.selectCharacteristicsByParentId(characteristicOrSubCharacteristic.getId(), session);
- for (CharacteristicDto subCharacteristic : subChracteristics) {
+ List<CharacteristicDto> subCharacteristics = dao.selectCharacteristicsByParentId(characteristicOrSubCharacteristic.getId(), session);
+ for (CharacteristicDto subCharacteristic : subCharacteristics) {
disableCharacteristic(subCharacteristic, updateDate, session);
}
}
diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java
index e9ed1c635c6..380d4d5a379 100644
--- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java
+++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java
@@ -20,6 +20,7 @@
package org.sonar.server.debt;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
@@ -35,15 +36,20 @@ import org.sonar.core.rule.RuleDto;
import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.rule.RuleRepositories;
import org.sonar.server.user.UserSession;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.Reader;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
+import static com.google.common.collect.Lists.newArrayList;
+
public class DebtModelRestore implements ServerComponent {
private final MyBatis mybatis;
@@ -51,31 +57,44 @@ public class DebtModelRestore implements ServerComponent {
private final RuleDao ruleDao;
private final DebtModelOperations debtModelOperations;
private final TechnicalDebtModelRepository debtModelPluginRepository;
+ private final RuleRepositories ruleRepositories;
private final DebtCharacteristicsXMLImporter importer;
private final System2 system2;
public DebtModelRestore(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, TechnicalDebtModelRepository debtModelPluginRepository,
- DebtCharacteristicsXMLImporter importer) {
- this(mybatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, importer, System2.INSTANCE);
+ RuleRepositories ruleRepositories, DebtCharacteristicsXMLImporter importer) {
+ this(mybatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, ruleRepositories, importer, System2.INSTANCE);
}
@VisibleForTesting
DebtModelRestore(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, TechnicalDebtModelRepository debtModelPluginRepository,
- DebtCharacteristicsXMLImporter importer,
+ RuleRepositories ruleRepositories, DebtCharacteristicsXMLImporter importer,
System2 system2) {
this.mybatis = mybatis;
this.dao = dao;
this.ruleDao = ruleDao;
this.debtModelOperations = debtModelOperations;
this.debtModelPluginRepository = debtModelPluginRepository;
+ this.ruleRepositories = ruleRepositories;
this.importer = importer;
this.system2 = system2;
}
/**
- * Restore model from all provided plugins
+ * Restore from provided model
+ */
+ public void restore() {
+ restore(Collections.<RuleRepositories.Repository>emptyList());
+ }
+
+ /**
+ * Restore from plugins providing rules for a given language
*/
- public void restoreFromProvidedModel() {
+ public void restore(String languageKey) {
+ restore(ruleRepositories.repositoriesForLang(languageKey));
+ }
+
+ private void restore(Collection<RuleRepositories.Repository> repositories) {
checkPermission();
Date updateDate = new Date(system2.now());
@@ -84,7 +103,7 @@ public class DebtModelRestore implements ServerComponent {
List<CharacteristicDto> persisted = dao.selectEnabledCharacteristics();
DebtModel providedModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL);
restoreCharacteristics(providedModel, persisted, updateDate, session);
- resetOverridingRuleDebt(updateDate, session);
+ resetOverridingRuleDebt(repositories, updateDate, session);
session.commit();
} finally {
@@ -92,8 +111,14 @@ public class DebtModelRestore implements ServerComponent {
}
}
- private void resetOverridingRuleDebt(Date updateDate, SqlSession session) {
- for (RuleDto rule : ruleDao.selectOverridingDebt(session)) {
+ private void resetOverridingRuleDebt(Collection<RuleRepositories.Repository> repositories, Date updateDate, SqlSession session) {
+ List<String> repositoryKeys = newArrayList(Iterables.transform(repositories, new Function<RuleRepositories.Repository, String>() {
+ @Override
+ public String apply(RuleRepositories.Repository input) {
+ return input.getKey();
+ }
+ }));
+ for (RuleDto rule : ruleDao.selectOverridingDebt(repositoryKeys, session)) {
rule.setCharacteristicId(null);
rule.setRemediationFunction(null);
rule.setRemediationFactor(null);
@@ -121,7 +146,7 @@ public class DebtModelRestore implements ServerComponent {
}
}
- private CharacteristicDto restoreCharacteristic(DebtCharacteristic targetCharacteristic, @Nullable Integer parentId, List<CharacteristicDto> sourceCharacteristics,
+ private CharacteristicDto restoreCharacteristic(DebtCharacteristic targetCharacteristic, @Nullable Integer parentId, List<CharacteristicDto> sourceCharacteristics,
Date updateDate, SqlSession session) {
CharacteristicDto sourceCharacteristic = dtoByKey(sourceCharacteristics, targetCharacteristic.key());
if (sourceCharacteristic == null) {
diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
index 1049d0b1c25..c21e1d16b8a 100644
--- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
+++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
@@ -77,12 +77,22 @@ public class DebtModelService implements DebtModel {
* Disable characteristic and sub characteristic or only sub characteristic.
* Will also update every rules linked to sub characteristics by setting characteristic id to -1 and remove function, factor and offset.
*/
- public void delete(int characteristicOrSubCharactteristicId) {
- debtModelOperations.delete(characteristicOrSubCharactteristicId);
+ public void delete(int characteristicId) {
+ debtModelOperations.delete(characteristicId);
}
+ /**
+ * Restore from provided model
+ */
public void restore(){
- debtModelRestore.restoreFromProvidedModel();
+ debtModelRestore.restore();
+ }
+
+ /**
+ * Restore from plugins providing rules for a given language
+ */
+ public void restore(String languageKey) {
+ debtModelRestore.restore(languageKey);
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java
index e90865b2e3a..4c96c20172e 100644
--- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java
@@ -39,6 +39,7 @@ import org.sonar.core.rule.RuleDto;
import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.rule.RuleRepositories;
import org.sonar.server.user.MockUserSession;
import java.io.Reader;
@@ -76,6 +77,9 @@ public class DebtModelRestoreTest {
DebtCharacteristicsXMLImporter characteristicsXMLImporter;
@Mock
+ RuleRepositories ruleRepositories;
+
+ @Mock
System2 system2;
Date now = DateUtils.parseDate("2014-03-19");
@@ -109,7 +113,7 @@ public class DebtModelRestoreTest {
when(debtModelPluginRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
when(characteristicsXMLImporter.importXML(eq(defaultModelReader))).thenReturn(defaultModel);
- debtModelRestore = new DebtModelRestore(myBatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, characteristicsXMLImporter, system2);
+ debtModelRestore = new DebtModelRestore(myBatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, ruleRepositories, characteristicsXMLImporter, system2);
}
@Test
@@ -125,21 +129,24 @@ public class DebtModelRestoreTest {
ArgumentCaptor<CharacteristicDto> characteristicArgument = ArgumentCaptor.forClass(CharacteristicDto.class);
verify(dao, times(2)).insert(characteristicArgument.capture(), eq(session));
- assertThat(characteristicArgument.getAllValues().get(0).getId()).isEqualTo(10);
- assertThat(characteristicArgument.getAllValues().get(0).getKey()).isEqualTo("PORTABILITY");
- assertThat(characteristicArgument.getAllValues().get(0).getName()).isEqualTo("Portability");
- assertThat(characteristicArgument.getAllValues().get(0).getParentId()).isNull();
- assertThat(characteristicArgument.getAllValues().get(0).getOrder()).isEqualTo(1);
- assertThat(characteristicArgument.getAllValues().get(0).getCreatedAt()).isEqualTo(now);
- assertThat(characteristicArgument.getAllValues().get(0).getUpdatedAt()).isNull();
-
- assertThat(characteristicArgument.getAllValues().get(1).getId()).isEqualTo(11);
- assertThat(characteristicArgument.getAllValues().get(1).getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
- assertThat(characteristicArgument.getAllValues().get(1).getName()).isEqualTo("Compiler");
- assertThat(characteristicArgument.getAllValues().get(1).getParentId()).isEqualTo(10);
- assertThat(characteristicArgument.getAllValues().get(1).getOrder()).isNull();
- assertThat(characteristicArgument.getAllValues().get(1).getCreatedAt()).isEqualTo(now);
- assertThat(characteristicArgument.getAllValues().get(1).getUpdatedAt()).isNull();
+
+ CharacteristicDto dto1 = characteristicArgument.getAllValues().get(0);
+ assertThat(dto1.getId()).isEqualTo(10);
+ assertThat(dto1.getKey()).isEqualTo("PORTABILITY");
+ assertThat(dto1.getName()).isEqualTo("Portability");
+ assertThat(dto1.getParentId()).isNull();
+ assertThat(dto1.getOrder()).isEqualTo(1);
+ assertThat(dto1.getCreatedAt()).isEqualTo(now);
+ assertThat(dto1.getUpdatedAt()).isNull();
+
+ CharacteristicDto dto2 = characteristicArgument.getAllValues().get(1);
+ assertThat(dto2.getId()).isEqualTo(11);
+ assertThat(dto2.getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
+ assertThat(dto2.getName()).isEqualTo("Compiler");
+ assertThat(dto2.getParentId()).isEqualTo(10);
+ assertThat(dto2.getOrder()).isNull();
+ assertThat(dto2.getCreatedAt()).isEqualTo(now);
+ assertThat(dto2.getUpdatedAt()).isNull();
}
@Test
@@ -160,21 +167,24 @@ public class DebtModelRestoreTest {
ArgumentCaptor<CharacteristicDto> characteristicArgument = ArgumentCaptor.forClass(CharacteristicDto.class);
verify(dao, times(2)).update(characteristicArgument.capture(), eq(session));
- assertThat(characteristicArgument.getAllValues().get(0).getId()).isEqualTo(1);
- assertThat(characteristicArgument.getAllValues().get(0).getKey()).isEqualTo("PORTABILITY");
- assertThat(characteristicArgument.getAllValues().get(0).getName()).isEqualTo("Portability");
- assertThat(characteristicArgument.getAllValues().get(0).getParentId()).isNull();
- assertThat(characteristicArgument.getAllValues().get(0).getOrder()).isEqualTo(1);
- assertThat(characteristicArgument.getAllValues().get(0).getCreatedAt()).isEqualTo(oldDate);
- assertThat(characteristicArgument.getAllValues().get(0).getUpdatedAt()).isEqualTo(now);
-
- assertThat(characteristicArgument.getAllValues().get(1).getId()).isEqualTo(2);
- assertThat(characteristicArgument.getAllValues().get(1).getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
- assertThat(characteristicArgument.getAllValues().get(1).getName()).isEqualTo("Compiler");
- assertThat(characteristicArgument.getAllValues().get(1).getParentId()).isEqualTo(1);
- assertThat(characteristicArgument.getAllValues().get(1).getOrder()).isNull();
- assertThat(characteristicArgument.getAllValues().get(1).getCreatedAt()).isEqualTo(oldDate);
- assertThat(characteristicArgument.getAllValues().get(1).getUpdatedAt()).isEqualTo(now);
+
+ CharacteristicDto dto1 = characteristicArgument.getAllValues().get(0);
+ assertThat(dto1.getId()).isEqualTo(1);
+ assertThat(dto1.getKey()).isEqualTo("PORTABILITY");
+ assertThat(dto1.getName()).isEqualTo("Portability");
+ assertThat(dto1.getParentId()).isNull();
+ assertThat(dto1.getOrder()).isEqualTo(1);
+ assertThat(dto1.getCreatedAt()).isEqualTo(oldDate);
+ assertThat(dto1.getUpdatedAt()).isEqualTo(now);
+
+ CharacteristicDto dto2 = characteristicArgument.getAllValues().get(1);
+ assertThat(dto2.getId()).isEqualTo(2);
+ assertThat(dto2.getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
+ assertThat(dto2.getName()).isEqualTo("Compiler");
+ assertThat(dto2.getParentId()).isEqualTo(1);
+ assertThat(dto2.getOrder()).isNull();
+ assertThat(dto2.getCreatedAt()).isEqualTo(oldDate);
+ assertThat(dto2.getUpdatedAt()).isEqualTo(now);
}
@Test
@@ -201,18 +211,18 @@ public class DebtModelRestoreTest {
new CharacteristicDto().setId(2).setKey("COMPILER_RELATED_PORTABILITY").setName("Compiler updated").setParentId(1).setCreatedAt(oldDate)
));
- when(ruleDao.selectOverridingDebt(session)).thenReturn(newArrayList(
+ when(ruleDao.selectOverridingDebt(Collections.<String>emptyList(), session)).thenReturn(newArrayList(
new RuleDto().setCharacteristicId(10).setRemediationFunction("LINEAR_OFFSET").setRemediationFactor("2h").setRemediationOffset("15min")
.setCreatedAt(oldDate).setUpdatedAt(oldDate)
));
- debtModelRestore.restoreFromProvidedModel();
+ debtModelRestore.restore();
verify(dao).selectEnabledCharacteristics();
verify(dao, times(2)).update(any(CharacteristicDto.class), eq(session));
verifyNoMoreInteractions(dao);
- verify(ruleDao).selectOverridingDebt(session);
+ verify(ruleDao).selectOverridingDebt(Collections.<String>emptyList(), session);
ArgumentCaptor<RuleDto> ruleArgument = ArgumentCaptor.forClass(RuleDto.class);
verify(ruleDao).update(ruleArgument.capture(), eq(session));
verifyNoMoreInteractions(ruleDao);
@@ -226,4 +236,41 @@ public class DebtModelRestoreTest {
verify(session).commit();
}
+
+ @Test
+ public void restore_from_language() throws Exception {
+ Date oldDate = DateUtils.parseDate("2014-01-01");
+
+ defaultModel
+ .addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
+ .addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setName("Compiler"), "PORTABILITY");
+
+ when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(
+ new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability updated").setOrder(2).setCreatedAt(oldDate),
+ new CharacteristicDto().setId(2).setKey("COMPILER_RELATED_PORTABILITY").setName("Compiler updated").setParentId(1).setCreatedAt(oldDate)
+ ));
+
+ when(ruleDao.selectOverridingDebt(newArrayList("squid"), session)).thenReturn(newArrayList(
+ new RuleDto().setRepositoryKey("squid")
+ .setCharacteristicId(10).setRemediationFunction("LINEAR_OFFSET").setRemediationFactor("2h").setRemediationOffset("15min")
+ .setCreatedAt(oldDate).setUpdatedAt(oldDate)
+ ));
+
+ RuleRepositories.Repository squid = mock(RuleRepositories.Repository.class);
+ when(squid.getKey()).thenReturn("squid");
+ when(ruleRepositories.repositoriesForLang("java")).thenReturn(newArrayList(squid));
+
+ debtModelRestore.restore("java");
+
+ verify(dao).selectEnabledCharacteristics();
+ verify(dao, times(2)).update(any(CharacteristicDto.class), eq(session));
+ verifyNoMoreInteractions(dao);
+
+ verify(ruleDao).selectOverridingDebt(newArrayList("squid"), session);
+ ArgumentCaptor<RuleDto> ruleArgument = ArgumentCaptor.forClass(RuleDto.class);
+ verify(ruleDao).update(ruleArgument.capture(), eq(session));
+ verifyNoMoreInteractions(ruleDao);
+
+ verify(session).commit();
+ }
}
diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
index 33205fe2275..e02bb0133a3 100644
--- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
@@ -97,7 +97,13 @@ public class DebtModelServiceTest {
@Test
public void restore_provided_model() {
service.restore();
- verify(debtModelRestore).restoreFromProvidedModel();
+ verify(debtModelRestore).restore();
+ }
+
+ @Test
+ public void restore_from_language() {
+ service.restore("xoo");
+ verify(debtModelRestore).restore("xoo");
}
}