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