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();
}
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);
}
})));
}
}
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);
}
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);
}
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();
}
Aggregations