use of org.springframework.cloud.contract.spec.internal.FromFileProperty in project spring-cloud-contract by spring-cloud.
the class JaxRsRequestMethodWhen method appendMethodAndBody.
void appendMethodAndBody(SingleContractMetadata metadata) {
Request request = metadata.getContract().getRequest();
ContentType type = metadata.getInputTestContentType();
String method = request.getMethod().getServerValue().toString().toLowerCase();
if (request.getBody() != null) {
String contentType = StringUtils.hasText(metadata.getDefinedInputTestContentType()) ? metadata.getDefinedInputTestContentType() : type.getMimeType();
Object body = request.getBody().getServerValue();
String value;
if (body instanceof ExecutionProperty) {
value = body.toString();
} else if (body instanceof FromFileProperty) {
FromFileProperty fileProperty = (FromFileProperty) body;
value = fileProperty.isByte() ? this.bodyReader.readBytesFromFileString(metadata, fileProperty, CommunicationType.REQUEST) : this.bodyReader.readStringFromFileString(metadata, fileProperty, CommunicationType.REQUEST);
} else {
value = "\"" + requestBodyAsString(metadata) + "\"";
}
this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\", entity(" + value + ", \"" + contentType + "\"))");
} else {
this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\")");
}
}
use of org.springframework.cloud.contract.spec.internal.FromFileProperty in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method mapRequestMultipart.
private void mapRequestMultipart(YamlContract.Request yamlContractRequest, Request request) {
Multipart multipart = request.getMultipart();
if (multipart != null) {
yamlContractRequest.multipart = new YamlContract.Multipart();
Map<String, Object> map = (Map<String, Object>) MapConverter.getTestSideValues(multipart);
map.forEach((key, value) -> {
if (value instanceof NamedProperty) {
Object fileName = Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).orElse(null);
Object contentType = Optional.ofNullable(((NamedProperty) value).getContentType()).map(DslProperty::getServerValue).orElse(null);
Object fileContent = Optional.ofNullable(((NamedProperty) value).getValue()).map(DslProperty::getServerValue).orElse(null);
YamlContract.Named named = new YamlContract.Named();
named.paramName = key;
named.fileName = fileName instanceof String ? Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).map(Object::toString).orElse(null) : null;
named.fileContent = (String) Optional.ofNullable(fileContent).filter(f -> f instanceof String).orElse(null);
named.fileContentAsBytes = fileContent instanceof FromFileProperty ? new String(((FromFileProperty) fileContent).asBytes()) : null;
named.fileContentFromFileAsBytes = resolveFileNameAsBytes(fileContent);
named.contentType = (String) Optional.ofNullable(contentType).filter(f -> f instanceof String).orElse(null);
named.fileNameCommand = fileName instanceof ExecutionProperty ? fileName.toString() : null;
named.fileContentCommand = fileContent instanceof ExecutionProperty ? fileContent.toString() : null;
named.contentTypeCommand = contentType instanceof ExecutionProperty ? contentType.toString() : null;
yamlContractRequest.multipart.named.add(named);
} else {
yamlContractRequest.multipart.params.put(key, value != null ? value.toString() : null);
}
});
}
}
use of org.springframework.cloud.contract.spec.internal.FromFileProperty in project spring-cloud-contract by spring-cloud.
the class MockMvcBodyGiven method processInput.
private void processInput(BlockBuilder bb, SingleContractMetadata metadata) {
Object body;
Request request = metadata.getContract().getRequest();
Object serverValue = request.getBody().getServerValue();
if (serverValue instanceof ExecutionProperty || serverValue instanceof FromFileProperty) {
body = request.getBody().getServerValue();
} else {
body = this.bodyParser.requestBodyAsString(metadata);
}
bb.addIndented(getBodyString(metadata, body));
}
use of org.springframework.cloud.contract.spec.internal.FromFileProperty in project spring-cloud-contract by spring-cloud.
the class StubRunnerJmsMessageSelector method matchContract.
private Contract matchContract(Message message, Contract groovyDsl) {
List<String> unmatchedHeaders = headersMatch(message, groovyDsl);
if (!unmatchedHeaders.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Contract [" + groovyDsl + "] hasn't matched the following headers " + unmatchedHeaders);
}
return null;
}
Object inputMessage = StubRunnerJmsAccessor.getBody(message);
Object dslBody = MapConverter.getStubSideValues(groovyDsl.getInput().getMessageBody());
if (dslBody instanceof FromFileProperty) {
if (log.isDebugEnabled()) {
log.debug("Will compare file content");
}
FromFileProperty property = (FromFileProperty) dslBody;
if (property.isString()) {
// continue processing as if body was pure string
dslBody = property.asString();
} else if (!(inputMessage instanceof byte[])) {
if (log.isDebugEnabled()) {
log.debug("Contract provided byte comparison, but the input message is of type [" + inputMessage.getClass() + "]. Can't compare the two.");
}
return null;
} else {
boolean matches = Arrays.equals(property.asBytes(), (byte[]) inputMessage);
if (log.isDebugEnabled() && !matches) {
log.debug("Contract provided byte comparison, but the byte arrays don't match");
}
return matches ? groovyDsl : null;
}
}
if (matchViaContent(groovyDsl, inputMessage, dslBody)) {
return groovyDsl;
}
return null;
}
use of org.springframework.cloud.contract.spec.internal.FromFileProperty in project spring-cloud-contract by spring-cloud.
the class StubRunnerCamelPredicate method matchContract.
private Contract matchContract(Message message, Contract groovyDsl) {
List<String> unmatchedHeaders = headersMatch(message, groovyDsl);
if (!unmatchedHeaders.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Contract [" + groovyDsl + "] hasn't matched the following headers " + unmatchedHeaders);
}
return null;
}
Object inputMessage = message.getBody();
Object dslBody = MapConverter.getStubSideValues(groovyDsl.getInput().getMessageBody());
if (dslBody instanceof FromFileProperty) {
if (log.isDebugEnabled()) {
log.debug("Will compare file content");
}
FromFileProperty property = (FromFileProperty) dslBody;
if (property.isString()) {
// continue processing as if body was pure string
dslBody = property.asString();
} else if (!(inputMessage instanceof byte[])) {
if (log.isDebugEnabled()) {
log.debug("Contract provided byte comparison, but the input message is of type [" + inputMessage.getClass() + "]. Can't compare the two.");
}
return null;
} else {
boolean matches = Arrays.equals(property.asBytes(), (byte[]) inputMessage);
if (log.isDebugEnabled() && !matches) {
log.debug("Contract provided byte comparison, but the byte arrays don't match");
}
return matches ? groovyDsl : null;
}
}
if (matchViaContent(groovyDsl, inputMessage, dslBody)) {
return groovyDsl;
}
return null;
}
Aggregations