use of com.endava.cats.dsl.CatsDSLWords.ADDITIONAL_PROPERTIES in project cats by Endava.
the class ServiceCaller method replacePayloadWithRefData.
/**
* Besides reading data from the {@code --refData} file, this method will aso try to
* correlate POST recorded data with DELETE endpoints in order to maximize success rate of DELETE requests.
*
* @param data the current ServiceData context
* @return the initial payload with reference data replaced and matching POST correlations for DELETE requests
*/
String replacePayloadWithRefData(ServiceData data) {
if (!data.isReplaceRefData()) {
LOGGER.note("Bypassing reference data replacement for path {}!", data.getRelativePath());
return data.getPayload();
} else {
Map<String, String> refDataForCurrentPath = filesArguments.getRefData(data.getRelativePath());
LOGGER.note("Payload reference data replacement: path {} has the following reference data: {}", data.getRelativePath(), refDataForCurrentPath);
Map<String, String> refDataWithoutAdditionalProperties = refDataForCurrentPath.entrySet().stream().filter(stringStringEntry -> !stringStringEntry.getKey().equalsIgnoreCase(ADDITIONAL_PROPERTIES)).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
String payload = data.getPayload();
/*this will override refData for DELETE requests in order to provide valid entities that will get deleted*/
refDataWithoutAdditionalProperties.putAll(this.getPathParamFromCorrespondingPostIfDelete(data));
for (Map.Entry<String, String> entry : refDataWithoutAdditionalProperties.entrySet()) {
String refDataValue = catsDSLParser.parseAndGetResult(entry.getValue(), data.getPayload());
try {
if (CATS_REMOVE_FIELD.equalsIgnoreCase(refDataValue)) {
payload = JsonUtils.deleteNode(payload, entry.getKey());
} else {
FuzzingStrategy fuzzingStrategy = FuzzingStrategy.replace().withData(refDataValue);
boolean mergeFuzzing = data.getFuzzedFields().contains(entry.getKey());
payload = catsUtil.replaceField(payload, entry.getKey(), fuzzingStrategy, mergeFuzzing).getJson();
}
} catch (PathNotFoundException e) {
LOGGER.warning("Ref data key {} was not found within the payload!", entry.getKey());
}
}
payload = catsUtil.setAdditionalPropertiesToPayload(refDataForCurrentPath, payload);
LOGGER.note("Final payload after reference data replacement: {}", payload);
return payload;
}
}
Aggregations