Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.juneau.assertions.FluentMapAssertion Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you 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. *
// ***************************************************************************************************************************
package org.apache.juneau.assertions;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static java.util.Arrays.*;
import java.io.*;
import java.util.*;
import java.util.function.*;
import org.apache.juneau.cp.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.serializer.*;
/**
* Used for fluent assertion calls against maps.
*
* Test Methods:
*
*
* {@link FluentMapAssertion}
*
* {@link FluentMapAssertion#isEmpty() isEmpty()}
* {@link FluentMapAssertion#isNotEmpty() isNotEmpty()}
* {@link FluentMapAssertion#isContainsKey(String) isContainsKey(String)}
* {@link FluentMapAssertion#isNotContainsKey(String) isNotContainsKey(String)}
* {@link FluentMapAssertion#isSize(int) isSize(int)}
*
* {@link FluentObjectAssertion}
*
* {@link FluentObjectAssertion#isExists() isExists()}
* {@link FluentObjectAssertion#is(Object) is(Object)}
* {@link FluentObjectAssertion#is(Predicate) is(Predicate)}
* {@link FluentObjectAssertion#isNot(Object) isNot(Object)}
* {@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
* {@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
* {@link FluentObjectAssertion#isNull() isNull()}
* {@link FluentObjectAssertion#isNotNull() isNotNull()}
* {@link FluentObjectAssertion#isString(String) isString(String)}
* {@link FluentObjectAssertion#isJson(String) isJson(String)}
* {@link FluentObjectAssertion#isSame(Object) isSame(Object)}
* {@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
* {@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
* {@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
* {@link FluentObjectAssertion#isType(Class) isType(Class)}
* {@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
*
*
*
* Transform Methods:
*
*
* {@link FluentMapAssertion}
*
* {@link FluentMapAssertion#asValue(Object) asValue(Object)}
* {@link FluentMapAssertion#asValues(Object...) asValues(Object...)}
* {@link FluentMapAssertion#asValueMap(Object...) asValueMap(Object...)}
* {@link FluentMapAssertion#asSize() asSize()}
*
* {@link FluentObjectAssertion}
*
* {@link FluentObjectAssertion#asString() asString()}
* {@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
* {@link FluentObjectAssertion#asString(Function) asString(Function)}
* {@link FluentObjectAssertion#asJson() asJson()}
* {@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
* {@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
* {@link FluentObjectAssertion#asAny() asAny()}
*
*
*
* Configuration Methods:
*
*
* {@link Assertion}
*
* {@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
* {@link Assertion#setOut(PrintStream) setOut(PrintStream)}
* {@link Assertion#setSilent() setSilent()}
* {@link Assertion#setStdOut() setStdOut()}
* {@link Assertion#setThrowable(Class) setThrowable(Class)}
*
*
*
* See Also:
*
* @param The key type.
* @param The value type.
* @param The return type.
*/
@FluentSetters(returns="FluentMapAssertion")
public class FluentMapAssertion extends FluentObjectAssertion,R> {
//-----------------------------------------------------------------------------------------------------------------
// Static
//-----------------------------------------------------------------------------------------------------------------
private static final Messages MESSAGES = Messages.of(FluentMapAssertion.class, "Messages");
private static final String
MSG_mapWasNotEmpty = MESSAGES.getString("mapWasNotEmpty"),
MSG_mapDidNotContainExpectedKey = MESSAGES.getString("mapDidNotContainExpectedKey"),
MSG_mapContainedUnexpectedKey = MESSAGES.getString("mapContainedUnexpectedKey"),
MSG_mapWasEmpty = MESSAGES.getString("mapWasEmpty"),
MSG_mapDidNotHaveTheExpectedSize = MESSAGES.getString("mapDidNotHaveTheExpectedSize");
//-----------------------------------------------------------------------------------------------------------------
// Instance
//-----------------------------------------------------------------------------------------------------------------
/**
* Constructor.
*
* @param value
* The object being tested.
* Can be null .
* @param returns
* The object to return after a test method is called.
* If null , the test method returns this object allowing multiple test method calls to be
* used on the same assertion.
*/
public FluentMapAssertion(Map value, R returns) {
this(null, value, returns);
}
/**
* Chained constructor.
*
*
* Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
*
* @param creator
* The assertion that created this assertion.
* Should be null if this is the top-level assertion.
* @param value
* The object being tested.
* Can be null .
* @param returns
* The object to return after a test method is called.
* If null , the test method returns this object allowing multiple test method calls to be
* used on the same assertion.
*/
public FluentMapAssertion(Assertion creator, Map value, R returns) {
super(creator, value, returns);
}
//-----------------------------------------------------------------------------------------------------------------
// Transform methods
//-----------------------------------------------------------------------------------------------------------------
@Override /* FluentObjectAssertion */
public FluentMapAssertion asTransformed(Function,Map> function) {
return new FluentMapAssertion<>(this, function.apply(orElse(null)), returns());
}
/**
* Returns an object assertion on the value specified at the specified key.
*
*
* If the map is null or the map doesn't contain the specified key, the returned assertion is a null assertion
* (meaning {@link FluentAnyAssertion#isExists()} returns false ).
*
* @param key The key of the item to retrieve from the map.
* @return A new assertion.
*/
public FluentAnyAssertion asValue(K key) {
return new FluentAnyAssertion<>(this, get(key), returns());
}
/**
* Returns a {@link FluentListAssertion} of the values of the specified keys.
*
* If the map is null , the returned assertion is a null assertion
* (meaning {@link FluentObjectAssertion#isExists()} returns false ).
*
* @param keys The keys of the values to retrieve from the map.
* @return A new assertion.
*/
public FluentListAssertion asValues(@SuppressWarnings("unchecked") K...keys) {
return new FluentListAssertion<>(this, valueIsNull() ? null : stream(keys).map(x -> get(x)).collect(toList()), returns());
}
/**
* Extracts a subset of this map.
*
* @param keys The entries to extract.
* @return This object.
*/
@SuppressWarnings("unchecked")
public FluentMapAssertion asValueMap(K...keys) {
if (valueIsNull())
return new FluentMapAssertion<>(this, null, returns());
Map m1 = value(), m2 = CollectionUtils.map();
if (m1 != null)
for (K k : keys)
m2.put(k, m1.get(k));
return new FluentMapAssertion<>(this, m2, returns());
}
/**
* Returns an integer assertion on the size of this map.
*
*
* If the map is null , the returned assertion is a null assertion
* (meaning {@link FluentIntegerAssertion#isExists()} returns false ).
*
* @return A new assertion.
*/
public FluentIntegerAssertion asSize() {
return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().size(), returns());
}
//-----------------------------------------------------------------------------------------------------------------
// Test methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Asserts that the map exists and is empty.
*
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isEmpty() throws AssertionError {
if (! value().isEmpty())
throw error(MSG_mapWasNotEmpty);
return returns();
}
/**
* Asserts that the map exists and is not empty.
*
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isNotEmpty() throws AssertionError {
if (value().isEmpty())
throw error(MSG_mapWasEmpty);
return returns();
}
/**
* Asserts that the map contains the expected key.
*
* @param name The key name to check for.
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isContainsKey(String name) throws AssertionError {
if (value().containsKey(name))
return returns();
throw error(MSG_mapDidNotContainExpectedKey, name, value());
}
/**
* Asserts that the map contains the expected key.
*
* @param name The key name to check for.
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isNotContainsKey(String name) throws AssertionError {
if (! value().containsKey(name))
return returns();
throw error(MSG_mapContainedUnexpectedKey, name, value());
}
/**
* Asserts that the map exists and is the specified size.
*
* @param size The expected size.
* @return The fluent return object.
* @throws AssertionError If assertion failed or value was null .
*/
public R isSize(int size) throws AssertionError {
if (size2() != size)
throw error(MSG_mapDidNotHaveTheExpectedSize, size, size2());
return returns();
}
//-----------------------------------------------------------------------------------------------------------------
// Fluent setters
//-----------------------------------------------------------------------------------------------------------------
//
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentMapAssertion setMsg(String msg, Object...args) {
super.setMsg(msg, args);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentMapAssertion setOut(PrintStream value) {
super.setOut(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentMapAssertion setSilent() {
super.setSilent();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentMapAssertion setStdOut() {
super.setStdOut();
return this;
}
@Override /* GENERATED - org.apache.juneau.assertions.Assertion */
public FluentMapAssertion setThrowable(Class extends java.lang.RuntimeException> value) {
super.setThrowable(value);
return this;
}
//
//-----------------------------------------------------------------------------------------------------------------
// Utility methods
//-----------------------------------------------------------------------------------------------------------------
private V get(K key) {
return orElse(emptyMap()).get(key);
}
private int size2() {
return value().size();
}
}