]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Create restore from language action
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Mar 2014 15:17:04 +0000 (16:17 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Mar 2014 15:17:13 +0000 (16:17 +0100)
sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java
sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java
sonar-core/src/main/resources/org/sonar/core/rule/RuleMapper.xml
sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java
sonar-core/src/test/resources/org/sonar/core/rule/RuleDaoTest/select_overriding_debt_rules.xml
sonar-server/src/main/java/org/sonar/server/debt/DebtModelOperations.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelRestore.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelRestoreTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java

index 2f5e0441f3d62316718d43f9bac04e0490870dbe..0324dac5cb0cb7b6889e232979a7106fef327c84 100644 (file)
@@ -76,17 +76,17 @@ public class RuleDao implements BatchComponent, ServerComponent {
     return getMapper(session).selectByCharacteristicOrSubCharacteristicId(characteristicOrSubCharacteristicId);
   }
 
-  public List<RuleDto> selectOverridingDebt() {
+  public List<RuleDto> selectOverridingDebt(List<String> repositories) {
     SqlSession session = mybatis.openSession();
     try {
-      return selectOverridingDebt(session);
+      return selectOverridingDebt(repositories, session);
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public List<RuleDto> selectOverridingDebt(SqlSession session) {
-    return getMapper(session).selectOverridingDebt();
+  public List<RuleDto> selectOverridingDebt(List<String> repositories, SqlSession session) {
+    return getMapper(session).selectOverridingDebt(repositories);
   }
 
   @CheckForNull
index 5fd2b2f3658267b8184d489a1185b473f080a5cf..9b49070b0d09595f8c84218a33f261fc51ce5dc2 100644 (file)
@@ -32,7 +32,7 @@ public interface RuleMapper {
 
   List<RuleDto> selectByCharacteristicOrSubCharacteristicId(int id);
 
-  List<RuleDto> selectOverridingDebt();
+  List<RuleDto> selectOverridingDebt(@Param("repositories") List<String> repositories);
 
   RuleDto selectById(Integer id);
 
index aa93b05e4724a499ef9239ed171ebf4635e3fc59..7f61cd090d43815aad3494df46907245b462adf0 100644 (file)
     SELECT <include refid="selectColumns"/> FROM rules
     <where>
       AND (characteristic_id is NOT NULL or remediation_function IS NOT NULL)
+      <if test="repositories.size()>0">
+        AND
+        <foreach item="repo" index="index" collection="repositories" open="(" separator=" or " close=")">
+          plugin_name=#{repo}
+        </foreach>
+      </if>
     </where>
   </select>
 
index c7f0fa4c3fc40d296d1a26e552855648c926d3cc..1c638ff976cc225d96dcfcb7bcc0dc48a7b9b0d1 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.utils.DateUtils;
 import org.sonar.check.Cardinality;
 import org.sonar.core.persistence.AbstractDaoTestCase;
 
+import java.util.Collections;
 import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
@@ -158,7 +159,12 @@ public class RuleDaoTest extends AbstractDaoTestCase {
   public void select_overriding_debt_rules() throws Exception {
     setupData("select_overriding_debt_rules");
 
-    assertThat(dao.selectOverridingDebt()).hasSize(3);
+    assertThat(dao.selectOverridingDebt(Collections.<String>emptyList())).hasSize(3);
+
+    assertThat(dao.selectOverridingDebt(newArrayList("squid"))).hasSize(2);
+    assertThat(dao.selectOverridingDebt(newArrayList("java"))).hasSize(1);
+    assertThat(dao.selectOverridingDebt(newArrayList("squid", "java"))).hasSize(3);
+    assertThat(dao.selectOverridingDebt(newArrayList("unknown"))).isEmpty();
   }
 
   @Test
index 2e4d2271b40a27007efa9e05bfc61896542e8b76..2dbc8164bfac2221a2309b11fc08f5ccb4942841 100644 (file)
@@ -1,28 +1,28 @@
 <dataset>
 
-  <!-- Rule overriding debt and with default debt should be returned -->
+  <!-- Rule overriding debt and with default debt -->
   <rules id="1" plugin_rule_key="UselessImportCheck" plugin_name="squid" name="UselessImportCheck" description="Useless imports should be removed" status="READY"
          characteristic_id="2" default_characteristic_id="50"
          remediation_function="LINEAR_OFFSET" default_remediation_function="LINEAR_OFFSET"
          remediation_factor="5d" default_remediation_factor="5d"
          remediation_offset="10h" default_remediation_offset="10h" updated_at="2014-02-19"/>
 
-  <!-- Rule only overriding debt should be returned -->
+  <!-- Rule only overriding debt -->
   <rules id="2" plugin_rule_key="LeftCurlyBraceStartLineCheck" plugin_name="squid" name="LeftCurlyBraceStartLineCheck" description="Left curly braces should be located at the beginning of lines of code" status="READY"
          characteristic_id="3" default_characteristic_id="[null]"
          remediation_function="LINEAR_OFFSET" default_remediation_function="[null]"
          remediation_factor="5d" default_remediation_factor="[null]"
          remediation_offset="10h" default_remediation_offset="[null]" updated_at="2014-02-19"/>
 
-  <!-- Rule with only default debt should be returned -->
+  <!-- Rule with only default debt : never been returned -->
   <rules id="3" plugin_rule_key="CallToFileDeleteOnExitMethod" plugin_name="squid" name="CallToFileDeleteOnExitMethod" description="CallToFileDeleteOnExitMethod" status="READY"
          characteristic_id="[null]" default_characteristic_id="50"
          remediation_function="[null]" default_remediation_function="LINEAR_OFFSET"
          remediation_factor="[null]" default_remediation_factor="5d"
          remediation_offset="[null]" default_remediation_offset="10h" updated_at="2014-02-19"/>
 
-  <!-- Removed rule overriding debt : should be returned -->
-  <rules id="4" plugin_rule_key="ObjectFinalizeOverridenCallsSuperFinalizeCheck" plugin_name="squid" name="ObjectFinalizeOverridenCallsSuperFinalizeCheck" description="super.finalize() should be called at the end of Object.finalize() implementations" status="REMOVED"
+  <!-- Removed rule overriding debt -->
+  <rules id="4" plugin_rule_key="ObjectFinalizeOverridenCallsSuperFinalizeCheck" plugin_name="java" name="ObjectFinalizeOverridenCallsSuperFinalizeCheck" description="super.finalize() should be called at the end of Object.finalize() implementations" status="REMOVED"
          characteristic_id="3" default_characteristic_id="50"
          remediation_function="LINEAR" default_remediation_function="LINEAR_OFFSET"
          remediation_factor="5d" default_remediation_factor="5min"
index a87b0c51c177712f3364840787bc2c2c82a84d50..871f777a894e986da560fc30caf883e7d2d24d46 100644 (file)
@@ -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);
         }
       }
index e9ed1c635c685e7d309fd8ec1d74047f187e9032..380d4d5a3797000cb0c5f20ebe3a879393c8906c 100644 (file)
@@ -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) {
index 1049d0b1c254ea90278a817173175dec08638f99..c21e1d16b8a5f9a54f8b841f1c9ef44d4777851a 100644 (file)
@@ -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);
   }
 
 }
index e90865b2e3a2d7e9f140088cb6da04de0a6318f8..4c96c20172e03702cef9a2f3883d2667bc6eac37 100644 (file)
@@ -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;
@@ -75,6 +76,9 @@ public class DebtModelRestoreTest {
   @Mock
   DebtCharacteristicsXMLImporter characteristicsXMLImporter;
 
+  @Mock
+  RuleRepositories ruleRepositories;
+
   @Mock
   System2 system2;
 
@@ -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();
+  }
 }
index 33205fe22757839282722f04862fc44f96baad2a..e02bb0133a3aa6a8a2ce8809619f4a198966c33f 100644 (file)
@@ -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");
   }
 
 }