@Test public void search_by_template_key_with_params() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); RuleParamDto ruleParamDto = RuleParamDto.createFor(templateRule) .setName("regex") .setType("STRING") .setDescription("Reg ex") .setDefaultValue(".*"); dao.insert(dbSession, templateRule); dao.addRuleParam(dbSession, templateRule, ruleParamDto); RuleDto customRule = RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId()); RuleParamDto customRuleParam = RuleParamDto.createFor(customRule) .setName("regex") .setType("STRING") .setDescription("Reg ex") .setDefaultValue("a.*"); dao.insert(dbSession, customRule); dao.addRuleParam(dbSession, customRule, customRuleParam); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // get params assertThat(index.getByKey(templateRule.getKey()).params()).hasSize(1); assertThat(index.getByKey(customRule.getKey()).params()).hasSize(1); }
@Test public void search_by_template_key() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); dao.insert(dbSession, templateRule); dao.insert( dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId())); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // Only custom rule query = new RuleQuery().setTemplateKey("java:S001"); results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(1); assertThat(Iterables.getFirst(results.getHits(), null).key().rule()) .isEqualTo("S001_MY_CUSTOM"); assertThat(Iterables.getFirst(results.getHits(), null).templateKey()) .isEqualTo(RuleKey.of("java", "S001")); // null => no filter query = new RuleQuery().setTemplateKey(null); assertThat(index.search(query, new QueryContext()).getHits()).hasSize(2); }
private RuleKey createCustomRule( RuleKey ruleKey, NewRule newRule, RuleDto templateRuleDto, DbSession dbSession) { RuleDto ruleDto = RuleDto.createFor(ruleKey) .setTemplateId(templateRuleDto.getId()) .setConfigKey(templateRuleDto.getConfigKey()) .setName(newRule.name()) .setDescription(newRule.markdownDescription()) .setDescriptionFormat(Format.MARKDOWN) .setSeverity(newRule.severity()) .setStatus(newRule.status()) .setLanguage(templateRuleDto.getLanguage()) .setDefaultSubCharacteristicId(templateRuleDto.getDefaultSubCharacteristicId()) .setDefaultRemediationFunction(templateRuleDto.getDefaultRemediationFunction()) .setDefaultRemediationCoefficient(templateRuleDto.getDefaultRemediationCoefficient()) .setDefaultRemediationOffset(templateRuleDto.getDefaultRemediationOffset()) .setEffortToFixDescription(templateRuleDto.getEffortToFixDescription()) .setTags(templateRuleDto.getTags()) .setSystemTags(templateRuleDto.getSystemTags()); dbClient.ruleDao().insert(dbSession, ruleDto); for (RuleParamDto templateRuleParamDto : dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, templateRuleDto.getKey())) { String customRuleParamValue = Strings.emptyToNull(newRule.parameter(templateRuleParamDto.getName())); createCustomRuleParams(customRuleParamValue, ruleDto, templateRuleParamDto, dbSession); } return ruleKey; }
@Test public void scroll_byIds() throws Exception { Set<Integer> ids = new HashSet<Integer>(); for (int i = 0; i < 150; i++) { RuleDto rule = RuleTesting.newDto(RuleKey.of("scroll", "r_" + i)); dao.insert(dbSession, rule); dbSession.commit(); ids.add(rule.getId()); } List<Rule> rules = index.getByIds(ids); assertThat(rules).hasSize(ids.size()); }
@Test public void show_custom_rule() throws InterruptedException { RuleDto templateRule = RuleTesting.newDto(RuleKey.of("java", "S001")).setIsTemplate(true); dao.insert(dbSession, templateRule); dao.insert( dbSession, RuleTesting.newDto(RuleKey.of("java", "S001_MY_CUSTOM")) .setTemplateId(templateRule.getId())); dbSession.commit(); // find all RuleQuery query = new RuleQuery(); Result<Rule> results = index.search(query, new QueryContext()); assertThat(results.getHits()).hasSize(2); // find custom rule assertThat(index.getByKey(RuleKey.of("java", "S001_MY_CUSTOM")).templateKey()) .isEqualTo(RuleKey.of("java", "S001")); }
@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); }