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.util.function.*; 021 022import org.apache.http.*; 023import org.apache.juneau.commons.function.*; 024import org.apache.juneau.http.classic.response.*; 025import org.apache.juneau.bct.assertions.*; 026 027/** 028 * Used for fluent assertion calls against a response {@link StatusLine} object. 029 * 030 * <h2 class='section'>Example:</h2> 031 * <p class='bjava'> 032 * <jc>// Validates the response status code is 200 or 404.</jc> 033 * <jv>client</jv> 034 * .get(<jsf>URI</jsf>) 035 * .run() 036 * .assertStatus().asCode().isAny(200,404); 037 * </p> 038 * 039 * 040 * <h2 class='topic'>Test Methods</h2> 041 * <ul class='javatree'> 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 FluentResponseStatusLineAssertion} 062 * <ul class='javatreec'> 063 * <li class='jm'>{@link FluentResponseStatusLineAssertion#asCode() asCode()} 064 * <li class='jm'>{@link FluentResponseStatusLineAssertion#asReason() asReason()} 065 * <li class='jm'>{@link FluentResponseStatusLineAssertion#asProtocol() asProtocol()} 066 * <li class='jm'>{@link FluentResponseStatusLineAssertion#asMajor() asMajor()} 067 * <li class='jm'>{@link FluentResponseStatusLineAssertion#asMinor() asMinor()} 068 * </ul> 069 * <li class='jc'>{@link FluentObjectAssertion} 070 * <ul class='javatreec'> 071 * <li class='jm'>{@link FluentObjectAssertion#asString() asString()} 072 * <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)} 073 * <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)} 074 * <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()} 075 * </ul> 076 * </ul> 077 * 078 * <h2 class='topic'>Configuration Methods</h2> 079 * <ul class='javatree'> 080 * <li class='jc'>{@link Assertion} 081 * <ul class='javatreec'> 082 * <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)} 083 * <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)} 084 * <li class='jm'>{@link Assertion#setSilent() setSilent()} 085 * <li class='jm'>{@link Assertion#setStdOut() setStdOut()} 086 * <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)} 087 * </ul> 088 * </ul> 089 * 090 * <h2 class='section'>See Also:</h2><ul> 091 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a> 092 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClient">juneau-rest-client-classic</a> 093 * </ul> 094 * 095 * @param <R> The return type. 096 */ 097public class FluentResponseStatusLineAssertion<R> extends FluentObjectAssertion<StatusLine,R> { 098 099 /** 100 * Chained constructor. 101 * 102 * <p> 103 * Used when transforming one assertion into another so that the assertion config can be used by the new assertion. 104 * 105 * @param creator 106 * The assertion that created this assertion. 107 * <br>Should be <jk>null</jk> if this is the top-level assertion. 108 * @param value 109 * The object being tested. 110 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 111 * @param returns 112 * The object to return after a test method is called. 113 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 114 * used on the same assertion. 115 */ 116 public FluentResponseStatusLineAssertion(Assertion creator, StatusLine value, R returns) { 117 super(creator, value, returns); 118 setThrowable(BadRequest.class); 119 } 120 121 /** 122 * Constructor. 123 * 124 * @param value 125 * The object being tested. 126 * <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value). 127 * @param returns 128 * The object to return after a test method is called. 129 * <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be 130 * used on the same assertion. 131 */ 132 public FluentResponseStatusLineAssertion(StatusLine value, R returns) { 133 this(null, value, returns); 134 } 135 136 /** 137 * Returns an assertion against the status code on the response status object. 138 * 139 * @return An assertion against the status code on the response status object. 140 */ 141 public FluentIntegerAssertion<R> asCode() { 142 return new FluentIntegerAssertion<>(this, value().getStatusCode(), returns()); 143 } 144 145 /** 146 * Returns an assertion against the protocol major version on the response status object. 147 * 148 * @return An assertion against the protocol major version on the response status object. 149 */ 150 public FluentIntegerAssertion<R> asMajor() { 151 return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMajor(), returns()); 152 } 153 154 /** 155 * Returns an assertion against the protocol minor version on the response status object. 156 * 157 * @return An assertion against the protocol minor version on the response status object. 158 */ 159 public FluentIntegerAssertion<R> asMinor() { 160 return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMinor(), returns()); 161 } 162 163 /** 164 * Returns an assertion against the protocol on the response status object. 165 * 166 * @return An assertion against the protocol on the response status object. 167 */ 168 public FluentStringAssertion<R> asProtocol() { 169 return new FluentStringAssertion<>(this, value().getProtocolVersion().getProtocol(), returns()); 170 } 171 172 /** 173 * Returns an assertion against the reason phrase on the response status object. 174 * 175 * @return An assertion against the reason phrase on the response status object. 176 */ 177 public FluentStringAssertion<R> asReason() { 178 return new FluentStringAssertion<>(this, value().getReasonPhrase(), returns()); 179 } 180 181 @Override /* Overridden from Assertion */ 182 public FluentResponseStatusLineAssertion<R> setMsg(String msg, Object...args) { 183 super.setMsg(msg, args); 184 return this; 185 } 186 187 @Override /* Overridden from Assertion */ 188 public FluentResponseStatusLineAssertion<R> setOut(PrintStream value) { 189 super.setOut(value); 190 return this; 191 } 192 193 @Override /* Overridden from Assertion */ 194 public FluentResponseStatusLineAssertion<R> setSilent() { 195 super.setSilent(); 196 return this; 197 } 198 199 @Override /* Overridden from Assertion */ 200 public FluentResponseStatusLineAssertion<R> setStdOut() { 201 super.setStdOut(); 202 return this; 203 } 204 205 @Override /* Overridden from Assertion */ 206 public FluentResponseStatusLineAssertion<R> setThrowable(Class<? extends RuntimeException> value) { 207 super.setThrowable(value); 208 return this; 209 } 210}