ru.tinkoff.gatling.javaapi.Assertions Maven / Gradle / Ivy
The newest version!
package ru.tinkoff.gatling.javaapi;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.gatling.javaapi.core.Assertion;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static io.gatling.javaapi.core.CoreDsl.*;
import static java.lang.Double.parseDouble;
public final class Assertions {
private record RecordNFR(String key, HashMap value) {}
private record NFR(List nfr) {}
private Assertions() {
}
private static final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
private static NFR getNfr(String path) throws AssertionBuilderException {
mapper.findAndRegisterModules();
try {
return mapper.readValue(new File(path) , NFR.class);
} catch (MismatchedInputException e) {
throw new AssertionBuilderException("Incorrect file content " + path, e);
} catch (FileNotFoundException e) {
throw new AssertionBuilderException("NFR File not found " + path, e);
} catch (Exception e) {
throw new AssertionBuilderException("Unknown error " + path, e);
}
}
private static String toUtf(String baseString) {
return new String(baseString.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
}
private static String[] getGroupAndRequest(String key) {
return key.split(" / ");
}
private static List buildAssertion(RecordNFR record) {
return switch (toUtf(record.key())) {
case "Процент ошибок" -> buildErrorAssertion(record);
case "99 перцентиль времени выполнения" -> buildPercentileAssertion(record, 99.0);
case "95 перцентиль времени выполнения" -> buildPercentileAssertion(record, 95.0);
case "75 перцентиль времени выполнения" -> buildPercentileAssertion(record, 75.0);
case "50 перцентиль времени выполнения" -> buildPercentileAssertion(record, 50.0);
case "Максимальное время выполнения" -> buildMaxResponseTimeAssertion(record);
default -> new LinkedList<>();
};
}
private static List getListAssertions(List assertionList,
String key,
Assertion allAssertion,
Assertion detailsAssertion) {
if (Objects.equals(key, "all")) {
assertionList.add(allAssertion);
}
else {
assertionList.add(detailsAssertion);
}
return assertionList;
}
private static List buildPercentileAssertion(RecordNFR record, Double percentile) {
List assertionList = new LinkedList<>();
for (Map.Entry entry: record.value().entrySet() ) {
String key = entry.getKey();
String value = entry.getValue();
assertionList.addAll(getListAssertions(
assertionList,
key,
global().responseTime().percentile(percentile).lt(Integer.valueOf(value)),
details(getGroupAndRequest(key)).responseTime().percentile(percentile).lt(Integer.valueOf(value)))
);
}
return assertionList;
}
private static List buildErrorAssertion(RecordNFR record) {
List assertionList = new LinkedList<>();
for (Map.Entry entry: record.value().entrySet() ) {
String key = entry.getKey();
String value = entry.getValue();
assertionList.addAll(getListAssertions(
assertionList,
key,
global().failedRequests().percent().lt(parseDouble(value)),
details(getGroupAndRequest(key)).failedRequests().percent().lt(parseDouble(value)))
);
}
return assertionList;
}
private static List buildMaxResponseTimeAssertion(RecordNFR record) {
List assertionList = new LinkedList<>();
for (Map.Entry entry: record.value().entrySet() ) {
String key = entry.getKey();
String value = entry.getValue();
assertionList.addAll(getListAssertions(
assertionList,
key,
global().responseTime().max().lt(Integer.valueOf(value)),
details(getGroupAndRequest(key)).responseTime().max().lt(Integer.valueOf(value)))
);
}
return assertionList;
}
public static List assertionFromYaml(String path) {
List assertionList = new LinkedList<>();
NFR nfr = getNfr(path);
List recordNFRList = nfr.nfr();
recordNFRList.forEach(recordNFR -> assertionList.addAll(buildAssertion(recordNFR)));
return assertionList;
}
}