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