From 285ddd6e52f773cfbedaa93ace042acb0a107beb Mon Sep 17 00:00:00 2001 From: Julien Roy Date: Mon, 22 Feb 2021 21:14:31 +0100 Subject: [PATCH] Implement hasValuesSatisfying on changes and table rows (#110) --- .../db/api/AbstractAssertWithValues.java | 2 +- .../org/assertj/db/api/AbstractRowAssert.java | 16 +- .../assertj/db/api/AbstractValueAssert.java | 2 +- .../org/assertj/db/api/ChangeRowAssert.java | 13 +- .../api/assertions/AssertOnRowCondition.java | 53 +++++ .../impl/AssertionsOnRowCondition.java | 91 +++++++++ .../impl/AssertionsOnRowEquality.java | 5 +- .../impl/AssertionsOnValueCondition.java | 17 ++ .../org/assertj/db/error/ShouldSatisfy.java | 37 ++++ .../db/navigation/PositionWithChanges.java | 7 +- ...RowCondition_HasValuesSatisfying_Test.java | 132 ++++++++++++ ...AssertOnValueCondition_Satisfies_Test.java | 6 +- ...owCondition_HasValues_Satisfying_Test.java | 189 ++++++++++++++++++ 13 files changed, 555 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/assertj/db/api/assertions/AssertOnRowCondition.java create mode 100644 src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition.java create mode 100644 src/main/java/org/assertj/db/error/ShouldSatisfy.java create mode 100644 src/test/java/org/assertj/db/api/assertions/AssertOnRowCondition_HasValuesSatisfying_Test.java create mode 100644 src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition_HasValues_Satisfying_Test.java diff --git a/src/main/java/org/assertj/db/api/AbstractAssertWithValues.java b/src/main/java/org/assertj/db/api/AbstractAssertWithValues.java index b713df1a..1ed1df2e 100644 --- a/src/main/java/org/assertj/db/api/AbstractAssertWithValues.java +++ b/src/main/java/org/assertj/db/api/AbstractAssertWithValues.java @@ -590,6 +590,6 @@ public E doesNotHave(Condition condition) { /** {@inheritDoc} */ @Override public E satisfies(Condition condition) { - return AssertionsOnValueCondition.is(myself, info, value, condition); + return AssertionsOnValueCondition.satisfies(myself, info, value, condition); } } diff --git a/src/main/java/org/assertj/db/api/AbstractRowAssert.java b/src/main/java/org/assertj/db/api/AbstractRowAssert.java index d58b56e8..3c149ec2 100644 --- a/src/main/java/org/assertj/db/api/AbstractRowAssert.java +++ b/src/main/java/org/assertj/db/api/AbstractRowAssert.java @@ -13,8 +13,10 @@ package org.assertj.db.api; import org.assertj.db.api.assertions.AssertOnNumberOfColumns; +import org.assertj.db.api.assertions.AssertOnRowCondition; import org.assertj.db.api.assertions.AssertOnRowEquality; import org.assertj.db.api.assertions.AssertOnRowNullity; +import org.assertj.db.api.assertions.impl.AssertionsOnRowCondition; import org.assertj.db.api.assertions.impl.AssertionsOnValuesNullity; import org.assertj.db.api.assertions.impl.AssertionsOnNumberOfColumns; import org.assertj.db.api.assertions.impl.AssertionsOnRowEquality; @@ -34,7 +36,8 @@ * Base class for all {@link Row}s assertions. * * @author Régis Pouiller - * + * @author Julien Roy + * * @param The class of the actual value (an sub-class of {@link AbstractDbData}). * @param The class of the original assertion (an sub-class of {@link AbstractDbAssert}). * @param The class of the equivalent column assertion (an sub-class of {@link AbstractColumnAssert}). @@ -49,7 +52,8 @@ public abstract class AbstractRowAssert, A extends A ToValueFromRow, AssertOnRowEquality, AssertOnNumberOfColumns, - AssertOnRowNullity { + AssertOnRowNullity, + AssertOnRowCondition { /** * Position of navigation to value. @@ -63,7 +67,7 @@ public abstract class AbstractRowAssert, A extends A /** * Constructor. - * + * * @param originalDbAssert The original assert. That could be a {@link RequestAssert} or a {@link TableAssert}. * @param selfType Type of this assertion class : a sub-class of {@code AbstractRowAssert}. * @param valueType Class of the assert on the value : a sub-class of {@code AbstractRowValueAssert}. @@ -146,4 +150,10 @@ public R hasValues(Object... expected) { public R hasOnlyNotNullValues() { return AssertionsOnValuesNullity.hasOnlyNotNullValues(myself, info, getValuesList()); } + + /** {@inheritDoc} */ + @Override + public R hasValuesSatisfying(Object... expected) { + return AssertionsOnRowCondition.hasValuesSatisfying(myself, info, getValuesList(), expected); + } } diff --git a/src/main/java/org/assertj/db/api/AbstractValueAssert.java b/src/main/java/org/assertj/db/api/AbstractValueAssert.java index 9c68c5fb..4ab634d6 100644 --- a/src/main/java/org/assertj/db/api/AbstractValueAssert.java +++ b/src/main/java/org/assertj/db/api/AbstractValueAssert.java @@ -608,6 +608,6 @@ public V doesNotHave(Condition condition) { /** {@inheritDoc} */ @Override public V satisfies(Condition condition) { - return AssertionsOnValueCondition.is(myself, info, value, condition); + return AssertionsOnValueCondition.satisfies(myself, info, value, condition); } } diff --git a/src/main/java/org/assertj/db/api/ChangeRowAssert.java b/src/main/java/org/assertj/db/api/ChangeRowAssert.java index 302cd4a9..63c595b7 100644 --- a/src/main/java/org/assertj/db/api/ChangeRowAssert.java +++ b/src/main/java/org/assertj/db/api/ChangeRowAssert.java @@ -13,9 +13,11 @@ package org.assertj.db.api; import org.assertj.db.api.assertions.AssertOnNumberOfColumns; +import org.assertj.db.api.assertions.AssertOnRowCondition; import org.assertj.db.api.assertions.AssertOnRowEquality; import org.assertj.db.api.assertions.AssertOnRowOfChangeExistence; import org.assertj.db.api.assertions.impl.AssertionsOnNumberOfColumns; +import org.assertj.db.api.assertions.impl.AssertionsOnRowCondition; import org.assertj.db.api.assertions.impl.AssertionsOnRowEquality; import org.assertj.db.api.assertions.impl.AssertionsOnRowOfChangeExistence; import org.assertj.db.exception.AssertJDBException; @@ -33,6 +35,7 @@ * Assertion methods for a {@code Row} of a {@code Change}. * * @author Régis Pouiller + * @author Julien Roy */ public class ChangeRowAssert extends AbstractAssertWithOriginWithColumnsAndRowsFromChange @@ -40,7 +43,8 @@ public class ChangeRowAssert OriginWithValuesFromRow, AssertOnRowEquality, AssertOnNumberOfColumns, - AssertOnRowOfChangeExistence { + AssertOnRowOfChangeExistence, + AssertOnRowCondition { /** * Position of navigation to value. @@ -163,6 +167,13 @@ public ChangeRowAssert doesNotExist() { return AssertionsOnRowOfChangeExistence.doesNotExist(myself, info, row); } + /** {@inheritDoc} */ + @Override + public ChangeRowAssert hasValuesSatisfying(Object... expected) { + exists(); + return AssertionsOnRowCondition.hasValuesSatisfying(myself, info, row.getValuesList(), expected); + } + /** * Returns to level of assertion methods on a {@link org.assertj.db.type.Change}. * diff --git a/src/main/java/org/assertj/db/api/assertions/AssertOnRowCondition.java b/src/main/java/org/assertj/db/api/assertions/AssertOnRowCondition.java new file mode 100644 index 00000000..4d70d691 --- /dev/null +++ b/src/main/java/org/assertj/db/api/assertions/AssertOnRowCondition.java @@ -0,0 +1,53 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2015-2021 the original author or authors. + */ +package org.assertj.db.api.assertions; + +import org.assertj.core.api.Condition; + +/** + * Defines the assertion method on the a row satisfy conditions. + * + * @param The "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" + * for more details. + * @author Julien Roy + */ +public interface AssertOnRowCondition> { + + /** + * Verifies that the values of a row satisfy to conditions in parameter. + *

+ * Example where the assertion verifies that the values in the first {@code Row} of the {@code Table} satisfy to the + * conditions in parameter : + *

+ * + *

+   * assertThat(table).row().hasValuesSatisfying(new Condition(v -> v.equals("Weaver"), "isWeaver"));
+   * 
+ *

+ * Example where the assertion verifies that the values of the row at end point of the first change are equal to the + * values in parameter : + *

+ * + *

+   * assertThat(changes).change().rowAtEndPoint().hasValuesSatisfying(new Condition(v -> v.equals("Weaver"), "isWeaver"));
+   * 
+ * + * @param expected The expected conditions. + * @return {@code this} assertion object. + * @throws AssertionError If the values of the row are not satisfy to the conditions in parameters. + * @see org.assertj.db.api.AbstractRowAssert#hasValuesSatisfying(Condition) + * @see org.assertj.db.api.ChangeRowAssert#hasValuesSatisfying(Condition) + */ + T hasValuesSatisfying(Object... expected); +} diff --git a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition.java b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition.java new file mode 100644 index 00000000..b8af7d5d --- /dev/null +++ b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition.java @@ -0,0 +1,91 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2015-2021 the original author or authors. + */ +package org.assertj.db.api.assertions.impl; + +import org.assertj.core.api.Condition; +import org.assertj.core.api.WritableAssertionInfo; +import org.assertj.core.internal.Failures; +import org.assertj.db.api.AbstractAssert; +import org.assertj.db.type.Value; +import org.assertj.db.type.ValueType; +import org.assertj.db.util.Values; + +import java.util.List; + +import static org.assertj.db.error.ShouldBeCompatible.shouldBeCompatible; +import static org.assertj.db.error.ShouldBeEqual.shouldBeEqual; +import static org.assertj.db.error.ShouldHaveColumnsSize.shouldHaveColumnsSize; +import static org.assertj.db.error.ShouldSatisfy.shouldSatisfy; +import static org.assertj.db.util.Values.areEqual; + +/** + * Implements the assertion method on the matching with condition of a row. + * + * @author Julien Roy + * + * @see org.assertj.db.api.assertions.AssertOnRowCondition + */ +public class AssertionsOnRowCondition { + + /** + * To notice failures in the assertion. + */ + private static final Failures failures = Failures.instance(); + + /** + * Private constructor. + */ + private AssertionsOnRowCondition() { + // Empty + } + + public static > A hasValuesSatisfying(A assertion, WritableAssertionInfo info, + List valuesList, Object... expected) { + + if (valuesList.size() != expected.length) { + throw failures.failure(info, shouldHaveColumnsSize(valuesList.size(), expected.length)); + } + + int index = 0; + for (Value value : valuesList) { + Object object = expected[index]; + + if (object instanceof Condition) { + Condition condition = (Condition) object; + if (!condition.matches(value.getValue())) { + Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object); + throw failures.failure(info, shouldSatisfy(index, actual, condition)); + } + index++; + continue; + } + + if (!value.isComparisonPossible(object)) { + throw failures.failure(info, shouldBeCompatible(value, object)); + } + + if (!areEqual(value, object)) { + if (value.getValueType() == ValueType.BYTES) { + throw failures.failure(info, shouldBeEqual(index)); + } else { + Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object); + throw failures.failure(info, shouldBeEqual(index, actual, object)); + } + } + + index++; + } + + return assertion; + } +} diff --git a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowEquality.java b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowEquality.java index 582eecc4..b24e7ade 100644 --- a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowEquality.java +++ b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowEquality.java @@ -69,9 +69,8 @@ public static > A hasValues(A assertion, WritableAss if (value.getValueType() == ValueType.BYTES) { throw failures.failure(info, shouldBeEqual(index)); } else { - throw failures.failure(info, shouldBeEqual(index, Values.getRepresentationFromValueInFrontOfExpected(value, - expected[index]), - expected[index])); + Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, expected[index]); + throw failures.failure(info, shouldBeEqual(index, actual, expected[index])); } } index++; diff --git a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java index 66ed4c3a..03e27dab 100644 --- a/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java +++ b/src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java @@ -71,4 +71,21 @@ public static > A isNot(A assertion, WritableAsserti return assertion; } + /** + * Verifies that the value satisfies with condition. + * + * @param The type of the assertion which call this method. + * @param assertion The assertion which call this method. + * @param info Writable information about an assertion. + * @param value The value. + * @param condition The condition to use for validation. + * @return {@code this} assertion object. + * @throws AssertionError If the value is not equal to the number in parameter. + */ + @SuppressWarnings("unchecked") + public static > A satisfies(A assertion, WritableAssertionInfo info, Value value, Condition condition) { + conditions.assertSatisfies(info, value.getValue(), (Condition) condition); + return assertion; + } + } diff --git a/src/main/java/org/assertj/db/error/ShouldSatisfy.java b/src/main/java/org/assertj/db/error/ShouldSatisfy.java new file mode 100644 index 00000000..49219ceb --- /dev/null +++ b/src/main/java/org/assertj/db/error/ShouldSatisfy.java @@ -0,0 +1,37 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2015-2021 the original author or authors. + */ +package org.assertj.db.error; + +import org.assertj.core.api.Condition; +import org.assertj.core.error.BasicErrorMessageFactory; +import org.assertj.core.error.ErrorMessageFactory; + +/** + * Creates an error message indicating that an assertion that verifies that a value does not satisfying condition. + * + * @author Julien Roy + * + */ +public class ShouldSatisfy extends BasicErrorMessageFactory { + + private static final String EXPECTED_MESSAGE_WITH_INDEX = "%nExpecting that the value at index %s:%n %s%nto satisfy: %n %s"; + + public static ErrorMessageFactory shouldSatisfy(int index, Object actual, Condition condition) { + return new ShouldSatisfy(index, actual, condition); + } + + private ShouldSatisfy(int index, Object actual, Condition condition) { + super(EXPECTED_MESSAGE_WITH_INDEX, index, actual, condition); + } + +} diff --git a/src/main/java/org/assertj/db/navigation/PositionWithChanges.java b/src/main/java/org/assertj/db/navigation/PositionWithChanges.java index e49c5de3..7252e62d 100644 --- a/src/main/java/org/assertj/db/navigation/PositionWithChanges.java +++ b/src/main/java/org/assertj/db/navigation/PositionWithChanges.java @@ -125,7 +125,7 @@ public E getChangesInstance(Changes changes, ChangeType changeType, String table } try { - Class clazz = unProxy(myself.getClass()); + Class clazz = unProxy(myself.getClass()); Constructor constructor = actualElementClass.getDeclaredConstructor(clazz, Changes.class); instance = constructor.newInstance(myself, nextChanges); instance.as(getChangesDescription(changeType, tableName)); @@ -200,7 +200,7 @@ public N getChangeInstance(Changes changes, ChangeType changeType, String tableN } try { - Class clazz = unProxy(myself.getClass()); + Class clazz = unProxy(myself.getClass()); Constructor constructor = nextElementClass.getDeclaredConstructor(clazz, Change.class); instance = constructor.newInstance(myself, change); instance.as(getChangeDescription(changes, change, index, changeType, tableName)); @@ -248,8 +248,7 @@ public N getChangeInstanceWithPK(Changes changes, String tableName, Object... pk } index++; } - throw new AssertJDBException("No change found for table " + tableName + " and primary keys " + Arrays - .asList(pksValues)); + throw new AssertJDBException("No change found for table " + tableName + " and primary keys " + Arrays.asList(pksValues)); } /** diff --git a/src/test/java/org/assertj/db/api/assertions/AssertOnRowCondition_HasValuesSatisfying_Test.java b/src/test/java/org/assertj/db/api/assertions/AssertOnRowCondition_HasValuesSatisfying_Test.java new file mode 100644 index 00000000..288c9166 --- /dev/null +++ b/src/test/java/org/assertj/db/api/assertions/AssertOnRowCondition_HasValuesSatisfying_Test.java @@ -0,0 +1,132 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2015-2021 the original author or authors. + */ +package org.assertj.db.api.assertions; + +import org.assertj.core.api.Assertions; +import org.assertj.core.api.Condition; +import org.assertj.core.api.HamcrestCondition; +import org.assertj.db.api.ChangeRowAssert; +import org.assertj.db.api.TableRowAssert; +import org.assertj.db.common.AbstractTest; +import org.assertj.db.common.NeedReload; +import org.assertj.db.type.Changes; +import org.assertj.db.type.Table; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +import java.util.UUID; + +import static org.assertj.db.api.Assertions.assertThat; + +/** + * Tests on {@link AssertOnRowCondition} class : + * {@link AssertOnRowCondition#hasValuesSatisfying(Object...)} method. + * + * @author Julien Roy + * + */ +public class AssertOnRowCondition_HasValuesSatisfying_Test extends AbstractTest { + + /** + * This method tests the {@code hasValuesSatisfying} assertion method. + */ + @Test + @NeedReload + public void test_has_values() { + Table table = new Table(source, "actor"); + Changes changes = new Changes(table).setStartPointNow(); + updateChangesForTests(); + changes.setEndPointNow(); + + ChangeRowAssert changeRowAssert = assertThat(changes).change().rowAtEndPoint(); + ChangeRowAssert changeRowAssert2 = changeRowAssert + .hasValuesSatisfying( + 4, + new Condition(v -> v.equals("Murray"), "isMurray"), + new HamcrestCondition<>(CoreMatchers.is("Bill")), + "1950-09-21", + "30B443AE-C0C9-4790-9BEC-CE1380808435" + ) + .hasValuesSatisfying( + 4, + new Condition(v -> v.equals("Murray"), "isMurray"), + new HamcrestCondition<>(CoreMatchers.is("Bill")), + "1950-09-21", + UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435") + ); + Assertions.assertThat(changeRowAssert).isSameAs(changeRowAssert2); + + TableRowAssert tableRowAssert = assertThat(table).row(); + TableRowAssert tableRowAssert2 = tableRowAssert + .hasValuesSatisfying( + 1, + new Condition(v -> v.equals("Weaver"), "isWeaver"), + new HamcrestCondition<>(CoreMatchers.is("Susan Alexandra")), + "1949-10-08", + "30B443AE-C0C9-4790-9BEC-CE1380808435" + ) + .hasValuesSatisfying( + 1, + new Condition(v -> v.equals("Weaver"), "isWeaver"), + new HamcrestCondition<>(CoreMatchers.is("Susan Alexandra")), + "1949-10-08", + UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435") + ); + Assertions.assertThat(tableRowAssert).isSameAs(tableRowAssert2); + } + + /** + * This method should fail because the values are different. + */ + @Test + @NeedReload + public void should_fail_because_values_are_different() { + Table table = new Table(source, "actor"); + Changes changes = new Changes(table).setStartPointNow(); + updateChangesForTests(); + changes.setEndPointNow(); + + try { + assertThat(changes).change().rowAtEndPoint() + .hasValuesSatisfying( + 4, + "Murray", + new Condition(v -> v.equals("Billy"), "isBilly"), + "1950-09-21", + UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435") + ); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format( + "[Row at end point of Change at index 0 (with primary key : [4]) of Changes on ACTOR table of 'sa/jdbc:h2:mem:test' source] %n" + + "Expecting that the value at index 2:%n" + + " \"Bill\"%n" + + "to satisfy: %n" + + " isBilly")); + } + try { + assertThat(table).row().hasValuesSatisfying( + 1, + "Weaver", + new Condition(v -> v.equals("Sigourney"), "isSigourney"), + "1949-10-08", + UUID.fromString("648DFAC8-14AC-47F7-95CF-3475525A3BE3") + ); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[Row at index 0 of ACTOR table] %n" + + "Expecting that the value at index 2:%n" + + " \"Susan Alexandra\"%n" + + "to satisfy: %n" + + " isSigourney")); + } + } +} diff --git a/src/test/java/org/assertj/db/api/assertions/AssertOnValueCondition_Satisfies_Test.java b/src/test/java/org/assertj/db/api/assertions/AssertOnValueCondition_Satisfies_Test.java index 6ef367f0..15ad5731 100644 --- a/src/test/java/org/assertj/db/api/assertions/AssertOnValueCondition_Satisfies_Test.java +++ b/src/test/java/org/assertj/db/api/assertions/AssertOnValueCondition_Satisfies_Test.java @@ -81,7 +81,8 @@ public void should_fail_because_value_not_match_with_condition() { + "Changes on TEST table of 'sa/jdbc:h2:mem:test' source] %n" + "Expecting:%n" + " 2%n" - + "to be isZero")); + + "to satisfy:%n" + + " isZero")); } try { assertThat(table).column("var3").value().satisfies(zero); @@ -91,7 +92,8 @@ public void should_fail_because_value_not_match_with_condition() { .isEqualTo(String.format("[Value at index 0 of Column at index 2 (column name : VAR3) of TEST table] %n" + "Expecting:%n" + " 2%n" - + "to be isZero")); + + "to satisfy:%n" + + " isZero")); } } } diff --git a/src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition_HasValues_Satisfying_Test.java b/src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition_HasValues_Satisfying_Test.java new file mode 100644 index 00000000..e01a5ba9 --- /dev/null +++ b/src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition_HasValues_Satisfying_Test.java @@ -0,0 +1,189 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2015-2021 the original author or authors. + */ +package org.assertj.db.api.assertions.impl; + +import org.assertj.core.api.Assertions; +import org.assertj.core.api.Condition; +import org.assertj.core.api.HamcrestCondition; +import org.assertj.core.api.WritableAssertionInfo; +import org.assertj.db.api.TableAssert; +import org.assertj.db.common.AbstractTest; +import org.assertj.db.type.Table; +import org.assertj.db.type.Value; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +import java.sql.Date; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; + +import static org.assertj.db.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +/** + * Tests on {@link AssertionsOnRowCondition} class : + * {@link AssertionsOnRowCondition#hasValuesSatisfying(org.assertj.db.api.AbstractAssert, WritableAssertionInfo, List, Object...)} method. + * + * @author Julien Roy + * + */ +public class AssertionsOnRowCondition_HasValues_Satisfying_Test extends AbstractTest { + + /** + * This method tests the {@code hasValuesSatisfying} assertion method. + */ + @Test + public void test_has_values_satisfying() throws Exception { + WritableAssertionInfo info = new WritableAssertionInfo(); + Table table = new Table(); + TableAssert tableAssert = assertThat(table); + List list = Arrays.asList(getValue(null, 1), + getValue(null, "Weaver"), + getValue(null, "Sigourney"), + getValue(null, Date.valueOf("1949-10-08")), + getValue(null, new Locale("fr"))); + + TableAssert tableAssert2 = AssertionsOnRowCondition + .hasValuesSatisfying(tableAssert, info, list, + 1, + new Condition(v -> v.equals("Weaver"), "isWeaver"), + new HamcrestCondition<>(CoreMatchers.is("Sigourney")), + "1949-10-08", + Locale.FRENCH + ); + + Assertions.assertThat(tableAssert2).isSameAs(tableAssert); + } + + /** + * This method should fail because the values not satisfying. + */ + @Test + public void should_fail_because_values_are_different() throws Exception { + WritableAssertionInfo info = new WritableAssertionInfo(); + info.description("description"); + Table table = new Table(); + TableAssert tableAssert = assertThat(table); + List list = new ArrayList<>(Arrays.asList(getValue(null, 1), + getValue(null, "Weaver"), + getValue(null, "Sigourney"), + getValue(null, Date.valueOf("1949-10-08")))); + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, + new Condition(v -> v.equals("Weaverr"), "isWeaverr"), + "Sigourney", + "1949-10-08" + ); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting that the value at index 1:%n" + + " \"Weaver\"%n" + + "to satisfy: %n" + + " isWeaverr")); + } + } + + /** + * This method should fail because the values are different. + */ + @Test + public void should_fail_because_values_not_satisfying() throws Exception { + WritableAssertionInfo info = new WritableAssertionInfo(); + info.description("description"); + Table table = new Table(); + TableAssert tableAssert = assertThat(table); + List list = new ArrayList<>(Arrays.asList(getValue(null, 1), + getValue(null, "Weaver"), + getValue(null, "Sigourney"), + getValue(null, Date.valueOf("1949-10-08")))); + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, "Weaverr", "Sigourney", "1949-10-08"); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting that the value at index 1:%n" + + " <\"Weaver\">%n" + + "to be equal to: %n" + + " <\"Weaverr\">")); + } + } + + /** + * This method should fail because the types are not compatible. + */ + @Test + public void should_fail_because_types_are_not_compatible() throws Exception { + WritableAssertionInfo info = new WritableAssertionInfo(); + info.description("description"); + Table table = new Table(); + TableAssert tableAssert = assertThat(table); + List list = new ArrayList<>(Arrays.asList(getValue(null, 1), + getValue(null, "Weaver"), + getValue(null, null), + getValue(null, Date.valueOf("1949-10-08")))); + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, true, "Sigourney", "1949-10-08"); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting:%n" + + " \"TEXT\" : <\"Weaver\">%n" + + "to be compatible with %n" + + " java.lang.Boolean : ")); + } + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, "Weaver", "Sigourney", "1949-10-08"); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting:%n" + + " \"NOT_IDENTIFIED\" : %n" + + "to be compatible with %n" + + " java.lang.String : <\"Sigourney\">")); + } + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, null, "Sigourney", "1949-10-08"); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting:%n" + + " \"TEXT\" : <\"Weaver\">%n" + + "to be compatible with %n" + + " ")); + } + } + + /** + * This method should fail because the bytes values are different. + */ + @Test + public void should_fail_because_bytes_values_are_different() throws Exception { + WritableAssertionInfo info = new WritableAssertionInfo(); + info.description("description"); + Table table = new Table(); + TableAssert tableAssert = assertThat(table); + List list = new ArrayList<>(Arrays.asList(getValue(null, 1), getValue(null, new byte[] { 0, 1 }), + getValue(null, "Sigourney"), + getValue(null, Date.valueOf("1949-10-08")))); + try { + AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1, new byte[] { 2, 3 }, "Sigourney", "1949-10-08"); + fail("An exception must be raised"); + } catch (AssertionError e) { + Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n" + + "Expecting that the value at index 1 to be equal to the expected value but was not equal")); + } + } +}