|
| 1 | +package com.provectus.kafka.ui.settings.listeners; |
| 2 | + |
| 3 | +import com.provectus.kafka.ui.utilities.qaseUtils.annotations.Automation; |
| 4 | +import com.provectus.kafka.ui.utilities.qaseUtils.annotations.Status; |
| 5 | +import com.provectus.kafka.ui.utilities.qaseUtils.annotations.Suite; |
| 6 | +import io.qase.api.QaseClient; |
| 7 | +import io.qase.api.StepStorage; |
| 8 | +import io.qase.api.annotation.QaseId; |
| 9 | +import io.qase.client.ApiClient; |
| 10 | +import io.qase.client.api.CasesApi; |
| 11 | +import io.qase.client.model.*; |
| 12 | +import lombok.SneakyThrows; |
| 13 | +import lombok.extern.slf4j.Slf4j; |
| 14 | +import org.testng.Assert; |
| 15 | +import org.testng.ITestListener; |
| 16 | +import org.testng.ITestResult; |
| 17 | +import org.testng.TestListenerAdapter; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.*; |
| 21 | + |
| 22 | +import static io.qase.api.utils.IntegrationUtils.getCaseTitle; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +public class QaseCreateListener extends TestListenerAdapter implements ITestListener { |
| 26 | + |
| 27 | + private static final CasesApi QASE_API = getQaseApi(); |
| 28 | + |
| 29 | + private static CasesApi getQaseApi() { |
| 30 | + ApiClient apiClient = QaseClient.getApiClient(); |
| 31 | + apiClient.setApiKey(System.getProperty("QASEIO_API_TOKEN")); |
| 32 | + return new CasesApi(apiClient); |
| 33 | + } |
| 34 | + |
| 35 | + private static int getStatus(Method method) { |
| 36 | + if (method.isAnnotationPresent(Status.class)) |
| 37 | + return method.getDeclaredAnnotation(Status.class).status().getValue(); |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + private static int getAutomation(Method method) { |
| 42 | + if (method.isAnnotationPresent(Automation.class)) |
| 43 | + return method.getDeclaredAnnotation(Automation.class).state().getValue(); |
| 44 | + return 0; |
| 45 | + } |
| 46 | + |
| 47 | + @SneakyThrows |
| 48 | + private static HashMap<Long, String> getCaseTitlesAndIdsFromQase() { |
| 49 | + HashMap<Long, String> cases = new HashMap<>(); |
| 50 | + boolean getCases = true; |
| 51 | + int offSet = 0; |
| 52 | + while (getCases) { |
| 53 | + getCases = false; |
| 54 | + TestCaseListResponse response = QASE_API.getCases(System.getProperty("QASE_PROJECT_CODE"), |
| 55 | + new GetCasesFiltersParameter().status(GetCasesFiltersParameter.SERIALIZED_NAME_STATUS), 100, offSet); |
| 56 | + TestCaseListResponseAllOfResult result = response.getResult(); |
| 57 | + Assert.assertNotNull(result); |
| 58 | + List<TestCase> entities = result.getEntities(); |
| 59 | + Assert.assertNotNull(entities); |
| 60 | + if (entities.size() > 0) { |
| 61 | + for (TestCase testCase : entities) { |
| 62 | + cases.put(testCase.getId(), testCase.getTitle()); |
| 63 | + } |
| 64 | + offSet = offSet + 100; |
| 65 | + getCases = true; |
| 66 | + } |
| 67 | + } |
| 68 | + return cases; |
| 69 | + } |
| 70 | + |
| 71 | + private static boolean isCaseWithTitleExistInQase(Method method) { |
| 72 | + HashMap<Long, String> cases = getCaseTitlesAndIdsFromQase(); |
| 73 | + String title = getCaseTitle(method); |
| 74 | + if (cases.containsValue(title)) { |
| 75 | + for (Map.Entry<Long, String> map : cases.entrySet()) { |
| 76 | + if (map.getValue().matches(title)) { |
| 77 | + long id = map.getKey(); |
| 78 | + log.warn(String.format("Test case with @QaseTitle='%s' already exists with @QaseId=%d. " + |
| 79 | + "Please verify @QaseTitle annotation", title, id)); |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + @SneakyThrows |
| 89 | + public void onTestSuccess(final ITestResult testResult) { |
| 90 | + Method method = testResult.getMethod() |
| 91 | + .getConstructorOrMethod() |
| 92 | + .getMethod(); |
| 93 | + String title = getCaseTitle(method); |
| 94 | + if (!method.isAnnotationPresent(QaseId.class)) { |
| 95 | + if (title != null) { |
| 96 | + if (!isCaseWithTitleExistInQase(method)) { |
| 97 | + LinkedList<ResultCreateStepsInner> resultSteps = StepStorage.stopSteps(); |
| 98 | + LinkedList<TestCaseCreateStepsInner> createSteps = new LinkedList<>(); |
| 99 | + resultSteps.forEach(step -> { |
| 100 | + TestCaseCreateStepsInner caseStep = new TestCaseCreateStepsInner(); |
| 101 | + caseStep.setAction(step.getAction()); |
| 102 | + caseStep.setExpectedResult(step.getExpectedResult()); |
| 103 | + createSteps.add(caseStep); |
| 104 | + }); |
| 105 | + TestCaseCreate newCase = new TestCaseCreate(); |
| 106 | + newCase.setTitle(title); |
| 107 | + newCase.setStatus(getStatus(method)); |
| 108 | + newCase.setAutomation(getAutomation(method)); |
| 109 | + newCase.setSteps(createSteps); |
| 110 | + if (method.isAnnotationPresent(Suite.class)) { |
| 111 | + long suiteId = method.getDeclaredAnnotation(Suite.class).id(); |
| 112 | + newCase.suiteId(suiteId); |
| 113 | + } |
| 114 | + Long id = Objects.requireNonNull(QASE_API.createCase(System.getProperty("QASE_PROJECT_CODE"), |
| 115 | + newCase).getResult()).getId(); |
| 116 | + log.info(String.format("New test case '%s' was created with @QaseId=%d", title, id)); |
| 117 | + } |
| 118 | + } else |
| 119 | + log.warn("To create new test case in Qase.io please add @QaseTitle annotation"); |
| 120 | + } else |
| 121 | + log.warn("To create new test case in Qase.io please remove @QaseId annotation"); |
| 122 | + } |
| 123 | +} |
0 commit comments