Skip to content

Commit

Permalink
Added listener before stop test case (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry.Gridnev <[email protected]>
  • Loading branch information
gibiw and Dmitry.Gridnev authored Dec 2, 2022
1 parent 4fe5016 commit 5f93ed3
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 52 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=1.1.5
version=1.1.6

org.gradle.daemon=true
org.gradle.parallel=true
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-cucumber4/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Tags

Use tags to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-cucumber5/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Tags

Use tags to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-cucumber6/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Tags

Use tags to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-cucumber7/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Tags

Use tags to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-jbehave/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Meta

Use tags to specify information about autotest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void afterScenario() {
final ExecutableTest test = executableTest.get();
final String uuid = test.getUuid();

if (exampleUuids.isEmpty()) {
if (exampleUuids.isEmpty() && !test.isAfter()) {
adapterManager.stopTestCase(uuid);
} else {
for (String exampleUuid : exampleUuids) {
Expand All @@ -136,6 +136,7 @@ public void afterScenario() {

exampleUuids.clear();
}

adapterManager.stopClassContainer(classUUID.get());
adapterManager.stopMainContainer(launcherUUID.get());
executableTest.remove();
Expand Down Expand Up @@ -185,5 +186,12 @@ public void failed(final String step, final Throwable cause) {
}
}
);

adapterManager.stopTestCase(uuid);
executableTest.get().setAfterStatus();

if (exampleUuids.contains(uuid)) {
exampleUuids.remove(uuid);
}
}
}
2 changes: 2 additions & 0 deletions testit-adapter-junit4/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Annotations

Use annotations to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-junit5/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Annotations

Use annotations to specify information about autotest.
Expand Down
2 changes: 2 additions & 0 deletions testit-adapter-testng/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ maven test -DtmsUrl=http://localhost:8080 -DtmsPrivateToken=Token -DtmsProjectId

If you want to enable debug mode then see [How to enable debug logging?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

If you want to add attachment for a failed test then see [How to add an attachment for a failed test?](https://github.com/testit-tms/adapters-java/tree/main/testit-java-commons)

### Annotations

Use annotations to specify information about autotest.
Expand Down
36 changes: 35 additions & 1 deletion testit-java-commons/Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
# How to enable debug logging?
Create **simplelogger.properties** file in the resource directory of the project:
```
```text
org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd'T'HH:mm:ss.SSSZ
```

# How to add an attachment for a failed test?
You need to implement the AdapterListener interface and override the beforeTestStop method.
For example:

```java
import ru.testit.services.Adapter;
import ru.testit.listener.AdapterListener;
import ru.testit.models.ItemStatus;
import ru.testit.models.TestResult;
import ru.testit.services.Adapter;

public class AttachmentManager implements AdapterListener {

@Override
public void beforeTestStop(final TestResult result) {
if (result.getItemStatus().equals(ItemStatus.FAILED)) {
// Add a screenshot
Adapter.addAttachments("Screenshot.jpg", new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)));

// Add log file
Adapter.addAttachments("/logs/failed.log");

// Add any text
Adapter.addAttachments("any text", "file.txt");
}
}
}
```

After that, you need to add file **ru.testit.listener.AdapterListener** to **resources/META-INT/services** folder:
```text
<your-package>.AttachmentManager
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.testit.listener;

import ru.testit.models.TestResult;

public interface AdapterListener extends DefaultListener {
default void beforeTestStop(final TestResult result) {
//do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.testit.listener;

public interface DefaultListener {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.testit.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.testit.models.TestResult;

import java.util.List;
import java.util.function.BiConsumer;

public class ListenerManager {
private static final Logger LOGGER = LoggerFactory.getLogger(ListenerManager.class);
private List<AdapterListener> listeners;

public ListenerManager(final List<AdapterListener> listeners) {

this.listeners = listeners;
}

public void beforeTestStop(final TestResult result) {
runSafelyMethod(listeners, AdapterListener::beforeTestStop, result);
}

protected <T extends DefaultListener, S> void runSafelyMethod(final List<T> listeners,
final BiConsumer<T, S> method,
final S object) {
listeners.forEach(listener -> {
try {
method.accept(listener, object);
} catch (Exception e) {
LOGGER.error("Could not invoke listener method", e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.testit.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

public final class ServiceLoaderListener {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceLoaderListener.class);

private ServiceLoaderListener() {
throw new IllegalStateException("Do not have instance");
}

public static <T> List<T> load(final Class<T> type, final ClassLoader classLoader) {
final List<T> loaded = new ArrayList<>();
final Iterator<T> iterator = ServiceLoader.load(type, classLoader).iterator();
while (nextSafely(iterator)) {
try {
final T next = iterator.next();
loaded.add(next);
LOGGER.debug("Found type {}", type);
} catch (Exception e) {
LOGGER.error("Could not load listener {}: {}", type, e);
}
}
return loaded;
}

private static boolean nextSafely(final Iterator iterator) {
try {
return iterator.hasNext();
} catch (Exception e) {
LOGGER.error("nextSafely failed", e);
return false;
}
}
}
24 changes: 24 additions & 0 deletions testit-java-commons/src/main/java/ru/testit/services/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;

public final class Adapter {
Expand Down Expand Up @@ -112,6 +114,28 @@ public static void addAttachments(String content, String fileName) {
}
}

public static void addAttachments(String fileName, InputStream inputStream) {
if (fileName == null) {
LOGGER.error("Attachment name is empty");
return;
}

Path path = Paths.get(fileName);
try {
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
LOGGER.error(String.format("Can not write file '%s':", fileName), e);
}

addAttachments(fileName);

try {
Files.deleteIfExists(path);
} catch (IOException e) {
LOGGER.error(String.format("Can not delete file '%s':", fileName), e);
}
}

/**
* @deprecated This method is no longer acceptable to compute time between versions.
* <p> Use {@link Adapter#addAttachments(String attachment)} instead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import ru.testit.clients.ApiClient;
import ru.testit.clients.ClientConfiguration;
import ru.testit.clients.TmsApiClient;
import ru.testit.listener.AdapterListener;
import ru.testit.listener.ListenerManager;
import ru.testit.listener.ServiceLoaderListener;
import ru.testit.models.*;
import ru.testit.properties.AdapterConfig;
import ru.testit.properties.AdapterMode;
Expand All @@ -32,7 +35,13 @@ public class AdapterManager {
private final ClientConfiguration clientConfiguration;
private final AdapterConfig adapterConfig;

private final ListenerManager listenerManager;

public AdapterManager(ClientConfiguration clientConfiguration, AdapterConfig adapterConfig) {
this(clientConfiguration, adapterConfig, getDefaultListenerManager());
}

public AdapterManager(ClientConfiguration clientConfiguration, AdapterConfig adapterConfig, ListenerManager listenerManager) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Client configurations: {}", clientConfiguration);
LOGGER.debug("Adapter configurations: {}", adapterConfig);
Expand All @@ -45,6 +54,7 @@ public AdapterManager(ClientConfiguration clientConfiguration, AdapterConfig ada
this.threadContext = new ThreadContext();
this.client = new TmsApiClient(this.clientConfiguration);
this.writer = new HttpWriter(this.clientConfiguration, this.client, this.storage);
this.listenerManager = listenerManager;
}

public AdapterManager(
Expand All @@ -53,14 +63,16 @@ public AdapterManager(
ThreadContext threadContext,
ResultStorage storage,
Writer writer,
ApiClient client
ApiClient client,
ListenerManager listenerManager
) {
this.adapterConfig = adapterConfig;
this.clientConfiguration = clientConfiguration;
this.threadContext = threadContext;
this.storage = storage;
this.writer = writer;
this.client = client;
this.listenerManager = listenerManager;
}

public void startTests() {
Expand Down Expand Up @@ -285,6 +297,8 @@ public void stopTestCase(final String uuid) {
}
final TestResult testResult = found.get();

listenerManager.beforeTestStop(testResult);

testResult.setItemStage(ItemStage.FINISHED)
.setStop(System.currentTimeMillis());

Expand Down Expand Up @@ -666,4 +680,9 @@ private void validateAdapterConfig() {
throw new RuntimeException(error);
}
}

private static ListenerManager getDefaultListenerManager() {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return new ListenerManager(ServiceLoaderListener.load(AdapterListener.class, classLoader));
}
}
Loading

0 comments on commit 5f93ed3

Please sign in to comment.