Skip to content

Commit

Permalink
Feature/lifecycle (#10)
Browse files Browse the repository at this point in the history
* Refactor http client

* Implement UriBuilder

* Add lifecycle class

* Generify HttpClient

* Increase version

* Fix bug with cnfig values overriding

* Fix javadoc
  • Loading branch information
paveliam authored Jun 1, 2020
1 parent e75290e commit 2ac3314
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 376 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ To use this adaptor with Cucumber 5 you have to add the following dependency:
</dependency>
```

Also you have to add plugin `aquality.tracking.integrations.cucumber5jvm.AqualityTrackingCucumber5Jvm` to the Cucumber Test Runner.
Also you have to add plugin `aquality.tracking.integrations.cucumber5jvm.AqualityTrackingCucumber5Jvm` to the Cucumber Test Runner.

#### How to increase version for all modules

```bash
mvn versions:set -DnewVersion={new_version}
```
6 changes: 3 additions & 3 deletions aquality-tracking-cucumber5-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>aquality-tracking-cucumber5-jvm</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<parent>
<artifactId>aquality-tracking-integrations</artifactId>
<groupId>com.github.aquality-automation</groupId>
<version>1.0.0</version>
<version>1.1.0</version>
</parent>

<name>Aquality Tracking Cucumber 5 JVM</name>
Expand All @@ -28,7 +28,7 @@
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-tracking-integrations-core</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
package aquality.tracking.integrations.cucumber5jvm;

import aquality.tracking.integrations.core.Configuration;
import aquality.tracking.integrations.core.ServicesModule;
import aquality.tracking.integrations.core.endpoints.ISuiteEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestResultEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestRunEndpoints;
import aquality.tracking.integrations.core.models.Suite;
import aquality.tracking.integrations.core.models.Test;
import aquality.tracking.integrations.core.models.TestResult;
import aquality.tracking.integrations.core.models.TestRun;
import aquality.tracking.integrations.core.utilities.FileUtils;
import com.google.inject.Guice;
import com.google.inject.Injector;
import aquality.tracking.integrations.core.AqualityTrackingLifecycle;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.*;

import java.io.File;
import java.nio.file.Paths;
import java.util.Collections;

import static java.lang.String.format;

public class AqualityTrackingCucumber5Jvm implements ConcurrentEventListener {

private static final Injector INJECTOR = Guice.createInjector(new ServicesModule());

private static TestRun currentTestRun;
private static Suite currentSuite;

private final ThreadLocal<Test> currentTest = new ThreadLocal<>();
private final ThreadLocal<TestResult> currentTestResult = new ThreadLocal<>();

private final Configuration configuration;
private final ISuiteEndpoints suiteEndpoints;
private final ITestRunEndpoints testRunEndpoints;
private final ITestEndpoints testEndpoints;
private final ITestResultEndpoints testResultEndpoints;
private final AqualityTrackingLifecycle lifecycle;

public AqualityTrackingCucumber5Jvm() {
configuration = INJECTOR.getInstance(Configuration.class);
suiteEndpoints = INJECTOR.getInstance(ISuiteEndpoints.class);
testRunEndpoints = INJECTOR.getInstance(ITestRunEndpoints.class);
testEndpoints = INJECTOR.getInstance(ITestEndpoints.class);
testResultEndpoints = INJECTOR.getInstance(ITestResultEndpoints.class);
lifecycle = new AqualityTrackingLifecycle();
}

@Override
public void setEventPublisher(final EventPublisher eventPublisher) {
if (configuration.isEnabled()) {
if (lifecycle.isEnabled()) {
eventPublisher.registerHandlerFor(TestRunStarted.class, this::handleTestRunStartedEvent);
eventPublisher.registerHandlerFor(TestRunFinished.class, this::handleTestRunFinishedEvent);

Expand All @@ -60,47 +28,26 @@ public void setEventPublisher(final EventPublisher eventPublisher) {
}

private void handleTestRunStartedEvent(final TestRunStarted event) {
Suite suite = suiteEndpoints.createSuite(configuration.getSuiteName());
TestRun testRun = testRunEndpoints.startTestRun(suite.getId(), configuration.getBuildName(),
configuration.getEnvironment(), configuration.getExecutor(),
configuration.getCiBuild(), configuration.isDebug());
setCurrentSuite(suite);
setCurrentTestRun(testRun);
}

private static void setCurrentSuite(final Suite suite) {
currentSuite = suite;
}

private static void setCurrentTestRun(final TestRun testRun) {
currentTestRun = testRun;
lifecycle.startTestRun();
}

private void handleTestRunFinishedEvent(final TestRunFinished event) {
testRunEndpoints.finishTestRun(currentTestRun.getId());
lifecycle.finishTestRun();
}

private void handleTestCaseStartedEvent(final TestCaseStarted event) {
String testName = new TestCaseNameParser(event.getTestCase()).getScenarioName();
Test test = testEndpoints.createOrUpdateTest(testName, Collections.singletonList(currentSuite));
currentTest.set(test);
TestResult testResult = testResultEndpoints.startTestResult(currentTestRun.getId(), currentTest.get().getId());
currentTestResult.set(testResult);
lifecycle.startTestExecution(testName);
}

private void handleTestCaseFinishedEvent(final TestCaseFinished event) {
TestCaseResultParser testCaseResultParser = new TestCaseResultParser(event.getResult());
TestCaseResultParser.TestCaseResult testCaseResult = testCaseResultParser.parse();
testResultEndpoints.finishTestResult(currentTestResult.get().getId(),
testCaseResult.getFinalResultId(), testCaseResult.getFailReason());
currentTest.remove();
currentTestResult.remove();
lifecycle.finishTestExecution(testCaseResult.getFinalResultId(), testCaseResult.getFailReason());
}

private void handleEmbedEvent(final EmbedEvent event) {
String filePath = Paths.get(configuration.getAttachmentsDirectory(),
format("%s_%s", event.getTestCase().getId(), event.getName())).toString();
File attachmentFile = FileUtils.writeToFile(filePath, event.getData());
testResultEndpoints.addAttachment(currentTestResult.get().getId(), attachmentFile);
String fileName = format("%s_%s", event.getTestCase().getId(), event.getName());
lifecycle.addAttachment(fileName, event.getData());
}
}
4 changes: 2 additions & 2 deletions aquality-tracking-integrations-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>aquality-tracking-integrations-core</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<parent>
<artifactId>aquality-tracking-integrations</artifactId>
<groupId>com.github.aquality-automation</groupId>
<version>1.0.0</version>
<version>1.1.0</version>
</parent>

<name>Aquality Tracking integrations core</name>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aquality.tracking.integrations.core;

import aquality.tracking.integrations.core.configuration.IConfiguration;
import aquality.tracking.integrations.core.endpoints.ISuiteEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestResultEndpoints;
import aquality.tracking.integrations.core.endpoints.ITestRunEndpoints;
import aquality.tracking.integrations.core.models.Suite;
import aquality.tracking.integrations.core.models.Test;
import aquality.tracking.integrations.core.models.TestResult;
import aquality.tracking.integrations.core.models.TestRun;
import aquality.tracking.integrations.core.utilities.FileUtils;
import com.google.inject.Guice;
import com.google.inject.Injector;

import java.io.File;
import java.nio.file.Paths;
import java.util.Collections;

public class AqualityTrackingLifecycle {

private static final Injector INJECTOR = Guice.createInjector(new ServicesModule());

private static TestRun currentTestRun;
private static Suite currentSuite;

private final ThreadLocal<Test> currentTest = new ThreadLocal<>();
private final ThreadLocal<TestResult> currentTestResult = new ThreadLocal<>();

private final IConfiguration configuration;
private final ISuiteEndpoints suiteEndpoints;
private final ITestRunEndpoints testRunEndpoints;
private final ITestEndpoints testEndpoints;
private final ITestResultEndpoints testResultEndpoints;

public AqualityTrackingLifecycle() {
configuration = INJECTOR.getInstance(IConfiguration.class);
suiteEndpoints = INJECTOR.getInstance(ISuiteEndpoints.class);
testRunEndpoints = INJECTOR.getInstance(ITestRunEndpoints.class);
testEndpoints = INJECTOR.getInstance(ITestEndpoints.class);
testResultEndpoints = INJECTOR.getInstance(ITestResultEndpoints.class);
}

public boolean isEnabled() {
return configuration.isEnabled();
}

public void startTestRun() {
Suite suite = suiteEndpoints.createSuite(configuration.getSuiteName());
TestRun testRun = testRunEndpoints.startTestRun(suite.getId(), configuration.getBuildName(),
configuration.getEnvironment(), configuration.getExecutor(),
configuration.getCiBuild(), configuration.isDebug());
setCurrentSuite(suite);
setCurrentTestRun(testRun);
}

private static void setCurrentSuite(final Suite suite) {
currentSuite = suite;
}

private static void setCurrentTestRun(final TestRun testRun) {
currentTestRun = testRun;
}

public void startTestExecution(final String testName) {
Test test = testEndpoints.createOrUpdateTest(testName, Collections.singletonList(currentSuite));
currentTest.set(test);
TestResult testResult = testResultEndpoints.startTestResult(currentTestRun.getId(), currentTest.get().getId());
currentTestResult.set(testResult);
}

public void addAttachment(final String fileName, final byte[] attachmentData) {
String filePath = Paths.get(configuration.getAttachmentsDirectory(), fileName).toString();
File attachmentFile = FileUtils.writeToFile(filePath, attachmentData);
testResultEndpoints.addAttachment(currentTestResult.get().getId(), attachmentFile);
}

public void finishTestExecution(final int finalResultId, final String failReason) {
testResultEndpoints.finishTestResult(currentTestResult.get().getId(), finalResultId, failReason);
currentTest.remove();
currentTestResult.remove();
}

public void finishTestRun() {
testRunEndpoints.finishTestRun(currentTestRun.getId());
}
}

This file was deleted.

Loading

0 comments on commit 2ac3314

Please sign in to comment.