Added failOnInProgress

with this change your plugin can fail the build if you have any in progress contracts

fixes gh-1156
This commit is contained in:
Marcin Grzejszczak
2019-08-08 14:15:24 +02:00
parent 875ac13427
commit 909930a6f4
11 changed files with 207 additions and 5 deletions

View File

@@ -89,6 +89,16 @@ class TestGenerator {
.build()
}
protected TestGenerator(ContractVerifierConfigProperties configProperties, SingleTestGenerator generator, FileSaver saver, ContractFileScanner contractFileScanner) {
this.configProperties = configProperties
if (configProperties.contractsDslDir == null) {
throw new ContractVerifierException("Stubs directory not found under " + configProperties.contractsDslDir)
}
this.generator = generator
this.saver = saver
this.contractFileScanner = contractFileScanner
}
int generate() {
generateTestClasses(basePackageName())
NamesUtil.recrusiveDirectoryToPackage(configProperties.generatedTestSourcesDir)
@@ -113,6 +123,15 @@ class TestGenerator {
void generateTestClasses(final String basePackageName) {
ListMultimap<Path, ContractMetadata> contracts = contractFileScanner.
findContracts()
Set<Map.Entry<Path,Collection<ContractMetadata>>> inProgress = contracts.asMap().entrySet()
.findAll { Map.Entry<Path, Collection<ContractMetadata>> entry -> entry.value.any { it.anyInProgress() }}
if (!inProgress.isEmpty() && configProperties.failOnInProgress) {
throw new IllegalStateException("In progress contracts found in paths [" + inProgress.collect { it.key.toString() }.join(",") + "] and the switch [failOnInProgress] is set to [true]. Either unmark those contracts as in progress, or set the switch to [false].")
}
processAllNotInProgress(contracts,basePackageName)
}
@PackageScope Set<Map.Entry<Path,Collection<ContractMetadata>>> processAllNotInProgress(ListMultimap<Path,ContractMetadata> contracts, String basePackageName) {
contracts.asMap().entrySet()
.findAll { Map.Entry<Path, Collection<ContractMetadata>> entry -> !entry.value.any { it.anyInProgress() }}
.each {

View File

@@ -175,6 +175,14 @@ public class ContractVerifierConfigProperties {
*/
private boolean excludeBuildFolders;
/**
* If set to true then if any contracts that are in progress are found, will break the
* build. On the producer side you need to be explicit about the fact that you have
* contracts in progress and take into consideration that you might be causing false
* positive test execution results on the consumer side.
*/
private boolean failOnInProgress = true;
@Deprecated
public void setTargetFramework(TestFramework targetFramework) {
log.warn("Please use the [testFramework] field. [targetFramework] is deprecated");
@@ -366,4 +374,12 @@ public class ContractVerifierConfigProperties {
this.excludeBuildFolders = excludeBuildFolders;
}
public boolean isFailOnInProgress() {
return this.failOnInProgress;
}
public void setFailOnInProgress(boolean failOnInProgress) {
this.failOnInProgress = failOnInProgress;
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier;
import java.io.File;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.assertj.core.api.BDDAssertions;
import org.junit.Test;
import org.mockito.BDDMockito;
import wiremock.com.google.common.collect.ArrayListMultimap;
import wiremock.com.google.common.collect.ListMultimap;
import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.verifier.builder.SingleTestGenerator;
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
import org.springframework.cloud.contract.verifier.file.ContractFileScanner;
import org.springframework.cloud.contract.verifier.file.ContractMetadata;
public class TestGeneratorTests {
@Test
public void should_throw_exception_when_in_progress_contracts_found() {
// given:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
properties.setFailOnInProgress(true);
properties.setContractsDslDir(new File("."));
SingleTestGenerator singleTestGenerator = BDDMockito
.mock(SingleTestGenerator.class);
FileSaver fileSaver = BDDMockito.mock(FileSaver.class);
// and:
ArrayListMultimap<Path, ContractMetadata> multimap = ArrayListMultimap.create();
Path path = new File(".").toPath();
multimap.put(path,
new ContractMetadata(path, false, 0, null, Contract.make(it -> {
it.inProgress();
it.request(r -> {
r.method(r.GET());
r.url("/foo");
});
it.response(r -> {
r.status(r.OK());
});
})));
ContractFileScanner scanner = new ContractFileScanner(null, null, null) {
@Override
public ListMultimap<Path, ContractMetadata> findContracts() {
return multimap;
}
};
// and:
TestGenerator testGenerator = new TestGenerator(properties, singleTestGenerator,
fileSaver, scanner);
// then:
BDDAssertions.thenThrownBy(() -> {
// when:
testGenerator.generateTestClasses("com.example");
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("In progress contracts found in");
}
@Test
public void should_not_throw_exception_when_in_progress_contracts_found_but_the_fail_on_in_progress_switch_is_off() {
// given:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
properties.setFailOnInProgress(false);
properties.setContractsDslDir(new File("."));
SingleTestGenerator singleTestGenerator = BDDMockito
.mock(SingleTestGenerator.class);
FileSaver fileSaver = BDDMockito.mock(FileSaver.class);
// and:
ArrayListMultimap<Path, ContractMetadata> multimap = ArrayListMultimap.create();
Path path = new File(".").toPath();
multimap.put(path,
new ContractMetadata(path, false, 0, null, Contract.make(it -> {
it.inProgress();
it.request(r -> {
r.method(r.GET());
r.url("/foo");
});
it.response(r -> {
r.status(r.OK());
});
})));
ContractFileScanner scanner = new ContractFileScanner(null, null, null) {
@Override
public ListMultimap<Path, ContractMetadata> findContracts() {
return multimap;
}
};
// and:
TestGenerator testGenerator = new TestGenerator(properties, singleTestGenerator,
fileSaver, scanner) {
@Override
Set<Map.Entry<Path, Collection<ContractMetadata>>> processAllNotInProgress(
ListMultimap<Path, ContractMetadata> contracts,
String basePackageName) {
return null;
}
};
// when:
testGenerator.generateTestClasses("com.example");
// then: noExceptionThrown()
}
}

View File

@@ -16,14 +16,20 @@
package org.springframework.cloud.contract.verifier.builder
import java.nio.file.Paths
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Issue
import spock.lang.Specification
import wiremock.com.google.common.collect.ArrayListMultimap
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.FileSaver
import org.springframework.cloud.contract.verifier.TestGenerator
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
import org.springframework.cloud.contract.verifier.config.TestFramework
import org.springframework.cloud.contract.verifier.file.ContractFileScanner
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.SyntaxChecker
import org.springframework.util.FileSystemUtils