|
| 1 | +package de.tum.cit.aet.helios.tests; |
| 2 | + |
| 3 | +import de.tum.cit.aet.helios.branch.Branch; |
| 4 | +import de.tum.cit.aet.helios.branch.BranchRepository; |
| 5 | +import de.tum.cit.aet.helios.gitrepo.GitRepository; |
| 6 | +import jakarta.transaction.Transactional; |
| 7 | +import java.time.OffsetDateTime; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.log4j.Log4j2; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | + |
| 14 | +/** Service for managing test case statistics and detecting flaky tests. */ |
| 15 | +@Service |
| 16 | +@Log4j2 |
| 17 | +@RequiredArgsConstructor |
| 18 | +public class TestCaseStatisticsService { |
| 19 | + |
| 20 | + private final TestCaseStatisticsRepository statisticsRepository; |
| 21 | + private final BranchRepository branchRepository; |
| 22 | + |
| 23 | + /** |
| 24 | + * Updates statistics for a test case, creating a new entry if it doesn't exist. |
| 25 | + * |
| 26 | + * @param testName the name of the test |
| 27 | + * @param className the class name of the test |
| 28 | + * @param testSuiteName the test suite name |
| 29 | + * @param branchName the branch name |
| 30 | + * @param hasFailed whether the test failed in this run |
| 31 | + * @return the updated statistics |
| 32 | + */ |
| 33 | + @Transactional |
| 34 | + public TestCaseStatistics updateStatistics( |
| 35 | + String testName, |
| 36 | + String className, |
| 37 | + String testSuiteName, |
| 38 | + String branchName, |
| 39 | + boolean hasFailed) { |
| 40 | + Optional<TestCaseStatistics> existingStats = |
| 41 | + statisticsRepository.findByTestNameAndClassNameAndTestSuiteNameAndBranchName( |
| 42 | + testName, className, testSuiteName, branchName); |
| 43 | + |
| 44 | + TestCaseStatistics statistics; |
| 45 | + if (existingStats.isPresent()) { |
| 46 | + statistics = existingStats.get(); |
| 47 | + } else { |
| 48 | + statistics = new TestCaseStatistics(); |
| 49 | + statistics.setTestName(testName); |
| 50 | + statistics.setClassName(className); |
| 51 | + statistics.setTestSuiteName(testSuiteName); |
| 52 | + statistics.setBranchName(branchName); |
| 53 | + statistics.setTotalRuns(0); |
| 54 | + statistics.setFailedRuns(0); |
| 55 | + statistics.setFailureRate(0.0); |
| 56 | + statistics.setFlaky(false); |
| 57 | + statistics.setLastUpdated(OffsetDateTime.now()); |
| 58 | + } |
| 59 | + |
| 60 | + statistics.addRun(hasFailed); |
| 61 | + return statisticsRepository.save(statistics); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Updates statistics for multiple test cases from a test suite. |
| 66 | + * |
| 67 | + * @param testSuite the test suite containing test cases |
| 68 | + * @param branchName the branch name |
| 69 | + */ |
| 70 | + @Transactional |
| 71 | + public void updateStatisticsForTestSuite(TestSuite testSuite, String branchName) { |
| 72 | + String testSuiteName = testSuite.getName(); |
| 73 | + |
| 74 | + for (TestCase testCase : testSuite.getTestCases()) { |
| 75 | + boolean hasFailed = |
| 76 | + testCase.getStatus() == TestCase.TestStatus.FAILED |
| 77 | + || testCase.getStatus() == TestCase.TestStatus.ERROR; |
| 78 | + |
| 79 | + updateStatistics( |
| 80 | + testCase.getName(), testCase.getClassName(), testSuiteName, branchName, hasFailed); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets statistics for a specific test case on a specific branch. |
| 86 | + * |
| 87 | + * @param testName the name of the test |
| 88 | + * @param className the class name of the test |
| 89 | + * @param testSuiteName the test suite name |
| 90 | + * @param branchName the branch name |
| 91 | + * @return the statistics if found |
| 92 | + */ |
| 93 | + public Optional<TestCaseStatistics> getStatistics( |
| 94 | + String testName, String className, String testSuiteName, String branchName) { |
| 95 | + return statisticsRepository.findByTestNameAndClassNameAndTestSuiteNameAndBranchName( |
| 96 | + testName, className, testSuiteName, branchName); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets all statistics for a specific branch. |
| 101 | + * |
| 102 | + * @param branchName the branch name |
| 103 | + * @return list of statistics for all tests on the branch |
| 104 | + */ |
| 105 | + public List<TestCaseStatistics> getStatisticsForBranch(String branchName) { |
| 106 | + return statisticsRepository.findByBranchName(branchName); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets flaky or non-flaky tests for a specific branch. |
| 111 | + * |
| 112 | + * @param branchName the branch name |
| 113 | + * @param isFlaky whether to find flaky or non-flaky tests |
| 114 | + * @return list of flaky or non-flaky test statistics |
| 115 | + */ |
| 116 | + public List<TestCaseStatistics> getTestsByFlakinessForBranch(String branchName, boolean isFlaky) { |
| 117 | + return statisticsRepository.findByBranchNameAndIsFlaky(branchName, isFlaky); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Gets flaky or non-flaky tests for the default branch of a repository. |
| 122 | + * |
| 123 | + * @param repository the repository |
| 124 | + * @param isFlaky whether to find flaky or non-flaky tests |
| 125 | + * @return list of flaky or non-flaky test statistics for the default branch |
| 126 | + */ |
| 127 | + public List<TestCaseStatistics> getTestsByFlakinessForDefaultBranch( |
| 128 | + GitRepository repository, boolean isFlaky) { |
| 129 | + Optional<Branch> defaultBranch = |
| 130 | + branchRepository.findAll().stream() |
| 131 | + .filter(branch -> branch.getRepository().equals(repository) && branch.isDefault()) |
| 132 | + .findFirst(); |
| 133 | + |
| 134 | + if (defaultBranch.isPresent()) { |
| 135 | + return getTestsByFlakinessForBranch(defaultBranch.get().getName(), isFlaky); |
| 136 | + } else { |
| 137 | + log.warn("No default branch found for repository {}", repository.getNameWithOwner()); |
| 138 | + return List.of(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Gets all statistics for the default branch of a repository. |
| 144 | + * |
| 145 | + * @param repository the repository |
| 146 | + * @return list of statistics for all tests on the default branch |
| 147 | + */ |
| 148 | + public List<TestCaseStatistics> getStatisticsForDefaultBranch(GitRepository repository) { |
| 149 | + Optional<Branch> defaultBranch = |
| 150 | + branchRepository.findAll().stream() |
| 151 | + .filter(branch -> branch.getRepository().equals(repository) && branch.isDefault()) |
| 152 | + .findFirst(); |
| 153 | + |
| 154 | + if (defaultBranch.isPresent()) { |
| 155 | + return getStatisticsForBranch(defaultBranch.get().getName()); |
| 156 | + } else { |
| 157 | + log.warn("No default branch found for repository {}", repository.getNameWithOwner()); |
| 158 | + return List.of(); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments