001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client.assertion;
014
015import java.io.*;
016import java.util.function.*;
017
018import org.apache.http.*;
019import org.apache.juneau.assertions.*;
020import org.apache.juneau.http.response.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.serializer.*;
023
024/**
025 * Used for fluent assertion calls against a response {@link StatusLine} object.
026 *
027 * <h5 class='section'>Example:</h5>
028 * <p class='bjava'>
029 *    <jc>// Validates the response status code is 200 or 404.</jc>
030 *    <jv>client</jv>
031 *       .get(<jsf>URI</jsf>)
032 *       .run()
033 *       .assertStatus().asCode().isAny(200,404);
034 * </p>
035 *
036 *
037 * <h5 class='topic'>Test Methods</h5>
038 * <p>
039 * <ul class='javatree'>
040 *    <li class='jc'>{@link FluentObjectAssertion}
041 *    <ul class='javatreec'>
042 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
043 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
044 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
045 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
046 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
047 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
048 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
049 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
050 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
051 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
058 *    </ul>
059 * </ul>
060 *
061 * <h5 class='topic'>Transform Methods</h5>
062 * <p>
063 * <ul class='javatree'>
064 *    <li class='jc'>{@link FluentResponseStatusLineAssertion}
065 *    <ul class='javatreec'>
066 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asCode() asCode()}
067 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asReason() asReason()}
068 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asProtocol() asProtocol()}
069 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asMajor() asMajor()}
070 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asMinor() asMinor()}
071 *    </ul>
072 *    <li class='jc'>{@link FluentObjectAssertion}
073 *    <ul class='javatreec'>
074 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
075 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
076 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
077 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
078 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
079 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
080 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
081 * </ul>
082 * </ul>
083 *
084 * <h5 class='topic'>Configuration Methods</h5>
085 * <p>
086 * <ul class='javatree'>
087 *    <li class='jc'>{@link Assertion}
088 *    <ul class='javatreec'>
089 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
090 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
091 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
092 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
093 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
094 *    </ul>
095 * </ul>
096 *
097 * <h5 class='section'>See Also:</h5><ul>
098 *    <li class='link'><a class="doclink" href="../../../../../../index.html#ja.Overview">juneau-assertions</a>
099 *    <li class='link'><a class="doclink" href="../../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
100 * </ul>
101 *
102 * @param <R> The return type.
103 */
104@FluentSetters(returns="FluentResponseStatusLineAssertion<R>")
105public class FluentResponseStatusLineAssertion<R> extends FluentObjectAssertion<StatusLine,R> {
106
107   //-----------------------------------------------------------------------------------------------------------------
108   // Constructors
109   //-----------------------------------------------------------------------------------------------------------------
110
111   /**
112    * Constructor.
113    *
114    * @param value
115    *    The object being tested.
116    *    <br>Can be <jk>null</jk>.
117    * @param returns
118    *    The object to return after a test method is called.
119    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
120    * used on the same assertion.
121    */
122   public FluentResponseStatusLineAssertion(StatusLine value, R returns) {
123      this(null, value, returns);
124   }
125
126   /**
127    * Chained constructor.
128    *
129    * <p>
130    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
131    *
132    * @param creator
133    *    The assertion that created this assertion.
134    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
135    * @param value
136    *    The object being tested.
137    *    <br>Can be <jk>null</jk>.
138    * @param returns
139    *    The object to return after a test method is called.
140    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
141    * used on the same assertion.
142    */
143   public FluentResponseStatusLineAssertion(Assertion creator, StatusLine value, R returns) {
144      super(creator, value, returns);
145      setThrowable(BadRequest.class);
146   }
147
148   //-----------------------------------------------------------------------------------------------------------------
149   // Transform methods
150   //-----------------------------------------------------------------------------------------------------------------
151
152   /**
153    * Returns an assertion against the status code on the response status object.
154    *
155    * @return An assertion against the status code on the response status object.
156    */
157   public FluentIntegerAssertion<R> asCode() {
158      return new FluentIntegerAssertion<>(this, value().getStatusCode(), returns());
159   }
160
161   /**
162    * Returns an assertion against the reason phrase on the response status object.
163    *
164    * @return An assertion against the reason phrase on the response status object.
165    */
166   public FluentStringAssertion<R> asReason() {
167      return new FluentStringAssertion<>(this, value().getReasonPhrase(), returns());
168   }
169
170   /**
171    * Returns an assertion against the protocol on the response status object.
172    *
173    * @return An assertion against the protocol on the response status object.
174    */
175   public FluentStringAssertion<R> asProtocol() {
176      return new FluentStringAssertion<>(this, value().getProtocolVersion().getProtocol(), returns());
177   }
178
179   /**
180    * Returns an assertion against the protocol major version on the response status object.
181    *
182    * @return An assertion against the protocol major version on the response status object.
183    */
184   public FluentIntegerAssertion<R> asMajor() {
185      return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMajor(), returns());
186   }
187
188   /**
189    * Returns an assertion against the protocol minor version on the response status object.
190    *
191    * @return An assertion against the protocol minor version on the response status object.
192    */
193   public FluentIntegerAssertion<R> asMinor() {
194      return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMinor(), returns());
195   }
196
197   //-----------------------------------------------------------------------------------------------------------------
198   // Fluent setters
199   //-----------------------------------------------------------------------------------------------------------------
200
201   // <FluentSetters>
202
203   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
204   public FluentResponseStatusLineAssertion<R> setMsg(String msg, Object...args) {
205      super.setMsg(msg, args);
206      return this;
207   }
208
209   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
210   public FluentResponseStatusLineAssertion<R> setOut(PrintStream value) {
211      super.setOut(value);
212      return this;
213   }
214
215   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
216   public FluentResponseStatusLineAssertion<R> setSilent() {
217      super.setSilent();
218      return this;
219   }
220
221   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
222   public FluentResponseStatusLineAssertion<R> setStdOut() {
223      super.setStdOut();
224      return this;
225   }
226
227   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
228   public FluentResponseStatusLineAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
229      super.setThrowable(value);
230      return this;
231   }
232
233   // </FluentSetters>
234}