Search in sources :

Example 1 with Contract

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

the class MappingGenerator method toMappings.

static Collection<Path> toMappings(File contractFile, Collection<Contract> contracts, File mappingsFolder) {
    StubGeneratorProvider provider = new StubGeneratorProvider();
    Collection<StubGenerator> stubGenerators = provider.allOrDefault(new DslToWireMockClientConverter());
    if (log.isDebugEnabled()) {
        log.debug("Found following matching stub generators " + stubGenerators);
    }
    Collection<Path> mappings = new LinkedList<>();
    for (StubGenerator stubGenerator : stubGenerators) {
        Map<Contract, String> map = stubGenerator.convertContents(contractFile.getName(), new ContractMetadata(contractFile.toPath(), false, contracts.size(), null, contracts));
        for (Map.Entry<Contract, String> entry : map.entrySet()) {
            String value = entry.getValue();
            File mapping = new File(mappingsFolder, StringUtils.stripFilenameExtension(contractFile.getName()) + "_" + Math.abs(entry.getKey().hashCode()) + stubGenerator.fileExtension());
            mappings.add(storeFile(mapping.toPath(), value.getBytes()));
        }
    }
    return mappings;
}
Also used : Path(java.nio.file.Path) StubGenerator(org.springframework.cloud.contract.verifier.converter.StubGenerator) LinkedList(java.util.LinkedList) ContractMetadata(org.springframework.cloud.contract.verifier.file.ContractMetadata) StubGeneratorProvider(org.springframework.cloud.contract.verifier.converter.StubGeneratorProvider) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) File(java.io.File) DslToWireMockClientConverter(org.springframework.cloud.contract.verifier.wiremock.DslToWireMockClientConverter)

Example 2 with Contract

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

the class CustomModeSchemeProtocolGiven method apply.

@Override
public MethodVisitor<Given> apply(SingleContractMetadata metadata) {
    Contract contract = metadata.getContract();
    ContractVerifierHttpMetaData httpMetadata = ContractVerifierHttpMetaData.fromMetadata(contract.getMetadata());
    this.blockBuilder.addIndented(".scheme(\"" + httpMetadata.getScheme().name() + "\")").addEmptyLine();
    this.blockBuilder.addIndented(".protocol(\"" + httpMetadata.getProtocol().toString() + "\")");
    return this;
}
Also used : ContractVerifierHttpMetaData(org.springframework.cloud.contract.verifier.http.ContractVerifierHttpMetaData) Contract(org.springframework.cloud.contract.spec.Contract)

Example 3 with Contract

use of org.springframework.cloud.contract.spec.Contract 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 4 with Contract

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

the class ToYamlConverter method doReplaceContractWithYaml.

protected static void doReplaceContractWithYaml(ContractConverter converter, File file) {
    if (log.isDebugEnabled()) {
        log.debug("Will replace contract [{}] with a YAML version", file.getName());
    }
    // base dir: target/copied_contracts/contracts/
    // target/copied_contracts/contracts/foo/baz/bar.groovy
    Collection<Contract> collection = converter.convertFrom(file);
    if (log.isDebugEnabled()) {
        log.debug("Converted file [{}] to collection of [{}] contracts", file, collection.size());
    }
    List<YamlContract> yamls = yamlContractConverter.convertTo(collection);
    if (log.isDebugEnabled()) {
        log.debug("Converted collection of [{}] contracts to [{}] YAML contracts", collection.size(), yamls.size());
    }
    // rm target/copied_contracts/contracts/foo/baz/bar.groovy
    file.delete();
    // [contracts/foo/baz/bar.groovy] -> [contracts/foo/baz/bar.yml]
    Map<String, byte[]> stored = yamlContractConverter.store(yamls);
    if (log.isDebugEnabled()) {
        log.debug("Dumped YAMLs to following file names {}", stored.keySet());
    }
    stored.forEach((key, value) -> {
        File ymlContractVersion = new File(file.getParentFile(), key);
        // store the YMLs instead of groovy files
        try {
            Files.write(ymlContractVersion.toPath(), value);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (log.isDebugEnabled()) {
            log.debug("Written file [{}] with YAML contract definition", ymlContractVersion);
        }
    });
}
Also used : IOException(java.io.IOException) Contract(org.springframework.cloud.contract.spec.Contract) File(java.io.File)

Example 5 with Contract

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

the class YamlToContracts method convertFrom.

Collection<Contract> convertFrom(File contractFile) {
    ClassLoader classLoader = YamlContractConverter.class.getClassLoader();
    YAMLMapper mapper = new YAMLMapper();
    try {
        Iterable<Object> iterables = new Yaml().loadAll(Files.newInputStream(contractFile.toPath()));
        Collection<Contract> contracts = new ArrayList<>();
        int counter = 0;
        for (Object document : iterables) {
            List<Contract> processedYaml = processYaml(counter, document, mapper, classLoader, contractFile);
            contracts.addAll(processedYaml);
            counter = counter + 1;
        }
        return contracts;
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    } catch (IllegalStateException ise) {
        throw ise;
    } catch (Exception e1) {
        throw new IllegalStateException("Exception occurred while processing the file [" + contractFile + "]", e1);
    } finally {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
}
Also used : YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Yaml(org.yaml.snakeyaml.Yaml) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) URLClassLoader(java.net.URLClassLoader) Contract(org.springframework.cloud.contract.spec.Contract)

Aggregations

Contract (org.springframework.cloud.contract.spec.Contract)44 Collection (java.util.Collection)12 File (java.io.File)10 List (java.util.List)10 HashMap (java.util.HashMap)8 Map (java.util.Map)7 Test (org.junit.jupiter.api.Test)7 StubConfiguration (org.springframework.cloud.contract.stubrunner.StubConfiguration)6 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)5 Bean (org.springframework.context.annotation.Bean)5 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)5 ArrayList (java.util.ArrayList)4 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 OutputMessage (org.springframework.cloud.contract.spec.internal.OutputMessage)3 QueryParameter (org.springframework.cloud.contract.spec.internal.QueryParameter)3 SpringCloudContractRestDocs.dslContract (org.springframework.cloud.contract.wiremock.restdocs.SpringCloudContractRestDocs.dslContract)3