Search in sources :

Example 1 with Cause

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());
}
Also used : LRUCache(org.mockserver.cache.LRUCache) ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) java.util(java.util) FileReader(org.mockserver.file.FileReader) SERVER_CONFIGURATION(org.mockserver.log.model.LogEntry.LogMessageType.SERVER_CONFIGURATION) ArrayUtils(org.apache.commons.lang3.ArrayUtils) MockServerMatcherNotifier(org.mockserver.mock.listeners.MockServerMatcherNotifier) Expectation(org.mockserver.mock.Expectation) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Configuration(org.mockserver.configuration.Configuration) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) FilePath(org.mockserver.file.FilePath) LogEntry(org.mockserver.log.model.LogEntry) RequestMatchers(org.mockserver.mock.RequestMatchers) Level(org.slf4j.event.Level) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MockServerLogger(org.mockserver.logging.MockServerLogger) Cause(org.mockserver.mock.listeners.MockServerMatcherNotifier.Cause) Cause(org.mockserver.mock.listeners.MockServerMatcherNotifier.Cause) LogEntry(org.mockserver.log.model.LogEntry)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Constructor (java.lang.reflect.Constructor)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 StringUtils.isNotBlank (org.apache.commons.lang3.StringUtils.isNotBlank)1 LRUCache (org.mockserver.cache.LRUCache)1 Configuration (org.mockserver.configuration.Configuration)1 FilePath (org.mockserver.file.FilePath)1 FileReader (org.mockserver.file.FileReader)1 LogEntry (org.mockserver.log.model.LogEntry)1 SERVER_CONFIGURATION (org.mockserver.log.model.LogEntry.LogMessageType.SERVER_CONFIGURATION)1 MockServerLogger (org.mockserver.logging.MockServerLogger)1 Expectation (org.mockserver.mock.Expectation)1 RequestMatchers (org.mockserver.mock.RequestMatchers)1 MockServerMatcherNotifier (org.mockserver.mock.listeners.MockServerMatcherNotifier)1 Cause (org.mockserver.mock.listeners.MockServerMatcherNotifier.Cause)1 ExpectationSerializer (org.mockserver.serialization.ExpectationSerializer)1