All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.rest.assertions.FluentRequestContentAssertion 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.assertions;

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.httppart.*;
import org.apache.juneau.serializer.*;

/**
 * Used for fluent assertion calls against {@link RequestContent} objects.
 *
 * 
Test Methods
*

*

    *
  • {@link FluentRequestContentAssertion} *
      *
    • {@link FluentRequestContentAssertion#is(String) is(String)} *
    • {@link FluentRequestContentAssertion#isContains(String...) isContains(String...)} *
    • {@link FluentRequestContentAssertion#isNotContains(String...) isNotContains(String...)} *
    • {@link FluentRequestContentAssertion#isEmpty() isEmpty()} *
    • {@link FluentRequestContentAssertion#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 FluentRequestContentAssertion} *
      *
    • {@link FluentRequestContentAssertion#asBytes() asBytes()} *
    • {@link FluentRequestContentAssertion#as(Class) as(Class)} *
    • {@link FluentRequestContentAssertion#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="FluentRequestContentAssertion") public class FluentRequestContentAssertion 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 FluentRequestContentAssertion(RequestContent 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 FluentRequestContentAssertion(Assertion creator, RequestContent value, R returns) { super(creator, value, returns); setThrowable(BadRequest.class); } //----------------------------------------------------------------------------------------------------------------- // Transform methods //----------------------------------------------------------------------------------------------------------------- /** * Provides the ability to perform fluent-style assertions on the bytes of the request content. * *

Examples:
*

* // Validates the request content equals the text "foo". * request * .assertContent().asBytes().asHex().is("666F6F"); *

* *
Notes:
    *
  • * If no charset was found on the Content-Type request header, "UTF-8" is assumed. *
  • * When using this method, the content is automatically cached by calling the {@link RequestContent#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 content to a type using {@link RequestContent#as(Class)} and then returns the value as an object assertion. * *
Examples:
*

* // Validates the request content bean is the expected value. * request * .assertContent() * .as(MyBean.class) * .asJson().is("{foo:'bar'}"); *

* *
Notes:
    *
  • * If no charset was found on the Content-Type request header, "UTF-8" is assumed. *
  • * When using this method, the content is automatically cached by calling the {@link RequestContent#cache()}. *
  • * The input stream is automatically closed after this call. *
* *

* See Complex Data Types for information on defining complex generic types of {@link Map Maps} and {@link Collection Collections}. * * @param The object type to create. * @param type The object type to create. * @return A new fluent assertion object. */ public FluentObjectAssertion as(Class type) { return new FluentObjectAssertion<>(valueAsType(type), returns()); } /** * Converts the content to a type using {@link RequestContent#as(Type,Type...)} and then returns the value as an object assertion. * *

Examples:
*

* // Validates the request content bean is the expected value. * request * .assertContent() * .as(Map.class,String.class,Integer.class) * .asJson().is("{foo:123}"); *

* *
Notes:
    *
  • * If no charset was found on the Content-Type request header, "UTF-8" is assumed. *
  • * When using this method, the content is automatically cached by calling the {@link RequestContent#cache()}. *
  • * The input stream is automatically closed after this call. *
* *

* See Complex Data Types for information on defining complex generic types of {@link Map Maps} and {@link Collection Collections}. * * @param The type to create. * @param type The object type to create. * @param args Optional type arguments. * @return A new fluent assertion object. */ public FluentObjectAssertion as(Type type, Type...args) { return new FluentObjectAssertion<>(valueAsType(type, args), returns()); } //----------------------------------------------------------------------------------------------------------------- // Test methods //----------------------------------------------------------------------------------------------------------------- /** * Asserts that the content contains the specified value. * * @param values The value to check against. * @return This object. * @throws AssertionError If assertion failed. */ public R is(String values) throws AssertionError { return asString().is(values); } /** * Asserts that the text contains all of the specified substrings. * * @param values The values to check against. * @return This object. * @throws AssertionError If assertion failed. */ public R isContains(String...values) throws AssertionError { return asString().isContains(values); } /** * Asserts that the content doesn't contain any of the specified substrings. * * @param values The values to check against. * @return This object. * @throws AssertionError If assertion failed. */ public R isNotContains(String...values) throws AssertionError { return asString().isNotContains(values); } /** * Asserts that the content is empty. * * @return This object. * @throws AssertionError If assertion failed. */ public R isEmpty() { return asString().isEmpty(); } /** * Asserts that the content is not empty. * * @return This object. * @throws AssertionError If assertion failed. */ public R isNotEmpty() { return asString().isNotEmpty(); } //----------------------------------------------------------------------------------------------------------------- // Helper methods. //----------------------------------------------------------------------------------------------------------------- @Override protected String valueAsString() throws AssertionError { try { return value().cache().asString(); } catch (IOException e) { throw error(e, "Exception occurred during call."); } } private byte[] valueAsBytes() throws AssertionError { try { return value().cache().asBytes(); } catch (IOException e) { throw error(e, "Exception occurred during call."); } } private T valueAsType(Class c) throws AssertionError { try { return value().cache().as(c); } catch (IOException e) { throw error(e, "Exception occurred during call."); } } private T valueAsType(Type c, Type...args) throws AssertionError { try { return value().cache().as(c, args); } catch (IOException e) { throw error(e, "Exception occurred during call."); } } //----------------------------------------------------------------------------------------------------------------- // Fluent setters //----------------------------------------------------------------------------------------------------------------- // @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentRequestContentAssertion setMsg(String msg, Object...args) { super.setMsg(msg, args); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentRequestContentAssertion setOut(PrintStream value) { super.setOut(value); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentRequestContentAssertion setSilent() { super.setSilent(); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentRequestContentAssertion setStdOut() { super.setStdOut(); return this; } @Override /* GENERATED - org.apache.juneau.assertions.Assertion */ public FluentRequestContentAssertion setThrowable(Class value) { super.setThrowable(value); return this; } // }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy