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