org.apache.juneau.rest.client.assertion.FluentResponseBodyAssertion 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.rest.client.assertion;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;
import org.apache.juneau.assertions.*;
import org.apache.juneau.http.response.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.rest.client.*;
import org.apache.juneau.serializer.*;
/**
* Used for fluent assertion calls against {@link ResponseContent} objects.
*
* Test Methods
*
*
* - {@link FluentResponseBodyAssertion}
*
* - {@link FluentResponseBodyAssertion#is(String) is(String)}
*
- {@link FluentResponseBodyAssertion#isContains(String...) isContains(String...)}
*
- {@link FluentResponseBodyAssertion#isNotContains(String...) isNotContains(String...)}
*
- {@link FluentResponseBodyAssertion#isEmpty() isEmpty()}
*
- {@link FluentResponseBodyAssertion#isNotEmpty() isNotEmpty()}
*
* - {@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 FluentResponseBodyAssertion}
*
* - {@link FluentResponseBodyAssertion#asBytes() asBytes()}
*
- {@link FluentResponseBodyAssertion#as(Class) as(Class)}
*
- {@link FluentResponseBodyAssertion#as(Type,Type...) as(Type,Type...)}
*
* - {@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 return type.
*/
@FluentSetters(returns="FluentResponseBodyAssertion")
public class FluentResponseBodyAssertion extends FluentObjectAssertion {
//-----------------------------------------------------------------------------------------------------------------
// Constructors
//-----------------------------------------------------------------------------------------------------------------
/**
* 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 FluentResponseBodyAssertion(ResponseContent 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 FluentResponseBodyAssertion(Assertion creator, ResponseContent value, R returns) {
super(creator, value, returns);
setThrowable(BadRequest.class);
}
//-----------------------------------------------------------------------------------------------------------------
// Transform methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Provides the ability to perform fluent-style assertions on this response body.
*
*
Examples:
*
* // Validates the response body equals the text "OK".
* client
* .get(URI )
* .run()
* .assertContent().is("OK" );
*
* // Validates the response body contains the text "OK".
* client
* .get(URI )
* .run()
* .assertContent().isContains("OK" );
*
* // Validates the response body passes a predicate test.
* client
* .get(URI )
* .run()
* .assertContent().is(x -> x .contains("OK" ));
*
* // Validates the response body matches a regular expression.
* client
* .get(URI )
* .run()
* .assertContent().isPattern(".*OK.*" );
*
* // Validates the response body matches a regular expression using regex flags.
* client
* .get(URI )
* .run()
* .assertContent().isPattern(".*OK.*" , MULTILINE & CASE_INSENSITIVE );
*
* // Validates the response body matches a regular expression in the form of an existing Pattern.
* Pattern pattern = Pattern.compile (".*OK.*" );
* client
* .get(URI )
* .run()
* .assertContent().isPattern(pattern );
*
*
*
* The assertion test returns the original response object allowing you to chain multiple requests like so:
*
* // Validates the response body matches a regular expression.
* MyBean bean = client
* .get(URI )
* .run()
* .assertContent().isPattern(".*OK.*" );
* .assertContent().isNotPattern(".*ERROR.*" )
* .getContent().as(MyBean.class );
*
*
* Notes:
* -
* If no charset was found on the
Content-Type
response header, "UTF-8" is assumed.
* -
* When using this method, the body is automatically cached by calling the {@link ResponseContent#cache()}.
*
-
* The input stream is automatically closed after this call.
*
*
* @return A new fluent assertion object.
*/
@Override
public FluentStringAssertion asString() {
return new FluentStringAssertion<>(valueAsString(), returns());
}
/**
* Provides the ability to perform fluent-style assertions on the bytes of the response body.
*
* Examples:
*
* // Validates the response body equals the text "foo".
* client
* .get(URI )
* .run()
* .assertContent().asBytes().asHex().is("666F6F" );
*
*
* Notes:
* -
* If no charset was found on the
Content-Type
response header, "UTF-8" is assumed.
* -
* When using this method, the body is automatically cached by calling the {@link ResponseContent#cache()}.
*
-
* The input stream is automatically closed after this call.
*
*
* @return A new fluent assertion object.
*/
public FluentByteArrayAssertion asBytes() {
return new FluentByteArrayAssertion<>(valueAsBytes(), returns());
}
/**
* Converts the body to a type using {@link ResponseContent#as(Class)} and then returns the value as an object assertion.
*
* Examples:
*
* // Validates the response body as a list of strings and validates the length.
* client
* .get("/myBean" )
* .run()
* .assertContent().as(List.class , String.class ).is(x -> x .size() > 0);
*
*
* @param The object type to create.
* @param type The object type to create.
* @return A new fluent assertion object.
*/
public FluentAnyAssertion as(Class type) {
return new FluentAnyAssertion<>(valueAsType(type), returns());
}
/**
* Converts the body to a type using {@link ResponseContent#as(Type,Type...)} and then returns the value as an object assertion.
*
* Examples:
*
* // Validates the response body as a list of strings and validates the length.
* client
* .get("/myBean" )
* .run()
* .assertContent().as(List.class , String.class ).is(x -> x .size() > 0);
*
*
*
* See Complex Data Types for information on defining complex generic types of {@link Map Maps} and {@link Collection Collections}.
*
* @param type The object type to create.
* @param args Optional type arguments.
* @return A new fluent assertion object.
*/
public FluentAnyAssertion