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 RequestFormParam} 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 FluentRequestFormParamAssertion} 077 * <ul class='javatreec'> 078 * <li class='jm'>{@link FluentRequestFormParamAssertion#asBoolean() asBoolean()} 079 * <li class='jm'>{@link FluentRequestFormParamAssertion#asInteger() asInteger()} 080 * <li class='jm'>{@link FluentRequestFormParamAssertion#asLong() asLong()} 081 * <li class='jm'>{@link FluentRequestFormParamAssertion#asZonedDateTime() asZonedDateTime()} 082 * <li class='jm'>{@link FluentRequestFormParamAssertion#as(Class) as(Class)} 083 * <li class='jm'>{@link FluentRequestFormParamAssertion#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 * </ul> 122 * 123 * @param <R> The return type. 124 */ 125public class FluentRequestFormParamAssertion<R> extends FluentStringAssertion<R> { 126 127 private final RequestFormParam value; 128 129 /** 130 * Chained constructor. 131 * 132 * <p> 133 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 134 * 135 * @param creator 136 * The assertion that created this assertion. 137 * <br>Should be <jk>null</jk> if this is the top-level assertion. 138 * @param value 139 * The object being tested. 140 * <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). 141 * @param returns 142 * The object to return after a test method is called. 143 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 144 * used on the same assertion. 145 */ 146 public FluentRequestFormParamAssertion(Assertion creator, RequestFormParam value, R returns) { 147 super(creator, value == null ? null : value.asString().orElse(null), returns); 148 this.value = value; 149 setThrowable(BadRequest.class); 150 } 151 152 /** 153 * Constructor. 154 * 155 * @param value 156 * The object being tested. 157 * <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). 158 * @param returns 159 * The object to return after a test method is called. 160 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 161 * used on the same assertion. 162 */ 163 public FluentRequestFormParamAssertion(RequestFormParam value, R returns) { 164 this(null, value, returns); 165 } 166 167 /** 168 * Converts the parameter value to a type using {@link RequestFormParam#as(Class)} and then returns the value as an any-object assertion. 169 * 170 * @param <T> The object type to create. 171 * @param type The object type to create. 172 * @return A new fluent assertion object. 173 */ 174 public <T> FluentAnyAssertion<T,R> as(Class<T> type) { 175 return new FluentAnyAssertion<>(value == null ? null : value.as(type).orElse(null), returns()); 176 } 177 178 /** 179 * Converts the parameter value to a type using {@link RequestFormParam#as(Type,Type...)} and then returns the value as an any-object assertion. 180 * 181 * <p> 182 * 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}. 183 * 184 * @param type The object type to create. 185 * @param args Optional type arguments. 186 * @return A new fluent assertion object. 187 */ 188 public FluentAnyAssertion<Object,R> as(Type type, Type...args) { 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 FluentRequestFormParamAssertion<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.asDatePart().asZonedDateTime().orElse(null), returns()); 236 } 237 238 @Override /* Overridden from Assertion */ 239 public FluentRequestFormParamAssertion<R> setMsg(String msg, Object...args) { 240 super.setMsg(msg, args); 241 return this; 242 } 243 244 @Override /* Overridden from Assertion */ 245 public FluentRequestFormParamAssertion<R> setOut(PrintStream value) { 246 super.setOut(value); 247 return this; 248 } 249 250 @Override /* Overridden from Assertion */ 251 public FluentRequestFormParamAssertion<R> setSilent() { 252 super.setSilent(); 253 return this; 254 } 255 256 @Override /* Overridden from Assertion */ 257 public FluentRequestFormParamAssertion<R> setStdOut() { 258 super.setStdOut(); 259 return this; 260 } 261 262 @Override /* Overridden from Assertion */ 263 public FluentRequestFormParamAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 264 super.setThrowable(value); 265 return this; 266 } 267}