@Override public void handle(Request request, Response response) throws Exception { QProfileReference reference = QProfileReference.from(request); try (DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = wsSupport.getProfile(dbSession, reference); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); Date since = parseStartingDateOrDateTime(request.param(PARAM_SINCE)); if (since != null) { query.setFromIncluded(since.getTime()); } Date to = parseEndingDateOrDateTime(request.param(PARAM_TO)); if (to != null) { query.setToExcluded(to.getTime()); } int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); query.setPage(page, pageSize); int total = dbClient.qProfileChangeDao().countByQuery(dbSession, query); List<Change> changelogs = load(dbSession, query); Map<String, UserDto> usersByUuid = getUsersByUserUuid(dbSession, changelogs); Map<Integer, RuleDefinitionDto> rulesByRuleIds = getRulesByRuleIds(dbSession, changelogs); writeResponse(response.newJsonWriter(), total, page, pageSize, changelogs, usersByUuid, rulesByRuleIds); } }
@Test public void selectByQuery_supports_pagination_of_changes() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change4 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); query.setOffset(2); query.setLimit(1); List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, query); assertThat(changes) .extracting(QProfileChangeDto::getUuid) .containsExactly(change2.getUuid()); }
@Test public void countByQuery() { QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization()); long start = system2.now(); insertChange(profile1, "ACTIVATED", null, null); insertChange(profile1, "ACTIVATED", null, null); insertChange(profile2, "ACTIVATED", null, null); long end = system2.now(); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(2); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile2.getKee()))).isEqualTo(1); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery("does_not_exist"))).isEqualTo(0); QProfileChangeQuery query = new QProfileChangeQuery(profile1.getKee()); query.setToExcluded(start); assertThat(underTest.countByQuery(dbSession, query)).isEqualTo(0); QProfileChangeQuery query2 = new QProfileChangeQuery(profile1.getKee()); query2.setToExcluded(end); assertThat(underTest.countByQuery(dbSession, query2)).isEqualTo(2); }
@Test public void selectByQuery_returns_changes_in_a_range_of_dates() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change4 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); query.setFromIncluded(change1.getCreatedAt() + 1); query.setToExcluded(change4.getCreatedAt()); assertThat(underTest.selectByQuery(dbSession, query)) .extracting(QProfileChangeDto::getUuid) .containsExactly(change3.getUuid(), change2.getUuid()); }
@Test public void selectByQuery_returns_empty_list_if_profile_does_not_exist() { List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, new QProfileChangeQuery("P1")); assertThat(changes).isEmpty(); }
@Test public void selectByQuery_returns_changes_after_given_date() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); query.setFromIncluded(change1.getCreatedAt() + 1); assertThat(underTest.selectByQuery(dbSession, query)) .extracting(QProfileChangeDto::getUuid) .containsExactly(change3.getUuid(), change2.getUuid()); }
private void verifyActiveRuleInDb(QProfileDto profile, RuleDefinitionDto rule, String expectedSeverity) { ActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByKey(dbSession, ActiveRuleKey.of(profile, rule.getKey())).get(); assertThat(activeRule.getId()).isPositive(); assertThat(activeRule.getInheritance()).isNull(); assertThat(activeRule.doesOverride()).isFalse(); assertThat(activeRule.getRuleId()).isEqualTo(rule.getId()); assertThat(activeRule.getProfileId()).isEqualTo(profile.getId()); assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity); assertThat(activeRule.getCreatedAt()).isPositive(); assertThat(activeRule.getUpdatedAt()).isPositive(); List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(params).isEmpty(); QProfileChangeQuery changeQuery = new QProfileChangeQuery(profile.getKee()); QProfileChangeDto change = db.getDbClient().qProfileChangeDao().selectByQuery(dbSession, changeQuery).stream() .filter(c -> c.getDataAsMap().get("ruleId").equals(String.valueOf(rule.getId()))) .findFirst() .get(); assertThat(change.getChangeType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED.name()); assertThat(change.getCreatedAt()).isPositive(); assertThat(change.getUuid()).isNotEmpty(); assertThat(change.getUserUuid()).isNull(); assertThat(change.getRulesProfileUuid()).isEqualTo(profile.getRulesProfileUuid()); assertThat(change.getDataAsMap().get("severity")).isEqualTo(expectedSeverity); }
@Test public void selectByQuery_returns_changes_before_given_date() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change2 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeDto change3 = insertChange(profile, "ACTIVATED", null, null); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); query.setToExcluded(change2.getCreatedAt() + 1); assertThat(underTest.selectByQuery(dbSession, query)) .extracting(QProfileChangeDto::getUuid) .containsExactly(change2.getUuid(), change1.getUuid()); }
@Override public void handle(Request request, Response response) throws Exception { QProfileReference reference = QProfileReference.from(request); try (DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = wsSupport.getProfile(dbSession, reference); QProfileChangeQuery query = new QProfileChangeQuery(profile.getKee()); Date since = parseStartingDateOrDateTime(request.param(PARAM_SINCE)); if (since != null) { query.setFromIncluded(since.getTime()); } Date to = parseEndingDateOrDateTime(request.param(PARAM_TO)); if (to != null) { query.setToExcluded(to.getTime()); } int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); query.setPage(page, pageSize); int total = dbClient.qProfileChangeDao().countByQuery(dbSession, query); List<Change> changelogs = load(dbSession, query); Map<String, UserDto> usersByUuid = getUsersByUserUuid(dbSession, changelogs); Map<Integer, RuleDefinitionDto> rulesByRuleIds = getRulesByRuleIds(dbSession, changelogs); writeResponse(response.newJsonWriter(), total, page, pageSize, changelogs, usersByUuid, rulesByRuleIds); } }
@Test public void deleteByProfileKeys_does_nothing_if_row_with_specified_key_does_not_exist() { QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization()); insertChange(profile1.getRulesProfileUuid(), "ACTIVATED", null, null); underTest.deleteByRulesProfileUuids(dbSession, asList("does not exist")); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(1); }
@Test public void deleteByRulesProfileUuids() { QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization()); insertChange(profile1, "ACTIVATED", null, null); insertChange(profile1, "ACTIVATED", null, null); insertChange(profile2, "ACTIVATED", null, null); underTest.deleteByRulesProfileUuids(dbSession, asList(profile1.getRulesProfileUuid())); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile1.getKee()))).isEqualTo(0); assertThat(underTest.countByQuery(dbSession, new QProfileChangeQuery(profile2.getKee()))).isEqualTo(1); }
@Test public void test_selectByQuery_mapping() { QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto inserted = insertChange(profile, "ACTIVATED", "theLogin", "theData"); List<QProfileChangeDto> result = underTest.selectByQuery(dbSession, new QProfileChangeQuery(profile.getKee())); assertThat(result).hasSize(1); QProfileChangeDto change = result.get(0); assertThat(change.getRulesProfileUuid()).isEqualTo(inserted.getRulesProfileUuid()); assertThat(change.getUserUuid()).isEqualTo(inserted.getUserUuid()); assertThat(change.getData()).isEqualTo(inserted.getData()); assertThat(change.getChangeType()).isEqualTo(inserted.getChangeType()); assertThat(change.getUuid()).isEqualTo(inserted.getUuid()); assertThat(change.getCreatedAt()).isEqualTo(inserted.getCreatedAt()); }
@Test public void active_rule_change() { UserDto user = db.users().insertUser(); userSession.logIn(user); OrganizationDto organization = db.organizations().insert(); QProfileDto qProfile = db.qualityProfiles().insert(organization); RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage())); RuleActivation ruleActivation = RuleActivation.create(rule.getId(), Severity.CRITICAL, Collections.emptyMap()); qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation)); assertThat(db.getDbClient().qProfileChangeDao().selectByQuery(db.getSession(), new QProfileChangeQuery(qProfile.getKee()))) .extracting(QProfileChangeDto::getUserUuid, QProfileChangeDto::getDataAsMap) .containsExactlyInAnyOrder(tuple(user.getUuid(), ImmutableMap.of("ruleId", Integer.toString(rule.getId()), "severity", Severity.CRITICAL))); } }
@Test public void selectByQuery_returns_changes_ordered_by_descending_date() { QProfileDto profile1 = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileDto profile2 = db.qualityProfiles().insert(db.getDefaultOrganization()); QProfileChangeDto change1OnP1 = insertChange(profile1, "ACTIVATED", null, null); QProfileChangeDto change2OnP1 = insertChange(profile1, "ACTIVATED", null, null); QProfileChangeDto changeOnP2 = insertChange(profile2, "ACTIVATED", null, null); List<QProfileChangeDto> changes = underTest.selectByQuery(dbSession, new QProfileChangeQuery(profile1.getKee())); assertThat(changes) .extracting(QProfileChangeDto::getUuid) .containsExactly(change2OnP1.getUuid(), change1OnP1.getUuid()); }