Added SpringCloudContractAssertions

we're hiding the details related to assertions in our new abstraction over assertj assertions. That way we'll be able to add new methods in an easy way. There will be no need to create annonymous conditions etc.
This commit is contained in:
Marcin Grzejszczak
2017-02-08 14:02:29 +01:00
parent b94a074e41
commit f34039bbfb
11 changed files with 142 additions and 93 deletions

View File

@@ -20,7 +20,6 @@ import java.io.File;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -97,18 +96,6 @@ public class PluginIT {
.assertErrorFreeLog();
}
@Test
@Ignore("Ignored, because of bug accurest#245")
public void should_build_project_project_with_complex_configuration() throws Exception {
File basedir = this.resources.getBasedir("complex-configuration");
this.maven.forProject(basedir)
.execute("package")
.assertErrorFreeLog()
.assertLogText("Tests run: 2, Failures: 0, Errors: 0, Skipped: 1")
.assertLogText("Running com.blogspot.toomuchcoding.frauddetection.ContractVerifierTest")
.assertErrorFreeLog();
}
@Test
public void should_convert_Accurest_Contracts_to_WireMock_Stubs_mappings() throws Exception {
File basedir = this.resources.getBasedir("pomless");

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
@@ -49,27 +49,9 @@
<!-- verifier test dependencies-->
<!-- tag::dependencies[] -->
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.toomuchcoding.jsonassert</groupId>
<artifactId>jsonassert</artifactId>
<version>0.4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.4.1</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${it-plugin.version}</version>
<scope>test</scope>
</dependency>
<!-- end::dependencies[] -->

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
@@ -50,21 +50,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.toomuchcoding.jsonassert</groupId>
<artifactId>jsonassert</artifactId>
<version>0.4.8</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${it-plugin.version}</version>
<scope>test</scope>
</dependency>
<!-- end::dependencies[] -->

View File

@@ -28,7 +28,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<version>1.5.1.RELEASE</version>
</parent>
<dependencies>
@@ -49,27 +49,9 @@
<!-- verifier test dependencies-->
<!-- tag::dependencies[] -->
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.toomuchcoding.jsonassert</groupId>
<artifactId>jsonassert</artifactId>
<version>0.4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.4.1</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${it-plugin.version}</version>
<scope>test</scope>
</dependency>
<!-- end::dependencies[] -->

View File

@@ -88,7 +88,7 @@ class JavaTestGenerator implements SingleTestGenerator {
clazz.addImport('com.jayway.restassured.response.Response')
}
clazz.addImport('org.junit.Test')
clazz.addStaticImport('org.assertj.core.api.Assertions.assertThat')
clazz.addStaticImport('org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat')
}
if (configProperties.ruleClassForTests) {
clazz.addImport('org.junit.Rule').addRule(configProperties.ruleClassForTests)

View File

@@ -342,20 +342,8 @@ abstract class MethodBodyBuilder {
}
protected void buildCustomMatchingConditionForEachElement(BlockBuilder bb, String path, String valueAsParam) {
String method = "assertThat(parsedJson.read(${path}, java.util.Collection.class)).as(\"All elements match regex\").are("
String newCondition = "new org.assertj.core.api.Condition<Object>() {"
String overriddenMethod = "@Override public boolean matches(Object o) {"
String matches = "return ((String)o).matches(${valueAsParam})"
String methodEnd = "}"
String classEnd = "})"
String method = "assertThat(parsedJson.read(${path}, java.util.Collection.class)).allElementsMatch(${valueAsParam})"
bb.addLine(postProcessJsonPathCall(method))
bb.startBlock().startBlock().addLine(newCondition)
bb.startBlock().addLine(overriddenMethod)
bb.startBlock().addLine(postProcessJsonPathCall(matches))
addColonIfRequired(bb)
bb.endBlock().addLine(methodEnd)
bb.endBlock().addLine(classEnd)
bb.endBlock().endBlock()
}
protected Object value(def body, BodyMatcher bodyMatcher) {

View File

@@ -0,0 +1,38 @@
package org.springframework.cloud.contract.verifier.assertion;
import java.util.Iterator;
import org.assertj.core.api.IterableAssert;
/**
* Extension to {@link Iterable} assertions
*
* @author Marcin Grzejszczak
* @since 1.1.0
*/
public class CollectionAssert<ELEMENT> extends IterableAssert<ELEMENT> {
public CollectionAssert(Iterable<? extends ELEMENT> actual) {
super(actual);
}
public CollectionAssert(Iterator<? extends ELEMENT> actual) {
super(actual);
}
/**
* Asserts all elements of the collection whether they match a regular expression
* @param regex - regular expression to check against
* @return this
*/
public CollectionAssert allElementsMatch(String regex) {
isNotNull();
isNotEmpty();
for (Object anActual : this.actual) {
String value = anActual.toString();
if (!value.matches(regex)) {
failWithMessage("The value <%s> doesn't match the regex <%s>", value, regex);
}
}
return this;
}
}

View File

@@ -0,0 +1,22 @@
package org.springframework.cloud.contract.verifier.assertion;
import org.assertj.core.api.Assertions;
/**
* Assertions used by the generated tests
*
* @author Marcin Grzejszczak
* @since 1.1.0
*/
public class SpringCloudContractAssertions extends Assertions {
/**
* Creates a new instance of <code>{@link CollectionAssert}</code>.
*
* @param actual the actual value.
* @return the created assertion object.
*/
public static <ELEMENT> CollectionAssert<ELEMENT> assertThat(Iterable<? extends ELEMENT> actual) {
return new CollectionAssert<>(actual);
}
}

View File

@@ -0,0 +1,66 @@
package org.springframework.cloud.contract.verifier.assertion;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Test;
/**
* @author Marcin Grzejszczak
*/
public class CollectionAssertTests {
@Test
public void should_not_throw_an_exception_when_all_elements_match_regex() {
Collection collection = collection();
SpringCloudContractAssertions.assertThat(collection).allElementsMatch("[a-z]");
}
@Test
public void should_throw_an_exception_when_at_least_one_element_doesnt_match_regex() {
Collection collection = collection();
try {
SpringCloudContractAssertions.assertThat(collection).allElementsMatch("[0-9]");
Assertions.fail("should throw exception");
} catch (AssertionError e) {
Assertions.assertThat(e).hasMessageContaining("The value <a> doesn't match the regex <[0-9]>");
}
}
@Test
public void should_throw_an_exception_when_collection_is_null() {
Collection collection = null;
try {
SpringCloudContractAssertions.assertThat(collection).allElementsMatch("foo");
Assertions.fail("should throw exception");
} catch (AssertionError e) {
Assertions.assertThat(e).hasMessageContaining("Expecting actual not to be null");
}
}
@Test
public void should_throw_an_exception_when_collection_is_empty() {
Collection collection = new ArrayList();
try {
SpringCloudContractAssertions.assertThat(collection).allElementsMatch("foo");
Assertions.fail("should throw exception");
} catch (AssertionError e) {
Assertions.assertThat(e).hasMessageContaining("Expecting actual not to be empty");
}
}
private Collection<String> collection() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
return list;
}
}

View File

@@ -222,11 +222,7 @@ class MockMvcMethodBodyBuilderWithMatchersSpec extends Specification implements
builder.appendTo(blockBuilder)
def test = blockBuilder.toString()
then:
test.contains('assertThat(parsedJson.read("' + rootElement + '.phoneNumbers[*].number", java.util.Collection.class)).as("All elements match regex").are(')
test.contains('new org.assertj.core.api.Condition<Object>() {')
test.contains('@Override public boolean matches(Object o) {')
test.contains('return ((String)o).matches("^[0-9]{3} [0-9]{3}-[0-9]{4}' + rootElement + '")')
test.contains('assertThat(parsedJson.read("' + rootElement + '..number", String.class)).matches("^[0-9]{3} [0-9]{3}-[0-9]{4}' + rootElement + '")')
test.contains('assertThat(parsedJson.read("' + rootElement + '.phoneNumbers[*].number", java.util.Collection.class)).allElementsMatch("^[0-9]{3} [0-9]{3}-[0-9]{4}' + rootElement + '")')
!test.contains('cursor')
and:
try {

View File

@@ -40,7 +40,7 @@ class SyntaxChecker {
"javax.ws.rs.client.Entity.*",
"org.springframework.cloud.contract.verifier.messaging.util.ContractVerifierMessagingUtil.headers",
"com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson",
"org.assertj.core.api.Assertions.assertThat"
"org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat"
].collect { "import static ${it};"}.join("\n")
static void tryToCompile(String builderName, String test) {