private int countTotalOfCustomMeasures(DbSession dbSession, ComponentDto project) { return dbClient.customMeasureDao().countByComponentUuid(dbSession, project.uuid()); }
@Override public void handle(Request request, Response response) throws Exception { long id = request.mandatoryParamAsLong(PARAM_ID); try (DbSession dbSession = dbClient.openSession(false)) { CustomMeasureDto customMeasure = dbClient.customMeasureDao().selectById(dbSession, id); checkArgument(customMeasure != null, "Custom measure with id '%s' does not exist", id); checkPermission(dbSession, customMeasure); dbClient.customMeasureDao().delete(dbSession, id); dbSession.commit(); } response.noContent(); }
private List<CustomMeasureDto> searchCustomMeasures(DbSession dbSession, ComponentDto project, SearchOptions searchOptions) { return dbClient.customMeasureDao().selectByComponentUuid(dbSession, project.uuid(), searchOptions.getOffset(), searchOptions.getLimit()); }
private void checkMeasureDoesNotExistAlready(DbSession dbSession, ComponentDto component, MetricDto metric) { int nbMeasuresOnSameMetricAndMeasure = dbClient.customMeasureDao().countByComponentIdAndMetricId(dbSession, component.uuid(), metric.getId()); checkRequest(nbMeasuresOnSameMetricAndMeasure == 0, "A measure already exists for project '%s' and metric '%s'", component.getDbKey(), metric.getKey()); }
private void insertManualMeasureFor(ComponentDto... componentDtos) { Arrays.stream(componentDtos).forEach(componentDto -> dbClient.customMeasureDao().insert(dbSession, new CustomMeasureDto() .setComponentUuid(componentDto.uuid()) .setMetricId(new Random().nextInt()))); dbSession.commit(); }
private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) { checkArgument(!areOneOfTheMandatoryArgumentsEmpty(template), "The mandatory arguments '%s','%s' and '%s' must not be empty", PARAM_KEY, PARAM_NAME, PARAM_TYPE); if (metricIsNotInDb(metricInDb)) { return; } checkRequest(!isMetricEnabled(metricInDb), "An active metric already exist with key: " + metricInDb.getKey()); checkRequest(!isMetricNonCustom(metricInDb), "An non custom metric already exist with key: %s", metricInDb.getKey()); if (hasMetricTypeChanged(metricInDb, template)) { List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId()); checkRequest(!hasAssociatedCustomMeasures(customMeasures), "You're trying to change the type '%s' while there are associated measures.", metricInDb.getValueType()); } }
private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) { checkRequest(isMetricFoundInDb(metricInDb) && !isMetricDisabled(metricInDb) && isMetricCustom(metricInDb), "No active custom metric has been found for id '%d'.", template.getId()); checkNoOtherMetricWithTargetKey(dbSession, metricInDb, template); if (haveMetricTypeChanged(metricInDb, template)) { List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId()); checkRequest(!haveAssociatedCustomMeasures(customMeasures), "You're trying to change the type '%s' while there are associated custom measures.", metricInDb.getValueType()); } }
@SafeVarargs public final CustomMeasureDto insertCustomMeasure(UserDto user, ComponentDto component, MetricDto metricDto, Consumer<CustomMeasureDto>... consumers) { Preconditions.checkArgument(metricDto.isUserManaged(),"Custom measure must be created from a custom metric"); CustomMeasureDto dto = newCustomMeasureDto().setUserUuid(user.getUuid()).setComponentUuid(component.uuid()).setMetricId(metricDto.getId()); Arrays.stream(consumers).forEach(c -> c.accept(dto)); dbClient.customMeasureDao().insert(dbSession, dto); dbSession.commit(); return dto; }
@Test public void delete_associated_measures() { loggedAsSystemAdministrator(); MetricDto metric = insertCustomMetric("custom-key"); CustomMeasureDto customMeasure = newCustomMeasureDto().setMetricId(metric.getId()); CustomMeasureDto undeletedCustomMeasure = newCustomMeasureDto().setMetricId(metric.getId() + 1); dbClient.customMeasureDao().insert(db.getSession(), customMeasure); dbClient.customMeasureDao().insert(db.getSession(), undeletedCustomMeasure); db.getSession().commit(); newRequest().setParam("keys", "custom-key").execute(); assertThat(dbClient.customMeasureDao().selectById(db.getSession(), customMeasure.getId())).isNull(); assertThat(dbClient.customMeasureDao().selectById(db.getSession(), undeletedCustomMeasure.getId())).isNotNull(); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn().checkIsSystemAdministrator(); try (DbSession dbSession = dbClient.openSession(false)) { List<Integer> ids = loadIds(dbSession, request); dbClient.metricDao().disableCustomByIds(dbSession, ids); dbClient.customMeasureDao().deleteByMetricIds(dbSession, ids); dbClient.gateConditionDao().deleteConditionsWithInvalidMetrics(dbSession); dbSession.commit(); } response.noContent(); }
private long insertCustomMeasure(ComponentDto component) { CustomMeasureDto dto = newCustomMeasureDto().setComponentUuid(component.uuid()); dbClient.customMeasureDao().insert(dbSession, dto); dbSession.commit(); return dto.getId(); }
@Test public void project_administrator_can_delete_custom_measures() throws Exception { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); long id = insertCustomMeasure(project); newRequest().setParam(PARAM_ID, valueOf(id)).execute(); assertThat(dbClient.customMeasureDao().selectById(dbSession, id)).isNull(); }
@Test public void list_metrics_where_no_existing_custom_measure() throws Exception { MetricDto metric = insertCustomMetric("metric-key-1"); insertCustomMetric("metric-key-2"); insertProject("project-uuid-2", "project-key-2"); CustomMeasureDto customMeasure = newCustomMeasureDto() .setComponentUuid(defaultProject.uuid()) .setMetricId(metric.getId()); dbClient.customMeasureDao().insert(dbSession, customMeasure); dbSession.commit(); String response = newRequest().outputAsString(); assertThat(response).contains("metric-key-2") .doesNotContain("metric-key-1"); }
@Test public void fail_when_custom_measures_and_type_changed() throws Exception { expectedException.expect(ServerException.class); int id = insertMetric(newDefaultMetric()); dbClient.customMeasureDao().insert(dbSession, newCustomMeasureDto().setMetricId(id)); dbSession.commit(); newRequest() .setParam(PARAM_ID, String.valueOf(id)) .setParam(PARAM_TYPE, ValueType.BOOL.name()) .execute(); }
@Test public void fail_when_metric_key_is_not_well_formatted() throws Exception { int id = insertMetric(newDefaultMetric()); dbClient.customMeasureDao().insert(dbSession, newCustomMeasureDto().setMetricId(id)); dbSession.commit(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Malformed metric key 'not well formatted key'. Allowed characters are alphanumeric, '-', '_', with at least one non-digit."); newRequest() .setParam(PARAM_ID, String.valueOf(id)) .setParam(PARAM_KEY, "not well formatted key") .execute(); }
private void insertCustomMeasure(String componentUuid, Metric metric, double value) { dbTester.getDbClient().customMeasureDao().insert(dbTester.getSession(), CustomMeasureTesting.newCustomMeasureDto() .setComponentUuid(componentUuid) .setMetricId(metric.getId()) .setValue(value)); dbTester.getSession().commit(); }
private void insertCustomMeasure(String componentUuid, Metric metric, String value) { dbTester.getDbClient().customMeasureDao().insert(dbTester.getSession(), CustomMeasureTesting.newCustomMeasureDto() .setComponentUuid(componentUuid) .setMetricId(metric.getId()) .setTextValue(value)); dbTester.getSession().commit(); }
@Test public void create_text_custom_measure_with_project_key() { MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true).setValueType(STRING.name())); ComponentDto project = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(ADMIN, project); ws.newRequest() .setParam(CreateAction.PARAM_PROJECT_KEY, project.getKey()) .setParam(CreateAction.PARAM_METRIC_ID, metric.getId().toString()) .setParam(CreateAction.PARAM_VALUE, "whatever-value") .execute(); assertThat(db.getDbClient().customMeasureDao().selectByMetricId(db.getSession(), metric.getId())) .extracting(CustomMeasureDto::getDescription, CustomMeasureDto::getTextValue, CustomMeasureDto::getValue, CustomMeasureDto::getComponentUuid) .containsExactlyInAnyOrder(tuple(null, "whatever-value", 0d, project.uuid())); }
@Test public void update_description_only() { ComponentDto project = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(ADMIN, project); MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true).setValueType(STRING.name())); CustomMeasureDto customMeasure = db.measures().insertCustomMeasure(user, project, metric); ws.newRequest() .setParam(PARAM_ID, String.valueOf(customMeasure.getId())) .setParam(PARAM_DESCRIPTION, "new-custom-measure-description") .execute(); assertThat(db.getDbClient().customMeasureDao().selectByMetricId(db.getSession(), metric.getId())) .extracting(CustomMeasureDto::getDescription, CustomMeasureDto::getTextValue, CustomMeasureDto::getValue) .containsExactlyInAnyOrder( tuple("new-custom-measure-description", customMeasure.getTextValue(), customMeasure.getValue())); }
@Test public void create_level_type_custom_measure_in_db() { MetricDto metric = db.measures().insertMetric(m -> m.setUserManaged(true).setValueType(LEVEL.name())); ComponentDto project = db.components().insertPrivateProject(); UserDto user = db.users().insertUser(); userSession.logIn(user).addProjectPermission(ADMIN, project); ws.newRequest() .setParam(CreateAction.PARAM_PROJECT_ID, project.uuid()) .setParam(CreateAction.PARAM_METRIC_ID, metric.getId().toString()) .setParam(CreateAction.PARAM_VALUE, Metric.Level.ERROR.name()) .execute(); assertThat(db.getDbClient().customMeasureDao().selectByMetricId(db.getSession(), metric.getId())) .extracting(CustomMeasureDto::getDescription, CustomMeasureDto::getTextValue, CustomMeasureDto::getValue, CustomMeasureDto::getComponentUuid) .containsExactlyInAnyOrder(tuple(null, Metric.Level.ERROR.name(), 0d, project.uuid())); }