Skip to content

Commit

Permalink
Implement hasValuesSatisfying on changes and table rows (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
VanRoy committed Feb 23, 2021
1 parent ef4ce65 commit ce5df86
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/assertj/db/api/AbstractValueAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public static <A extends AbstractAssert<?>> A hasValuesSatisfying(A assertion, W
Condition<Object> condition = (Condition<Object>) object;
if (!condition.matches(value.getValue())) {
Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object);
throw failures.failure(info, shouldBeEqual(index, actual, object));
throw failures.failure(info, shouldSatisfy(index, actual, condition));
}
index++;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@ public static <A extends AbstractAssert<?>> A isNot(A assertion, WritableAsserti
return assertion;
}

/**
* Verifies that the value satisfies with condition.
*
* @param <A> 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 extends AbstractAssert<?>> A satisfies(A assertion, WritableAssertionInfo info, Value value, Condition<?> condition) {
conditions.assertSatisfies(info, value.getValue(), (Condition<? super Object>) condition);
return assertion;
}

}
37 changes: 37 additions & 0 deletions src/main/java/org/assertj/db/error/ShouldSatisfy.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,31 @@ public void test_has_values() {
"1950-09-21",
"30B443AE-C0C9-4790-9BEC-CE1380808435"
)
.hasValues(4, "Murray", "Bill", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
.hasValuesSatisfying(
4,
new Condition<String>(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
.hasValues(1, "Weaver", "Susan Alexandra", "1949-10-08", "30B443AE-C0C9-4790-9BEC-CE1380808435")
.hasValues(1, "Weaver", "Susan Alexandra", "1949-10-08", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
.hasValuesSatisfying(
1,
new Condition<String>(v -> v.equals("Weaver"), "isWeaver"),
new HamcrestCondition<>(CoreMatchers.is("Susan Alexandra")),
"1949-10-08",
"30B443AE-C0C9-4790-9BEC-CE1380808435"
)
.hasValuesSatisfying(
1,
new Condition<String>(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);
}

Expand All @@ -80,24 +98,35 @@ public void should_fail_because_values_are_different() {

try {
assertThat(changes).change().rowAtEndPoint()
.hasValues(4, "Murray", "Billy", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
.hasValuesSatisfying(
4,
"Murray",
new Condition<String>(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 be equal to: %n"
+ " <\"Billy\">"));
+ " \"Bill\"%n"
+ "to satisfy: %n"
+ " isBilly"));
}
try {
assertThat(table).row().hasValues(1, "Weaver", "Sigourney", "1949-10-08",
UUID.fromString("648DFAC8-14AC-47F7-95CF-3475525A3BE3"));
assertThat(table).row().hasValuesSatisfying(
1,
"Weaver",
new Condition<String>(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 be equal to: %n"
+ " <\"Sigourney\">"));
+ " \"Susan Alexandra\"%n"
+ "to satisfy: %n"
+ " isSigourney"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,39 @@ public void test_has_values_satisfying() throws Exception {
}

/**
* This method should fail because the values are different.
* 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<Value> 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<String>(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();
Expand Down

0 comments on commit ce5df86

Please sign in to comment.