Загрузка данных
public class AllureMarkupConfiguration {
@JsonProperty("markupSets")
private List<MarkupSet> markupSets = List.of();
public static AllureMarkupConfiguration readFromResources(String resourcePath) {
try (InputStream inputStream = AllureMarkupConfiguration.class.getResourceAsStream(resourcePath)) {
if (inputStream == null) {
return empty();
} else {
ObjectMapper mapper = new ObjectMapper();
return (AllureMarkupConfiguration)mapper.readValue(inputStream, AllureMarkupConfiguration.class);
}
} catch (IOException e) {
throw new AllureMarkupConfigurationException("Error when reading resource file '" + resourcePath + "'", e);
}
}
public static AllureMarkupConfiguration empty() {
return new AllureMarkupConfiguration();
}
private AllureMarkupConfiguration() {
}
public List<MarkupSet> getMarkupSets() {
return this.markupSets;
}
public void addMarkupSet(MarkupSet markupSet) {
this.markupSets.add(markupSet);
}
}
public class AllureMarkupConfigurationException extends RuntimeException {
public AllureMarkupConfigurationException(String message) {
super(message);
}
public AllureMarkupConfigurationException(String message, Throwable cause) {
super(message, cause);
}
}
public class LabelSet {
@JsonProperty("label")
private String label;
@JsonProperty("value")
private String value;
public LabelSet() {
this.label = null;
this.value = null;
}
public LabelSet(String label, String value) {
this.label = label;
this.value = value;
}
public static LabelSet of(String label, String value) {
return new LabelSet(label, value);
}
public String getLabel() {
return this.label;
}
public String getValue() {
return this.value;
}
}
public class MarkupSet {
@JsonProperty("packages")
private List<String> packages;
@JsonProperty("labelSets")
private List<LabelSet> labelSets;
public MarkupSet() {
this.packages = null;
this.labelSets = null;
}
public MarkupSet(List<String> packages, List<LabelSet> labelSets) {
this.packages = packages;
this.labelSets = labelSets;
}
public static MarkupSet of(List<String> packages, LabelSet labelSet) {
return new MarkupSet(packages, List.of(labelSet));
}
public static MarkupSet of(List<String> packages, List<LabelSet> labelSets) {
return new MarkupSet(packages, labelSets);
}
public List<String> getPackages() {
return this.packages;
}
public List<LabelSet> getLabelSets() {
return this.labelSets;
}
}
public class AllureMarkup {
private static final Logger log = LoggerFactory.getLogger(AllureMarkup.class);
private static final Pattern LABEL_VALUE_PATTERN = Pattern.compile("(?<allureLabelValue>\\w+)\\[(?<packages>.*)\\]");
private static AllureMarkupConfiguration markupConfiguration = null;
public static void markup(Class<?> testClass) {
markup(testClass.getCanonicalName());
}
public static void markup(Class<?> testClass, String resourcesFile) {
markup(testClass.getCanonicalName(), resourcesFile);
}
public static void markup(String testClassName) {
initMarkupConfiguration("/allure-markup.json");
markupTestClass(testClassName);
}
public static void markup(String testClassName, String resourcesFile) {
initMarkupConfiguration(resourcesFile);
markupTestClass(testClassName);
}
private static void markupTestClass(String testClassName) {
markupConfiguration.getMarkupSets().forEach((markupSet) -> markupSet.getPackages().forEach((packageName) -> {
if (testClassName.startsWith(packageName)) {
markupSet.getLabelSets().forEach((labelSet) -> {
Allure.label(labelSet.getLabel(), labelSet.getValue());
log.info("For test: '" + testClassName + "' added label: '" + labelSet.getLabel() + "' = '" + labelSet.getValue() + "'");
});
}
}));
}
private static synchronized void initMarkupConfiguration(String resourcesFile) {
if (markupConfiguration == null) {
markupConfiguration = AllureMarkupConfiguration.readFromResources(resourcesFile);
List<Map.Entry<String, String>> envLabels = (List)System.getenv().entrySet().stream().filter((stringEntry) -> ((String)stringEntry.getKey()).contains("allure.label")).collect(Collectors.toList());
envLabels.forEach((stringEntry) -> {
String label = ((String)stringEntry.getKey()).substring("allure.label.".length());
Matcher labelValueMatcher = LABEL_VALUE_PATTERN.matcher((CharSequence)stringEntry.getValue());
if (labelValueMatcher.find()) {
String labelValue = labelValueMatcher.group("allureLabelValue").trim();
List<String> packages = (List)Stream.of(labelValueMatcher.group("packages").trim().split(",")).filter((s) -> !s.isEmpty()).collect(Collectors.toList());
MarkupSet markupSet = MarkupSet.of(packages, LabelSet.of(label, labelValue));
markupConfiguration.addMarkupSet(markupSet);
}
});
}
}
}
@ExtendWith(AllureMarkupExtension.class)
@SetEnvironmentConfiguration(ApiEnvironmentConfiguration.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Примеры автотестов на бэк")
public class ExampleTest {
@Test
@DisplayName("Какой-то тест")
@Description("Добавление тела запроса прям в тесте")
{
"markupSets": [
{
"packages": [
""
],
"labelSets": [
{
"label": "Severity",
"value": "BLOCKER"
}
]
}
]
}
json в target/test-classes кладется, но в отчете приоритет теста не меняется