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 * <h5 class='topic'>Test Methods</h5> 034 * <p> 035 * <ul class='javatree'> 036 * <li class='jc'>{@link FluentStringAssertion} 037 * <ul class='javatreec'> 038 * <li class='jm'>{@link FluentStringAssertion#is(String) is(String)} 039 * <li class='jm'>{@link FluentStringAssertion#isNot(String) isNot(String)} 040 * <li class='jm'>{@link FluentStringAssertion#isLines(String...) isLines(String...)} 041 * <li class='jm'>{@link FluentStringAssertion#isSortedLines(String...) isSortedLines(String...)} 042 * <li class='jm'>{@link FluentStringAssertion#isIc(String) isIc(String)} 043 * <li class='jm'>{@link FluentStringAssertion#isNotIc(String) isNotIc(String)} 044 * <li class='jm'>{@link FluentStringAssertion#isContains(String...) isContains(String...)} 045 * <li class='jm'>{@link FluentStringAssertion#isNotContains(String...) isNotContains(String...)} 046 * <li class='jm'>{@link FluentStringAssertion#isEmpty() isEmpty()} 047 * <li class='jm'>{@link FluentStringAssertion#isNotEmpty() isNotEmpty()} 048 * <li class='jm'>{@link FluentStringAssertion#isString(Object) isString(Object)} 049 * <li class='jm'>{@link FluentStringAssertion#isMatches(String) isMatches(String)} 050 * <li class='jm'>{@link FluentStringAssertion#isPattern(String) isPattern(String)} 051 * <li class='jm'>{@link FluentStringAssertion#isPattern(String,int) isPattern(String,int)} 052 * <li class='jm'>{@link FluentStringAssertion#isPattern(Pattern) isPattern(Pattern)} 053 * <li class='jm'>{@link FluentStringAssertion#isStartsWith(String) isStartsWith(String)} 054 * <li class='jm'>{@link FluentStringAssertion#isEndsWith(String) isEndsWith(String)} 055 * </ul> 056 * <li class='jc'>{@link FluentObjectAssertion} 057 * <ul class='javatreec'> 058 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 059 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 060 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 061 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 062 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 063 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 064 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 065 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 066 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 067 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 068 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 069 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 070 * </ul> 071 * </ul> 072 * 073 * <h5 class='topic'>Transform Methods</h5> 074 * <p> 075 * <ul class='javatree'> 076 * <li class='jc'>{@link FluentResponseHeaderAssertion} 077 * <ul class='javatreec'> 078 * <li class='jm'>{@link FluentResponseHeaderAssertion#asBoolean() asBoolean()} 079 * <li class='jm'>{@link FluentResponseHeaderAssertion#asInteger() asInteger()} 080 * <li class='jm'>{@link FluentResponseHeaderAssertion#asLong() asLong()} 081 * <li class='jm'>{@link FluentResponseHeaderAssertion#asZonedDateTime() asZonedDateTime()} 082 * <li class='jm'>{@link FluentResponseHeaderAssertion#as(Class) as(Class)} 083 * <li class='jm'>{@link FluentResponseHeaderAssertion#as(Type,Type...) as(Type,Type...)} 084 * </ul> 085 * <li class='jc'>{@link FluentStringAssertion} 086 * <ul class='javatreec'> 087 * <li class='jm'>{@link FluentStringAssertion#asReplaceAll(String,String) asReplaceAll(String,String)} 088 * <li class='jm'>{@link FluentStringAssertion#asReplace(String,String) asReplace(String,String)} 089 * <li class='jm'>{@link FluentStringAssertion#asUrlDecode() asUrlDecode()} 090 * <li class='jm'>{@link FluentStringAssertion#asLc() asLc()} 091 * <li class='jm'>{@link FluentStringAssertion#asUc() asUc()} 092 * <li class='jm'>{@link FluentStringAssertion#asLines() asLines()} 093 * <li class='jm'>{@link FluentStringAssertion#asSplit(String) asSplit(String)} 094 * <li class='jm'>{@link FluentStringAssertion#asLength() asLength()} 095 * <li class='jm'>{@link FluentStringAssertion#asOneLine() asOneLine()} 096 * </ul> 097 * <li class='jc'>{@link FluentObjectAssertion} 098 * <ul class='javatreec'> 099 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 100 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 101 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 102 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 103 * </ul> 104 * </ul> 105 * 106 * <h5 class='topic'>Configuration Methods</h5> 107 * <p> 108 * <ul class='javatree'> 109 * <li class='jc'>{@link Assertion} 110 * <ul class='javatreec'> 111 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 112 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 113 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 114 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 115 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 116 * </ul> 117 * </ul> 118 * 119 * <h5 class='section'>See Also:</h5><ul> 120 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 121 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClient">juneau-rest-client-classic</a> 122 * </ul> 123 * 124 * @param <R> The return type. 125 */ 126public class FluentResponseHeaderAssertion<R> extends FluentStringAssertion<R> { 127 128 private final ResponseHeader value; 129 130 /** 131 * Chained constructor. 132 * 133 * <p> 134 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 135 * 136 * @param creator 137 * The assertion that created this assertion. 138 * <br>Should be <jk>null</jk> if this is the top-level assertion. 139 * @param value 140 * The object being tested. 141 * <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). 142 * @param returns 143 * The object to return after a test method is called. 144 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 145 * used on the same assertion. 146 */ 147 public FluentResponseHeaderAssertion(Assertion creator, ResponseHeader value, R returns) { 148 super(creator, value == null ? null : value.asString().orElse(null), returns); 149 this.value = value; 150 setThrowable(BadRequest.class); 151 } 152 153 /** 154 * Constructor. 155 * 156 * @param value 157 * The object being tested. 158 * <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). 159 * @param returns 160 * The object to return after a test method is called. 161 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 162 * used on the same assertion. 163 */ 164 public FluentResponseHeaderAssertion(ResponseHeader value, R returns) { 165 this(null, value, returns); 166 } 167 168 /** 169 * Converts the parameter value to a type using {@link ResponseHeader#as(Class)} and then returns the value as an any-object assertion. 170 * 171 * @param <T> The object type to create. 172 * @param type The object type to create. 173 * @return A new fluent assertion object. 174 * @throws RestCallException If value could not be parsed. 175 */ 176 public <T> FluentAnyAssertion<T,R> as(Class<T> type) throws RestCallException { 177 return new FluentAnyAssertion<>(value == null ? null : value.as(type).orElse(null), returns()); 178 } 179 180 /** 181 * Converts the parameter value to a type using {@link ResponseHeader#as(Type,Type...)} and then returns the value as an any-object assertion. 182 * 183 * <p> 184 * 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}. 185 * 186 * @param type The object type to create. 187 * @param args Optional type arguments. 188 * @return A new fluent assertion object. 189 * @throws RestCallException If value could not be parsed. 190 */ 191 public FluentAnyAssertion<Object,R> as(Type type, Type...args) throws RestCallException { 192 return new FluentAnyAssertion<>(value == null ? null : value.as(type, args).orElse(null), returns()); 193 } 194 195 /** 196 * Converts this object assertion into a boolean assertion. 197 * 198 * @return A new assertion. 199 * @throws AssertionError If object is not a boolean. 200 */ 201 public FluentBooleanAssertion<R> asBoolean() { 202 return new FluentBooleanAssertion<>(this, value == null ? null : value.asBoolean().orElse(null), returns()); 203 } 204 205 /** 206 * Converts this object assertion into an integer assertion. 207 * 208 * @return A new assertion. 209 * @throws AssertionError If object is not an integer. 210 */ 211 public FluentIntegerAssertion<R> asInteger() { 212 return new FluentIntegerAssertion<>(this, value == null ? null : value.asInteger().orElse(null), returns()); 213 } 214 215 @Override /* Overridden from FluentStringAssertion */ 216 public FluentResponseHeaderAssertion<R> asJavaStrings() { 217 super.asJavaStrings(); 218 return this; 219 } 220 221 /** 222 * Converts this object assertion into a long assertion. 223 * 224 * @return A new assertion. 225 * @throws AssertionError If object is not a long. 226 */ 227 public FluentLongAssertion<R> asLong() { 228 return new FluentLongAssertion<>(this, value == null ? null : value.asLong().orElse(null), returns()); 229 } 230 231 /** 232 * Converts this object assertion into a zoned-datetime assertion. 233 * 234 * @return A new assertion. 235 * @throws AssertionError If object is not a zoned-datetime. 236 */ 237 public FluentZonedDateTimeAssertion<R> asZonedDateTime() { 238 return new FluentZonedDateTimeAssertion<>(this, value == null ? null : value.asDateHeader().asZonedDateTime().orElse(null), returns()); 239 } 240 241 @Override /* Overridden from Assertion */ 242 public FluentResponseHeaderAssertion<R> setMsg(String msg, Object...args) { 243 super.setMsg(msg, args); 244 return this; 245 } 246 247 @Override /* Overridden from Assertion */ 248 public FluentResponseHeaderAssertion<R> setOut(PrintStream value) { 249 super.setOut(value); 250 return this; 251 } 252 253 @Override /* Overridden from Assertion */ 254 public FluentResponseHeaderAssertion<R> setSilent() { 255 super.setSilent(); 256 return this; 257 } 258 259 @Override /* Overridden from Assertion */ 260 public FluentResponseHeaderAssertion<R> setStdOut() { 261 super.setStdOut(); 262 return this; 263 } 264 265 @Override /* Overridden from Assertion */ 266 public FluentResponseHeaderAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 267 super.setThrowable(value); 268 return this; 269 } 270}