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.lang.reflect.*;
017import java.util.*;
018import java.util.function.*;
019import java.util.regex.*;
020
021import org.apache.juneau.assertions.*;
022import org.apache.juneau.http.response.*;
023import org.apache.juneau.internal.*;
024import org.apache.juneau.rest.client.*;
025import org.apache.juneau.serializer.*;
026
027/**
028 * Used for fluent assertion calls against {@link ResponseHeader} objects.
029 *
030 * <h5 class='topic'>Test Methods</h5>
031 * <p>
032 * <ul class='javatree'>
033 *    <li class='jc'>{@link FluentStringAssertion}
034 *    <ul class='javatreec'>
035 *       <li class='jm'>{@link FluentStringAssertion#is(String) is(String)}
036 *       <li class='jm'>{@link FluentStringAssertion#isNot(String) isNot(String)}
037 *       <li class='jm'>{@link FluentStringAssertion#isLines(String...) isLines(String...)}
038 *       <li class='jm'>{@link FluentStringAssertion#isSortedLines(String...) isSortedLines(String...)}
039 *       <li class='jm'>{@link FluentStringAssertion#isIc(String) isIc(String)}
040 *       <li class='jm'>{@link FluentStringAssertion#isNotIc(String) isNotIc(String)}
041 *       <li class='jm'>{@link FluentStringAssertion#isContains(String...) isContains(String...)}
042 *       <li class='jm'>{@link FluentStringAssertion#isNotContains(String...) isNotContains(String...)}
043 *       <li class='jm'>{@link FluentStringAssertion#isEmpty() isEmpty()}
044 *       <li class='jm'>{@link FluentStringAssertion#isNotEmpty() isNotEmpty()}
045 *       <li class='jm'>{@link FluentStringAssertion#isString(Object) isString(Object)}
046 *       <li class='jm'>{@link FluentStringAssertion#isMatches(String) isMatches(String)}
047 *       <li class='jm'>{@link FluentStringAssertion#isPattern(String) isPattern(String)}
048 *       <li class='jm'>{@link FluentStringAssertion#isPattern(String,int) isPattern(String,int)}
049 *       <li class='jm'>{@link FluentStringAssertion#isPattern(Pattern) isPattern(Pattern)}
050 *       <li class='jm'>{@link FluentStringAssertion#isStartsWith(String) isStartsWith(String)}
051 *       <li class='jm'>{@link FluentStringAssertion#isEndsWith(String) isEndsWith(String)}
052 *    </ul>
053 *    <li class='jc'>{@link FluentObjectAssertion}
054 *    <ul class='javatreec'>
055 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
056 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
057 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
061 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
062 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
063 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
064 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
065 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
066 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
067 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
068 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
069 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
070 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
071 *    </ul>
072 * </ul>
073 *
074 * <h5 class='topic'>Transform Methods</h5>
075 * <p>
076 * <ul class='javatree'>
077 *    <li class='jc'>{@link FluentResponseHeaderAssertion}
078 *    <ul class='javatreec'>
079 *       <li class='jm'>{@link FluentResponseHeaderAssertion#asBoolean() asBoolean()}
080 *       <li class='jm'>{@link FluentResponseHeaderAssertion#asInteger() asInteger()}
081 *       <li class='jm'>{@link FluentResponseHeaderAssertion#asLong() asLong()}
082 *       <li class='jm'>{@link FluentResponseHeaderAssertion#asZonedDateTime() asZonedDateTime()}
083 *       <li class='jm'>{@link FluentResponseHeaderAssertion#as(Class) as(Class)}
084 *       <li class='jm'>{@link FluentResponseHeaderAssertion#as(Type,Type...) as(Type,Type...)}
085 *    </ul>
086 *    <li class='jc'>{@link FluentStringAssertion}
087 *    <ul class='javatreec'>
088 *       <li class='jm'>{@link FluentStringAssertion#asReplaceAll(String,String) asReplaceAll(String,String)}
089 *       <li class='jm'>{@link FluentStringAssertion#asReplace(String,String) asReplace(String,String)}
090 *       <li class='jm'>{@link FluentStringAssertion#asUrlDecode() asUrlDecode()}
091 *       <li class='jm'>{@link FluentStringAssertion#asLc() asLc()}
092 *       <li class='jm'>{@link FluentStringAssertion#asUc() asUc()}
093 *       <li class='jm'>{@link FluentStringAssertion#asLines() asLines()}
094 *       <li class='jm'>{@link FluentStringAssertion#asSplit(String) asSplit(String)}
095 *       <li class='jm'>{@link FluentStringAssertion#asLength() asLength()}
096 *       <li class='jm'>{@link FluentStringAssertion#asOneLine() asOneLine()}
097 *    </ul>
098 *    <li class='jc'>{@link FluentObjectAssertion}
099 *    <ul class='javatreec'>
100 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
101 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
102 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
103 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
104 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
105 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
106 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
107 * </ul>
108 * </ul>
109 *
110 * <h5 class='topic'>Configuration Methods</h5>
111 * <p>
112 * <ul class='javatree'>
113 *    <li class='jc'>{@link Assertion}
114 *    <ul class='javatreec'>
115 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
116 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
117 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
118 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
119 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
120 *    </ul>
121 * </ul>
122 *
123 * <h5 class='section'>See Also:</h5><ul>
124 *    <li class='link'><a class="doclink" href="../../../../../../index.html#ja.Overview">juneau-assertions</a>
125 *    <li class='link'><a class="doclink" href="../../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
126 * </ul>
127 *
128 * @param <R> The return type.
129 */
130@FluentSetters(returns="FluentResponseHeaderAssertion<R>")
131public class FluentResponseHeaderAssertion<R> extends FluentStringAssertion<R> {
132
133   private final ResponseHeader value;
134
135   //-----------------------------------------------------------------------------------------------------------------
136   // Constructors
137   //-----------------------------------------------------------------------------------------------------------------
138
139   /**
140    * Constructor.
141    *
142    * @param value
143    *    The object being tested.
144    *    <br>Can be <jk>null</jk>.
145    * @param returns
146    *    The object to return after a test method is called.
147    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
148    * used on the same assertion.
149    */
150   public FluentResponseHeaderAssertion(ResponseHeader value, R returns) {
151      this(null, value, returns);
152   }
153
154   /**
155    * Chained constructor.
156    *
157    * <p>
158    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
159    *
160    * @param creator
161    *    The assertion that created this assertion.
162    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
163    * @param value
164    *    The object being tested.
165    *    <br>Can be <jk>null</jk>.
166    * @param returns
167    *    The object to return after a test method is called.
168    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
169    * used on the same assertion.
170    */
171   public FluentResponseHeaderAssertion(Assertion creator, ResponseHeader value, R returns) {
172      super(creator, value.asString().orElse(null), returns);
173      this.value = value;
174      setThrowable(BadRequest.class);
175   }
176
177   //-----------------------------------------------------------------------------------------------------------------
178   // Transform methods
179   //-----------------------------------------------------------------------------------------------------------------
180
181   /**
182    * Converts this object assertion into a boolean assertion.
183    *
184    * @return A new assertion.
185    * @throws AssertionError If object is not a boolean.
186    */
187   public FluentBooleanAssertion<R> asBoolean() {
188      return new FluentBooleanAssertion<>(this, value.asBoolean().orElse(null), returns());
189   }
190
191   /**
192    * Converts this object assertion into an integer assertion.
193    *
194    * @return A new assertion.
195    * @throws AssertionError If object is not an integer.
196    */
197   public FluentIntegerAssertion<R> asInteger() {
198      return new FluentIntegerAssertion<>(this, value.asInteger().orElse(null), returns());
199   }
200
201   /**
202    * Converts this object assertion into a long assertion.
203    *
204    * @return A new assertion.
205    * @throws AssertionError If object is not a long.
206    */
207   public FluentLongAssertion<R> asLong() {
208      return new FluentLongAssertion<>(this, value.asLong().orElse(null), returns());
209   }
210
211   /**
212    * Converts this object assertion into a zoned-datetime assertion.
213    *
214    * @return A new assertion.
215    * @throws AssertionError If object is not a zoned-datetime.
216    */
217   public FluentZonedDateTimeAssertion<R> asZonedDateTime() {
218      return new FluentZonedDateTimeAssertion<>(this, value.asDateHeader().asZonedDateTime().orElse(null), returns());
219   }
220
221   /**
222    * Converts the parameter value to a type using {@link ResponseHeader#as(Class)} and then returns the value as an any-object assertion.
223    *
224    * @param <T> The object type to create.
225    * @param type The object type to create.
226    * @return A new fluent assertion object.
227    * @throws RestCallException If value could not be parsed.
228    */
229   public <T> FluentAnyAssertion<T,R> as(Class<T> type) throws RestCallException {
230      return new FluentAnyAssertion<>(value.as(type).orElse(null), returns());
231   }
232
233   /**
234    * Converts the parameter value to a type using {@link ResponseHeader#as(Type,Type...)} and then returns the value as an any-object assertion.
235    *
236    * <p>
237    * See <a class="doclink" href="../../../../../../index.html#jm.ComplexDataTypes">Complex Data Types</a> for information on defining complex generic types of {@link Map Maps} and {@link Collection Collections}.
238    *
239    * @param type The object type to create.
240    * @param args Optional type arguments.
241    * @return A new fluent assertion object.
242    * @throws RestCallException If value could not be parsed.
243    */
244   public FluentAnyAssertion<Object,R> as(Type type, Type...args) throws RestCallException {
245      return new FluentAnyAssertion<>(value.as(type, args).orElse(null), returns());
246   }
247
248   //-----------------------------------------------------------------------------------------------------------------
249   // Fluent setters
250   //-----------------------------------------------------------------------------------------------------------------
251
252   // <FluentSetters>
253
254   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
255   public FluentResponseHeaderAssertion<R> setMsg(String msg, Object...args) {
256      super.setMsg(msg, args);
257      return this;
258   }
259
260   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
261   public FluentResponseHeaderAssertion<R> setOut(PrintStream value) {
262      super.setOut(value);
263      return this;
264   }
265
266   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
267   public FluentResponseHeaderAssertion<R> setSilent() {
268      super.setSilent();
269      return this;
270   }
271
272   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
273   public FluentResponseHeaderAssertion<R> setStdOut() {
274      super.setStdOut();
275      return this;
276   }
277
278   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
279   public FluentResponseHeaderAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
280      super.setThrowable(value);
281      return this;
282   }
283
284   @Override /* GENERATED - org.apache.juneau.assertions.FluentStringAssertion */
285   public FluentResponseHeaderAssertion<R> asJavaStrings() {
286      super.asJavaStrings();
287      return this;
288   }
289
290   // </FluentSetters>
291}