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