001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.client.classic.assertion; 018 019import java.io.*; 020import java.lang.reflect.*; 021import java.util.*; 022import java.util.function.*; 023import java.util.regex.*; 024 025import org.apache.juneau.commons.function.*; 026import org.apache.juneau.http.classic.response.*; 027import org.apache.juneau.rest.client.classic.*; 028import org.apache.juneau.bct.assertions.*; 029 030/** 031 * Used for fluent assertion calls against {@link ResponseHeader} objects. 032 * 033 * <h2 class='topic'>Test Methods</h2> 034 * <ul class='javatree'> 035 * <li class='jc'>{@link FluentStringAssertion} 036 * <ul class='javatreec'> 037 * <li class='jm'>{@link FluentStringAssertion#is(String) is(String)} 038 * <li class='jm'>{@link FluentStringAssertion#isNot(String) isNot(String)} 039 * <li class='jm'>{@link FluentStringAssertion#isLines(String...) isLines(String...)} 040 * <li class='jm'>{@link FluentStringAssertion#isSortedLines(String...) isSortedLines(String...)} 041 * <li class='jm'>{@link FluentStringAssertion#isIc(String) isIc(String)} 042 * <li class='jm'>{@link FluentStringAssertion#isNotIc(String) isNotIc(String)} 043 * <li class='jm'>{@link FluentStringAssertion#isContains(String...) isContains(String...)} 044 * <li class='jm'>{@link FluentStringAssertion#isNotContains(String...) isNotContains(String...)} 045 * <li class='jm'>{@link FluentStringAssertion#isEmpty() isEmpty()} 046 * <li class='jm'>{@link FluentStringAssertion#isNotEmpty() isNotEmpty()} 047 * <li class='jm'>{@link FluentStringAssertion#isString(Object) isString(Object)} 048 * <li class='jm'>{@link FluentStringAssertion#isMatches(String) isMatches(String)} 049 * <li class='jm'>{@link FluentStringAssertion#isPattern(String) isPattern(String)} 050 * <li class='jm'>{@link FluentStringAssertion#isPattern(String,int) isPattern(String,int)} 051 * <li class='jm'>{@link FluentStringAssertion#isPattern(Pattern) isPattern(Pattern)} 052 * <li class='jm'>{@link FluentStringAssertion#isStartsWith(String) isStartsWith(String)} 053 * <li class='jm'>{@link FluentStringAssertion#isEndsWith(String) isEndsWith(String)} 054 * </ul> 055 * <li class='jc'>{@link FluentObjectAssertion} 056 * <ul class='javatreec'> 057 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 058 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 059 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 060 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 061 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 062 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 063 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 064 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 065 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 066 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 067 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 068 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 069 * </ul> 070 * </ul> 071 * 072 * <h2 class='topic'>Transform Methods</h2> 073 * <ul class='javatree'> 074 * <li class='jc'>{@link FluentResponseHeaderAssertion} 075 * <ul class='javatreec'> 076 * <li class='jm'>{@link FluentResponseHeaderAssertion#asBoolean() asBoolean()} 077 * <li class='jm'>{@link FluentResponseHeaderAssertion#asInteger() asInteger()} 078 * <li class='jm'>{@link FluentResponseHeaderAssertion#asLong() asLong()} 079 * <li class='jm'>{@link FluentResponseHeaderAssertion#asZonedDateTime() asZonedDateTime()} 080 * <li class='jm'>{@link FluentResponseHeaderAssertion#as(Class) as(Class)} 081 * <li class='jm'>{@link FluentResponseHeaderAssertion#as(Type,Type...) as(Type,Type...)} 082 * </ul> 083 * <li class='jc'>{@link FluentStringAssertion} 084 * <ul class='javatreec'> 085 * <li class='jm'>{@link FluentStringAssertion#asReplaceAll(String,String) asReplaceAll(String,String)} 086 * <li class='jm'>{@link FluentStringAssertion#asReplace(String,String) asReplace(String,String)} 087 * <li class='jm'>{@link FluentStringAssertion#asUrlDecode() asUrlDecode()} 088 * <li class='jm'>{@link FluentStringAssertion#asLc() asLc()} 089 * <li class='jm'>{@link FluentStringAssertion#asUc() asUc()} 090 * <li class='jm'>{@link FluentStringAssertion#asLines() asLines()} 091 * <li class='jm'>{@link FluentStringAssertion#asSplit(String) asSplit(String)} 092 * <li class='jm'>{@link FluentStringAssertion#asLength() asLength()} 093 * <li class='jm'>{@link FluentStringAssertion#asOneLine() asOneLine()} 094 * </ul> 095 * <li class='jc'>{@link FluentObjectAssertion} 096 * <ul class='javatreec'> 097 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 098 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 099 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 100 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 101 * </ul> 102 * </ul> 103 * 104 * <h2 class='topic'>Configuration Methods</h2> 105 * <ul class='javatree'> 106 * <li class='jc'>{@link Assertion} 107 * <ul class='javatreec'> 108 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 109 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 110 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 111 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 112 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 113 * </ul> 114 * </ul> 115 * 116 * <h2 class='section'>See Also:</h2><ul> 117 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 118 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClient">juneau-rest-client-classic</a> 119 * </ul> 120 * 121 * @param <R> The return type. 122 */ 123public class FluentResponseHeaderAssertion<R> extends FluentStringAssertion<R> { 124 125 private final ResponseHeader value; 126 127 /** 128 * Chained constructor. 129 * 130 * <p> 131 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 132 * 133 * @param creator 134 * The assertion that created this assertion. 135 * <br>Should be <jk>null</jk> if this is the top-level assertion. 136 * @param value 137 * The object being tested. 138 * <br>Can be <jk>null</jk>, in which case this assertion behaves as if testing a header with no value (e.g. {@link #isNull()} passes, transform methods yield <jk>null</jk>-valued assertions). 139 * @param returns 140 * The object to return after a test method is called. 141 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 142 * used on the same assertion. 143 */ 144 public FluentResponseHeaderAssertion(Assertion creator, ResponseHeader value, R returns) { 145 super(creator, value == null ? null : value.asString().orElse(null), returns); 146 this.value = value; 147 setThrowable(BadRequest.class); 148 } 149 150 /** 151 * Constructor. 152 * 153 * @param value 154 * The object being tested. 155 * <br>Can be <jk>null</jk>, in which case this assertion behaves as if testing a header with no value (e.g. {@link #isNull()} passes, transform methods yield <jk>null</jk>-valued assertions). 156 * @param returns 157 * The object to return after a test method is called. 158 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 159 * used on the same assertion. 160 */ 161 public FluentResponseHeaderAssertion(ResponseHeader value, R returns) { 162 this(null, value, returns); 163 } 164 165 /** 166 * Converts the parameter value to a type using {@link ResponseHeader#as(Class)} and then returns the value as an any-object assertion. 167 * 168 * @param <T> The object type to create. 169 * @param type The object type to create. 170 * @return A new fluent assertion object. 171 * @throws RestCallException If value could not be parsed. 172 */ 173 public <T> FluentAnyAssertion<T,R> as(Class<T> type) throws RestCallException { 174 return new FluentAnyAssertion<>(value == null ? null : value.as(type).orElse(null), returns()); 175 } 176 177 /** 178 * Converts the parameter value to a type using {@link ResponseHeader#as(Type,Type...)} and then returns the value as an any-object assertion. 179 * 180 * <p> 181 * See <a class="doclink" href="https://juneau.apache.org/docs/topics/ComplexDataTypes">Complex Data Types</a> for information on defining complex generic types of {@link Map Maps} and {@link Collection Collections}. 182 * 183 * @param type The object type to create. 184 * @param args Optional type arguments. 185 * @return A new fluent assertion object. 186 * @throws RestCallException If value could not be parsed. 187 */ 188 public FluentAnyAssertion<Object,R> as(Type type, Type...args) throws RestCallException { 189 return new FluentAnyAssertion<>(value == null ? null : value.as(type, args).orElse(null), returns()); 190 } 191 192 /** 193 * Converts this object assertion into a boolean assertion. 194 * 195 * @return A new assertion. 196 * @throws AssertionError If object is not a boolean. 197 */ 198 public FluentBooleanAssertion<R> asBoolean() { 199 return new FluentBooleanAssertion<>(this, value == null ? null : value.asBoolean().orElse(null), returns()); 200 } 201 202 /** 203 * Converts this object assertion into an integer assertion. 204 * 205 * @return A new assertion. 206 * @throws AssertionError If object is not an integer. 207 */ 208 public FluentIntegerAssertion<R> asInteger() { 209 return new FluentIntegerAssertion<>(this, value == null ? null : value.asInteger().orElse(null), returns()); 210 } 211 212 @Override /* Overridden from FluentStringAssertion */ 213 public FluentResponseHeaderAssertion<R> asJavaStrings() { 214 super.asJavaStrings(); 215 return this; 216 } 217 218 /** 219 * Converts this object assertion into a long assertion. 220 * 221 * @return A new assertion. 222 * @throws AssertionError If object is not a long. 223 */ 224 public FluentLongAssertion<R> asLong() { 225 return new FluentLongAssertion<>(this, value == null ? null : value.asLong().orElse(null), returns()); 226 } 227 228 /** 229 * Converts this object assertion into a zoned-datetime assertion. 230 * 231 * @return A new assertion. 232 * @throws AssertionError If object is not a zoned-datetime. 233 */ 234 public FluentZonedDateTimeAssertion<R> asZonedDateTime() { 235 return new FluentZonedDateTimeAssertion<>(this, value == null ? null : value.asDateHeader().asZonedDateTime().orElse(null), returns()); 236 } 237 238 @Override /* Overridden from Assertion */ 239 public FluentResponseHeaderAssertion<R> setMsg(String msg, Object...args) { 240 super.setMsg(msg, args); 241 return this; 242 } 243 244 @Override /* Overridden from Assertion */ 245 public FluentResponseHeaderAssertion<R> setOut(PrintStream value) { 246 super.setOut(value); 247 return this; 248 } 249 250 @Override /* Overridden from Assertion */ 251 public FluentResponseHeaderAssertion<R> setSilent() { 252 super.setSilent(); 253 return this; 254 } 255 256 @Override /* Overridden from Assertion */ 257 public FluentResponseHeaderAssertion<R> setStdOut() { 258 super.setStdOut(); 259 return this; 260 } 261 262 @Override /* Overridden from Assertion */ 263 public FluentResponseHeaderAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 264 super.setThrowable(value); 265 return this; 266 } 267}