Search in sources :

Example 1 with TRACE

use of org.slf4j.event.Level.TRACE in project mockserver by mock-server.

the class HttpRequestsPropertiesMatcher method apply.

@VisibleForTesting
public boolean apply(RequestDefinition requestDefinition, List<LogEntry> logEntries) {
    OpenAPIDefinition openAPIDefinition = requestDefinition instanceof OpenAPIDefinition ? (OpenAPIDefinition) requestDefinition : null;
    if (this.openAPIDefinition == null || !this.openAPIDefinition.equals(openAPIDefinition)) {
        this.openAPIDefinition = openAPIDefinition;
        if (openAPIDefinition != null && isNotBlank(openAPIDefinition.getSpecUrlOrPayload())) {
            httpRequestPropertiesMatchers = new ArrayList<>();
            httpRequests = new ArrayList<>();
            OpenAPISerialiser openAPISerialiser = new OpenAPISerialiser(mockServerLogger);
            try {
                OpenAPI openAPI = buildOpenAPI(openAPIDefinition.getSpecUrlOrPayload(), mockServerLogger);
                try {
                    final Map<String, List<Pair<String, Operation>>> stringListMap = openAPISerialiser.retrieveOperations(openAPI, openAPIDefinition.getOperationId());
                    stringListMap.forEach((path, operations) -> operations.forEach(methodOperationPair -> {
                        Operation operation = methodOperationPair.getValue();
                        if (operation.getRequestBody() != null && operation.getRequestBody().getContent() != null) {
                            operation.getRequestBody().getContent().forEach(handleRequestBody(openAPIDefinition, openAPI, path, methodOperationPair, Boolean.TRUE.equals(operation.getRequestBody().getRequired()), logEntries));
                        } else {
                            HttpRequest httpRequest = createHttpRequest(openAPIDefinition, openAPI, path, methodOperationPair);
                            try {
                                addRequestMatcher(openAPIDefinition, methodOperationPair, httpRequest, "");
                            } catch (Throwable throwable) {
                                logEntries.add(new LogEntry().setLogLevel(ERROR).setMessageFormat("exception while creating adding request matcher for operation:{}method:{}in open api:{}").setArguments(methodOperationPair.getRight().getOperationId(), methodOperationPair.getLeft(), openAPIDefinition));
                            }
                        }
                    }));
                } catch (Throwable throwable) {
                    logEntries.add(new LogEntry().setLogLevel(ERROR).setMessageFormat((StringUtils.isBlank(throwable.getMessage()) || !throwable.getMessage().contains(OPEN_API_LOAD_ERROR) ? OPEN_API_LOAD_ERROR + (isNotBlank(throwable.getMessage()) ? ", " : "") : "") + throwable.getMessage() + " for open api:{}").setArguments(openAPIDefinition));
                }
            } catch (Throwable throwable) {
                String message = (StringUtils.isBlank(throwable.getMessage()) || !throwable.getMessage().contains(OPEN_API_LOAD_ERROR) ? OPEN_API_LOAD_ERROR + (isNotBlank(throwable.getMessage()) ? ", " : "") : "") + throwable.getMessage();
                throw new IllegalArgumentException(message, throwable);
            }
            logEntries.forEach(mockServerLogger::logEvent);
        }
        this.hashCode = 0;
        if (MockServerLogger.isEnabled(TRACE)) {
            mockServerLogger.logEvent(new LogEntry().setLogLevel(TRACE).setHttpRequest(requestDefinition).setMessageFormat("created request matcher{}for open api definition{}").setArguments(this, requestDefinition));
        }
        return true;
    } else {
        return false;
    }
}
Also used : ObjectMapperFactory(org.mockserver.serialization.ObjectMapperFactory) java.util(java.util) ParameterStyle(org.mockserver.model.ParameterStyle) JsonNodeExampleSerializer(org.mockserver.openapi.examples.JsonNodeExampleSerializer) Parameter(io.swagger.v3.oas.models.parameters.Parameter) TRACE(org.slf4j.event.Level.TRACE) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) OpenAPISerialiser(org.mockserver.openapi.OpenAPISerialiser) NottableSchemaString.schemaString(org.mockserver.model.NottableSchemaString.schemaString) Operation(io.swagger.v3.oas.models.Operation) StringUtils(org.apache.commons.lang3.StringUtils) ERROR(org.slf4j.event.Level.ERROR) Pair(org.apache.commons.lang3.tuple.Pair) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Level(org.slf4j.event.Level) JsonSchemaBody.jsonSchema(org.mockserver.model.JsonSchemaBody.jsonSchema) BiConsumer(java.util.function.BiConsumer) Schema(io.swagger.v3.oas.models.media.Schema) NottableOptionalString.optional(org.mockserver.model.NottableOptionalString.optional) OpenAPIParser(org.mockserver.openapi.OpenAPIParser) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) OPEN_API_LOAD_ERROR(org.mockserver.openapi.OpenAPIParser.OPEN_API_LOAD_ERROR) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) OpenAPIParser.buildOpenAPI(org.mockserver.openapi.OpenAPIParser.buildOpenAPI) MediaType(io.swagger.v3.oas.models.media.MediaType) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NottableString.string(org.mockserver.model.NottableString.string) Configuration(org.mockserver.configuration.Configuration) org.mockserver.model(org.mockserver.model) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) LogEntry(org.mockserver.log.model.LogEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MockServerLogger(org.mockserver.logging.MockServerLogger) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE) Encoding(io.swagger.v3.oas.models.media.Encoding) AUTHORIZATION(io.netty.handler.codec.http.HttpHeaderNames.AUTHORIZATION) Joiner(com.google.common.base.Joiner) NottableSchemaString.schemaString(org.mockserver.model.NottableSchemaString.schemaString) Operation(io.swagger.v3.oas.models.Operation) OpenAPISerialiser(org.mockserver.openapi.OpenAPISerialiser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) OpenAPIParser.buildOpenAPI(org.mockserver.openapi.OpenAPIParser.buildOpenAPI) LogEntry(org.mockserver.log.model.LogEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Joiner (com.google.common.base.Joiner)1 AUTHORIZATION (io.netty.handler.codec.http.HttpHeaderNames.AUTHORIZATION)1 CONTENT_TYPE (io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 Operation (io.swagger.v3.oas.models.Operation)1 Encoding (io.swagger.v3.oas.models.media.Encoding)1 MediaType (io.swagger.v3.oas.models.media.MediaType)1 Schema (io.swagger.v3.oas.models.media.Schema)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 SecurityRequirement (io.swagger.v3.oas.models.security.SecurityRequirement)1 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)1 IOException (java.io.IOException)1 java.util (java.util)1 BiConsumer (java.util.function.BiConsumer)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StringUtils.isNotBlank (org.apache.commons.lang3.StringUtils.isNotBlank)1 Pair (org.apache.commons.lang3.tuple.Pair)1