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