Skip to content

Commit

Permalink
Feature/4 attachments (#9)
Browse files Browse the repository at this point in the history
* Update version to 1.0.0

* Fix Sonar issues

* Add possibility to override all config values

* Implement attachments

* Fix Sonar issue

* Headers refactoring
  • Loading branch information
paveliam authored May 27, 2020
1 parent 54bb7e3 commit e75290e
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 66 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ The repository contains adaptors for JVM-based test frameworks.

## How to use

1. Add [aqualityTracking.json](./aquality-tracking-integrations-core/src/main/resources/aqualityTracking.json) file with corresponding values to `src/test/resources`.
2. Define dependencies according to the selected test framework (see below).
3. Run tests with the following parameters:
1. Define dependencies according to the selected test framework (see below).
2. Add [aqualityTracking.json](./aquality-tracking-integrations-core/src/main/resources/aqualityTracking.json) file with corresponding values to `src/test/resources`.

You are able to override these values from CI build using Maven properties:

```bash
mvn clean test -Daquality.enabled={true/false} -Daquality.suiteName={test_suite_name} -Daquality.buildName={build_name} -Daquality.environment={execution_env} -Daquality.ciBuild={link_to_ci_build} -Daquality.debug={true/false}
mvn clean test -Daquality.enabled={true/false}
-Daquality.host={aquality_tracking_address}
-Daquality.token={api_token}
-Daquality.projectId={project_id}
-Daquality.executor={name_of_executor}
-Daquality.suiteName={test_suite_name}
-Daquality.buildName={build_name}
-Daquality.environment={execution_env}
-Daquality.ciBuild={link_to_ci_build}
-Daquality.debug={true/false}
-Daquality.attachmentsDirectory={path_to_directory_with_attachments}
```

Mandatory parameters: `aquality.suiteName`, `aquality.buildName`.

## Cucumber 5 [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.aquality-automation/aquality-tracking-cucumber5-jvm/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.aquality-automation/aquality-tracking-cucumber5-jvm)

To use this adaptor with Cucumber 5 you have to add the following dependency:
Expand Down
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>0.1.3</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<parent>
<artifactId>aquality-tracking-integrations</artifactId>
<groupId>com.github.aquality-automation</groupId>
<version>0.1.3</version>
<version>1.0.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>0.1.3</version>
<version>1.0.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@
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 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 InheritableThreadLocal<>();
private final ThreadLocal<TestResult> currentTestResult = new InheritableThreadLocal<>();
private final ThreadLocal<Test> currentTest = new ThreadLocal<>();
private final ThreadLocal<TestResult> currentTestResult = new ThreadLocal<>();

private final Configuration configuration;
private final ISuiteEndpoints suiteEndpoints;
Expand All @@ -49,14 +54,26 @@ public void setEventPublisher(final EventPublisher eventPublisher) {

eventPublisher.registerHandlerFor(TestCaseStarted.class, this::handleTestCaseStartedEvent);
eventPublisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinishedEvent);

eventPublisher.registerHandlerFor(EmbedEvent.class, this::handleEmbedEvent);
}
}

private void handleTestRunStartedEvent(final TestRunStarted event) {
currentSuite = suiteEndpoints.createSuite(configuration.getSuiteName());
currentTestRun = testRunEndpoints.startTestRun(currentSuite.getId(), configuration.getBuildName(),
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;
}

private void handleTestRunFinishedEvent(final TestRunFinished event) {
Expand All @@ -79,4 +96,11 @@ private void handleTestCaseFinishedEvent(final TestCaseFinished event) {
currentTest.remove();
currentTestResult.remove();
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static aquality.tracking.integrations.core.utilities.FileReader.getFileSource;
import static aquality.tracking.integrations.core.utilities.FileUtils.getFileSource;
import static java.lang.String.format;

class TestCaseNameParser {
Expand Down
22 changes: 19 additions & 3 deletions aquality-tracking-integrations-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@
<modelVersion>4.0.0</modelVersion>

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

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

<name>Aquality Tracking integrations core</name>
<description>Aquality Tracking integration core for JVM-based test frameworks.</description>
<url>https://github.com/aquality-automation/aquality-tracking-integrations-java</url>

<properties>
<httpcomponents.version>4.5.12</httpcomponents.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
<version>${httpcomponents.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpcomponents.version}</version>
</dependency>

<dependency>
Expand All @@ -36,6 +46,12 @@
<artifactId>guice</artifactId>
<version>4.2.3</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package aquality.tracking.integrations.core;

import org.apache.http.*;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -20,29 +23,41 @@

public class AqualityHttpClient implements IHttpClient {

@Override
public String sendGET(final URI uri, final List<Header> headers) {
HttpGet request = new HttpGet(uri);
addHeaders(request, headers);
return sendRequest(request);
}

@Override
public String sendPOST(final URI uri, final List<Header> headers, final String body) {
HttpPost request = new HttpPost(uri);
addHeaders(request, headers);
try {
request.setEntity(new StringEntity(body));
return sendPOST(uri, headers, new StringEntity(body));
} catch (UnsupportedEncodingException e) {
throw new AqualityUncheckedException(format("Exception occurred during set request body:%n%s", body), e);
}
}

@Override
public String sendPOST(final URI uri, final List<Header> headers, final HttpEntity entity) {
HttpPost request = new HttpPost(uri);
addHeaders(request, headers);
request.setEntity(entity);
return sendRequest(request);
}

private String sendRequest(final HttpUriRequest httpRequest) {
final String response;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse httpResponse = httpClient.execute(httpRequest)) {
processStatusLine(httpResponse.getStatusLine());
response = processEntity(httpResponse.getEntity());

int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new AqualityUncheckedException(format("Status code: %d. Reason: %s.%nRequest URI: %s",
statusCode, response, httpRequest.getURI()));
}
}
} catch (IOException e) {
throw new AqualityUncheckedException(format("Exception occurred during sending request to %s",
Expand All @@ -55,13 +70,6 @@ private void addHeaders(HttpRequest request, List<Header> headers) {
headers.forEach(request::addHeader);
}

private void processStatusLine(StatusLine statusLine) {
int statusCode = statusLine.getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new AqualityUncheckedException(format("Status code: %d", statusCode));
}
}

private String processEntity(HttpEntity httpEntity) {
try {
return EntityUtils.toString(httpEntity, StandardCharsets.UTF_8.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,38 @@

@Data
public class Configuration {
private boolean enabled;
private String host;
private String token;
private int projectId;
private String executor;

private boolean enabled;
private String suiteName;
private String buildName;
private String environment;
private String ciBuild;
private boolean debug;
private String attachmentsDirectory;

public boolean isEnabled() {
return getValueOrDefault("aquality.enabled", Boolean.class, enabled);
}

public String getHost() {
return getValueOrDefault("aquality.host", String.class, host);
}

public String getToken() {
return getValueOrDefault("aquality.token", String.class, token);
}

public int getProjectId() {
return getValueOrDefault("aquality.projectId", Integer.class, projectId);
}

public String getExecutor() {
return getValueOrDefault("aquality.executor", String.class, executor);
}

public String getSuiteName() {
return getValueOrDefault("aquality.suiteName", String.class, suiteName);
}
Expand All @@ -41,4 +57,8 @@ public String getCiBuild() {
public boolean isDebug() {
return getValueOrDefault("aquality.debug", Boolean.class, debug);
}

public String getAttachmentsDirectory() {
return getValueOrDefault("aquality.attachmentsDirectory", String.class, attachmentsDirectory);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aquality.tracking.integrations.core;

import org.apache.http.Header;
import org.apache.http.HttpEntity;

import java.net.URI;
import java.util.List;
Expand All @@ -23,4 +24,13 @@ public interface IHttpClient {
* @return Response as string.
*/
String sendPOST(final URI uri, final List<Header> headers, final String body);

/**
* Sends POST request to specified uri.
* @param uri Request URI.
* @param headers Request headers.
* @param entity Request entity.
* @return Response as string.
*/
String sendPOST(final URI uri, final List<Header> headers, final HttpEntity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import aquality.tracking.integrations.core.models.TestResult;

import java.io.File;

public interface ITestResultEndpoints {

/**
Expand All @@ -14,10 +16,17 @@ public interface ITestResultEndpoints {

/**
* Sets Test Result.
* @param testResultId Id of Test Run to finish.
* @param testResultId Id of Test Result to finish.
* @param finalResultId Id of the result: {@link aquality.tracking.integrations.core.FinalResultId}
* @param failReason Reason of test failure. If not needed - pass null.
* @return Instance of finished Test Result.
*/
TestResult finishTestResult(int testResultId, int finalResultId, final String failReason);

/**
* Add attachment to Test Result.
* @param testResultId Id of Test Result.
* @param file Attachment file.
*/
void addAttachment(int testResultId, final File file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.String.format;
import static org.apache.http.entity.ContentType.APPLICATION_JSON;

public abstract class AqualityTrackingEndpoints {

Expand All @@ -35,11 +34,9 @@ protected AqualityTrackingEndpoints(Configuration configuration, IHttpClient htt
this.httpClient = httpClient;
}

protected List<Header> getHeaders() {
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader(HttpHeaders.ACCEPT, "application/json"));
headers.add(getBasicAuthHeader());
return headers;
protected Headers getDefaultHeaders() {
Headers defaultHeaders = new Headers(getBasicAuthHeader());
return defaultHeaders.add(HttpHeaders.ACCEPT, APPLICATION_JSON);
}

private Header getBasicAuthHeader() {
Expand Down
Loading

0 comments on commit e75290e

Please sign in to comment.