Search in sources :

Example 1 with NOT_SET

use of com.endava.cats.model.util.JsonUtils.NOT_SET in project cats by Endava.

the class CustomFuzzerUtil method checkVerifiesAndReport.

private void checkVerifiesAndReport(String request, CatsResponse response, String verify, String expectedResponseCode) {
    Map<String, String> verifies = this.parseYmlEntryIntoMap(verify);
    Map<String, String> responseValues = this.matchVariablesWithTheResponse(response, verifies, Map.Entry::getKey);
    log.info("Parameters to verify: {}", verifies);
    log.info("Parameters matched to response: {}", responseValues);
    if (responseValues.entrySet().stream().anyMatch(entry -> entry.getValue().equalsIgnoreCase(NOT_SET))) {
        log.error("There are Verify parameters which were not present in the response!");
        testCaseListener.reportError(log, "The following Verify parameters were not present in the response: {}", responseValues.entrySet().stream().filter(entry -> entry.getValue().equalsIgnoreCase(NOT_SET)).map(Map.Entry::getKey).collect(Collectors.toList()));
    } else {
        StringBuilder errorMessages = new StringBuilder();
        verifies.forEach((key, value) -> {
            String valueToCheck = responseValues.get(key);
            String parsedVerifyValue = this.getVerifyValue(request, response, value);
            Matcher verifyMatcher = Pattern.compile(parsedVerifyValue).matcher(valueToCheck);
            if (!verifyMatcher.matches()) {
                errorMessages.append(String.format(NOT_MATCHING_ERROR, key, valueToCheck, parsedVerifyValue));
            }
        });
        if (errorMessages.length() == 0 && expectedResponseCode.equalsIgnoreCase(response.responseCodeAsString())) {
            testCaseListener.reportInfo(log, "Response matches all 'verify' parameters");
        } else if (errorMessages.length() == 0) {
            testCaseListener.reportWarn(log, "Response matches all 'verify' parameters, but response code doesn't match expected response code: expected [{}], actual [{}]", expectedResponseCode, response.responseCodeAsString());
        } else {
            testCaseListener.reportError(log, errorMessages.toString());
        }
    }
}
Also used : Arrays(java.util.Arrays) PrettyLoggerFactory(io.github.ludovicianul.prettylogger.PrettyLoggerFactory) CatsDSLWords(com.endava.cats.dsl.CatsDSLWords) HashMap(java.util.HashMap) CatsResponse(com.endava.cats.model.CatsResponse) PrettyLogger(io.github.ludovicianul.prettylogger.PrettyLogger) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) NOT_SET(com.endava.cats.model.util.JsonUtils.NOT_SET) Map(java.util.Map) FuzzingData(com.endava.cats.model.FuzzingData) CustomFuzzerBase(com.endava.cats.fuzzer.fields.base.CustomFuzzerBase) CatsUtil(com.endava.cats.util.CatsUtil) FuzzingStrategy(com.endava.cats.model.FuzzingStrategy) TestCaseListener(com.endava.cats.report.TestCaseListener) IOException(java.io.IOException) JsonUtils(com.endava.cats.model.util.JsonUtils) Collectors(java.util.stream.Collectors) ResponseCodeFamily(com.endava.cats.http.ResponseCodeFamily) ServiceData(com.endava.cats.io.ServiceData) AbstractMap(java.util.AbstractMap) List(java.util.List) ServiceCaller(com.endava.cats.io.ServiceCaller) CatsDSLParser(com.endava.cats.dsl.CatsDSLParser) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Matcher(java.util.regex.Matcher)

Aggregations

CatsDSLParser (com.endava.cats.dsl.CatsDSLParser)1 CatsDSLWords (com.endava.cats.dsl.CatsDSLWords)1 CustomFuzzerBase (com.endava.cats.fuzzer.fields.base.CustomFuzzerBase)1 ResponseCodeFamily (com.endava.cats.http.ResponseCodeFamily)1 ServiceCaller (com.endava.cats.io.ServiceCaller)1 ServiceData (com.endava.cats.io.ServiceData)1 CatsResponse (com.endava.cats.model.CatsResponse)1 FuzzingData (com.endava.cats.model.FuzzingData)1 FuzzingStrategy (com.endava.cats.model.FuzzingStrategy)1 JsonUtils (com.endava.cats.model.util.JsonUtils)1 NOT_SET (com.endava.cats.model.util.JsonUtils.NOT_SET)1 TestCaseListener (com.endava.cats.report.TestCaseListener)1 CatsUtil (com.endava.cats.util.CatsUtil)1 PrettyLogger (io.github.ludovicianul.prettylogger.PrettyLogger)1 PrettyLoggerFactory (io.github.ludovicianul.prettylogger.PrettyLoggerFactory)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1