public Optional<ActiveRuleDto> delete(DbSession dbSession, ActiveRuleKey key) { Optional<ActiveRuleDto> activeRule = selectByKey(dbSession, key); if (activeRule.isPresent()) { mapper(dbSession).deleteParameters(activeRule.get().getId()); mapper(dbSession).delete(activeRule.get().getId()); } return activeRule; }
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; }
public ActiveRuleDto insert(DbSession dbSession, ActiveRuleDto item) { checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED); checkArgument(item.getRuleId() != null, RULE_IS_NOT_PERSISTED); checkArgument(item.getId() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED); mapper(dbSession).insert(item); return item; }
public ActiveRuleDto update(DbSession dbSession, ActiveRuleDto item) { checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED); checkArgument(item.getRuleId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED); checkArgument(item.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED); mapper(dbSession).update(item); return item; }
public void commitAndIndex(DbSession dbSession, Collection<ActiveRuleChange> changes) { List<EsQueueDto> items = changes.stream() .map(ActiveRuleChange::getActiveRule) .map(ar -> newQueueDto(String.valueOf(ar.getId()), ID_TYPE_ACTIVE_RULE_ID, String.valueOf(ar.getRuleId()))) .collect(toArrayList()); dbClient.esQueueDao().insert(dbSession, items); dbSession.commit(); postCommit(dbSession, items); }
@Override public List<ActiveRuleChange> deleteRule(DbSession dbSession, RuleDefinitionDto rule) { List<ActiveRuleChange> changes = new ArrayList<>(); List<Integer> activeRuleIds = new ArrayList<>(); db.activeRuleDao().selectByRuleIdOfAllOrganizations(dbSession, rule.getId()).forEach(ar -> { activeRuleIds.add(ar.getId()); changes.add(new ActiveRuleChange(ActiveRuleChange.Type.DEACTIVATED, ar, rule)); }); db.activeRuleDao().deleteByIds(dbSession, activeRuleIds); db.activeRuleDao().deleteParamsByActiveRuleIds(dbSession, activeRuleIds); return changes; }
private void verifyOnlyIndexed(ActiveRuleDto... expected) { List<String> docs = es.getIds(INDEX_TYPE_ACTIVE_RULE); assertThat(docs).hasSize(expected.length); for (ActiveRuleDto activeRuleDto : expected) { assertThat(docs).contains(activeRuleDto.getId().toString()); } }
private void compareActivationParams(DbSession session, ActiveRuleDto leftRule, ActiveRuleDto rightRule, QProfileComparisonResult result) { RuleKey key = leftRule.getRuleKey(); Map<String, String> leftParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, leftRule.getId())); Map<String, String> rightParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, rightRule.getId())); if (leftParams.equals(rightParams) && leftRule.getSeverityString().equals(rightRule.getSeverityString())) { result.same.put(key, leftRule); } else { ActiveRuleDiff diff = new ActiveRuleDiff(); diff.leftSeverity = leftRule.getSeverityString(); diff.rightSeverity = rightRule.getSeverityString(); diff.paramDifference = Maps.difference(leftParams, rightParams); result.modified.put(key, diff); } }
public void deleteParamsByRuleParamOfAllOrganizations(DbSession dbSession, RuleParamDto param) { List<ActiveRuleDto> activeRules = selectByRuleIdOfAllOrganizations(dbSession, param.getRuleId()); for (ActiveRuleDto activeRule : activeRules) { for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleId(dbSession, activeRule.getId())) { if (activeParam.getKey().equals(param.getName())) { deleteParam(dbSession, activeParam); } } } }
@Test public void deleteByIds() { ActiveRuleDto ar1 = underTest.insert(dbSession, newRow(profile1, rule1)); ActiveRuleDto ar2 = underTest.insert(dbSession, newRow(profile1, rule2)); ActiveRuleDto ar3 = underTest.insert(dbSession, newRow(profile2, rule1)); underTest.deleteByIds(dbSession, asList(ar1.getId(), ar3.getId())); assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1); assertThat(underTest.selectByProfile(dbSession, profile1)) .extracting(ActiveRuleDto::getId) .containsExactly(ar2.getId()); }
@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); }
private void verify(ActiveRuleDoc doc1, QProfileDto profile, ActiveRuleDto activeRule) { assertThat(doc1) .matches(doc -> doc.getId().equals("" + activeRule.getId())) .matches(doc -> doc.getRuleProfileUuid().equals(profile.getRulesProfileUuid())) .matches(doc -> doc.getSeverity().equals(activeRule.getSeverityString())); }
@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); }
private static void assertThatRuleHasParams(DbTester db, ActiveRuleDto activeRule, Tuple... expectedParams) { assertThat(db.getDbClient().activeRuleDao().selectParamsByActiveRuleId(db.getSession(), activeRule.getId())) .extracting(ActiveRuleParamDto::getKey, ActiveRuleParamDto::getValue) .containsExactlyInAnyOrder(expectedParams); }
@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 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 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 commitAndIndex_keeps_elements_to_recover_in_ES_QUEUE_on_errors() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); es.lockWrites(INDEX_TYPE_ACTIVE_RULE); commitAndIndex(rule1, ar); EsQueueDto expectedItem = EsQueueDto.create(INDEX_TYPE_ACTIVE_RULE.format(), "" + ar.getId(), "activeRuleId", valueOf(ar.getRuleId())); assertThatEsQueueContainsExactly(expectedItem); es.unlockWrites(INDEX_TYPE_ACTIVE_RULE); }
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; }
@Test public void scrollByRuleProfileForIndexing() { ActiveRuleDto ar1 = db.qualityProfiles().activateRule(profile1, rule1); ActiveRuleDto ar2 = db.qualityProfiles().activateRule(profile2, rule1); ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2); Accumulator accumulator = new Accumulator(); underTest.scrollByRuleProfileForIndexing(dbSession, profile2.getRulesProfileUuid(), accumulator); assertThat(accumulator.list) .extracting(IndexedActiveRuleDto::getId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey, IndexedActiveRuleDto::getRuleProfileUuid, IndexedActiveRuleDto::getSeverity) .containsExactlyInAnyOrder( tuple((long) ar2.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()), tuple((long) ar3.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity())); }