Example #1
0
  @Override
  public Test getFullTest(Long id) {
    Test test = testDAO.get(id);
    if (test == null) {
      return null;
    }
    test = test.clone();
    // TODO: return by named query, with optimized fetching
    Collection<Metric> metrics = test.getMetrics();
    if (metrics != null) {
      List<Metric> clonedMetrics = new ArrayList<Metric>();
      for (Metric metric : metrics) {
        clonedMetrics.add(metric);
      }
      test.setMetrics(clonedMetrics);
    }

    Collection<User> subscribers = test.getSubscribers();
    if (subscribers != null) {
      List<User> subscribersClone = new ArrayList<>();
      for (User subscriber : subscribers) {
        subscribersClone.add(subscriber.clone());
      }
      test.setSubscribers(subscribersClone);
    }

    Collection<Alert> alerts = test.getAlerts();
    if (alerts != null) {
      List<Alert> alertsClone = new ArrayList<>();
      for (Alert alert : alerts) {
        List<Tag> tagsClone = new ArrayList<>();
        for (Tag tag : alert.getTags()) {
          tagsClone.add(tag.clone());
        }
        Alert alertClone = alert.clone();
        alertClone.setTags(tagsClone);
        alertsClone.add(alertClone);
      }
      test.setAlerts(alertsClone);
    }

    return test;
  }
Example #2
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;
    }
  }