@Test public void delete_sub_characteristic() { DbSession batchSession = mock(DbSession.class); when(dbClient.openSession(true)).thenReturn(batchSession); when(ruleDao.findRulesByDebtSubCharacteristicId(batchSession, 2)) .thenReturn( newArrayList( new RuleDto() .setSubCharacteristicId(2) .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setRemediationCoefficient("2h") .setRemediationOffset("5min") .setDefaultSubCharacteristicId(10) .setDefaultRemediationFunction( DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setDefaultRemediationCoefficient("4h") .setDefaultRemediationOffset("15min"))); when(dao.selectById(2, batchSession)).thenReturn(subCharacteristicDto); service.delete(2); verify(ruleDao).update(eq(batchSession), ruleCaptor.capture()); RuleDto ruleDto = ruleCaptor.getValue(); assertThat(ruleDto.getUpdatedAt()).isEqualTo(now); // Overridden debt data are disabled assertThat(ruleDto.getSubCharacteristicId()).isEqualTo(-1); assertThat(ruleDto.getRemediationFunction()).isNull(); assertThat(ruleDto.getRemediationCoefficient()).isNull(); assertThat(ruleDto.getRemediationOffset()).isNull(); // Default debt data should not be touched assertThat(ruleDto.getDefaultSubCharacteristicId()).isEqualTo(10); assertThat(ruleDto.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET"); assertThat(ruleDto.getDefaultRemediationCoefficient()).isEqualTo("4h"); assertThat(ruleDto.getDefaultRemediationOffset()).isEqualTo("15min"); verify(dao).update(characteristicCaptor.capture(), eq(batchSession)); CharacteristicDto characteristicDto = characteristicCaptor.getValue(); // Sub characteristic is disable assertThat(characteristicDto.getId()).isEqualTo(2); assertThat(characteristicDto.isEnabled()).isFalse(); assertThat(characteristicDto.getUpdatedAt()).isEqualTo(now); }
@Test public void reset_model_on_custom_rules() { when(characteristicsXMLImporter.importXML(any(Reader.class))) .thenReturn( new DebtModel() .addRootCharacteristic( new DefaultDebtCharacteristic() .setKey("PORTABILITY") .setName("Portability") .setOrder(1)) .addSubCharacteristic( new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY")); when(dao.selectEnabledCharacteristics(session)) .thenReturn( newArrayList( new CharacteristicDto() .setId(1) .setKey("PORTABILITY") .setName("Portability updated") .setOrder(2) .setCreatedAt(oldDate), new CharacteristicDto() .setId(2) .setKey("COMPILER") .setName("Compiler updated") .setParentId(1) .setCreatedAt(oldDate))); when(ruleDao.selectEnabledAndNonManual(session)) .thenReturn( newArrayList( // Template rule new RuleDto() .setId(5) .setRepositoryKey("squid") .setRuleKey("XPath") .setSubCharacteristicId(2) .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setRemediationCoefficient("2h") .setRemediationOffset("15min"), // Custom rule new RuleDto() .setId(6) .setRepositoryKey("squid") .setRuleKey("XPath_1369910135") .setTemplateId(5) .setSubCharacteristicId(2) .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setRemediationCoefficient("2h") .setRemediationOffset("15min"))); RulesDefinition.Context context = new RulesDefinition.Context(); // Template rule RulesDefinition.NewRepository repo = context.createRepository("squid", "java").setName("XPath"); RulesDefinition.NewRule newRule = repo.createRule("XPath") .setName("XPath") .setHtmlDescription("XPath") .setSeverity(Severity.BLOCKER) .setStatus(RuleStatus.BETA) .setDebtSubCharacteristic("COMPILER"); newRule.setDebtRemediationFunction( newRule.debtRemediationFunctions().linearWithOffset("4h", "20min")); repo.done(); when(defLoader.load()).thenReturn(context); debtModelBackup.reset(); verify(ruleDao).selectEnabledAndNonManual(session); verify(ruleDao, times(2)).update(eq(session), ruleCaptor.capture()); verifyNoMoreInteractions(ruleDao); verify(session).commit(); RuleDto rule = ruleCaptor.getAllValues().get(1); assertThat(rule.getId()).isEqualTo(6); assertThat(rule.getDefaultSubCharacteristicId()).isEqualTo(2); assertThat(rule.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET"); assertThat(rule.getDefaultRemediationCoefficient()).isEqualTo("4h"); assertThat(rule.getDefaultRemediationOffset()).isEqualTo("20min"); assertThat(rule.getUpdatedAt()).isEqualTo(now); assertThat(rule.getSubCharacteristicId()).isNull(); assertThat(rule.getRemediationFunction()).isNull(); assertThat(rule.getRemediationCoefficient()).isNull(); assertThat(rule.getRemediationOffset()).isNull(); assertThat(rule.getUpdatedAt()).isEqualTo(now); }
@Test public void reset_model_when_no_default_value() { when(characteristicsXMLImporter.importXML(any(Reader.class))) .thenReturn( new DebtModel() .addRootCharacteristic( new DefaultDebtCharacteristic() .setKey("PORTABILITY") .setName("Portability") .setOrder(1)) .addSubCharacteristic( new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY")); when(dao.selectEnabledCharacteristics(session)) .thenReturn( newArrayList( new CharacteristicDto() .setId(1) .setKey("PORTABILITY") .setName("Portability updated") .setOrder(2) .setCreatedAt(oldDate), new CharacteristicDto() .setId(2) .setKey("COMPILER") .setName("Compiler updated") .setParentId(1) .setCreatedAt(oldDate))); when(ruleDao.selectEnabledAndNonManual(session)) .thenReturn( newArrayList( new RuleDto() .setRepositoryKey("squid") .setRuleKey("NPE") .setDefaultSubCharacteristicId(10) .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.toString()) .setDefaultRemediationCoefficient("2h") .setSubCharacteristicId(2) .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString()) .setRemediationCoefficient("2h") .setRemediationOffset("15min"))); RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository repo = context.createRepository("squid", "java").setName("Squid"); repo.createRule("NPE") .setName("Detect NPE") .setHtmlDescription("Detect <code>java.lang.NullPointerException</code>") .setSeverity(Severity.BLOCKER) .setStatus(RuleStatus.BETA); repo.done(); when(defLoader.load()).thenReturn(context); debtModelBackup.reset(); verify(dao).selectEnabledCharacteristics(session); verify(dao, times(2)).update(any(CharacteristicDto.class), eq(session)); verifyNoMoreInteractions(dao); verify(ruleDao).selectEnabledAndNonManual(session); verify(ruleDao).update(eq(session), ruleCaptor.capture()); verifyNoMoreInteractions(ruleDao); verify(session).commit(); RuleDto rule = ruleCaptor.getValue(); assertThat(rule.getDefaultSubCharacteristicId()).isNull(); assertThat(rule.getDefaultRemediationFunction()).isNull(); assertThat(rule.getDefaultRemediationCoefficient()).isNull(); assertThat(rule.getDefaultRemediationOffset()).isNull(); assertThat(rule.getUpdatedAt()).isEqualTo(now); }