From c2b19b397d2c9ba785d76207c4fec6512524d84e Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 7 Oct 2021 16:17:37 +0200 Subject: [PATCH] Changed the backward incompatible change --- .../verifier/assertion/CollectionAssert.java | 18 +- .../assertion/ContractCollectionAssert.java | 122 +++++++++++ .../assertion/ContractIterableAssert.java | 198 ------------------ .../SpringCloudContractAssertions.java | 8 +- 4 files changed, 141 insertions(+), 205 deletions(-) create mode 100644 spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractCollectionAssert.java delete mode 100644 spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractIterableAssert.java 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 dd6c1081ba..0523f15ab7 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 @@ -16,10 +16,14 @@ package org.springframework.cloud.contract.verifier.assertion; -import java.util.Collection; import java.util.Iterator; import java.util.Map; +import org.assertj.core.api.IterableAssert; +import org.assertj.core.util.Streams; + +import static java.util.stream.Collectors.toList; + /** * Extension to {@link Iterable} assertions. * @@ -27,12 +31,20 @@ import java.util.Map; * @author Marcin Grzejszczak * @since 1.1.0 */ -public class CollectionAssert extends org.assertj.core.api.CollectionAssert { +public class CollectionAssert extends IterableAssert { - public CollectionAssert(Collection actual) { + public CollectionAssert(Iterable actual) { super(actual); } + public CollectionAssert(Iterator actual) { + super(toIterable(actual)); + } + + private static Iterable toIterable(Iterator iterator) { + return Streams.stream(iterator).collect(toList()); + } + /** * Asserts all elements of the collection whether they match a regular expression. * @param regex - regular expression to check against diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractCollectionAssert.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractCollectionAssert.java new file mode 100644 index 0000000000..566212034c --- /dev/null +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractCollectionAssert.java @@ -0,0 +1,122 @@ +/* + * Copyright 2013-2020 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.assertion; + +import java.util.Collection; + +/** + * Extension to {@link Iterable} assertions. + * + * @param type to assert + * @author Marcin Grzejszczak + * @since 3.1.0 + */ +public class ContractCollectionAssert extends org.assertj.core.api.CollectionAssert { + + private final CollectionAssert collectionAssert; + + public ContractCollectionAssert(Collection actual) { + super(actual); + this.collectionAssert = new CollectionAssert<>(actual); + } + + /** + * Asserts all elements of the collection whether they match a regular expression. + * @param regex - regular expression to check against + * @return this + */ + public ContractCollectionAssert allElementsMatch(String regex) { + this.collectionAssert.allElementsMatch(regex); + return this; + } + + /** + * Flattens the collection and checks whether size is greater than or equal to the + * provided value. + * @param size - the flattened collection should have size greater than or equal to + * this value + * @return this + */ + public ContractCollectionAssert hasFlattenedSizeGreaterThanOrEqualTo(int size) { + this.collectionAssert.hasFlattenedSizeGreaterThanOrEqualTo(size); + return this; + } + + /** + * Flattens the collection and checks whether size is less than or equal to the + * provided value. + * @param size - the flattened collection should have size less than or equal to this + * value + * @return this + */ + public ContractCollectionAssert hasFlattenedSizeLessThanOrEqualTo(int size) { + this.collectionAssert.hasFlattenedSizeLessThanOrEqualTo(size); + return this; + } + + /** + * Flattens the collection and checks whether size is between the provided value. + * @param lowerBound - the flattened collection should have size greater than or equal + * to this value + * @param higherBound - the flattened collection should have size less than or equal + * to this value + * @return this + */ + public ContractCollectionAssert hasFlattenedSizeBetween(int lowerBound, int higherBound) { + this.collectionAssert.hasFlattenedSizeBetween(lowerBound, higherBound); + return this; + } + + /** + * Checks whether size is greater than or equal to the provided value. + * @param size - the collection should have size greater than or equal to this value + * @return this + */ + public ContractCollectionAssert hasSizeGreaterThanOrEqualTo(int size) { + this.collectionAssert.hasSizeGreaterThanOrEqualTo(size); + return this; + } + + /** + * Checks whether size is less than or equal to the provided value. + * @param size - the collection should have size less than or equal to this value + * @return this + */ + public ContractCollectionAssert hasSizeLessThanOrEqualTo(int size) { + this.collectionAssert.hasSizeLessThanOrEqualTo(size); + return this; + } + + /** + * Checks whether size is between the provided value. + * @param lowerBound - the collection should have size greater than or equal to this + * value + * @param higherBound - the collection should have size less than or equal to this + * value + * @return this + */ + public ContractCollectionAssert hasSizeBetween(int lowerBound, int higherBound) { + this.collectionAssert.hasSizeBetween(lowerBound, higherBound); + return this; + } + + @Override + public ContractCollectionAssert as(String description, Object... args) { + return (ContractCollectionAssert) super.as(description, args); + } + +} diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractIterableAssert.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractIterableAssert.java deleted file mode 100644 index 403a567435..0000000000 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/ContractIterableAssert.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright 2013-2020 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.assertion; - -import java.util.Iterator; -import java.util.Map; - -import org.assertj.core.api.IterableAssert; -import org.assertj.core.util.Streams; - -import static java.util.stream.Collectors.toList; - -/** - * Extension to {@link Iterable} assertions. - * - * @param type to assert - * @author Marcin Grzejszczak - * @since 3.1.0 - */ -public class ContractIterableAssert extends IterableAssert { - - public ContractIterableAssert(Iterable actual) { - super(actual); - } - - public ContractIterableAssert(Iterator actual) { - super(toIterable(actual)); - } - - private static Iterable toIterable(Iterator iterator) { - return Streams.stream(iterator).collect(toList()); - } - - /** - * Asserts all elements of the collection whether they match a regular expression. - * @param regex - regular expression to check against - * @return this - */ - public ContractIterableAssert allElementsMatch(String regex) { - isNotNull(); - isNotEmpty(); - for (Object anActual : this.actual) { - if (anActual == null) { - failWithMessageRelatedToRegex(regex, anActual); - } - String value = anActual.toString(); - if (!value.matches(regex)) { - failWithMessageRelatedToRegex(regex, value); - } - } - return this; - } - - private void failWithMessageRelatedToRegex(String regex, Object value) { - failWithMessage("The value <%s> doesn't match the regex <%s>", value, regex); - } - - /** - * Flattens the collection and checks whether size is greater than or equal to the - * provided value. - * @param size - the flattened collection should have size greater than or equal to - * this value - * @return this - */ - public ContractIterableAssert hasFlattenedSizeGreaterThanOrEqualTo(int size) { - isNotNull(); - int flattenedSize = flattenedSize(0, this.actual); - if (!(flattenedSize >= size)) { - failWithMessage("The flattened size <%s> is not greater or equal to <%s>", flattenedSize, size); - } - return this; - } - - /** - * Flattens the collection and checks whether size is less than or equal to the - * provided value. - * @param size - the flattened collection should have size less than or equal to this - * value - * @return this - */ - public ContractIterableAssert hasFlattenedSizeLessThanOrEqualTo(int size) { - isNotNull(); - int flattenedSize = flattenedSize(0, this.actual); - if (!(flattenedSize <= size)) { - failWithMessage("The flattened size <%s> is not less or equal to <%s>", flattenedSize, size); - } - return this; - } - - /** - * Flattens the collection and checks whether size is between the provided value. - * @param lowerBound - the flattened collection should have size greater than or equal - * to this value - * @param higherBound - the flattened collection should have size less than or equal - * to this value - * @return this - */ - public ContractIterableAssert hasFlattenedSizeBetween(int lowerBound, int higherBound) { - isNotNull(); - int flattenedSize = flattenedSize(0, this.actual); - if (!(flattenedSize >= lowerBound && flattenedSize <= higherBound)) { - failWithMessage("The flattened size <%s> is not between <%s> and <%s>", flattenedSize, lowerBound, - higherBound); - } - return this; - } - - /** - * Checks whether size is greater than or equal to the provided value. - * @param size - the collection should have size greater than or equal to this value - * @return this - */ - public ContractIterableAssert hasSizeGreaterThanOrEqualTo(int size) { - isNotNull(); - int actualSize = size(this.actual); - if (!(actualSize >= size)) { - failWithMessage("The size <%s> is not greater or equal to <%s>", actualSize, size); - } - return this; - } - - /** - * Checks whether size is less than or equal to the provided value. - * @param size - the collection should have size less than or equal to this value - * @return this - */ - public ContractIterableAssert hasSizeLessThanOrEqualTo(int size) { - isNotNull(); - int actualSize = size(this.actual); - if (!(actualSize <= size)) { - failWithMessage("The size <%s> is not less or equal to <%s>", actualSize, size); - } - return this; - } - - /** - * Checks whether size is between the provided value. - * @param lowerBound - the collection should have size greater than or equal to this - * value - * @param higherBound - the collection should have size less than or equal to this - * value - * @return this - */ - public ContractIterableAssert hasSizeBetween(int lowerBound, int higherBound) { - isNotNull(); - int size = size(this.actual); - if (!(size >= lowerBound && size <= higherBound)) { - failWithMessage("The size <%s> is not between <%s> and <%s>", size, lowerBound, higherBound); - } - return this; - } - - @Override - public ContractIterableAssert as(String description, Object... args) { - return (ContractIterableAssert) super.as(description, args); - } - - private int flattenedSize(int counter, Object object) { - if (object instanceof Map) { - return counter + ((Map) object).size(); - } - if (object instanceof Iterator) { - Iterator iterator = ((Iterator) object); - while (iterator.hasNext()) { - Object next = iterator.next(); - counter = flattenedSize(counter, next); - } - return counter; - } - if (object instanceof Iterable) { - return flattenedSize(counter, ((Iterable) object).iterator()); - } - return ++counter; - } - - private int size(Iterable iterable) { - int size = 0; - for (Object value : iterable) { - size++; - } - return size; - } - -} diff --git a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/SpringCloudContractAssertions.java b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/SpringCloudContractAssertions.java index d8f459e9bf..2bdcc8a5ce 100644 --- a/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/SpringCloudContractAssertions.java +++ b/spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/assertion/SpringCloudContractAssertions.java @@ -28,18 +28,18 @@ public class SpringCloudContractAssertions extends Assertions { * @param actual the actual value. * @return the created assertion object. */ - public static CollectionAssert assertThat(Collection actual) { + public static CollectionAssert assertThat(Iterable actual) { return new CollectionAssert<>(actual); } /** - * Creates a new instance of {@link CollectionAssert}. + * Creates a new instance of {@link ContractCollectionAssert}. * @param type to assert * @param actual the actual value. * @return the created assertion object. */ - public static ContractIterableAssert assertThat(Iterable actual) { - return new ContractIterableAssert<>(actual); + public static ContractCollectionAssert assertThat(Collection actual) { + return new ContractCollectionAssert<>(actual); } }