Skip to content

Commit 285ddd6

Browse files
committed
Implement hasValuesSatisfying on changes and table rows (#110)
1 parent 0560024 commit 285ddd6

13 files changed

+555
-15
lines changed

src/main/java/org/assertj/db/api/AbstractAssertWithValues.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,6 @@ public E doesNotHave(Condition<?> condition) {
590590
/** {@inheritDoc} */
591591
@Override
592592
public E satisfies(Condition<?> condition) {
593-
return AssertionsOnValueCondition.is(myself, info, value, condition);
593+
return AssertionsOnValueCondition.satisfies(myself, info, value, condition);
594594
}
595595
}

src/main/java/org/assertj/db/api/AbstractRowAssert.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
package org.assertj.db.api;
1414

1515
import org.assertj.db.api.assertions.AssertOnNumberOfColumns;
16+
import org.assertj.db.api.assertions.AssertOnRowCondition;
1617
import org.assertj.db.api.assertions.AssertOnRowEquality;
1718
import org.assertj.db.api.assertions.AssertOnRowNullity;
19+
import org.assertj.db.api.assertions.impl.AssertionsOnRowCondition;
1820
import org.assertj.db.api.assertions.impl.AssertionsOnValuesNullity;
1921
import org.assertj.db.api.assertions.impl.AssertionsOnNumberOfColumns;
2022
import org.assertj.db.api.assertions.impl.AssertionsOnRowEquality;
@@ -34,7 +36,8 @@
3436
* Base class for all {@link Row}s assertions.
3537
*
3638
* @author Régis Pouiller
37-
*
39+
* @author Julien Roy
40+
*
3841
* @param <D> The class of the actual value (an sub-class of {@link AbstractDbData}).
3942
* @param <A> The class of the original assertion (an sub-class of {@link AbstractDbAssert}).
4043
* @param <C> The class of the equivalent column assertion (an sub-class of {@link AbstractColumnAssert}).
@@ -49,7 +52,8 @@ public abstract class AbstractRowAssert<D extends AbstractDbData<D>, A extends A
4952
ToValueFromRow<RV>,
5053
AssertOnRowEquality<R>,
5154
AssertOnNumberOfColumns<R>,
52-
AssertOnRowNullity<R> {
55+
AssertOnRowNullity<R>,
56+
AssertOnRowCondition<R> {
5357

5458
/**
5559
* Position of navigation to value.
@@ -63,7 +67,7 @@ public abstract class AbstractRowAssert<D extends AbstractDbData<D>, A extends A
6367

6468
/**
6569
* Constructor.
66-
*
70+
*
6771
* @param originalDbAssert The original assert. That could be a {@link RequestAssert} or a {@link TableAssert}.
6872
* @param selfType Type of this assertion class : a sub-class of {@code AbstractRowAssert}.
6973
* @param valueType Class of the assert on the value : a sub-class of {@code AbstractRowValueAssert}.
@@ -146,4 +150,10 @@ public R hasValues(Object... expected) {
146150
public R hasOnlyNotNullValues() {
147151
return AssertionsOnValuesNullity.hasOnlyNotNullValues(myself, info, getValuesList());
148152
}
153+
154+
/** {@inheritDoc} */
155+
@Override
156+
public R hasValuesSatisfying(Object... expected) {
157+
return AssertionsOnRowCondition.hasValuesSatisfying(myself, info, getValuesList(), expected);
158+
}
149159
}

src/main/java/org/assertj/db/api/AbstractValueAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,6 @@ public V doesNotHave(Condition<?> condition) {
608608
/** {@inheritDoc} */
609609
@Override
610610
public V satisfies(Condition<?> condition) {
611-
return AssertionsOnValueCondition.is(myself, info, value, condition);
611+
return AssertionsOnValueCondition.satisfies(myself, info, value, condition);
612612
}
613613
}

src/main/java/org/assertj/db/api/ChangeRowAssert.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
package org.assertj.db.api;
1414

1515
import org.assertj.db.api.assertions.AssertOnNumberOfColumns;
16+
import org.assertj.db.api.assertions.AssertOnRowCondition;
1617
import org.assertj.db.api.assertions.AssertOnRowEquality;
1718
import org.assertj.db.api.assertions.AssertOnRowOfChangeExistence;
1819
import org.assertj.db.api.assertions.impl.AssertionsOnNumberOfColumns;
20+
import org.assertj.db.api.assertions.impl.AssertionsOnRowCondition;
1921
import org.assertj.db.api.assertions.impl.AssertionsOnRowEquality;
2022
import org.assertj.db.api.assertions.impl.AssertionsOnRowOfChangeExistence;
2123
import org.assertj.db.exception.AssertJDBException;
@@ -33,14 +35,16 @@
3335
* Assertion methods for a {@code Row} of a {@code Change}.
3436
*
3537
* @author Régis Pouiller
38+
* @author Julien Roy
3639
*/
3740
public class ChangeRowAssert
3841
extends AbstractAssertWithOriginWithColumnsAndRowsFromChange<ChangeRowAssert, ChangeAssert>
3942
implements RowElement,
4043
OriginWithValuesFromRow<ChangesAssert, ChangeAssert, ChangeColumnAssert, ChangeRowAssert, ChangeRowValueAssert>,
4144
AssertOnRowEquality<ChangeRowAssert>,
4245
AssertOnNumberOfColumns<ChangeRowAssert>,
43-
AssertOnRowOfChangeExistence<ChangeRowAssert> {
46+
AssertOnRowOfChangeExistence<ChangeRowAssert>,
47+
AssertOnRowCondition<ChangeRowAssert> {
4448

4549
/**
4650
* Position of navigation to value.
@@ -163,6 +167,13 @@ public ChangeRowAssert doesNotExist() {
163167
return AssertionsOnRowOfChangeExistence.doesNotExist(myself, info, row);
164168
}
165169

170+
/** {@inheritDoc} */
171+
@Override
172+
public ChangeRowAssert hasValuesSatisfying(Object... expected) {
173+
exists();
174+
return AssertionsOnRowCondition.hasValuesSatisfying(myself, info, row.getValuesList(), expected);
175+
}
176+
166177
/**
167178
* Returns to level of assertion methods on a {@link org.assertj.db.type.Change}.
168179
*
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2015-2021 the original author or authors.
12+
*/
13+
package org.assertj.db.api.assertions;
14+
15+
import org.assertj.core.api.Condition;
16+
17+
/**
18+
* Defines the assertion method on the a row satisfy conditions.
19+
*
20+
* @param <T> The "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
21+
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
22+
* for more details.
23+
* @author Julien Roy
24+
*/
25+
public interface AssertOnRowCondition<T extends AssertOnRowCondition<T>> {
26+
27+
/**
28+
* Verifies that the values of a row satisfy to conditions in parameter.
29+
* <p>
30+
* Example where the assertion verifies that the values in the first {@code Row} of the {@code Table} satisfy to the
31+
* conditions in parameter :
32+
* </p>
33+
*
34+
* <pre><code class='java'>
35+
* assertThat(table).row().hasValuesSatisfying(new Condition<String>(v -> v.equals("Weaver"), "isWeaver"));
36+
* </code></pre>
37+
* <p>
38+
* Example where the assertion verifies that the values of the row at end point of the first change are equal to the
39+
* values in parameter :
40+
* </p>
41+
*
42+
* <pre><code class='java'>
43+
* assertThat(changes).change().rowAtEndPoint().hasValuesSatisfying(new Condition<String>(v -> v.equals("Weaver"), "isWeaver"));
44+
* </code></pre>
45+
*
46+
* @param expected The expected conditions.
47+
* @return {@code this} assertion object.
48+
* @throws AssertionError If the values of the row are not satisfy to the conditions in parameters.
49+
* @see org.assertj.db.api.AbstractRowAssert#hasValuesSatisfying(Condition)
50+
* @see org.assertj.db.api.ChangeRowAssert#hasValuesSatisfying(Condition)
51+
*/
52+
T hasValuesSatisfying(Object... expected);
53+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2015-2021 the original author or authors.
12+
*/
13+
package org.assertj.db.api.assertions.impl;
14+
15+
import org.assertj.core.api.Condition;
16+
import org.assertj.core.api.WritableAssertionInfo;
17+
import org.assertj.core.internal.Failures;
18+
import org.assertj.db.api.AbstractAssert;
19+
import org.assertj.db.type.Value;
20+
import org.assertj.db.type.ValueType;
21+
import org.assertj.db.util.Values;
22+
23+
import java.util.List;
24+
25+
import static org.assertj.db.error.ShouldBeCompatible.shouldBeCompatible;
26+
import static org.assertj.db.error.ShouldBeEqual.shouldBeEqual;
27+
import static org.assertj.db.error.ShouldHaveColumnsSize.shouldHaveColumnsSize;
28+
import static org.assertj.db.error.ShouldSatisfy.shouldSatisfy;
29+
import static org.assertj.db.util.Values.areEqual;
30+
31+
/**
32+
* Implements the assertion method on the matching with condition of a row.
33+
*
34+
* @author Julien Roy
35+
*
36+
* @see org.assertj.db.api.assertions.AssertOnRowCondition
37+
*/
38+
public class AssertionsOnRowCondition {
39+
40+
/**
41+
* To notice failures in the assertion.
42+
*/
43+
private static final Failures failures = Failures.instance();
44+
45+
/**
46+
* Private constructor.
47+
*/
48+
private AssertionsOnRowCondition() {
49+
// Empty
50+
}
51+
52+
public static <A extends AbstractAssert<?>> A hasValuesSatisfying(A assertion, WritableAssertionInfo info,
53+
List<Value> valuesList, Object... expected) {
54+
55+
if (valuesList.size() != expected.length) {
56+
throw failures.failure(info, shouldHaveColumnsSize(valuesList.size(), expected.length));
57+
}
58+
59+
int index = 0;
60+
for (Value value : valuesList) {
61+
Object object = expected[index];
62+
63+
if (object instanceof Condition) {
64+
Condition<Object> condition = (Condition<Object>) object;
65+
if (!condition.matches(value.getValue())) {
66+
Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object);
67+
throw failures.failure(info, shouldSatisfy(index, actual, condition));
68+
}
69+
index++;
70+
continue;
71+
}
72+
73+
if (!value.isComparisonPossible(object)) {
74+
throw failures.failure(info, shouldBeCompatible(value, object));
75+
}
76+
77+
if (!areEqual(value, object)) {
78+
if (value.getValueType() == ValueType.BYTES) {
79+
throw failures.failure(info, shouldBeEqual(index));
80+
} else {
81+
Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object);
82+
throw failures.failure(info, shouldBeEqual(index, actual, object));
83+
}
84+
}
85+
86+
index++;
87+
}
88+
89+
return assertion;
90+
}
91+
}

src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowEquality.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public static <A extends AbstractAssert<?>> A hasValues(A assertion, WritableAss
6969
if (value.getValueType() == ValueType.BYTES) {
7070
throw failures.failure(info, shouldBeEqual(index));
7171
} else {
72-
throw failures.failure(info, shouldBeEqual(index, Values.getRepresentationFromValueInFrontOfExpected(value,
73-
expected[index]),
74-
expected[index]));
72+
Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, expected[index]);
73+
throw failures.failure(info, shouldBeEqual(index, actual, expected[index]));
7574
}
7675
}
7776
index++;

src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ public static <A extends AbstractAssert<?>> A isNot(A assertion, WritableAsserti
7171
return assertion;
7272
}
7373

74+
/**
75+
* Verifies that the value satisfies with condition.
76+
*
77+
* @param <A> The type of the assertion which call this method.
78+
* @param assertion The assertion which call this method.
79+
* @param info Writable information about an assertion.
80+
* @param value The value.
81+
* @param condition The condition to use for validation.
82+
* @return {@code this} assertion object.
83+
* @throws AssertionError If the value is not equal to the number in parameter.
84+
*/
85+
@SuppressWarnings("unchecked")
86+
public static <A extends AbstractAssert<?>> A satisfies(A assertion, WritableAssertionInfo info, Value value, Condition<?> condition) {
87+
conditions.assertSatisfies(info, value.getValue(), (Condition<? super Object>) condition);
88+
return assertion;
89+
}
90+
7491
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2015-2021 the original author or authors.
12+
*/
13+
package org.assertj.db.error;
14+
15+
import org.assertj.core.api.Condition;
16+
import org.assertj.core.error.BasicErrorMessageFactory;
17+
import org.assertj.core.error.ErrorMessageFactory;
18+
19+
/**
20+
* Creates an error message indicating that an assertion that verifies that a value does not satisfying condition.
21+
*
22+
* @author Julien Roy
23+
*
24+
*/
25+
public class ShouldSatisfy extends BasicErrorMessageFactory {
26+
27+
private static final String EXPECTED_MESSAGE_WITH_INDEX = "%nExpecting that the value at index %s:%n %s%nto satisfy: %n %s";
28+
29+
public static ErrorMessageFactory shouldSatisfy(int index, Object actual, Condition<?> condition) {
30+
return new ShouldSatisfy(index, actual, condition);
31+
}
32+
33+
private ShouldSatisfy(int index, Object actual, Condition<?> condition) {
34+
super(EXPECTED_MESSAGE_WITH_INDEX, index, actual, condition);
35+
}
36+
37+
}

src/main/java/org/assertj/db/navigation/PositionWithChanges.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public E getChangesInstance(Changes changes, ChangeType changeType, String table
125125
}
126126

127127
try {
128-
Class clazz = unProxy(myself.getClass());
128+
Class<?> clazz = unProxy(myself.getClass());
129129
Constructor<E> constructor = actualElementClass.getDeclaredConstructor(clazz, Changes.class);
130130
instance = constructor.newInstance(myself, nextChanges);
131131
instance.as(getChangesDescription(changeType, tableName));
@@ -200,7 +200,7 @@ public N getChangeInstance(Changes changes, ChangeType changeType, String tableN
200200
}
201201

202202
try {
203-
Class clazz = unProxy(myself.getClass());
203+
Class<?> clazz = unProxy(myself.getClass());
204204
Constructor<N> constructor = nextElementClass.getDeclaredConstructor(clazz, Change.class);
205205
instance = constructor.newInstance(myself, change);
206206
instance.as(getChangeDescription(changes, change, index, changeType, tableName));
@@ -248,8 +248,7 @@ public N getChangeInstanceWithPK(Changes changes, String tableName, Object... pk
248248
}
249249
index++;
250250
}
251-
throw new AssertJDBException("No change found for table " + tableName + " and primary keys " + Arrays
252-
.asList(pksValues));
251+
throw new AssertJDBException("No change found for table " + tableName + " and primary keys " + Arrays.asList(pksValues));
253252
}
254253

255254
/**

0 commit comments

Comments
 (0)