private CharacteristicDto restoreCharacteristic(
     DebtCharacteristic targetCharacteristic,
     @Nullable Integer parentId,
     List<CharacteristicDto> sourceCharacteristics,
     Date updateDate,
     DbSession session) {
   CharacteristicDto sourceCharacteristic =
       characteristicByKey(targetCharacteristic.key(), sourceCharacteristics, false);
   if (sourceCharacteristic == null) {
     CharacteristicDto newCharacteristic =
         toDto(targetCharacteristic, parentId).setCreatedAt(updateDate);
     dbClient.debtCharacteristicDao().insert(newCharacteristic, session);
     return newCharacteristic;
   } else {
     // Update only if modifications
     if (ObjectUtils.notEqual(sourceCharacteristic.getName(), targetCharacteristic.name())
         || ObjectUtils.notEqual(sourceCharacteristic.getOrder(), targetCharacteristic.order())
         || ObjectUtils.notEqual(sourceCharacteristic.getParentId(), parentId)) {
       sourceCharacteristic.setName(targetCharacteristic.name());
       sourceCharacteristic.setOrder(targetCharacteristic.order());
       sourceCharacteristic.setParentId(parentId);
       sourceCharacteristic.setUpdatedAt(updateDate);
       dbClient.debtCharacteristicDao().update(sourceCharacteristic, session);
     }
     return sourceCharacteristic;
   }
 }
  @CheckForNull
  private static RuleDebt toRuleDebt(RuleDto rule, DebtModel debtModel) {
    RuleDebt ruleDebt =
        new RuleDebt().setRuleKey(RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey()));
    Integer effectiveSubCharacteristicId =
        rule.getSubCharacteristicId() != null
            ? rule.getSubCharacteristicId()
            : rule.getDefaultSubCharacteristicId();
    DebtCharacteristic subCharacteristic =
        (effectiveSubCharacteristicId != null
                && !RuleDto.DISABLED_CHARACTERISTIC_ID.equals(effectiveSubCharacteristicId))
            ? debtModel.characteristicById(effectiveSubCharacteristicId)
            : null;
    if (subCharacteristic != null) {
      ruleDebt.setSubCharacteristicKey(subCharacteristic.key());

      String overriddenFunction = rule.getRemediationFunction();
      String defaultFunction = rule.getDefaultRemediationFunction();
      if (overriddenFunction != null) {
        ruleDebt.setFunction(overriddenFunction);
        ruleDebt.setCoefficient(rule.getRemediationCoefficient());
        ruleDebt.setOffset(rule.getRemediationOffset());
        return ruleDebt;
      } else if (defaultFunction != null) {
        ruleDebt.setFunction(defaultFunction);
        ruleDebt.setCoefficient(rule.getDefaultRemediationCoefficient());
        ruleDebt.setOffset(rule.getDefaultRemediationOffset());
        return ruleDebt;
      }
    }
    return null;
  }
  @VisibleForTesting
  List<CharacteristicDto> restoreCharacteristics(
      DebtModel targetModel, Date updateDate, DbSession session) {
    List<CharacteristicDto> sourceCharacteristics =
        dbClient.debtCharacteristicDao().selectEnabledCharacteristics(session);

    List<CharacteristicDto> result = newArrayList();

    // Create new characteristics
    for (DebtCharacteristic characteristic : targetModel.rootCharacteristics()) {
      CharacteristicDto rootCharacteristicDto =
          restoreCharacteristic(characteristic, null, sourceCharacteristics, updateDate, session);
      result.add(rootCharacteristicDto);
      for (DebtCharacteristic subCharacteristic :
          targetModel.subCharacteristics(characteristic.key())) {
        result.add(
            restoreCharacteristic(
                subCharacteristic,
                rootCharacteristicDto.getId(),
                sourceCharacteristics,
                updateDate,
                session));
      }
    }
    // Disable no more existing characteristics
    for (CharacteristicDto sourceCharacteristic : sourceCharacteristics) {
      if (targetModel.characteristicByKey(sourceCharacteristic.getKey()) == null) {
        debtModelOperations.delete(sourceCharacteristic, updateDate, session);
      }
    }
    return result;
  }
 private static CharacteristicDto toDto(
     DebtCharacteristic characteristic, @Nullable Integer parentId) {
   return new CharacteristicDto()
       .setKey(characteristic.key())
       .setName(characteristic.name())
       .setOrder(characteristic.order())
       .setParentId(parentId)
       .setEnabled(true)
       .setCreatedAt(((DefaultDebtCharacteristic) characteristic).createdAt())
       .setUpdatedAt(((DefaultDebtCharacteristic) characteristic).updatedAt());
 }
  @Test
  public void move_down() {
    when(dao.selectById(10, session))
        .thenReturn(new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2));
    when(dao.selectEnabledRootCharacteristics(session))
        .thenReturn(
            newArrayList(
                new CharacteristicDto().setId(10).setKey("MEMORY_EFFICIENCY").setOrder(2),
                new CharacteristicDto().setId(2).setKey("PORTABILITY").setOrder(3)));

    DebtCharacteristic result = service.moveDown(10);

    verify(dao, times(2)).update(characteristicCaptor.capture(), eq(session));

    assertThat(result.order()).isEqualTo(3);
    assertThat(characteristicCaptor.getAllValues().get(0).getOrder()).isEqualTo(2);
    assertThat(characteristicCaptor.getAllValues().get(0).getUpdatedAt()).isEqualTo(now);
    assertThat(characteristicCaptor.getAllValues().get(1).getOrder()).isEqualTo(3);
    assertThat(characteristicCaptor.getAllValues().get(1).getUpdatedAt()).isEqualTo(now);
  }