@CheckForNull private ActiveRuleParamDto createParamDto(RuleParamDto param, @Nullable String value) { if (value == null) { return null; } ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(param); paramDto.setValue(validateParam(param, value)); return paramDto; }
@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())); } } }
@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); }
@Test public void select_params_by_active_rule_ids() { ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER); underTest.insert(dbSession, activeRule1); underTest.insertParam(dbSession, activeRule1, ActiveRuleParamDto.createFor(rule1Param1)); underTest.insertParam(dbSession, activeRule1, ActiveRuleParamDto.createFor(rule1Param2)); ActiveRuleDto activeRule2 = createFor(profile1, rule2).setSeverity(BLOCKER); underTest.insert(dbSession, activeRule2); underTest.insertParam(dbSession, activeRule2, ActiveRuleParamDto.createFor(rule2Param1)); dbSession.commit(); assertThat(underTest.selectParamsByActiveRuleIds(dbSession, asList(activeRule1.getId(), activeRule2.getId()))).hasSize(3); }
@Test public void deleteParametersByRuleProfileUuids_does_nothing_if_keys_are_empty() { ActiveRuleDto activeRuleInProfile1 = newRow(profile1, rule1); underTest.insert(dbSession, activeRuleInProfile1); ActiveRuleParamDto param1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRuleInProfile1, param1); underTest.deleteParametersByRuleProfileUuids(dbSession, emptyList()); assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile1.getId())) .hasSize(1); }
@Test public void deleteParamsByActiveRuleIds() { ActiveRuleDto ar1 = underTest.insert(dbSession, newRow(profile1, rule1)); ActiveRuleParamDto param = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, ar1, param); ActiveRuleDto ar2 = underTest.insert(dbSession, newRow(profile1, rule2)); ActiveRuleParamDto param2 = ActiveRuleParamDto.createFor(rule2Param1).setValue("bar"); underTest.insertParam(dbSession, ar2, param2); underTest.deleteParamsByActiveRuleIds(dbSession, asList(ar1.getId())); assertThat(underTest.selectParamsByActiveRuleId(dbSession, ar1.getId())).hasSize(0); assertThat(underTest.selectParamsByActiveRuleId(dbSession, ar2.getId())).hasSize(1); }
activeRuleParamDto = ActiveRuleParamDto.createFor(context.getRule().getParam(param.getKey())); activeRuleParamDto.setValue(param.getValue()); dao.insertParam(dbSession, activeRule.get(), activeRuleParamDto);
@Test public void deleteParam_deletes_rows_by_id() { ActiveRuleDto activeRule = newRow(profile1, rule1); underTest.insert(dbSession, activeRule); ActiveRuleParamDto param = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, param); underTest.deleteParam(dbSession, param); assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId())).hasSize(0); }
@Test public void deleteParamsByRuleParamOfAllOrganizations() { ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER); underTest.insert(dbSession, activeRule1); ActiveRuleParamDto activeRuleParam1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule1, activeRuleParam1); ActiveRuleDto activeRule2 = createFor(profile2, rule1).setSeverity(BLOCKER); underTest.insert(dbSession, activeRule2); ActiveRuleParamDto activeRuleParam2 = ActiveRuleParamDto.createFor(rule1Param1).setValue("bar"); underTest.insertParam(dbSession, activeRule2, activeRuleParam2); List<Integer> activeRuleIds = asList(activeRule1.getId(), activeRule2.getId()); assertThat(underTest.selectParamsByActiveRuleIds(dbSession, activeRuleIds)).hasSize(2); underTest.deleteParamsByRuleParamOfAllOrganizations(dbSession, rule1Param1); assertThat(underTest.selectParamsByActiveRuleIds(dbSession, activeRuleIds)).isEmpty(); }
private ActiveRuleDto createActiveRuleWithParam(RuleDefinitionDto rule, QProfileDto profile, String value) { ActiveRuleDto activeRule = createActiveRule(rule, profile); RuleParamDto paramDto = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value); dbClient.activeRuleDao().insertParam(session, activeRule, activeRuleParam); return activeRule; }
@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")); }
@Test public void insertParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); 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("foo")); }
private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivationContext context, DbSession dbSession) { ActiveRuleDao dao = db.activeRuleDao(); RuleWrapper rule = context.getRule(); ActiveRuleDto activeRule = new ActiveRuleDto(); activeRule.setProfileId(context.getRulesProfile().getId()); activeRule.setRuleId(rule.get().getId()); activeRule.setKey(ActiveRuleKey.of(context.getRulesProfile(), rule.get().getKey())); String severity = change.getSeverity(); if (severity != null) { activeRule.setSeverity(severity); } ActiveRuleInheritance inheritance = change.getInheritance(); if (inheritance != null) { activeRule.setInheritance(inheritance.name()); } activeRule.setUpdatedAt(system2.now()); activeRule.setCreatedAt(system2.now()); dao.insert(dbSession, activeRule); for (Map.Entry<String, String> param : change.getParameters().entrySet()) { if (param.getValue() != null) { ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(rule.getParam(param.getKey())); paramDto.setValue(param.getValue()); dao.insertParam(dbSession, activeRule, paramDto); } } return activeRule; }
@Test public void deleteParametersByRuleProfileUuids_deletes_rows_by_rule_profile_uuids() { ActiveRuleDto activeRuleInProfile1 = newRow(profile1, rule1); underTest.insert(dbSession, activeRuleInProfile1); ActiveRuleParamDto param1 = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRuleInProfile1, param1); ActiveRuleDto activeRuleInProfile2 = newRow(profile2, rule1); underTest.insert(dbSession, activeRuleInProfile2); ActiveRuleParamDto param2 = ActiveRuleParamDto.createFor(rule1Param1).setValue("bar"); underTest.insertParam(dbSession, activeRuleInProfile2, param2); underTest.deleteParametersByRuleProfileUuids(dbSession, asList(profile1.getRulesProfileUuid(), "does_not_exist")); assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile1.getId())).isEmpty(); assertThat(underTest.selectParamsByActiveRuleId(dbSession, activeRuleInProfile2.getId())) .extracting(ActiveRuleParamDto::getKey, ActiveRuleParamDto::getValue) .containsExactly(tuple(rule1Param1.getName(), "bar")); }
@Test public void insertParam_fails_when_active_rule_id_is_null() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("ActiveRule is not persisted"); underTest.insertParam(dbSession, createFor(profile1, rule1).setId(null), ActiveRuleParamDto.createFor(rule1Param1).setValue("activeValue1")); }
@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 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)); }
ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue()); dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
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; }
ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(ruleParam).setValue(null); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleChanges.get(0).getActiveRule(), activeRuleParam);