@Test public void updateParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); underTest.updateParam(dbSession, activeRuleParam.setValue("bar")); List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(reloaded).hasSize(1); assertThat(reloaded.get(0)) .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleId().equals(activeRule.getId())) .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) .matches(p -> p.getValue().equals("bar")); }
public static ActiveRuleParamDto createFor(RuleParamDto param) { Preconditions.checkArgument(param.getId() != null, "Parameter is not persisted"); return new ActiveRuleParamDto() .setKey(param.getName()) .setRulesParameterId(param.getId()); }
private QProfileDto createCustomProfile(OrganizationDto org) { QProfileDto profile = db.qualityProfiles().insert(org, p -> p.setLanguage("xoo").setIsBuiltIn(false)); ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto() .setRulesParameterId(ruleParam.getId()) .setKey("foo") .setValue("bar"); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam); db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto() .setChangeType(ActiveRuleChange.Type.ACTIVATED.name()) .setRulesProfileUuid(profile.getRulesProfileUuid())); db.commit(); return profile; }
.setSeverity(Severity.BLOCKER); db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto); db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, new ActiveRuleParamDto() .setActiveRuleId(activeRuleDto.getId()) .setRulesParameterId(ruleParam1.getId()) .setKey(ruleParam1.getName()) .setValue(ruleParam1.getDefaultValue())); dbSession.commit(); Map<String, ActiveRuleParamDto> activeRuleParamsByKey = ActiveRuleParamDto.groupByKey(activeRuleParams); assertThat(activeRuleParamsByKey.get("regex").getValue()).isEqualTo("b.*"); assertThat(activeRuleParamsByKey.get("message").getValue()).isEqualTo("a message"); assertThat(activeRuleParamsByKey.get("format")).isNull();
public ActiveRuleParamDto insertParam(DbSession dbSession, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) { checkArgument(activeRule.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED); checkArgument(activeRuleParam.getId() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED); Preconditions.checkNotNull(activeRuleParam.getRulesParameterId(), RULE_PARAM_IS_NOT_PERSISTED); activeRuleParam.setActiveRuleId(activeRule.getId()); mapper(dbSession).insertParameter(activeRuleParam); return activeRuleParam; }
ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(ruleParam).setValue(null); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleChanges.get(0).getActiveRule(), activeRuleParam); assertThat(activeList.getParamsList()).extracting("key", "value").containsExactlyInAnyOrder( tuple(ruleParam.getName(), ruleParam.getDefaultValue()), tuple(activeRuleParam.getKey(), ""));
@Test public void insertParam_fails_when_id_is_not_null() { thrown.expect(NullPointerException.class); thrown.expectMessage("Rule param is not persisted"); underTest.insertParam(dbSession, createFor(profile1, rule1).setId(100), ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setRulesParameterId(null)); }
@Test public void groupByKey() { assertThat(ActiveRuleParamDto.groupByKey(Collections.emptyList())).isEmpty(); Collection<ActiveRuleParamDto> dtos = Arrays.asList( new ActiveRuleParamDto().setKey("foo"), new ActiveRuleParamDto().setKey("bar") ); Map<String, ActiveRuleParamDto> group = ActiveRuleParamDto.groupByKey(dtos); assertThat(group.keySet()).containsOnly("foo", "bar"); } }
private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, RuleParamDto param) { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(param) .setValue("20") .setActiveRuleId(activeRule.getId()); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto); return activeRule; }
public static Map<String, ActiveRuleParamDto> groupByKey(Collection<ActiveRuleParamDto> params) { Map<String, ActiveRuleParamDto> result = new HashMap<>(); for (ActiveRuleParamDto param : params) { result.put(param.getKey(), param); } return result; } }
@Test public void insertParam_fails_when_active_rule_param_id_is_null() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("ActiveRuleParam is already persisted"); underTest.insertParam(dbSession, createFor(profile1, rule1).setId(100), ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1").setId(100)); }
@Override public OrgActiveRuleDto apply(@Nonnull ActiveRuleParamDto input) { return activeRuleById.get(input.getActiveRuleId()); } }
@CheckForNull String getParamValue(String key) { ActiveRuleParamDto param = paramsByKey.get(key); return param != null ? param.getValue() : null; } }
@Test public void select_params_by_active_rule_id() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(BLOCKER); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1); underTest.insertParam(dbSession, activeRule, activeRuleParam1); ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule1Param2); underTest.insertParam(dbSession, activeRule, activeRuleParam2); dbSession.commit(); assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId())).hasSize(2); }
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId)); ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId); for (ActiveRuleDto activeRule : activeRuleDtos) { // TODO all rules should be loaded by using one query with all active rule keys as parameter Rule rule = ruleFinder.findByKey(activeRule.getRuleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId()); for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); } } return target; }
@Override public void accept(@Nonnull ActiveRuleDto activeRuleDto) { Map<String, ActiveRuleParamDto> activeRuleParamByKey = from(activeRuleParams.get(activeRuleDto)) .uniqueIndex(ActiveRuleParamDto::getKey); ActiveRuleParamDto activeRuleParamDto = activeRuleParamByKey.get(ruleParamDto.getName()); if (activeRuleParamDto != null) { dbClient.activeRuleDao().updateParam(dbSession, activeRuleParamDto.setValue(ruleParamDto.getDefaultValue())); } else { dbClient.activeRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(ruleParamDto.getDefaultValue())); } } }