From 9450bb3a734632fbab90d0451bcfebf20116c2cf Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Sat, 28 Sep 2019 10:28:35 +0200 Subject: [PATCH 1/2] Fix flattened size counter. Fixes gh-1108. (#1229) Signed-off-by: Olga Maciaszek-Sharma --- .../verifier/assertion/CollectionAssert.java | 8 ++--- .../assertion/CollectionAssertTests.java | 29 +++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java index aff89ab5be..9b7f09839c 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/CollectionAssert.java @@ -172,7 +172,7 @@ public class CollectionAssert extends IterableAssert { if (object instanceof Map) { return counter + ((Map) object).size(); } - else if (object instanceof Iterator) { + if (object instanceof Iterator) { Iterator iterator = ((Iterator) object); while (iterator.hasNext()) { Object next = iterator.next(); @@ -180,10 +180,10 @@ public class CollectionAssert extends IterableAssert { } return counter; } - else if (object instanceof Collection) { - return flattenedSize(counter, ((Collection) object).iterator()); + if (object instanceof Iterable) { + return flattenedSize(counter, ((Iterable) object).iterator()); } - return counter; + return ++counter; } private int size(Iterable iterable) { diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java index f233eeb52d..beadae3f63 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/assertion/CollectionAssertTests.java @@ -17,6 +17,7 @@ package org.springframework.cloud.contract.verifier.assertion; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -101,7 +102,7 @@ public class CollectionAssertTests { SpringCloudContractAssertions.assertThat(collection) .hasFlattenedSizeGreaterThanOrEqualTo(0) - .hasFlattenedSizeGreaterThanOrEqualTo(4); + .hasFlattenedSizeGreaterThanOrEqualTo(7); } @Test @@ -110,12 +111,12 @@ public class CollectionAssertTests { try { SpringCloudContractAssertions.assertThat(collection) - .hasFlattenedSizeGreaterThanOrEqualTo(5); + .hasFlattenedSizeGreaterThanOrEqualTo(8); Assertions.fail("should throw exception"); } catch (AssertionError e) { Assertions.assertThat(e).hasMessageContaining( - "The flattened size <4> is not greater or equal to <5>"); + "The flattened size <7> is not greater or equal to <8>"); } } @@ -139,8 +140,8 @@ public class CollectionAssertTests { Collection collection = nestedCollection(); SpringCloudContractAssertions.assertThat(collection) - .hasFlattenedSizeLessThanOrEqualTo(5) - .hasFlattenedSizeLessThanOrEqualTo(4); + .hasFlattenedSizeLessThanOrEqualTo(8) + .hasFlattenedSizeLessThanOrEqualTo(7); } @Test @@ -154,7 +155,7 @@ public class CollectionAssertTests { } catch (AssertionError e) { Assertions.assertThat(e).hasMessageContaining( - "The flattened size <4> is not less or equal to <1>"); + "The flattened size <7> is not less or equal to <1>"); } } @@ -177,8 +178,8 @@ public class CollectionAssertTests { public void should_not_throw_an_exception_when_flattened_size_is_between_the_provided_sizes() { Collection collection = nestedCollection(); - SpringCloudContractAssertions.assertThat(collection).hasFlattenedSizeBetween(1, 5) - .hasFlattenedSizeBetween(4, 4); + SpringCloudContractAssertions.assertThat(collection).hasFlattenedSizeBetween(1, 8) + .hasFlattenedSizeBetween(7, 7); } @Test @@ -187,12 +188,12 @@ public class CollectionAssertTests { try { SpringCloudContractAssertions.assertThat(collection) - .hasFlattenedSizeBetween(5, 7); + .hasFlattenedSizeBetween(8, 9); Assertions.fail("should throw exception"); } catch (AssertionError e) { Assertions.assertThat(e).hasMessageContaining( - "The flattened size <4> is not between <5> and <7>"); + "The flattened size <7> is not between <8> and <9>"); } } @@ -353,8 +354,8 @@ public class CollectionAssertTests { } private Collection nestedCollection() { - List list = new ArrayList<>(); - List list1 = new ArrayList<>(); + List list = new ArrayList(); + List list1 = new ArrayList(); Map map1 = new HashMap<>(); map1.put("a", "1"); map1.put("b", "2"); @@ -362,10 +363,14 @@ public class CollectionAssertTests { List list2 = new ArrayList<>(); Map map2 = new HashMap<>(); map2.put("d", "4"); + List list3 = new ArrayList(); + List innerList = Arrays.asList("A", "B", "C"); list.add(list1); list.add(list2); + list.add(list3); list1.add(map1); list2.add(map2); + list3.add(innerList); return list; } From a2845713a19db7c3fb7f0e86ab50c1bc84eb057d Mon Sep 17 00:00:00 2001 From: Karol Lipiec Date: Wed, 2 Oct 2019 14:03:54 +0200 Subject: [PATCH 2/2] Added support of query parameters for RestDocs for WireMock stubs (#1237) * Added support of query parameters for RestDocs for Stubs fixes #112 --- .../wiremock/restdocs/WireMockSnippet.java | 4 +- .../restdocs/WireMockSnippetTests.java | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippet.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippet.java index 6b76ce84c8..be3fe93029 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippet.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippet.java @@ -57,7 +57,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.patch; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.put; import static com.github.tomakehurst.wiremock.client.WireMock.trace; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; /** * Represents a snippet for a WireMock stub. @@ -236,7 +236,7 @@ public class WireMockSnippet implements Snippet { } private UrlPattern requestPattern(Operation operation) { - return urlEqualTo(operation.getRequest().getUri().getPath()); + return urlPathEqualTo(operation.getRequest().getUri().getPath()); } private HttpHeaders responseHeaders(Operation operation) { diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java index 88437954e9..af40a141aa 100644 --- a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/restdocs/WireMockSnippetTests.java @@ -28,7 +28,9 @@ import java.util.Map; import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern; import com.github.tomakehurst.wiremock.matching.EqualToXmlPattern; +import com.github.tomakehurst.wiremock.matching.MultiValuePattern; import com.github.tomakehurst.wiremock.stubbing.StubMapping; +import org.assertj.core.api.Assertions; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -48,6 +50,7 @@ import org.springframework.restdocs.operation.OperationResponse; import org.springframework.restdocs.operation.Parameters; import org.springframework.restdocs.operation.RequestCookie; +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static org.assertj.core.api.Assertions.assertThat; /** @@ -172,6 +175,24 @@ public class WireMockSnippetTests { WireMockStubMapping.buildFrom(new String(Files.readAllBytes(stub.toPath()))); } + @Test + public void should_accept_query_params() throws IOException { + this.operation = operation(requestGetWithQueryParam(), response(), + this.context); + WireMockSnippet snippet = new WireMockSnippet(); + + snippet.document(this.operation); + + File stub = new File(this.outputFolder, "stubs/foo.json"); + assertThat(stub).exists(); + StubMapping stubMapping = WireMockStubMapping + .buildFrom(new String(Files.readAllBytes(stub.toPath()))); + assertThat(stubMapping.getRequest().getUrlPath()).isEqualTo("/bar"); + assertThat(stubMapping.getRequest() + .getQueryParameters()) + .containsOnly(Assertions.entry("myParam", MultiValuePattern.of(equalTo(("myValue"))))); + } + private Operation operation(OperationRequest request, OperationResponse response, RestDocumentationContext context) { return operation("foo", request, response, context); @@ -468,4 +489,51 @@ public class WireMockSnippetTests { }; } + private OperationRequest requestGetWithQueryParam() { + return new OperationRequest() { + @Override + public byte[] getContent() { + return new byte[0]; + } + + @Override + public String getContentAsString() { + return ""; + } + + @Override + public HttpHeaders getHeaders() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add(HttpHeaders.CONTENT_TYPE, + MediaType.APPLICATION_JSON_VALUE); + return httpHeaders; + } + + @Override + public HttpMethod getMethod() { + return HttpMethod.GET; + } + + @Override + public Parameters getParameters() { + return null; + } + + @Override + public Collection getParts() { + return null; + } + + @Override + public URI getUri() { + return URI.create("https://foo/bar?myParam=myValue"); + } + + @Override + public Collection getCookies() { + return Collections.emptySet(); + } + }; + } + }