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