public MetricRepositoryRule add(Metric metric) {
    requireNonNull(metric.getKey(), "key can not be null");
    requireNonNull(metric.getId(), "id can not be null");

    checkState(
        !metricsByKey.containsKey(metric.getKey()),
        format("Repository already contains a metric for key %s", metric.getKey()));
    checkState(
        !metricsById.containsKey((long) metric.getId()),
        format("Repository already contains a metric for id %s", metric.getId()));

    metricsByKey.put(metric.getKey(), metric);
    metricsById.put((long) metric.getId(), metric);

    return this;
  }
示例#2
0
  @Override
  // @Secured TODO: we need to handle this property, since getTestByRelation will not work as metric
  // is associated with more tests
  public void removeMetric(Metric metric, Test test) throws ServiceException {
    Metric freshMetric = metricDAO.get(metric.getId());
    Test freshTest = testDAO.get(test.getId());

    // List<Test> newTests = freshMetric.getTests().stream().filter(o ->
    // !o.equals(freshTest)).collect(Collectors.toList());
    // freshMetric.setTests(newTests);

    // List<Metric> newMetrics = freshTest.getMetrics().stream().filter(o ->
    // !o.equals(freshMetric)).collect(Collectors.toList());
    // freshTest.setMetrics(newMetrics);

    freshMetric.getTests().remove(freshTest);
    freshTest.getMetrics().remove(freshMetric);

    metricDAO.update(freshMetric);
    testDAO.update(freshTest);

    if (freshMetric.getTests() == null || freshMetric.getTests().isEmpty()) {
      metricDAO.remove(freshMetric);
    }
  }
示例#3
0
  // --------------------------------------------------------------------------
  public DataPoint setMetricRef(Metric table) {
    // We add the record to the transaction if one of the key values change
    if (m_metricId.setValue(table.getId())) {
      if ((m_dirtyFlags.isEmpty()) && (GenOrmDataSource.getGenOrmConnection() != null))
        GenOrmDataSource.getGenOrmConnection().addToTransaction(this);

      m_dirtyFlags.set(METRIC_ID_FIELD_META.getDirtyFlag());
    }

    return ((DataPoint) this);
  }
示例#4
0
 public void setMetric(Metric metric) {
   if (metric == null) {
     throw new DaoException(
         "To-one property 'metric_id' has not-null constraint; cannot set to-one to null");
   }
   synchronized (this) {
     this.metric = metric;
     metric_id = metric.getId();
     metric__resolvedKey = metric_id;
   }
 }
示例#5
0
 @Override
 @Secured
 public Value addValue(Value value) throws ServiceException {
   TestExecution exec = testExecutionDAO.get(value.getTestExecution().getId());
   if (exec == null) {
     throw new ServiceException(
         "serviceException.addValue.testExecutionNotFound", value.getTestExecution().getName());
   }
   Metric metric = null;
   if (value.getMetric().getId() != null) {
     metric = metricDAO.get(value.getMetric().getId());
   } else {
     List<Metric> metrics =
         metricDAO.getMetricByNameAndGroup(
             value.getMetric().getName(), exec.getTest().getGroupId());
     if (metrics.size() > 0) {
       metric = metricDAO.get(metrics.get(0).getId());
     }
   }
   if (metric == null) {
     throw new ServiceException("serviceException.metricNotFound", value.getMetric().getName());
   }
   value.setTestExecution(exec);
   value.setMetric(metric);
   // check if other values for given metric exist, if yes, we can only add one if both old and new
   // one have at least one parameter
   List<Value> existingValuesForMetric = valueDAO.find(exec.getId(), metric.getId());
   if (!existingValuesForMetric.isEmpty()) {
     for (Value v : existingValuesForMetric) {
       if (!v.hasParameters()) {
         throw new ServiceException("serviceException.unparametrizedMultiValue");
       }
     }
     if (!value.hasParameters()) {
       throw new ServiceException("serviceException.unparametrizedMultiValue");
     }
   }
   Value freshValue = valueDAO.create(value);
   Value freshValueClone = freshValue.clone();
   List<ValueParameter> newParams = new ArrayList<ValueParameter>();
   if (value.hasParameters()) {
     for (ValueParameter valueParameter : value.getParameters()) {
       valueParameter.setValue(freshValue);
       newParams.add(valueParameterDAO.create(valueParameter).clone());
       newParams.get(newParams.size() - 1).setValue(freshValueClone);
     }
   }
   freshValueClone.setParameters(newParams.isEmpty() ? null : newParams);
   return freshValueClone;
 }
示例#6
0
  @Override
  @Secured
  public Metric addMetric(Test test, Metric metric) throws ServiceException {
    Test freshTest = testDAO.get(test.getId());
    if (freshTest.getMetrics() == null) {
      freshTest.setMetrics(new ArrayList<>());
    }

    if (metric.getId() != null) {
      // associating an existing metric with the test
      Metric freshMetric = metricDAO.get(metric.getId());
      if (freshMetric == null) {
        throw new ServiceException("serviceException.metricNotFound", metric.getName().toString());
      }

      if (freshMetric.getTests() == null) {
        freshMetric.setTests(new ArrayList<>());
      }

      for (Test testForMetric : freshMetric.getTests()) {
        if (!testForMetric.getGroupId().equals(freshTest.getGroupId())) {
          throw new ServiceException("serviceException.metricSharingOnlyInGroup");
        }
        if (testForMetric.getId().equals(freshTest.getId())) {
          throw new ServiceException(
              "serviceException.metricAlreadyExists", freshTest.getUid(), freshMetric.getName());
        }
      }

      freshMetric.getTests().add(freshTest);
      freshTest.getMetrics().add(freshMetric);

      freshMetric = metricDAO.update(freshMetric);
      testDAO.update(freshTest);

      return freshMetric;
    } else {
      // creating a new metric object
      if (metric.getName() == null) {
        throw new IllegalArgumentException("Metric name is mandatory");
      }
      // metric name needs to be unique in the metric space of a certain groupId
      // does it exist in a test with same group id (including the target test) ?
      List<Metric> existingMetricsForGroup =
          metricDAO.getMetricByNameAndGroup(metric.getName(), freshTest.getGroupId());
      for (Metric existingMetric : existingMetricsForGroup) {
        if (existingMetric.getName().equals(metric.getName())) {
          Metric freshMetric = metricDAO.get(existingMetric.getId());

          if (freshMetric.getTests().stream().anyMatch(t -> t.getId().equals(freshTest.getId()))) {
            throw new ServiceException(
                "serviceException.metricAlreadyExists", freshTest.getUid(), freshMetric.getName());
          }
        }
      }

      metric.setTests(Arrays.asList(freshTest));
      Metric freshMetric = metricDAO.create(metric);

      freshTest.getMetrics().add(freshMetric);
      testDAO.update(freshTest);

      return freshMetric;
    }
  }