@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 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 @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 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 public SearchResultWrapper<Test> getAvailableTests() { TestSearchTO search = new TestSearchTO(); search.setGroupFilter(GroupFilter.MY_GROUPS); List<String> groups = userService.getLoggedUserGroupNames(); return testDAO.searchTests(search, groups); }
@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 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 List<String> getTestsByPrefix(String prefix) { List<Test> tests = testDAO.findByUIDPrefix(prefix); List<String> testuids = new ArrayList<String>(); for (Test test : tests) { if (userService.isLoggedUserInGroup(test.getGroupId())) { testuids.add(test.getUid()); } } return testuids; }
@Override public Test createTest(Test test) throws ServiceException { if (!userService.isLoggedUserInGroup(test.getGroupId())) { throw new org.perfrepo.web.security.SecurityException( "securityException.userNotInGroup.createTest", userService.getLoggedUser().getUsername(), test.getGroupId()); } if (testDAO.findByUid(test.getUid()) != null) { throw new ServiceException("serviceException.testUidExists", test.getUid()); } Test createdTest = testDAO.create(test); // store metrics if (test.getMetrics() != null) { for (Metric metric : test.getMetrics()) { addMetric(test, metric); } } return createdTest; }
@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 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 SearchResultWrapper<Test> searchTest(TestSearchTO search) { return testDAO.searchTests(search, userService.getLoggedUserGroupNames()); }
@Secured @Override public Test updateTest(Test test) { return testDAO.update(test); }
@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 Test getTest(Long id) { return testDAO.get(id); }
@Override public List<Metric> getAvailableMetrics(Test test) { Test t = testDAO.get(test.getId()); return EntityUtils.removeAllById(metricDAO.getMetricByGroup(t.getGroupId()), t.getMetrics()); }
@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> getTestMetrics(Test test) { Test t = testDAO.get(test.getId()); return t.getSortedMetrics(); }
@Override public List<Test> getAllTests() { return testDAO.getAll(); }
@Override public Test getTestByUID(String uid) { return testDAO.findByUid(uid); }