@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<Test> getAllFullTests() { List<Test> r = testDAO.getAll(); List<Test> rcopy = new ArrayList<Test>(r.size()); for (Test t : r) { rcopy.add(getFullTest(t.getId())); } return rcopy; }
@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 @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()); }