use of org.mockserver.mock.listeners.MockServerMatcherNotifier.Cause in project mockserver by mock-server.
the class ExpectationInitializerLoader method retrieveExpectationsFromFile.
public List<Expectation> retrieveExpectationsFromFile(String initialLogMessage, String expectationLogMessage, String completedLogMessage, Cause.Type causeType) {
List<String> initializationJsonPaths = ExpectationInitializerLoader.expandedInitializationJsonPaths(configuration.initializationJsonPath());
return initializationJsonPaths.stream().flatMap(initializationJsonPath -> {
Expectation[] expectations = new Expectation[0];
if (isNotBlank(initializationJsonPath)) {
if (isNotBlank(initialLogMessage) && MockServerLogger.isEnabled(INFO)) {
mockServerLogger.logEvent(new LogEntry().setType(SERVER_CONFIGURATION).setLogLevel(INFO).setMessageFormat(initialLogMessage).setArguments(initializationJsonPath));
}
List<String> expectationIds = new ArrayList<>();
try {
String jsonExpectations = FileReader.readFileFromClassPathOrPath(initializationJsonPath);
if (isNotBlank(jsonExpectations)) {
expectations = expectationSerializer.deserializeArray(jsonExpectations, true, (expectationString, deserialisedExpectations) -> {
for (int i = 0; i < deserialisedExpectations.size(); i++) {
int counter = 0;
String expectationId;
do {
expectationId = UUID.nameUUIDFromBytes(String.valueOf(Objects.hash(initializationJsonPath, expectationString, i, counter++)).getBytes(StandardCharsets.UTF_8)).toString();
} while (expectationIds.contains(expectationId) && counter < 50);
expectationIds.add(expectationId);
deserialisedExpectations.get(i).withIdIfNull(expectationId);
}
return deserialisedExpectations;
});
}
} catch (Throwable throwable) {
if (MockServerLogger.isEnabled(WARN)) {
mockServerLogger.logEvent(new LogEntry().setType(SERVER_CONFIGURATION).setLogLevel(WARN).setMessageFormat(expectationLogMessage).setArguments(initializationJsonPath).setThrowable(throwable));
}
}
}
if (MockServerLogger.isEnabled(TRACE)) {
mockServerLogger.logEvent(new LogEntry().setLogLevel(TRACE).setMessageFormat(completedLogMessage).setArguments(Arrays.asList(expectations), initializationJsonPath));
}
requestMatchers.update(expectations, new Cause(initializationJsonPath, causeType));
return Arrays.stream(expectations);
}).collect(Collectors.toList());
}
Aggregations