Ejemplo n.º 1
0
 private void checkNoOtherMetricWithTargetKey(
     DbSession dbSession, MetricDto metricInDb, MetricDto template) {
   String targetKey = template.getKey();
   MetricDto metricWithTargetKey = dbClient.metricDao().selectByKey(dbSession, targetKey);
   if (isMetricFoundInDb(metricWithTargetKey)
       && !metricInDb.getId().equals(metricWithTargetKey.getId())) {
     throw new BadRequestException(
         String.format("The key '%s' is already used by an existing metric.", targetKey));
   }
 }
Ejemplo n.º 2
0
  @Test
  public void fail_if_not_logged_in() throws Exception {
    userSessionRule.anonymous();
    expectedException.expect(UnauthorizedException.class);
    MetricDto metric =
        MetricTesting.newMetricDto().setEnabled(true).setValueType(ValueType.STRING.name());
    dbClient.metricDao().insert(dbSession, metric);
    ComponentDto component = ComponentTesting.newProjectDto("project-uuid");
    dbClient.componentDao().insert(dbSession, component);
    CustomMeasureDto customMeasure =
        newCustomMeasureDto()
            .setMetricId(metric.getId())
            .setComponentUuid(component.uuid())
            .setCreatedAt(system.now())
            .setDescription("custom-measure-description")
            .setTextValue("text-measure-value");
    dbClient.customMeasureDao().insert(dbSession, customMeasure);
    dbSession.commit();

    ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION)
        .setParam(PARAM_ID, String.valueOf(customMeasure.getId()))
        .setParam(PARAM_DESCRIPTION, "new-custom-measure-description")
        .setParam(PARAM_VALUE, "1984")
        .execute();
  }
Ejemplo n.º 3
0
  @Test
  public void update_description_only() throws Exception {
    MetricDto metric = insertNewMetric(ValueType.STRING);
    ComponentDto component = insertNewProject("project-uuid");
    CustomMeasureDto customMeasure =
        newCustomMeasure(component, metric)
            .setMetricId(metric.getId())
            .setComponentUuid(component.uuid())
            .setCreatedAt(system.now())
            .setDescription("custom-measure-description")
            .setTextValue("text-measure-value");
    dbClient.customMeasureDao().insert(dbSession, customMeasure);
    dbSession.commit();
    when(system.now()).thenReturn(123_456_789L);

    ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION)
        .setParam(PARAM_ID, String.valueOf(customMeasure.getId()))
        .setParam(PARAM_VALUE, "new-text-measure-value")
        .execute();

    CustomMeasureDto updatedCustomMeasure =
        dbClient.customMeasureDao().selectOrFail(dbSession, customMeasure.getId());
    assertThat(updatedCustomMeasure.getTextValue()).isEqualTo("new-text-measure-value");
    assertThat(updatedCustomMeasure.getDescription()).isEqualTo("custom-measure-description");
    assertThat(updatedCustomMeasure.getUpdatedAt()).isEqualTo(123_456_789L);
    assertThat(customMeasure.getCreatedAt()).isEqualTo(updatedCustomMeasure.getCreatedAt());
  }
Ejemplo n.º 4
0
  @Test
  public void returns_full_object_in_response() throws Exception {
    MetricDto metric =
        MetricTesting.newMetricDto()
            .setEnabled(true)
            .setValueType(ValueType.STRING.name())
            .setKey("metric-key");
    dbClient.metricDao().insert(dbSession, metric);
    ComponentDto component = ComponentTesting.newProjectDto("project-uuid").setKey("project-key");
    dbClient.componentDao().insert(dbSession, component);
    CustomMeasureDto customMeasure =
        newCustomMeasure(component, metric)
            .setCreatedAt(100_000_000L)
            .setDescription("custom-measure-description")
            .setTextValue("text-measure-value");
    dbClient.customMeasureDao().insert(dbSession, customMeasure);
    dbSession.commit();
    when(system.now()).thenReturn(123_456_789L);

    WsTester.Result response =
        ws.newPostRequest(CustomMeasuresWs.ENDPOINT, UpdateAction.ACTION)
            .setParam(PARAM_ID, String.valueOf(customMeasure.getId()))
            .setParam(PARAM_DESCRIPTION, "new-custom-measure-description")
            .setParam(PARAM_VALUE, "new-text-measure-value")
            .execute();

    response.assertJson(getClass(), "custom-measure.json");
    String responseAsString = response.outputAsString();
    assertThat(responseAsString)
        .matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", customMeasure.getId()));
    assertThat(responseAsString)
        .matches(String.format(".*\"id\"\\s*:\\s*\"%s\".*", metric.getId()));
    assertThat(responseAsString).matches(".*createdAt.*updatedAt.*");
  }
  public QualityGateConditionDto createCondition(
      DbSession dbSession,
      long qGateId,
      String metricKey,
      String operator,
      @Nullable String warningThreshold,
      @Nullable String errorThreshold,
      @Nullable Integer period) {
    getNonNullQgate(dbSession, qGateId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(
        getConditions(dbSession, qGateId, null), metric, period);

    QualityGateConditionDto newCondition =
        new QualityGateConditionDto()
            .setQualityGateId(qGateId)
            .setMetricId(metric.getId())
            .setMetricKey(metric.getKey())
            .setOperator(operator)
            .setWarningThreshold(warningThreshold)
            .setErrorThreshold(errorThreshold)
            .setPeriod(period);
    dbClient.gateConditionDao().insert(newCondition, dbSession);
    return newCondition;
  }
Ejemplo n.º 6
0
 private static void writeMetric(JsonWriter json, MetricDto metric) {
   json.beginObject();
   json.prop(FIELD_ID, String.valueOf(metric.getId()));
   json.prop(FIELD_KEY, metric.getKey());
   json.prop(FIELD_TYPE, metric.getValueType());
   json.prop(FIELD_NAME, metric.getShortName());
   json.prop(FIELD_DOMAIN, metric.getDomain());
   json.prop(FIELD_DESCRIPTION, metric.getDescription());
   json.endObject();
 }
Ejemplo n.º 7
0
 private void checkMetricInDbAndTemplate(
     DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) {
   if (!isMetricFoundInDb(metricInDb)
       || isMetricDisabled(metricInDb)
       || !isMetricCustom(metricInDb)) {
     throw new BadRequestException(
         String.format("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());
     if (haveAssociatedCustomMeasures(customMeasures)) {
       throw new BadRequestException(
           String.format(
               "You're trying to change the type '%s' while there are associated custom measures.",
               metricInDb.getValueType()));
     }
   }
 }
Ejemplo n.º 8
0
 @Override
 public Metric apply(@Nonnull MetricDto dto) {
   Metric<Serializable> metric = new Metric<>();
   metric.setId(dto.getId());
   metric.setKey(dto.getKey());
   metric.setDescription(dto.getDescription());
   metric.setName(dto.getShortName());
   metric.setBestValue(dto.getBestValue());
   metric.setDomain(dto.getDomain());
   metric.setEnabled(dto.isEnabled());
   metric.setDirection(dto.getDirection());
   metric.setHidden(dto.isHidden());
   metric.setQualitative(dto.isQualitative());
   metric.setType(Metric.ValueType.valueOf(dto.getValueType()));
   metric.setOptimizedBestValue(dto.isOptimizedBestValue());
   metric.setUserManaged(dto.isUserManaged());
   metric.setWorstValue(dto.getWorstValue());
   return metric;
 }
  public QualityGateConditionDto updateCondition(
      DbSession dbSession,
      long condId,
      String metricKey,
      String operator,
      @Nullable String warningThreshold,
      @Nullable String errorThreshold,
      @Nullable Integer period) {
    QualityGateConditionDto condition = getNonNullCondition(dbSession, condId);
    MetricDto metric = getNonNullMetric(dbSession, metricKey);
    validateCondition(metric, operator, warningThreshold, errorThreshold, period);
    checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(
        getConditions(dbSession, condition.getQualityGateId(), condition.getId()), metric, period);

    condition
        .setMetricId(metric.getId())
        .setMetricKey(metric.getKey())
        .setOperator(operator)
        .setWarningThreshold(warningThreshold)
        .setErrorThreshold(errorThreshold)
        .setPeriod(period);
    dbClient.gateConditionDao().update(condition, dbSession);
    return condition;
  }
  private static void checkConditionDoesNotAlreadyExistOnSameMetricAndPeriod(
      Collection<QualityGateConditionDto> conditions,
      MetricDto metric,
      @Nullable final Integer period) {
    if (conditions.isEmpty()) {
      return;
    }

    boolean conditionExists =
        conditions
            .stream()
            .anyMatch(
                c ->
                    c.getMetricId() == metric.getId() && ObjectUtils.equals(c.getPeriod(), period));
    if (conditionExists) {
      String errorMessage =
          period == null
              ? format("Condition on metric '%s' already exists.", metric.getShortName())
              : format(
                  "Condition on metric '%s' over leak period already exists.",
                  metric.getShortName());
      throw new BadRequestException(errorMessage);
    }
  }
Ejemplo n.º 11
0
 private CustomMeasureDto newCustomMeasure(ComponentDto project, MetricDto metric) {
   return newCustomMeasureDto()
       .setMetricId(metric.getId())
       .setComponentUuid(project.uuid())
       .setCreatedAt(system.now());
 }