Search in sources :

Example 1 with OutputMessage

use of org.springframework.cloud.contract.spec.internal.OutputMessage in project spring-cloud-contract by spring-cloud.

the class Contract method outputMessage.

/**
 * The output part of the contract.
 * @param consumer function to manipulate the output message
 */
public void outputMessage(@DelegatesTo(OutputMessage.class) Closure consumer) {
    this.outputMessage = new OutputMessage();
    consumer.setDelegate(this.outputMessage);
    consumer.call();
}
Also used : OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage)

Example 2 with OutputMessage

use of org.springframework.cloud.contract.spec.internal.OutputMessage in project spring-cloud-contract by spring-cloud.

the class YamlToContracts method mapOutputBodyMatchers.

private void mapOutputBodyMatchers(YamlContract.OutputMessage yamlContractOutputMessage, OutputMessage dslContractOutputMessage) {
    if (yamlContractOutputMessage.matchers != null) {
        dslContractOutputMessage.bodyMatchers(dslContractOutputMessageBodyMatchers -> Optional.ofNullable(yamlContractOutputMessage.matchers.body).ifPresent(yamlContractBodyTestMatchers -> yamlContractBodyTestMatchers.forEach(yamlContractBodyTestMatcher -> {
            ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(yamlContractOutputMessage.headers), yamlContractOutputMessage.body);
            MatchingTypeValue value;
            switch(yamlContractBodyTestMatcher.type) {
                case by_date:
                    value = dslContractOutputMessageBodyMatchers.byDate();
                    break;
                case by_time:
                    value = dslContractOutputMessageBodyMatchers.byTime();
                    break;
                case by_timestamp:
                    value = dslContractOutputMessageBodyMatchers.byTimestamp();
                    break;
                case by_regex:
                    String regex = yamlContractBodyTestMatcher.value;
                    if (yamlContractBodyTestMatcher.predefined != null) {
                        regex = predefinedToPattern(yamlContractBodyTestMatcher.predefined).pattern();
                    }
                    value = dslContractOutputMessageBodyMatchers.byRegex(regex);
                    break;
                case by_equality:
                    value = dslContractOutputMessageBodyMatchers.byEquality();
                    break;
                case by_type:
                    value = dslContractOutputMessageBodyMatchers.byType(v -> {
                        if (yamlContractBodyTestMatcher.minOccurrence != null) {
                            v.minOccurrence(yamlContractBodyTestMatcher.minOccurrence);
                        }
                        if (yamlContractBodyTestMatcher.maxOccurrence != null) {
                            v.maxOccurrence(yamlContractBodyTestMatcher.maxOccurrence);
                        }
                    });
                    break;
                case by_command:
                    value = dslContractOutputMessageBodyMatchers.byCommand(yamlContractBodyTestMatcher.value);
                    break;
                case by_null:
                    value = dslContractOutputMessageBodyMatchers.byNull();
                    break;
                default:
                    throw new UnsupportedOperationException("The type " + "[" + yamlContractBodyTestMatcher.type + "] is unsupported. Hint: If " + "you're using <predefined> remember to pass < type:by_regex > ");
            }
            if (XML == contentType) {
                dslContractOutputMessageBodyMatchers.xPath(yamlContractBodyTestMatcher.path, value);
            } else {
                dslContractOutputMessageBodyMatchers.jsonPath(yamlContractBodyTestMatcher.path, value);
            }
        })));
    }
}
Also used : Arrays(java.util.Arrays) MatchingTypeValue(org.springframework.cloud.contract.spec.internal.MatchingTypeValue) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) NamesUtil(org.springframework.cloud.contract.verifier.util.NamesUtil) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) Request(org.springframework.cloud.contract.spec.internal.Request) XML(org.springframework.cloud.contract.verifier.util.ContentType.XML) OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) Header(org.springframework.cloud.contract.spec.internal.Header) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) URLClassLoader(java.net.URLClassLoader) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Input(org.springframework.cloud.contract.spec.internal.Input) Collectors.toSet(java.util.stream.Collectors.toSet) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) MapUtils(org.apache.commons.collections.MapUtils) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) Paths(java.nio.file.Paths) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(org.springframework.cloud.contract.spec.internal.Response) Collections(java.util.Collections) Headers(org.springframework.cloud.contract.spec.internal.Headers) StringUtils(org.springframework.util.StringUtils) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) MatchingTypeValue(org.springframework.cloud.contract.spec.internal.MatchingTypeValue)

Example 3 with OutputMessage

use of org.springframework.cloud.contract.spec.internal.OutputMessage in project spring-cloud-contract by spring-cloud.

the class Contract method outputMessage.

/**
 * The output part of the contract.
 * @param consumer function to manipulate the output message
 */
public void outputMessage(Consumer<OutputMessage> consumer) {
    this.outputMessage = new OutputMessage();
    consumer.accept(this.outputMessage);
}
Also used : OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage)

Example 4 with OutputMessage

use of org.springframework.cloud.contract.spec.internal.OutputMessage in project spring-cloud-contract by spring-cloud.

the class StubRunnerExecutor method sendMessage.

private void sendMessage(Contract groovyDsl) {
    OutputMessage outputMessage = groovyDsl.getOutputMessage();
    DslProperty<?> body = outputMessage.getBody();
    Headers headers = outputMessage.getHeaders();
    List<YamlContract> yamlContracts = yamlContractConverter.convertTo(Collections.singleton(groovyDsl));
    YamlContract contract = yamlContracts.get(0);
    setMessageType(contract, ContractVerifierMessageMetadata.MessageType.OUTPUT);
    // TODO: Json is harcoded here
    this.contractVerifierMessaging.send(JsonOutput.toJson(BodyExtractor.extractClientValueFromBody(body == null ? null : body.getClientValue())), headers == null ? null : headers.asStubSideMap(), outputMessage.getSentTo().getClientValue(), contract);
}
Also used : OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Headers(org.springframework.cloud.contract.spec.internal.Headers)

Example 5 with OutputMessage

use of org.springframework.cloud.contract.spec.internal.OutputMessage in project spring-cloud-contract by spring-cloud.

the class MessagePactCreator method createFromContract.

MessagePact createFromContract(List<Contract> contracts) {
    if (CollectionUtils.isEmpty(contracts)) {
        return null;
    }
    Names names = NamingUtil.name(contracts.get(0));
    MessagePactBuilder pactBuilder = MessagePactBuilder.consumer(names.getConsumer()).hasPactWith(names.getProducer());
    for (Contract contract : contracts) {
        pactBuilder = pactBuilder.given(getGiven(contract.getInput())).expectsToReceive(getOutcome(contract));
        if (contract.getOutputMessage() != null) {
            OutputMessage message = contract.getOutputMessage();
            if (message.getBody() != null) {
                DslPart pactResponseBody = BodyConverter.toPactBody(message.getBody(), clientValueExtractor);
                if (message.getBodyMatchers() != null) {
                    pactResponseBody.setMatchers(MatchingRulesConverter.matchingRulesForBody(message.getBodyMatchers()));
                }
                pactResponseBody.setGenerators(ValueGeneratorConverter.extract(message, DslProperty::getServerValue));
                pactBuilder = pactBuilder.withContent(pactResponseBody);
            }
            if (message.getHeaders() != null) {
                pactBuilder = pactBuilder.withMetadata(getMetadata(message.getHeaders()));
            }
        }
    }
    return pactBuilder.toPact();
}
Also used : OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) MessagePactBuilder(au.com.dius.pact.consumer.MessagePactBuilder) DslPart(au.com.dius.pact.consumer.dsl.DslPart) Contract(org.springframework.cloud.contract.spec.Contract)

Aggregations

OutputMessage (org.springframework.cloud.contract.spec.internal.OutputMessage)8 Contract (org.springframework.cloud.contract.spec.Contract)3 Headers (org.springframework.cloud.contract.spec.internal.Headers)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)2 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)2 Input (org.springframework.cloud.contract.spec.internal.Input)2 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)2 NotToEscapePattern (org.springframework.cloud.contract.spec.internal.NotToEscapePattern)2 Request (org.springframework.cloud.contract.spec.internal.Request)2 Response (org.springframework.cloud.contract.spec.internal.Response)2 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)2 XML (org.springframework.cloud.contract.verifier.util.ContentType.XML)2 ContentUtils.evaluateClientSideContentType (org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType)2