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