Search in sources :

Example 1 with YamlContract

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));
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Contract(org.springframework.cloud.contract.spec.Contract)

Example 2 with YamlContract

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();
    }
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Map(java.util.Map)

Example 3 with YamlContract

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);
}
Also used : QueryParameters(org.springframework.cloud.contract.spec.internal.QueryParameters) Parameters(com.github.tomakehurst.wiremock.extension.Parameters) YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) YamlContractConverter(org.springframework.cloud.contract.verifier.converter.YamlContractConverter) GString(groovy.lang.GString)

Example 4 with YamlContract

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);
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) YamlContractConverter(org.springframework.cloud.contract.verifier.converter.YamlContractConverter)

Example 5 with YamlContract

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;
            });
        }
    };
}
Also used : YamlContract(org.springframework.cloud.contract.verifier.converter.YamlContract) Message(javax.jms.Message) TimeUnit(java.util.concurrent.TimeUnit) MessageVerifier(org.springframework.cloud.contract.verifier.messaging.MessageVerifier) JMSException(javax.jms.JMSException) Map(java.util.Map) Nullable(javax.annotation.Nullable) Bean(org.springframework.context.annotation.Bean)

Aggregations

YamlContract (org.springframework.cloud.contract.verifier.converter.YamlContract)9 Map (java.util.Map)4 TimeUnit (java.util.concurrent.TimeUnit)3 MessageVerifier (org.springframework.cloud.contract.verifier.messaging.MessageVerifier)3 Bean (org.springframework.context.annotation.Bean)3 Nullable (javax.annotation.Nullable)2 MessageProperties (org.springframework.amqp.core.MessageProperties)2 YamlContractConverter (org.springframework.cloud.contract.verifier.converter.YamlContractConverter)2 Parameters (com.github.tomakehurst.wiremock.extension.Parameters)1 GString (groovy.lang.GString)1 HashMap (java.util.HashMap)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Logger (org.slf4j.Logger)1 Message (org.springframework.amqp.core.Message)1 Contract (org.springframework.cloud.contract.spec.Contract)1 Headers (org.springframework.cloud.contract.spec.internal.Headers)1 OutputMessage (org.springframework.cloud.contract.spec.internal.OutputMessage)1