@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 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); }
@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 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 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 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")); }
@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")); }
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 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_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)); }
@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)); }
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);
@Test public void show_rule_with_activation() { OrganizationDto organization = db.organizations().insert(); RuleDefinitionDto rule = db.rules().insert(); RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*")); RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule, organization, m -> m.setNoteData(null).setNoteUserUuid(null)); QProfileDto qProfile = db.qualityProfiles().insert(organization); ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, new ActiveRuleParamDto() .setRulesParameterId(ruleParam.getId()) .setKey(ruleParam.getName()) .setValue(".*?")); db.commit(); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .setParam(PARAM_ACTIVES, "true") .setParam(PARAM_ORGANIZATION, organization.getKey()) .executeProtobuf(ShowResponse.class); List<Rules.Active> actives = result.getActivesList(); assertThat(actives).extracting(Rules.Active::getQProfile).containsExactly(qProfile.getKee()); assertThat(actives).extracting(Rules.Active::getSeverity).containsExactly(activeRule.getSeverityString()); assertThat(actives).extracting(Rules.Active::getInherit).containsExactly("NONE"); assertThat(actives.get(0).getParamsList()) .extracting(Rules.Active.Param::getKey, Rules.Active.Param::getValue) .containsExactlyInAnyOrder(tuple(ruleParam.getName(), ".*?")); }
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; }
private RulesProfileDto createBuiltInProfile() { RulesProfileDto rulesProfileDto = new RulesProfileDto() .setIsBuiltIn(true) .setKee(Uuids.createFast()) .setLanguage("xoo") .setName("Sonar way"); db.getDbClient().qualityProfileDao().insert(dbSession, rulesProfileDto); ActiveRuleDto activeRuleDto = new ActiveRuleDto() .setProfileId(rulesProfileDto.getId()) .setRuleId(rule.getId()) .setSeverity(Severity.BLOCKER); db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto); 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(rulesProfileDto.getKee())); db.commit(); return rulesProfileDto; }