use of org.springframework.cloud.contract.verifier.converter.YamlContract in project spring-cloud-contract by spring-cloud.
the class BodyReader method storeContractAsYaml.
void storeContractAsYaml(SingleContractMetadata metadata) {
Contract contract = metadata.getContract();
List<YamlContract> contracts = this.converter.convertTo(Collections.singleton(contract));
Map<String, byte[]> store = this.converter.store(contracts);
store.forEach((name, bytes) -> writeFileForBothIdeAndBuildTool(metadata, bytes, name));
}
use of org.springframework.cloud.contract.verifier.converter.YamlContract in project spring-cloud-contract by spring-cloud.
the class GraphQlMatcher method match.
@Override
public MatchResult match(List<YamlContract> contracts, Request request, Parameters parameters) {
YamlContract contract = contracts.get(0);
// TODO: What if the body is in files?
Map body = (Map) contract.request.body;
try {
Map jsonBodyFromContract = body;
Map jsonBodyFromRequest = this.objectMapper.readerForMapOf(Object.class).readValue(request.getBody());
String query = (String) jsonBodyFromContract.get("query");
String queryFromRequest = (String) jsonBodyFromRequest.get("query");
Map variables = (Map) jsonBodyFromContract.get("variables");
Map variablesFromRequest = (Map) jsonBodyFromRequest.get("variables");
String operationName = (String) jsonBodyFromContract.get("operationName");
String operationNameFromRequest = (String) jsonBodyFromRequest.get("operationName");
boolean queryMatches = assertThat(() -> Assertions.assertThat(query).isEqualToIgnoringWhitespace(queryFromRequest));
boolean variablesMatch = assertThat(() -> JsonAssertions.assertThatJson(variables).isEqualTo(variablesFromRequest));
boolean operationMatches = StringUtils.equals(operationName, operationNameFromRequest);
return MatchResult.of(queryMatches && variablesMatch && operationMatches);
} catch (Exception e) {
if (log.isWarnEnabled()) {
log.warn("An exception occurred while trying to parse the graphql entries", e);
}
return MatchResult.noMatch();
}
}
use of org.springframework.cloud.contract.verifier.converter.YamlContract in project spring-cloud-contract by spring-cloud.
the class WireMockRequestStubStrategy method appendSpringCloudContractMatcher.
private void appendSpringCloudContractMatcher(ContractVerifierMetadata metadata, RequestPatternBuilder requestPatternBuilder) {
Parameters parameters = Parameters.one("tool", metadata.getTool() != null ? metadata.getTool() : "unknown");
YamlContractConverter converter = new YamlContractConverter();
List<YamlContract> contracts = converter.convertTo(Collections.singleton(contract));
Map<String, byte[]> store = converter.store(contracts);
parameters.put("contract", new String(store.entrySet().iterator().next().getValue()));
requestPatternBuilder.andMatching(SpringCloudContractRequestMatcher.NAME, parameters);
}
use of org.springframework.cloud.contract.verifier.converter.YamlContract in project spring-cloud-contract by spring-cloud.
the class ContractVerifierUtil method contract.
/**
* Helper method to convert a file to bytes.
* @param testClass - test class relative to which the file is stored
* @param relativePath - relative path to the file
* @return bytes of the file
* @since 3.0.0
*/
public static YamlContract contract(Object testClass, String relativePath) {
String path = fromRelativePath(relativePath);
byte[] bytes = fileToBytes(testClass, path);
List<YamlContract> read = new YamlContractConverter().read(bytes);
return read.isEmpty() ? null : read.get(0);
}
use of org.springframework.cloud.contract.verifier.converter.YamlContract in project spring-cloud-contract-samples by spring-cloud-samples.
the class TestConfig method standaloneMessageVerifier.
@Bean
MessageVerifier<Message> standaloneMessageVerifier(JmsTemplate jmsTemplate) {
return new MessageVerifier<>() {
@Override
public Message receive(String destination, long timeout, TimeUnit timeUnit, @Nullable YamlContract contract) {
return null;
}
@Override
public Message receive(String destination, YamlContract contract) {
return null;
}
@Override
public void send(Message message, String destination, @Nullable YamlContract contract) {
}
@Override
public <T> void send(T payload, Map<String, Object> headers, String destination, @Nullable YamlContract contract) {
jmsTemplate.send(destination, session -> {
Message message = session.createTextMessage(payload.toString());
headers.forEach((s, o) -> {
try {
message.setStringProperty(s, o.toString());
} catch (JMSException e) {
throw new IllegalStateException(e);
}
});
return message;
});
}
};
}
Aggregations