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 0560024 commit 285ddd6
Show file tree
Hide file tree
Showing 13 changed files with 555 additions and 15 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);
}
}
16 changes: 13 additions & 3 deletions src/main/java/org/assertj/db/api/AbstractRowAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +36,8 @@
* Base class for all {@link Row}s assertions.
*
* @author Régis Pouiller
*
* @author Julien Roy
*
* @param <D> The class of the actual value (an sub-class of {@link AbstractDbData}).
* @param <A> The class of the original assertion (an sub-class of {@link AbstractDbAssert}).
* @param <C> The class of the equivalent column assertion (an sub-class of {@link AbstractColumnAssert}).
Expand All @@ -49,7 +52,8 @@ public abstract class AbstractRowAssert<D extends AbstractDbData<D>, A extends A
ToValueFromRow<RV>,
AssertOnRowEquality<R>,
AssertOnNumberOfColumns<R>,
AssertOnRowNullity<R> {
AssertOnRowNullity<R>,
AssertOnRowCondition<R> {

/**
* Position of navigation to value.
Expand All @@ -63,7 +67,7 @@ public abstract class AbstractRowAssert<D extends AbstractDbData<D>, 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}.
Expand Down Expand Up @@ -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);
}
}
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);
}
}
13 changes: 12 additions & 1 deletion src/main/java/org/assertj/db/api/ChangeRowAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,14 +35,16 @@
* Assertion methods for a {@code Row} of a {@code Change}.
*
* @author Régis Pouiller
* @author Julien Roy
*/
public class ChangeRowAssert
extends AbstractAssertWithOriginWithColumnsAndRowsFromChange<ChangeRowAssert, ChangeAssert>
implements RowElement,
OriginWithValuesFromRow<ChangesAssert, ChangeAssert, ChangeColumnAssert, ChangeRowAssert, ChangeRowValueAssert>,
AssertOnRowEquality<ChangeRowAssert>,
AssertOnNumberOfColumns<ChangeRowAssert>,
AssertOnRowOfChangeExistence<ChangeRowAssert> {
AssertOnRowOfChangeExistence<ChangeRowAssert>,
AssertOnRowCondition<ChangeRowAssert> {

/**
* Position of navigation to value.
Expand Down Expand Up @@ -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}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T> The "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
* for more details.
* @author Julien Roy
*/
public interface AssertOnRowCondition<T extends AssertOnRowCondition<T>> {

/**
* Verifies that the values of a row satisfy to conditions in parameter.
* <p>
* Example where the assertion verifies that the values in the first {@code Row} of the {@code Table} satisfy to the
* conditions in parameter :
* </p>
*
* <pre><code class='java'>
* assertThat(table).row().hasValuesSatisfying(new Condition<String>(v -> v.equals("Weaver"), "isWeaver"));
* </code></pre>
* <p>
* 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 :
* </p>
*
* <pre><code class='java'>
* assertThat(changes).change().rowAtEndPoint().hasValuesSatisfying(new Condition<String>(v -> v.equals("Weaver"), "isWeaver"));
* </code></pre>
*
* @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);
}
Original file line number Diff line number Diff line change
@@ -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 extends AbstractAssert<?>> A hasValuesSatisfying(A assertion, WritableAssertionInfo info,
List<Value> 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<Object> condition = (Condition<Object>) 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public static <A extends AbstractAssert<?>> 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++;
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 @@ -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<E> constructor = actualElementClass.getDeclaredConstructor(clazz, Changes.class);
instance = constructor.newInstance(myself, nextChanges);
instance.as(getChangesDescription(changeType, tableName));
Expand Down Expand Up @@ -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<N> constructor = nextElementClass.getDeclaredConstructor(clazz, Change.class);
instance = constructor.newInstance(myself, change);
instance.as(getChangeDescription(changes, change, index, changeType, tableName));
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Loading

0 comments on commit 285ddd6

Please sign in to comment.