Skip to content

Commit

Permalink
Merge pull request #323 from TikhomirovSergey/0_20_25_ALPHA
Browse files Browse the repository at this point in the history
0.20.25-ALPHA
  • Loading branch information
TikhomirovSergey authored Mar 9, 2022
2 parents c5c7dd5 + 344f29f commit 7aeb611
Show file tree
Hide file tree
Showing 131 changed files with 2,078 additions and 1,728 deletions.
6 changes: 6 additions & 0 deletions allure.integration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ publishing {
}
}

test {
useTestNG() {
suites 'src/test/resources/suite.xml'
}
}

javadoc {
destinationDir = file("${buildDir}/../../../neptune.documentation/javadocs/$project.name")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.UUID;

import static io.qameta.allure.Allure.addAttachment;
import static io.qameta.allure.Allure.getLifecycle;
Expand All @@ -20,6 +19,7 @@
import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.UUID.randomUUID;
import static java.util.stream.Collectors.toList;
import static ru.tinkoff.qa.neptune.core.api.utils.ToArrayUtil.stringValueOfObjectOrArray;

Expand All @@ -31,7 +31,7 @@ public class AllureEventLogger implements EventLogger {

@Override
public void fireTheEventStarting(String message, Map<String, String> parameters) {
var uuid = UUID.randomUUID().toString();
var uuid = randomUUID().toString();
var result = new StepResult().setName(message)
.setParameters(parameters
.entrySet()
Expand All @@ -41,26 +41,26 @@ public void fireTheEventStarting(String message, Map<String, String> parameters)
.setValue(e.getValue()))
.collect(toList()));

if (stepUIIDs.size() == 0) {
allureLifecycle.startStep(uuid, result);
if (getStepUIIDs().isEmpty()) {
getAllureLifecycle().startStep(uuid, result);
} else {
allureLifecycle.startStep(stepUIIDs.getLast(), uuid, result);
getAllureLifecycle().startStep(getStepUIIDs().getLast(), uuid, result);
}
stepUIIDs.addLast(uuid);
results.put(uuid, null);
getStepUIIDs().addLast(uuid);
getResults().put(uuid, null);
}

@Override
public void fireThrownException(Throwable throwable) {
if (stepUIIDs.size() == 0) {
if (getStepUIIDs().isEmpty()) {
return;
}

var uuid = stepUIIDs.getLast();
allureLifecycle.updateStep(uuid, s -> s
var uuid = getStepUIIDs().getLast();
getAllureLifecycle().updateStep(uuid, s -> s
.setStatus(getStatus(throwable).orElse(BROKEN))
.setStatusDetails(getStatusDetails(throwable).orElse(null)));
results.put(uuid, getStatus(throwable).orElse(BROKEN));
getResults().put(uuid, getStatus(throwable).orElse(BROKEN));

var bos = new ByteArrayOutputStream();
var ps = new PrintStream(bos, true, UTF_8);
Expand All @@ -71,43 +71,43 @@ public void fireThrownException(Throwable throwable) {

@Override
public void fireReturnedValue(String resultDescription, Object returned) {
if (stepUIIDs.size() == 0) {
if (getStepUIIDs().isEmpty()) {
return;
}

var uuid = stepUIIDs.getLast();
allureLifecycle.updateStep(uuid, s -> s.setStatus(PASSED)
var uuid = getStepUIIDs().getLast();
getAllureLifecycle().updateStep(uuid, s -> s.setStatus(PASSED)
.getParameters()
.add(new Parameter()
.setName(resultDescription)
.setValue(stringValueOfObjectOrArray(returned))));
results.put(uuid, PASSED);
getResults().put(uuid, PASSED);
}

@Override
public void fireEventFinishing() {
if (stepUIIDs.size() == 0) {
if (getStepUIIDs().isEmpty()) {
return;
}

var uuid = stepUIIDs.getLast();
allureLifecycle.updateStep(uuid, stepResult -> {
if (results.get(uuid) == null) {
var uuid = getStepUIIDs().getLast();
getAllureLifecycle().updateStep(uuid, stepResult -> {
if (getResults().get(uuid) == null) {
stepResult.setStatus(PASSED);
}
});
allureLifecycle.stopStep(uuid);
stepUIIDs.removeLast();
getAllureLifecycle().stopStep(uuid);
getStepUIIDs().removeLast();
}

@Override
public void addParameters(Map<String, String> parameters) {
if (stepUIIDs.size() == 0) {
if (getStepUIIDs().isEmpty()) {
return;
}

var uuid = stepUIIDs.getLast();
allureLifecycle.updateStep(uuid, stepResult -> {
var uuid = getStepUIIDs().getLast();
getAllureLifecycle().updateStep(uuid, stepResult -> {
var params = stepResult.getParameters();
params.addAll(parameters
.entrySet()
Expand All @@ -118,4 +118,16 @@ public void addParameters(Map<String, String> parameters) {
});

}

AllureLifecycle getAllureLifecycle() {
return allureLifecycle;
}

LinkedList<String> getStepUIIDs() {
return stepUIIDs;
}

Map<String, Status> getResults() {
return results;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.tinkoff.qa.neptune.allure;

import io.qameta.allure.AllureResultsWriteException;
import org.apache.tika.Tika;
import ru.tinkoff.qa.neptune.core.api.event.firing.captors.CapturedFileInjector;

import java.io.File;
Expand All @@ -14,10 +15,19 @@ public class AllureFileInjector implements CapturedFileInjector {

@Override
public void inject(File toBeInjected, String message) {
String mime;
Tika tika = new Tika();

try {
mime = tika.detect(toBeInjected);
} catch (Exception e) {
mime = null;
}

try {
addAttachment(message, null, new FileInputStream(toBeInjected), getFileExtension(toBeInjected.getAbsolutePath()));
} catch (FileNotFoundException var4) {
throw new AllureResultsWriteException(var4.getMessage(), var4);
addAttachment(message, mime, new FileInputStream(toBeInjected), getFileExtension(toBeInjected.getAbsolutePath()));
} catch (FileNotFoundException e) {
throw new AllureResultsWriteException(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package ru.tinkoff.qa.neptune.allure;

import ru.tinkoff.qa.neptune.core.api.event.firing.captors.CapturedImageInjector;
import io.qameta.allure.AllureResultsWriteException;
import ru.tinkoff.qa.neptune.core.api.event.firing.captors.CapturedImageInjector;

import javax.imageio.ImageIO;
import java.awt.image.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static io.qameta.allure.Allure.addAttachment;
import static java.io.File.createTempFile;
Expand All @@ -17,7 +20,7 @@ public class AllureImageInjector implements CapturedImageInjector {
private InputStream inputStream(BufferedImage image) {
File picForLog = null;
try {
picForLog = createTempFile("picture", randomUUID().toString() + ".png");
picForLog = createTempFile("picture", randomUUID() + ".png");
ImageIO.write(image, "png", picForLog);
return new FileInputStream(picForLog);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.tinkoff.qa.neptune.allure;

import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.internal.AllureStorage;
import io.qameta.allure.model.TestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import java.util.UUID;

import static io.qameta.allure.Allure.getLifecycle;
import static java.util.UUID.randomUUID;

public class AbstractAllurePreparations {

final AllureLifecycle lifeCycle = getLifecycle();
final UUID testCaseUUID = randomUUID();
AllureStorage storage;

private static AllureStorage getAllureStorage(AllureLifecycle lifecycle) throws Exception {
var field = lifecycle.getClass().getDeclaredField("storage");
field.setAccessible(true);
return (AllureStorage) field.get(lifecycle);
}

@BeforeClass
public void beforeClass() throws Exception {
storage = getAllureStorage(lifeCycle);
storage.put(testCaseUUID.toString(), new TestResult());
lifeCycle.startTestCase(testCaseUUID.toString());
}

@AfterClass
public void afterClass() {
lifeCycle.stopTestCase(testCaseUUID.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ru.tinkoff.qa.neptune.allure;

import io.qameta.allure.AllureResultsWriteException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Paths;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.testng.Assert.fail;
import static ru.tinkoff.qa.neptune.core.api.event.firing.StaticEventFiring.fireEventStarting;
import static ru.tinkoff.qa.neptune.core.api.hamcrest.iterables.SetOfObjectsItemsMatcher.iterableHasItems;
import static ru.tinkoff.qa.neptune.core.api.hamcrest.pojo.PojoGetterReturnsMatcher.getterReturns;

public class AttachmentTest extends AbstractAllurePreparations {

private String rootStepUUID;

@BeforeClass
public void prepareStep() {
fireEventStarting("Some step", Map.of());
rootStepUUID = lifeCycle.getCurrentTestCaseOrStep().get();
}

@Test
public void textAttach() {
new AllureStringInjector().inject(new StringBuilder("ABCD"), "String attach");
var step = storage.getStep(rootStepUUID).get();
assertThat(step.getAttachments(),
iterableHasItems(1,
getterReturns("getName", "String attach"),
getterReturns("getSource", not(nullValue())),
getterReturns("getType", "text/plain")));
}

@Test
public void imageAttach() throws Exception {
var source = Paths.get("src/test/resources/picture.jpeg");
BufferedImage bi = ImageIO.read(source.toFile());

new AllureImageInjector().inject(bi, "Image attach");
var step = storage.getStep(rootStepUUID).get();


assertThat(step.getAttachments(),
iterableHasItems(1,
getterReturns("getName", "Image attach"),
getterReturns("getSource", not(nullValue())),
getterReturns("getType", "image/*")));
}

@Test
public void fileAttach() {
var source = Paths.get("src/test/resources/test.json").toFile();
new AllureFileInjector().inject(source, "File attach");

var step = storage.getStep(rootStepUUID).get();

assertThat(step.getAttachments(),
iterableHasItems(1,
getterReturns("getName", "File attach"),
getterReturns("getSource", not(nullValue())),
getterReturns("getType", "application/json")));
}

@Test(expectedExceptions = AllureResultsWriteException.class)
public void fileAttach2() {
var source = Paths.get("src/test/resources/not_existent").toFile();
new AllureFileInjector().inject(source, "File attach");

fail("Exception was expected");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ru.tinkoff.qa.neptune.allure;

import io.qameta.allure.model.Status;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Map;

import static io.qameta.allure.model.Status.BROKEN;
import static io.qameta.allure.model.Status.FAILED;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static ru.tinkoff.qa.neptune.core.api.event.firing.StaticEventFiring.*;

public class FailedStepLifeCycleTest extends AbstractAllurePreparations {

@DataProvider
public static Object[][] data1() {
return new Object[][]{
{new AssertionError("Test assertion error"), FAILED},
{new RuntimeException(), BROKEN}
};
}

@Test(dataProvider = "data1")
public void failedRootStep(Throwable thrown, Status expected) {
fireEventStarting("Some step", Map.of());
var stepId = lifeCycle.getCurrentTestCaseOrStep().get();
var result = storage.getStep(stepId).get();

fireThrownException(thrown);
fireEventFinishing();
assertThat(result.getStatus(), is(expected));
assertThat(result.getStop(), instanceOf(Long.class));
}

@Test(dataProvider = "data1")
public void failedNestedStep(Throwable thrown, Status expected) {
fireEventStarting("Some root step", Map.of());
var rootStepId = lifeCycle.getCurrentTestCaseOrStep().get();
var rootResult = storage.getStep(rootStepId).get();

fireEventStarting("Some nested step", Map.of());
var nestedStepId = lifeCycle.getCurrentTestCaseOrStep().get();
var nestedResult = storage.getStep(nestedStepId).get();

fireThrownException(thrown);
fireEventFinishing();

assertThat(rootResult.getStatus(), nullValue());
assertThat(rootResult.getStop(), nullValue());

assertThat(nestedResult.getStatus(), is(expected));
assertThat(nestedResult.getStop(), instanceOf(Long.class));
}

@Test(priority = -1)
public void failedWhenNoActiveStep() {
fireThrownException(new AssertionError("Test assertion error"));
assertThat(lifeCycle.getCurrentTestCaseOrStep().get(), is(testCaseUUID.toString()));
}
}

This file was deleted.

Loading

0 comments on commit 7aeb611

Please sign in to comment.