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.server.assertions; 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.response.*; 027import org.apache.juneau.rest.server.httppart.*; 028import org.apache.juneau.bct.assertions.*; 029 030/** 031 * Used for fluent assertion calls against {@link RequestHeader} 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 FluentRequestHeaderAssertion} 077 * <ul class='javatreec'> 078 * <li class='jm'>{@link FluentRequestHeaderAssertion#asBoolean() asBoolean()} 079 * <li class='jm'>{@link FluentRequestHeaderAssertion#asInteger() asInteger()} 080 * <li class='jm'>{@link FluentRequestHeaderAssertion#asLong() asLong()} 081 * <li class='jm'>{@link FluentRequestHeaderAssertion#asZonedDateTime() asZonedDateTime()} 082 * <li class='jm'>{@link FluentRequestHeaderAssertion#as(Class) as(Class)} 083 * </ul> 084 * <li class='jc'>{@link FluentStringAssertion} 085 * <ul class='javatreec'> 086 * <li class='jm'>{@link FluentStringAssertion#asReplaceAll(String,String) asReplaceAll(String,String)} 087 * <li class='jm'>{@link FluentStringAssertion#asReplace(String,String) asReplace(String,String)} 088 * <li class='jm'>{@link FluentStringAssertion#asUrlDecode() asUrlDecode()} 089 * <li class='jm'>{@link FluentStringAssertion#asLc() asLc()} 090 * <li class='jm'>{@link FluentStringAssertion#asUc() asUc()} 091 * <li class='jm'>{@link FluentStringAssertion#asLines() asLines()} 092 * <li class='jm'>{@link FluentStringAssertion#asSplit(String) asSplit(String)} 093 * <li class='jm'>{@link FluentStringAssertion#asLength() asLength()} 094 * <li class='jm'>{@link FluentStringAssertion#asOneLine() asOneLine()} 095 * </ul> 096 * <li class='jc'>{@link FluentObjectAssertion} 097 * <ul class='javatreec'> 098 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 099 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 100 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 101 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 102 * </ul> 103 * </ul> 104 * 105 * <h5 class='topic'>Configuration Methods</h5> 106 * <p> 107 * <ul class='javatree'> 108 * <li class='jc'>{@link Assertion} 109 * <ul class='javatreec'> 110 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 111 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 112 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 113 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 114 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 115 * </ul> 116 * </ul> 117 * 118 * <h5 class='section'>See Also:</h5><ul> 119 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 120 * </ul> 121 * 122 * @param <R> The return type. 123 */ 124public class FluentRequestHeaderAssertion<R> extends FluentStringAssertion<R> { 125 126 private final RequestHeader value; 127 128 /** 129 * Chained constructor. 130 * 131 * <p> 132 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 133 * 134 * @param creator 135 * The assertion that created this assertion. 136 * <br>Should be <jk>null</jk> if this is the top-level assertion. 137 * @param value 138 * The object being tested. 139 * <br>Can be <jk>null</jk>, in which case this assertion behaves as if testing a part with no value (e.g. {@link #isNull()} passes, transform methods yield <jk>null</jk>-valued assertions). 140 * @param returns 141 * The object to return after a test method is called. 142 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 143 * used on the same assertion. 144 */ 145 public FluentRequestHeaderAssertion(Assertion creator, RequestHeader value, R returns) { 146 super(creator, value == null ? null : value.asString().orElse(null), returns); 147 this.value = value; 148 setThrowable(BadRequest.class); 149 } 150 151 /** 152 * Constructor. 153 * 154 * @param value 155 * The object being tested. 156 * <br>Can be <jk>null</jk>, in which case this assertion behaves as if testing a part with no value (e.g. {@link #isNull()} passes, transform methods yield <jk>null</jk>-valued assertions). 157 * @param returns 158 * The object to return after a test method is called. 159 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 160 * used on the same assertion. 161 */ 162 public FluentRequestHeaderAssertion(RequestHeader value, R returns) { 163 this(null, value, returns); 164 } 165 166 /** 167 * Converts the parameter value to a type using {@link RequestHeader#as(Class)} and then returns the value as an any-object assertion. 168 * 169 * @param <T> The object type to create. 170 * @param type The object type to create. 171 * @return A new fluent assertion object. 172 */ 173 public <T> FluentAnyAssertion<T,R> as(Class<T> type) { 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 RequestHeader#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 */ 187 public FluentAnyAssertion<Object,R> as(Type type, Type...args) { 188 return new FluentAnyAssertion<>(value == null ? null : value.as(type, args).orElse(null), returns()); 189 } 190 191 /** 192 * Converts this object assertion into a boolean assertion. 193 * 194 * @return A new assertion. 195 * @throws AssertionError If object is not a boolean. 196 */ 197 public FluentBooleanAssertion<R> asBoolean() { 198 return new FluentBooleanAssertion<>(this, value == null ? null : value.asBoolean().orElse(null), returns()); 199 } 200 201 /** 202 * Converts this object assertion into an integer assertion. 203 * 204 * @return A new assertion. 205 * @throws AssertionError If object is not an integer. 206 */ 207 public FluentIntegerAssertion<R> asInteger() { 208 return new FluentIntegerAssertion<>(this, value == null ? null : value.asInteger().orElse(null), returns()); 209 } 210 211 @Override /* Overridden from FluentStringAssertion */ 212 public FluentRequestHeaderAssertion<R> asJavaStrings() { 213 super.asJavaStrings(); 214 return this; 215 } 216 217 /** 218 * Converts this object assertion into a long assertion. 219 * 220 * @return A new assertion. 221 * @throws AssertionError If object is not a long. 222 */ 223 public FluentLongAssertion<R> asLong() { 224 return new FluentLongAssertion<>(this, value == null ? null : value.asLong().orElse(null), returns()); 225 } 226 227 /** 228 * Converts this object assertion into a zoned-datetime assertion. 229 * 230 * @return A new assertion. 231 * @throws AssertionError If object is not a zoned-datetime. 232 */ 233 public FluentZonedDateTimeAssertion<R> asZonedDateTime() { 234 return new FluentZonedDateTimeAssertion<>(this, value == null ? null : value.asDateHeader().asZonedDateTime().orElse(null), returns()); 235 } 236 237 @Override /* Overridden from Assertion */ 238 public FluentRequestHeaderAssertion<R> setMsg(String msg, Object...args) { 239 super.setMsg(msg, args); 240 return this; 241 } 242 243 @Override /* Overridden from Assertion */ 244 public FluentRequestHeaderAssertion<R> setOut(PrintStream value) { 245 super.setOut(value); 246 return this; 247 } 248 249 @Override /* Overridden from Assertion */ 250 public FluentRequestHeaderAssertion<R> setSilent() { 251 super.setSilent(); 252 return this; 253 } 254 255 @Override /* Overridden from Assertion */ 256 public FluentRequestHeaderAssertion<R> setStdOut() { 257 super.setStdOut(); 258 return this; 259 } 260 261 @Override /* Overridden from Assertion */ 262 public FluentRequestHeaderAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 263 super.setThrowable(value); 264 return this; 265 } 266}