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.internal.*;
026import org.apache.juneau.serializer.*;
027
028/**
029 * Used for fluent assertion calls against a response {@link StatusLine} object.
030 *
031 * <h5 class='section'>Example:</h5>
032 * <p class='bjava'>
033 *    <jc>// Validates the response status code is 200 or 404.</jc>
034 *    <jv>client</jv>
035 *       .get(<jsf>URI</jsf>)
036 *       .run()
037 *       .assertStatus().asCode().isAny(200,404);
038 * </p>
039 *
040 *
041 * <h5 class='topic'>Test Methods</h5>
042 * <p>
043 * <ul class='javatree'>
044 *    <li class='jc'>{@link FluentObjectAssertion}
045 *    <ul class='javatreec'>
046 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
047 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
048 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
049 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
050 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
051 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
053 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
054 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
061 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
062 *    </ul>
063 * </ul>
064 *
065 * <h5 class='topic'>Transform Methods</h5>
066 * <p>
067 * <ul class='javatree'>
068 *    <li class='jc'>{@link FluentResponseStatusLineAssertion}
069 *    <ul class='javatreec'>
070 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asCode() asCode()}
071 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asReason() asReason()}
072 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asProtocol() asProtocol()}
073 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asMajor() asMajor()}
074 *       <li class='jm'>{@link FluentResponseStatusLineAssertion#asMinor() asMinor()}
075 *    </ul>
076 *    <li class='jc'>{@link FluentObjectAssertion}
077 *    <ul class='javatreec'>
078 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
079 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
080 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
081 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
082 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
083 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
084 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
085 * </ul>
086 * </ul>
087 *
088 * <h5 class='topic'>Configuration Methods</h5>
089 * <p>
090 * <ul class='javatree'>
091 *    <li class='jc'>{@link Assertion}
092 *    <ul class='javatreec'>
093 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
094 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
095 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
096 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
097 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
098 *    </ul>
099 * </ul>
100 *
101 * <h5 class='section'>See Also:</h5><ul>
102 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a>
103 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClientBasics">juneau-rest-client Basics</a>
104 * </ul>
105 *
106 * @param <R> The return type.
107 */
108public class FluentResponseStatusLineAssertion<R> extends FluentObjectAssertion<StatusLine,R> {
109
110   //-----------------------------------------------------------------------------------------------------------------
111   // Constructors
112   //-----------------------------------------------------------------------------------------------------------------
113
114   /**
115    * Constructor.
116    *
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(StatusLine value, R returns) {
126      this(null, value, returns);
127   }
128
129   /**
130    * Chained constructor.
131    *
132    * <p>
133    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
134    *
135    * @param creator
136    *    The assertion that created this assertion.
137    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
138    * @param value
139    *    The object being tested.
140    *    <br>Can be <jk>null</jk>.
141    * @param returns
142    *    The object to return after a test method is called.
143    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
144    * used on the same assertion.
145    */
146   public FluentResponseStatusLineAssertion(Assertion creator, StatusLine value, R returns) {
147      super(creator, value, returns);
148      setThrowable(BadRequest.class);
149   }
150
151   //-----------------------------------------------------------------------------------------------------------------
152   // Transform methods
153   //-----------------------------------------------------------------------------------------------------------------
154
155   /**
156    * Returns an assertion against the status code on the response status object.
157    *
158    * @return An assertion against the status code on the response status object.
159    */
160   public FluentIntegerAssertion<R> asCode() {
161      return new FluentIntegerAssertion<>(this, value().getStatusCode(), returns());
162   }
163
164   /**
165    * Returns an assertion against the reason phrase on the response status object.
166    *
167    * @return An assertion against the reason phrase on the response status object.
168    */
169   public FluentStringAssertion<R> asReason() {
170      return new FluentStringAssertion<>(this, value().getReasonPhrase(), returns());
171   }
172
173   /**
174    * Returns an assertion against the protocol on the response status object.
175    *
176    * @return An assertion against the protocol on the response status object.
177    */
178   public FluentStringAssertion<R> asProtocol() {
179      return new FluentStringAssertion<>(this, value().getProtocolVersion().getProtocol(), returns());
180   }
181
182   /**
183    * Returns an assertion against the protocol major version on the response status object.
184    *
185    * @return An assertion against the protocol major version on the response status object.
186    */
187   public FluentIntegerAssertion<R> asMajor() {
188      return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMajor(), returns());
189   }
190
191   /**
192    * Returns an assertion against the protocol minor version on the response status object.
193    *
194    * @return An assertion against the protocol minor version on the response status object.
195    */
196   public FluentIntegerAssertion<R> asMinor() {
197      return new FluentIntegerAssertion<>(this, value().getProtocolVersion().getMinor(), returns());
198   }
199
200   //-----------------------------------------------------------------------------------------------------------------
201   // Fluent setters
202   //-----------------------------------------------------------------------------------------------------------------
203   @Override /* Overridden from Assertion */
204   public FluentResponseStatusLineAssertion<R> setMsg(String msg, Object...args) {
205      super.setMsg(msg, args);
206      return this;
207   }
208
209   @Override /* Overridden from Assertion */
210   public FluentResponseStatusLineAssertion<R> setOut(PrintStream value) {
211      super.setOut(value);
212      return this;
213   }
214
215   @Override /* Overridden from Assertion */
216   public FluentResponseStatusLineAssertion<R> setSilent() {
217      super.setSilent();
218      return this;
219   }
220
221   @Override /* Overridden from Assertion */
222   public FluentResponseStatusLineAssertion<R> setStdOut() {
223      super.setStdOut();
224      return this;
225   }
226
227   @Override /* Overridden from Assertion */
228   public FluentResponseStatusLineAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
229      super.setThrowable(value);
230      return this;
231   }
232}