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.client.classic.assertion; 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.classic.response.*; 026import org.apache.juneau.rest.client.classic.*; 027import org.apache.juneau.bct.assertions.*; 028 029/** 030 * Used for fluent assertion calls against {@link ResponseContent} objects. 031 * 032 * <h2 class='topic'>Test Methods</h2> 033 * <ul class='javatree'> 034 * <li class='jc'>{@link FluentResponseBodyAssertion} 035 * <ul class='javatreec'> 036 * <li class='jm'>{@link FluentResponseBodyAssertion#is(String) is(String)} 037 * <li class='jm'>{@link FluentResponseBodyAssertion#isContains(String...) isContains(String...)} 038 * <li class='jm'>{@link FluentResponseBodyAssertion#isNotContains(String...) isNotContains(String...)} 039 * <li class='jm'>{@link FluentResponseBodyAssertion#isEmpty() isEmpty()} 040 * <li class='jm'>{@link FluentResponseBodyAssertion#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 FluentResponseBodyAssertion} 062 * <ul class='javatreec'> 063 * <li class='jm'>{@link FluentResponseBodyAssertion#asBytes() asBytes()} 064 * <li class='jm'>{@link FluentResponseBodyAssertion#as(Class) as(Class)} 065 * <li class='jm'>{@link FluentResponseBodyAssertion#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 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClient">juneau-rest-client-classic</a> 091 * </ul> 092 * 093 * @param <R> The return type. 094 */ 095@SuppressWarnings({ 096 "java:S115", // Constants use UPPER_snakeCase convention (e.g., MSG_exceptionDuringCall) 097 "java:S1192" // Duplicated literals read more clearly inline than as constants. 098}) 099public class FluentResponseBodyAssertion<R> extends FluentObjectAssertion<ResponseContent,R> { 100 101 /** 102 * Chained constructor. 103 * 104 * <p> 105 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 106 * 107 * @param creator 108 * The assertion that created this assertion. 109 * <br>Should be <jk>null</jk> if this is the top-level assertion. 110 * @param value 111 * The object being tested. 112 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 113 * @param returns 114 * The object to return after a test method is called. 115 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 116 * used on the same assertion. 117 */ 118 public FluentResponseBodyAssertion(Assertion creator, ResponseContent value, R returns) { 119 super(creator, value, returns); 120 setThrowable(BadRequest.class); 121 } 122 123 /** 124 * Constructor. 125 * 126 * @param value 127 * The object being tested. 128 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 129 * @param returns 130 * The object to return after a test method is called. 131 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 132 * used on the same assertion. 133 */ 134 public FluentResponseBodyAssertion(ResponseContent value, R returns) { 135 this(null, value, returns); 136 } 137 138 /** 139 * Converts the body to a type using {@link ResponseContent#as(Class)} and then returns the value as an object assertion. 140 * 141 * <h4 class='section'>Examples:</h4> 142 * <p class='bjava'> 143 * <jc>// Validates the response body as a list of strings and validates the length.</jc> 144 * <jv>client</jv> 145 * .get(<js>"/myBean"</js>) 146 * .run() 147 * .assertContent().as(List.<jk>class</jk>, String.<jk>class</jk>).is(<jv>x</jv> -> <jv>x</jv>.size() > 0); 148 * </p> 149 * 150 * @param <T> The object type to create. 151 * @param type The object type to create. 152 * @return A new fluent assertion object. 153 */ 154 public <T> FluentAnyAssertion<T,R> as(Class<T> type) { 155 return new FluentAnyAssertion<>(valueAsType(type), returns()); 156 } 157 158 /** 159 * Converts the body to a type using {@link ResponseContent#as(Type,Type...)} and then returns the value as an object assertion. 160 * 161 * <h4 class='section'>Examples:</h4> 162 * <p class='bjava'> 163 * <jc>// Validates the response body as a list of strings and validates the length.</jc> 164 * <jv>client</jv> 165 * .get(<js>"/myBean"</js>) 166 * .run() 167 * .assertContent().as(List.<jk>class</jk>, String.<jk>class</jk>).is(<jv>x</jv> -> <jv>x</jv>.size() > 0); 168 * </p> 169 * 170 * <p> 171 * 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}. 172 * 173 * @param type The object type to create. 174 * @param args Optional type arguments. 175 * @return A new fluent assertion object. 176 */ 177 public FluentAnyAssertion<Object,R> as(Type type, Type...args) { 178 return new FluentAnyAssertion<>(valueAsType(type, args), returns()); 179 } 180 181 /** 182 * Provides the ability to perform fluent-style assertions on the bytes of the response body. 183 * 184 * <h4 class='section'>Examples:</h4> 185 * <p class='bjava'> 186 * <jc>// Validates the response body equals the text "foo".</jc> 187 * <jv>client</jv> 188 * .get(<jsf>URI</jsf>) 189 * .run() 190 * .assertContent().asBytes().asHex().is(<js>"666F6F"</js>); 191 * </p> 192 * 193 * <h4 class='section'>Notes:</h4><ul> 194 * <li class='note'> 195 * If no charset was found on the <code>Content-Type</code> response header, <js>"UTF-8"</js> is assumed. 196 * <li class='note'> 197 * When using this method, the body is automatically cached by calling the {@link ResponseContent#cache()}. 198 * <li class='note'> 199 * The input stream is automatically closed after this call. 200 * </ul> 201 * 202 * @return A new fluent assertion object. 203 */ 204 public FluentByteArrayAssertion<R> asBytes() { 205 return new FluentByteArrayAssertion<>(valueAsBytes(), returns()); 206 } 207 208 /** 209 * Provides the ability to perform fluent-style assertions on this response body. 210 * 211 * <h4 class='section'>Examples:</h4> 212 * <p class='bjava'> 213 * <jc>// Validates the response body equals the text "OK".</jc> 214 * <jv>client</jv> 215 * .get(<jsf>URI</jsf>) 216 * .run() 217 * .assertContent().is(<js>"OK"</js>); 218 * 219 * <jc>// Validates the response body contains the text "OK".</jc> 220 * <jv>client</jv> 221 * .get(<jsf>URI</jsf>) 222 * .run() 223 * .assertContent().isContains(<js>"OK"</js>); 224 * 225 * <jc>// Validates the response body passes a predicate test.</jc> 226 * <jv>client</jv> 227 * .get(<jsf>URI</jsf>) 228 * .run() 229 * .assertContent().is(<jv>x</jv> -> <jv>x</jv>.contains(<js>"OK"</js>)); 230 * 231 * <jc>// Validates the response body matches a regular expression.</jc> 232 * <jv>client</jv> 233 * .get(<jsf>URI</jsf>) 234 * .run() 235 * .assertContent().isPattern(<js>".*OK.*"</js>); 236 * 237 * <jc>// Validates the response body matches a regular expression using regex flags.</jc> 238 * <jv>client</jv> 239 * .get(<jsf>URI</jsf>) 240 * .run() 241 * .assertContent().isPattern(<js>".*OK.*"</js>, <jsf>MULTILINE</jsf> & <jsf>CASE_INSENSITIVE</jsf>); 242 * 243 * <jc>// Validates the response body matches a regular expression in the form of an existing Pattern.</jc> 244 * Pattern <jv>pattern</jv> = Pattern.<jsm>compile</jsm>(<js>".*OK.*"</js>); 245 * <jv>client</jv> 246 * .get(<jsf>URI</jsf>) 247 * .run() 248 * .assertContent().isPattern(<jv>pattern</jv>); 249 * </p> 250 * 251 * <p> 252 * The assertion test returns the original response object allowing you to chain multiple requests like so: 253 * <p class='bjava'> 254 * <jc>// Validates the response body matches a regular expression.</jc> 255 * MyBean <jv>bean</jv> = <jv>client</jv> 256 * .get(<jsf>URI</jsf>) 257 * .run() 258 * .assertContent().isPattern(<js>".*OK.*"</js>); 259 * .assertContent().isNotPattern(<js>".*ERROR.*"</js>) 260 * .getContent().as(MyBean.<jk>class</jk>); 261 * </p> 262 * 263 * <h4 class='section'>Notes:</h4><ul> 264 * <li class='note'> 265 * If no charset was found on the <code>Content-Type</code> response header, <js>"UTF-8"</js> is assumed. 266 * <li class='note'> 267 * When using this method, the body is automatically cached by calling the {@link ResponseContent#cache()}. 268 * <li class='note'> 269 * The input stream is automatically closed after this call. 270 * </ul> 271 * 272 * @return A new fluent assertion object. 273 */ 274 @Override 275 public FluentStringAssertion<R> asString() { 276 return new FluentStringAssertion<>(valueAsString(), returns()); 277 } 278 279 /** 280 * Asserts that the body contains the specified value. 281 * 282 * @param value The value to check against. 283 * @return This object. 284 * @throws AssertionError If assertion failed. 285 */ 286 public R is(String value) throws AssertionError { 287 return asString().is(value); 288 } 289 290 /** 291 * Asserts that the text contains all of the specified substrings. 292 * 293 * @param values The values to check against. 294 * @return This object. 295 * @throws AssertionError If assertion failed. 296 */ 297 public R isContains(String...values) throws AssertionError { 298 return asString().isContains(values); 299 } 300 301 /** 302 * Asserts that the body is empty. 303 * 304 * @return This object. 305 * @throws AssertionError If assertion failed. 306 */ 307 public R isEmpty() { return asString().isEmpty(); } 308 309 /** 310 * Asserts that the body doesn't contain any of the specified substrings. 311 * 312 * @param values The values to check against. 313 * @return This object. 314 * @throws AssertionError If assertion failed. 315 */ 316 public R isNotContains(String...values) throws AssertionError { 317 return asString().isNotContains(values); 318 } 319 320 /** 321 * Asserts that the body is not empty. 322 * 323 * @return This object. 324 * @throws AssertionError If assertion failed. 325 */ 326 public R isNotEmpty() { return asString().isNotEmpty(); } 327 328 @Override /* Overridden from Assertion */ 329 public FluentResponseBodyAssertion<R> setMsg(String msg, Object...args) { 330 super.setMsg(msg, args); 331 return this; 332 } 333 334 @Override /* Overridden from Assertion */ 335 public FluentResponseBodyAssertion<R> setOut(PrintStream value) { 336 super.setOut(value); 337 return this; 338 } 339 340 @Override /* Overridden from Assertion */ 341 public FluentResponseBodyAssertion<R> setSilent() { 342 super.setSilent(); 343 return this; 344 } 345 346 @Override /* Overridden from Assertion */ 347 public FluentResponseBodyAssertion<R> setStdOut() { 348 super.setStdOut(); 349 return this; 350 } 351 352 @Override /* Overridden from Assertion */ 353 public FluentResponseBodyAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 354 super.setThrowable(value); 355 return this; 356 } 357 358 private byte[] valueAsBytes() throws AssertionError { 359 try { 360 return value().cache().asBytes(); 361 } catch (RestCallException e) { 362 throw error(e, "Exception occurred during call."); 363 } 364 } 365 366 private <T> T valueAsType(Type type, Type...args) throws AssertionError { 367 try { 368 return value().cache().as(type, args); 369 } catch (RestCallException e) { 370 throw error(e, "Exception occurred during call."); 371 } 372 } 373 374 @Override 375 protected String valueAsString() throws AssertionError { 376 try { 377 return value().cache().asString(); 378 } catch (RestCallException e) { 379 throw error(e, "Exception occurred during call."); 380 } 381 } 382}