org.assertj.neo4j.api.PropertyContainerAssert Maven / Gradle / Ivy
/**
* 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 2013-2017 the original author or authors.
*/
package org.assertj.neo4j.api;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
import org.neo4j.graphdb.PropertyContainer;
import static org.assertj.neo4j.error.ShouldHaveProperty.shouldHaveProperty;
import static org.assertj.neo4j.error.ShouldHavePropertyKey.shouldHavePropertyKey;
import static org.assertj.neo4j.error.ShouldNotHaveProperty.shouldNotHaveProperty;
import static org.assertj.neo4j.error.ShouldNotHavePropertyKey.shouldNotHavePropertyKey;
/**
* Assertions for Neo4J {@link PropertyContainer}
*
* @author Florent Biville
*/
public class PropertyContainerAssert, T extends PropertyContainer> extends
AbstractAssert {
protected PropertyContainerAssert(T actual, Class extends A> assertClass) {
super(actual, assertClass);
}
public T getActual() {
return actual;
}
/**
* Verifies that the actual {@link PropertyContainer} has the given property key
*
* Example:
*
*
* GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();
* Node node = graph.createNode();
* node.setProperty("firstName", "Homer");
*
* assertThat(node).hasPropertyKey("firstName");
*
* // it also works with relationships:
* Relationship relationship = homer.createRelationshipTo(donut, DynamicRelationshipType.withName("LOVES"));
* relationship.setProperty("firstName", "Homer");
*
* assertThat(relationship).hasPropertyKey("firstName");
*
*
* If the given key
is {@code null}, an {@link IllegalArgumentException} is thrown.
*
*
* @param key the property key to look for in the actual {@link PropertyContainer}
* @return this {@link PropertyContainerAssert} for assertions chaining
*
* @throws IllegalArgumentException if key
is {@code null}.
* @throws AssertionError if the actual {@link PropertyContainer} does not have a property with the given key.
*/
public A hasPropertyKey(String key) {
Objects.instance().assertNotNull(info, actual);
checkPropertyKeyIsNotNull(key);
if (!actual.hasProperty(key)) {
throw Failures.instance().failure(info, shouldHavePropertyKey(actual, key));
}
return myself;
}
/**
* Verifies that the actual {@link PropertyContainer} has the given property key with the given value
*
* Example:
*
*
* GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();
* ResourceIterator<String> loveLevelIterator = myExecutionEngine.execute(
* "MATCH (:CHARACTER)-[l:LOVES]->(:DOUGHNUT) RETURN l.level AS level").columnAs("level");
*
* assertThat(loveLevelIterator).hasSize(3);
*
*
* If the given size
is negative, an {@link IllegalArgumentException} is thrown.
*
*
* @param key the property key to look for in the actual {@link PropertyContainer}
* @param value the property value to look for in the actual {@link PropertyContainer}
* @return this {@link PropertyContainerAssert} for assertions chaining
*
* @throws IllegalArgumentException if key
is {@code null}.
* @throws IllegalArgumentException if value
is {@code null}.
* @throws AssertionError if the actual {@link PropertyContainer} does not have a property with given key and value.
*/
public A hasProperty(String key, Object value) {
hasPropertyKey(key);
checkPropertyValueIsNotNull(value);
if (!value.equals(actual.getProperty(key, null))) {
throw Failures.instance().failure(info, shouldHaveProperty(actual, key, value));
}
return myself;
}
/**
* Verifies that the actual {@link PropertyContainer} does not have the given property key
*
* Example:
*
*
* GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();
* Node node = graph.createNode();
* node.setProperty("firstName", "Homer");
*
* assertThat(node).doesNotHavePropertyKey("lastName");
*
* // it also works with relationships:
* Relationship relationship = homer.createRelationshipTo(donut, DynamicRelationshipType.withName("LOVES"));
* relationship.setProperty("firstName", "Homer");
*
* assertThat(relationship).doesNotHavePropertyKey("lastName");
*
*
* If the key
is {@code null}, an {@link IllegalArgumentException} is thrown.
*
*
* @param key the property key to look for in the actual {@link PropertyContainer}
* @return this {@link PropertyContainerAssert} for assertions chaining
*
* @throws IllegalArgumentException if key
is {@code null}.
* @throws AssertionError if the actual {@link PropertyContainer} has a property with given key.
*/
public A doesNotHavePropertyKey(String key) {
Objects.instance().assertNotNull(info, actual);
checkPropertyKeyIsNotNull(key);
if (actual.hasProperty(key)) {
throw Failures.instance().failure(info, shouldNotHavePropertyKey(actual, key));
}
return myself;
}
/**
* Verifies that the actual {@link PropertyContainer} does not have a property with given key and value.
*
* Example:
*
*
* GraphDatabaseService graph = new TestGraphDatabaseFactory().newImpermanentDatabase();
* Node node = graph.createNode();
* node.setProperty("firstName", "Homer");
*
* assertThat(node).doesNotHaveProperty("firstName", "Bart");
* assertThat(node).doesNotHaveProperty("lastName", "Homer");
*
* // it also works with relationships:
* Relationship relationship = homer.createRelationshipTo(donut, DynamicRelationshipType.withName("LOVES"));
* relationship.setProperty("firstName", "Homer");
*
* assertThat(relationship).doesNotHaveProperty("firstName", "Bart");
* assertThat(relationship).doesNotHaveProperty("lastName", "Homer");
*
*
*
* If any of the key
or value
is {@code null}, an {@link IllegalArgumentException} is
* thrown.
*
*
* @param key the property key to look for in the actual {@link PropertyContainer}
* @param value the property value to look for in the actual {@link PropertyContainer}
* @return this {@link PropertyContainerAssert} for assertions chaining
*
* @throws IllegalArgumentException if key
is {@code null}.
* @throws IllegalArgumentException if value
is {@code null}.
* @throws AssertionError if the actual {@link PropertyContainer} has a property with given key and value.
*/
public A doesNotHaveProperty(String key, Object value) {
Objects.instance().assertNotNull(info, actual);
checkPropertyKeyIsNotNull(key);
checkPropertyValueIsNotNull(value);
if (actual.hasProperty(key) && value.equals(actual.getProperty(key, null))) {
throw Failures.instance().failure(info, shouldNotHaveProperty(actual, key, value));
}
return myself;
}
private static void checkPropertyValueIsNotNull(Object value) {
if (value == null) {
throw new IllegalArgumentException("The value to look for should not be null");
}
}
private static void checkPropertyKeyIsNotNull(String key) {
if (key == null) {
throw new IllegalArgumentException("The key to look for should not be null");
}
}
}