@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);
    }
  }
  @Override
  public List<Metric> getAllMetrics(Long testId) {
    Test freshTest = testDAO.get(testId);
    if (freshTest.getMetrics() == null) {
      return new ArrayList<>();
    }

    List<Metric> metrics = freshTest.getMetrics().stream().collect(Collectors.toList());
    return metrics;
  }
  @Override
  public boolean isUserSubscribed(User user, Test test) {
    Test freshTest = testDAO.get(test.getId());
    Collection<User> testSubscribers = freshTest.getSubscribers();
    for (User testSubscriber : testSubscribers) {
      if (testSubscriber.getId().equals(user.getId())) {
        return true;
      }
    }

    return false;
  }
  @Override
  public void addSubscriber(User user, Test test) {
    Test freshTest = testDAO.get(test.getId());
    User freshUser = userDAO.get(user.getId());

    Collection<User> testSubscribers = freshTest.getSubscribers();
    if (testSubscribers.contains(freshUser)) {
      return;
    }

    testSubscribers.add(freshUser);
    testDAO.update(freshTest);
  }
  @Override
  public void removeSubscriber(User user, Test test) {
    Test freshTest = testDAO.get(test.getId());

    Collection<User> testSubscribers = new ArrayList<>(freshTest.getSubscribers());
    for (User testSubscriber : freshTest.getSubscribers()) {
      if (testSubscriber.getId().equals(user.getId())) {
        testSubscribers.remove(testSubscriber);
      }
    }

    test.setSubscribers(testSubscribers);
    testDAO.update(test);
  }
  @Override
  @Secured
  public void removeTest(Test test) throws ServiceException {
    Test freshTest = testDAO.get(test.getId());
    try {
      for (TestExecution testExecution : freshTest.getTestExecutions()) {
        removeTestExecution(testExecution);
      }
    } catch (ServiceException ex) {
      throw new ServiceException("serviceException.removeTest.cannotRemoveAllTestExecutions", ex);
    }

    testDAO.remove(freshTest);
  }
  @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;
  }
 @Override
 public Test getTest(Long id) {
   return testDAO.get(id);
 }
  @Override
  @Secured
  public TestExecution createTestExecution(TestExecution testExecution) throws ServiceException {
    // The test referred by test execution has to be an existing test
    Test test = testDAO.get(testExecution.getTest().getId());
    testExecution.setTest(test);
    TestExecution storedTestExecution = testExecutionDAO.create(testExecution);
    // execution params
    if (testExecution.getParameters() != null && testExecution.getParameters().size() > 0) {
      for (TestExecutionParameter param : testExecution.getParameters()) {
        param.setTestExecution(storedTestExecution);
        testExecutionParameterDAO.create(param);
      }
    }
    // tags
    if (testExecution.getTags() != null && testExecution.getTags().size() > 0) {
      for (Tag teg : testExecution.getTags()) {
        Tag tag = tagDAO.findByName(teg.getName());
        if (tag == null) {
          tag = tagDAO.create(teg);
        }

        Collection<TestExecution> tagTestExecutions = tag.getTestExecutions();
        if (tagTestExecutions == null) {
          tag.setTestExecutions(new ArrayList<>());
        }

        tag.getTestExecutions().add(storedTestExecution);
      }
    }
    // values
    if (testExecution.getValues() != null && !testExecution.getValues().isEmpty()) {
      for (Value value : testExecution.getValues()) {
        value.setTestExecution(storedTestExecution);
        if (value.getMetricName() == null) {
          throw new IllegalArgumentException("Metric name is mandatory");
        }
        Metric metric =
            test.getMetrics()
                .stream()
                .filter(m -> m.getName().equals(value.getMetricName()))
                .findFirst()
                .get();
        if (metric == null) {
          throw new ServiceException(
              "serviceException.metricNotInTest",
              test.getName(),
              test.getId().toString(),
              value.getMetricName());
        }
        value.setMetric(metric);
        valueDAO.create(value);
        if (value.getParameters() != null && value.getParameters().size() > 0) {
          for (ValueParameter vp : value.getParameters()) {
            vp.setValue(value);
            valueParameterDAO.create(vp);
          }
        }
      }
    }

    TestExecution clone = cloneAndFetch(storedTestExecution, true, true, true, true, true);
    log.debug("Created new test execution " + clone.getId());

    alertingService.processAlerts(clone);

    return clone;
  }
 @Override
 public List<Metric> getTestMetrics(Test test) {
   Test t = testDAO.get(test.getId());
   return t.getSortedMetrics();
 }
  @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;
    }
  }
 @Override
 public List<Metric> getAvailableMetrics(Test test) {
   Test t = testDAO.get(test.getId());
   return EntityUtils.removeAllById(metricDAO.getMetricByGroup(t.getGroupId()), t.getMetrics());
 }