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.*; 023 024import org.apache.juneau.commons.function.*; 025import org.apache.juneau.http.response.*; 026import org.apache.juneau.rest.server.httppart.*; 027import org.apache.juneau.bct.assertions.*; 028 029/** 030 * Used for fluent assertion calls against {@link RequestContent} objects. 031 * 032 * <h2 class='topic'>Test Methods</h2> 033 * <ul class='javatree'> 034 * <li class='jc'>{@link FluentRequestContentAssertion} 035 * <ul class='javatreec'> 036 * <li class='jm'>{@link FluentRequestContentAssertion#is(String) is(String)} 037 * <li class='jm'>{@link FluentRequestContentAssertion#isContains(String...) isContains(String...)} 038 * <li class='jm'>{@link FluentRequestContentAssertion#isNotContains(String...) isNotContains(String...)} 039 * <li class='jm'>{@link FluentRequestContentAssertion#isEmpty() isEmpty()} 040 * <li class='jm'>{@link FluentRequestContentAssertion#isNotEmpty() isNotEmpty()} 041 * </ul> 042 * <li class='jc'>{@link FluentObjectAssertion} 043 * <ul class='javatreec'> 044 * <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()} 045 * <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)} 046 * <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)} 047 * <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)} 048 * <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)} 049 * <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)} 050 * <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()} 051 * <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()} 052 * <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)} 053 * <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)} 054 * <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)} 055 * <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)} 056 * </ul> 057 * </ul> 058 * 059 * <h2 class='topic'>Transform Methods</h2> 060 * <ul class='javatree'> 061 * <li class='jc'>{@link FluentRequestContentAssertion} 062 * <ul class='javatreec'> 063 * <li class='jm'>{@link FluentRequestContentAssertion#asBytes() asBytes()} 064 * <li class='jm'>{@link FluentRequestContentAssertion#as(Class) as(Class)} 065 * <li class='jm'>{@link FluentRequestContentAssertion#as(Type,Type...) as(Type,Type...)} 066 * </ul> 067 * <li class='jc'>{@link FluentObjectAssertion} 068 * <ul class='javatreec'> 069 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 070 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 071 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 072 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 073 * </ul> 074 * </ul> 075 * 076 * <h2 class='topic'>Configuration Methods</h2> 077 * <ul class='javatree'> 078 * <li class='jc'>{@link Assertion} 079 * <ul class='javatreec'> 080 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 081 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 082 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 083 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 084 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 085 * </ul> 086 * </ul> 087 * 088 * <h2 class='section'>See Also:</h2><ul> 089 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 090 * </ul> 091 * 092 * @param <R> The return type. 093 */ 094@SuppressWarnings({ 095 "java:S115", // Constants use UPPER_snakeCase convention (e.g., MSG_exceptionDuringCall) 096 "java:S1192" // Duplicated literals read more clearly inline than as constants. 097}) 098public class FluentRequestContentAssertion<R> extends FluentObjectAssertion<RequestContent,R> { 099 100 /** 101 * Chained constructor. 102 * 103 * <p> 104 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 105 * 106 * @param creator 107 * The assertion that created this assertion. 108 * <br>Should be <jk>null</jk> if this is the top-level assertion. 109 * @param value 110 * The object being tested. 111 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 112 * @param returns 113 * The object to return after a test method is called. 114 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 115 * used on the same assertion. 116 */ 117 public FluentRequestContentAssertion(Assertion creator, RequestContent value, R returns) { 118 super(creator, value, returns); 119 setThrowable(BadRequest.class); 120 } 121 122 /** 123 * Constructor. 124 * 125 * @param value 126 * The object being tested. 127 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 128 * @param returns 129 * The object to return after a test method is called. 130 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 131 * used on the same assertion. 132 */ 133 public FluentRequestContentAssertion(RequestContent value, R returns) { 134 this(null, value, returns); 135 } 136 137 /** 138 * Converts the content to a type using {@link RequestContent#as(Class)} and then returns the value as an object assertion. 139 * 140 * <h4 class='section'>Examples:</h4> 141 * <p class='bjava'> 142 * <jc>// Validates the request content bean is the expected value.</jc> 143 * <jv>request</jv> 144 * .assertContent() 145 * .as(MyBean.<jk>class</jk>) 146 * .asJson().is(<js>"{foo:'bar'}"</js>); 147 * </p> 148 * 149 * <h4 class='section'>Notes:</h4><ul> 150 * <li class='note'> 151 * If no charset was found on the <code>Content-Type</code> request header, <js>"UTF-8"</js> is assumed. 152 * <li class='note'> 153 * When using this method, the content is automatically cached by calling the {@link RequestContent#cache()}. 154 * <li class='note'> 155 * The input stream is automatically closed after this call. 156 * </ul> 157 * 158 * <p> 159 * 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}. 160 * 161 * @param <T> The object type to create. 162 * @param type The object type to create. 163 * @return A new fluent assertion object. 164 */ 165 public <T> FluentObjectAssertion<T,R> as(Class<T> type) { 166 return new FluentObjectAssertion<>(valueAsType(type), returns()); 167 } 168 169 /** 170 * Converts the content to a type using {@link RequestContent#as(Type,Type...)} and then returns the value as an object assertion. 171 * 172 * <h4 class='section'>Examples:</h4> 173 * <p class='bjava'> 174 * <jc>// Validates the request content bean is the expected value.</jc> 175 * <jv>request</jv> 176 * .assertContent() 177 * .as(Map.<jk>class</jk>,String.<jk>class</jk>,Integer.<jk>class</jk>) 178 * .asJson().is(<js>"{foo:123}"</js>); 179 * </p> 180 * 181 * <h4 class='section'>Notes:</h4><ul> 182 * <li class='note'> 183 * If no charset was found on the <code>Content-Type</code> request header, <js>"UTF-8"</js> is assumed. 184 * <li class='note'> 185 * When using this method, the content is automatically cached by calling the {@link RequestContent#cache()}. 186 * <li class='note'> 187 * The input stream is automatically closed after this call. 188 * </ul> 189 * 190 * <p> 191 * 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}. 192 * 193 * @param <T> The type to create. 194 * @param type The object type to create. 195 * @param args Optional type arguments. 196 * @return A new fluent assertion object. 197 */ 198 public <T> FluentObjectAssertion<T,R> as(Type type, Type...args) { 199 return new FluentObjectAssertion<>(valueAsType(type, args), returns()); 200 } 201 202 /** 203 * Provides the ability to perform fluent-style assertions on the bytes of the request content. 204 * 205 * <h4 class='section'>Examples:</h4> 206 * <p class='bjava'> 207 * <jc>// Validates the request content equals the text "foo".</jc> 208 * <jv>request</jv> 209 * .assertContent().asBytes().asHex().is(<js>"666F6F"</js>); 210 * </p> 211 * 212 * <h4 class='section'>Notes:</h4><ul> 213 * <li class='note'> 214 * If no charset was found on the <code>Content-Type</code> request header, <js>"UTF-8"</js> is assumed. 215 * <li class='note'> 216 * When using this method, the content is automatically cached by calling the {@link RequestContent#cache()}. 217 * <li class='note'> 218 * The input stream is automatically closed after this call. 219 * </ul> 220 * 221 * @return A new fluent assertion object. 222 */ 223 public FluentByteArrayAssertion<R> asBytes() { 224 return new FluentByteArrayAssertion<>(valueAsBytes(), returns()); 225 } 226 227 /** 228 * Asserts that the content contains the specified value. 229 * 230 * @param values The value to check against. 231 * @return This object. 232 * @throws AssertionError If assertion failed. 233 */ 234 public R is(String values) throws AssertionError { 235 return asString().is(values); 236 } 237 238 /** 239 * Asserts that the text contains all of the specified substrings. 240 * 241 * @param values The values to check against. 242 * @return This object. 243 * @throws AssertionError If assertion failed. 244 */ 245 public R isContains(String...values) throws AssertionError { 246 return asString().isContains(values); 247 } 248 249 /** 250 * Asserts that the content is empty. 251 * 252 * @return This object. 253 * @throws AssertionError If assertion failed. 254 */ 255 public R isEmpty() { return asString().isEmpty(); } 256 257 /** 258 * Asserts that the content doesn't contain any of the specified substrings. 259 * 260 * @param values The values to check against. 261 * @return This object. 262 * @throws AssertionError If assertion failed. 263 */ 264 public R isNotContains(String...values) throws AssertionError { 265 return asString().isNotContains(values); 266 } 267 268 /** 269 * Asserts that the content is not empty. 270 * 271 * @return This object. 272 * @throws AssertionError If assertion failed. 273 */ 274 public R isNotEmpty() { return asString().isNotEmpty(); } 275 276 @Override /* Overridden from Assertion */ 277 public FluentRequestContentAssertion<R> setMsg(String msg, Object...args) { 278 super.setMsg(msg, args); 279 return this; 280 } 281 282 @Override /* Overridden from Assertion */ 283 public FluentRequestContentAssertion<R> setOut(PrintStream value) { 284 super.setOut(value); 285 return this; 286 } 287 288 @Override /* Overridden from Assertion */ 289 public FluentRequestContentAssertion<R> setSilent() { 290 super.setSilent(); 291 return this; 292 } 293 294 @Override /* Overridden from Assertion */ 295 public FluentRequestContentAssertion<R> setStdOut() { 296 super.setStdOut(); 297 return this; 298 } 299 300 @Override /* Overridden from Assertion */ 301 public FluentRequestContentAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 302 super.setThrowable(value); 303 return this; 304 } 305 306 private byte[] valueAsBytes() throws AssertionError { 307 try { 308 return value().cache().asBytes(); 309 } catch (IOException e) { 310 throw error(e, "Exception occurred during call."); 311 } 312 } 313 314 private <T> T valueAsType(Class<T> c) throws AssertionError { 315 try { 316 return value().cache().as(c); 317 } catch (IOException e) { 318 throw error(e, "Exception occurred during call."); 319 } 320 } 321 322 private <T> T valueAsType(Type c, Type...args) throws AssertionError { 323 try { 324 return value().cache().as(c, args); 325 } catch (IOException e) { 326 throw error(e, "Exception occurred during call."); 327 } 328 } 329 330 @Override 331 protected String valueAsString() throws AssertionError { 332 try { 333 return value().cache().asString(); 334 } catch (IOException e) { 335 throw error(e, "Exception occurred during call."); 336 } 337 } 338}