Move YamlContractConverter after available contract converters for producers (#687)

* Move YamlContractConverter list of available contract converters.

* Move available contract converters before YamlContractConverter
This commit is contained in:
SvenBayer
2018-07-11 20:40:17 +02:00
committed by Marcin Grzejszczak
parent 0a28d0848d
commit 4aab1da0f7
5 changed files with 93 additions and 3 deletions

View File

@@ -169,10 +169,10 @@ class StubRepository {
if (isContractDescriptor(file)) {
contractDescriptors
.addAll(ContractVerifierDslConverter.convertAsCollection(file.getParentFile(), file));
} else if (converter != null && converter.isAccepted(file)) {
contractDescriptors.addAll(converter.convertFrom(file));
} else if (YamlContractConverter.INSTANCE.isAccepted(file)) {
contractDescriptors.addAll(YamlContractConverter.INSTANCE.convertFrom(file));
} else if (converter != null) {
contractDescriptors.addAll(converter.convertFrom(file));
}
}
return super.visitFile(path, attrs);

View File

@@ -0,0 +1,30 @@
package org.springframework.cloud.contract.stubrunner;
import org.junit.Test;
import org.springframework.cloud.contract.spec.Contract;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
/**
* @author Sven Bayer
*/
public class StubRepositoryTest {
private static final File YAML_REPOSITORY_LOCATION = new File("src/test/resources/customYamlRepository");
@Test
public void should_prefer_custom_yaml_converter_over_standard() {
//given:
StubRepository repository = new StubRepository(YAML_REPOSITORY_LOCATION, new ArrayList<>(), new StubRunnerOptionsBuilder().build());
int expectedDescriptorsSize = 1;
//when:
Collection<Contract> descriptors = repository.getContracts();
//then:
assertEquals(expectedDescriptorsSize, descriptors.size());
}
}

View File

@@ -0,0 +1,42 @@
package org.springframework.cloud.contract.stubrunner;
import groovy.lang.Closure;
import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.spec.ContractConverter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
/**
* @author Sven Bayer
*/
public class TestCustomYamlContractConverter implements ContractConverter {
@Override
public boolean isAccepted(File file) {
if (!file.getName().endsWith(".yml") && !file.getName().endsWith(".yaml")) {
return false;
}
Optional<String> line;
try {
line = Files.lines(file.toPath()).findFirst();
} catch (IOException e) {
throw new IllegalStateException(e);
}
return line.isPresent() && line.get().startsWith("custom_format: 1.0");
}
@Override
public Collection<Contract> convertFrom(File file) {
return Collections.singleton(Contract.make(Closure.IDENTITY));
}
@Override
public Object convertTo(Collection contract) {
return new Object();
}
}

View File

@@ -1,2 +1,4 @@
org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions=\
org.springframework.cloud.contract.stubrunner.provider.wiremock.TestWireMockExtensions
org.springframework.cloud.contract.stubrunner.provider.wiremock.TestWireMockExtensions
org.springframework.cloud.contract.spec.ContractConverter=\
org.springframework.cloud.contract.stubrunner.TestCustomYamlContractConverter

View File

@@ -0,0 +1,16 @@
custom_format: 1.0
outbound:
request:
url: /foo
method: PUT
headers:
foo: bar
body:
foo: bar
inbound:
response:
status: 200
headers:
foo2: bar
body:
foo2: bar