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.assertions;
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.httppart.*;
025import org.apache.juneau.serializer.*;
026
027/**
028 * Used for fluent assertion calls against {@link RequestFormParam} 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 FluentRequestFormParamAssertion}
078 *    <ul class='javatreec'>
079 *       <li class='jm'>{@link FluentRequestFormParamAssertion#asBoolean() asBoolean()}
080 *       <li class='jm'>{@link FluentRequestFormParamAssertion#asInteger() asInteger()}
081 *       <li class='jm'>{@link FluentRequestFormParamAssertion#asLong() asLong()}
082 *       <li class='jm'>{@link FluentRequestFormParamAssertion#asZonedDateTime() asZonedDateTime()}
083 *       <li class='jm'>{@link FluentRequestFormParamAssertion#as(Class) as(Class)}
084 *       <li class='jm'>{@link FluentRequestFormParamAssertion#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 * </ul>
126 *
127 * @param <R> The return type.
128 */
129@FluentSetters(returns="FluentRequestFormParamAssertion<R>")
130public class FluentRequestFormParamAssertion<R> extends FluentStringAssertion<R> {
131
132   private final RequestFormParam value;
133
134   //-----------------------------------------------------------------------------------------------------------------
135   // Constructors
136   //-----------------------------------------------------------------------------------------------------------------
137
138   /**
139    * Constructor.
140    *
141    * @param value
142    *    The object being tested.
143    *    <br>Can be <jk>null</jk>.
144    * @param returns
145    *    The object to return after a test method is called.
146    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
147    * used on the same assertion.
148    */
149   public FluentRequestFormParamAssertion(RequestFormParam value, R returns) {
150      this(null, value, returns);
151   }
152
153   /**
154    * Chained constructor.
155    *
156    * <p>
157    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
158    *
159    * @param creator
160    *    The assertion that created this assertion.
161    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
162    * @param value
163    *    The object being tested.
164    *    <br>Can be <jk>null</jk>.
165    * @param returns
166    *    The object to return after a test method is called.
167    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
168    * used on the same assertion.
169    */
170   public FluentRequestFormParamAssertion(Assertion creator, RequestFormParam value, R returns) {
171      super(creator, value.asString().orElse(null), returns);
172      this.value = value;
173      setThrowable(BadRequest.class);
174   }
175
176   //-----------------------------------------------------------------------------------------------------------------
177   // Transform methods
178   //-----------------------------------------------------------------------------------------------------------------
179
180   /**
181    * Converts this object assertion into a boolean assertion.
182    *
183    * @return A new assertion.
184    * @throws AssertionError If object is not a boolean.
185    */
186   public FluentBooleanAssertion<R> asBoolean() {
187      return new FluentBooleanAssertion<>(this, value.asBoolean().orElse(null), returns());
188   }
189
190   /**
191    * Converts this object assertion into an integer assertion.
192    *
193    * @return A new assertion.
194    * @throws AssertionError If object is not an integer.
195    */
196   public FluentIntegerAssertion<R> asInteger() {
197      return new FluentIntegerAssertion<>(this, value.asInteger().orElse(null), returns());
198   }
199
200   /**
201    * Converts this object assertion into a long assertion.
202    *
203    * @return A new assertion.
204    * @throws AssertionError If object is not a long.
205    */
206   public FluentLongAssertion<R> asLong() {
207      return new FluentLongAssertion<>(this, value.asLong().orElse(null), returns());
208   }
209
210   /**
211    * Converts this object assertion into a zoned-datetime assertion.
212    *
213    * @return A new assertion.
214    * @throws AssertionError If object is not a zoned-datetime.
215    */
216   public FluentZonedDateTimeAssertion<R> asZonedDateTime() {
217      return new FluentZonedDateTimeAssertion<>(this, value.asDatePart().asZonedDateTime().orElse(null), returns());
218   }
219
220   /**
221    * Converts the parameter value to a type using {@link RequestFormParam#as(Class)} and then returns the value as an any-object assertion.
222    *
223    * @param <T> The object type to create.
224    * @param type The object type to create.
225    * @return A new fluent assertion object.
226    */
227   public <T> FluentAnyAssertion<T,R> as(Class<T> type) {
228      return new FluentAnyAssertion<>(value.as(type).orElse(null), returns());
229   }
230
231   /**
232    * Converts the parameter value to a type using {@link RequestFormParam#as(Type,Type...)} and then returns the value as an any-object assertion.
233    *
234    * <p>
235    * 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}.
236    *
237    * @param type The object type to create.
238    * @param args Optional type arguments.
239    * @return A new fluent assertion object.
240    */
241   public FluentAnyAssertion<Object,R> as(Type type, Type...args) {
242      return new FluentAnyAssertion<>(value.as(type, args).orElse(null), returns());
243   }
244
245   //-----------------------------------------------------------------------------------------------------------------
246   // Fluent setters
247   //-----------------------------------------------------------------------------------------------------------------
248
249   // <FluentSetters>
250
251   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
252   public FluentRequestFormParamAssertion<R> setMsg(String msg, Object...args) {
253      super.setMsg(msg, args);
254      return this;
255   }
256
257   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
258   public FluentRequestFormParamAssertion<R> setOut(PrintStream value) {
259      super.setOut(value);
260      return this;
261   }
262
263   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
264   public FluentRequestFormParamAssertion<R> setSilent() {
265      super.setSilent();
266      return this;
267   }
268
269   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
270   public FluentRequestFormParamAssertion<R> setStdOut() {
271      super.setStdOut();
272      return this;
273   }
274
275   @Override /* GENERATED - org.apache.juneau.assertions.Assertion */
276   public FluentRequestFormParamAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
277      super.setThrowable(value);
278      return this;
279   }
280
281   @Override /* GENERATED - org.apache.juneau.assertions.FluentStringAssertion */
282   public FluentRequestFormParamAssertion<R> asJavaStrings() {
283      super.asJavaStrings();
284      return this;
285   }
286
287   // </FluentSetters>
288}