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.assertions;
018
019import static org.apache.juneau.commons.utils.AssertionUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021
022import java.io.*;
023import java.time.temporal.*;
024import java.util.*;
025import java.util.function.*;
026
027import org.apache.juneau.cp.*;
028import org.apache.juneau.serializer.*;
029
030/**
031 * Used for fluent assertion calls against dates.
032 *
033 * <h5 class='section'>Example:</h5>
034 * <p class='bjava'>
035 *    <jc>// Validates the response expiration is after the current date.</jc>
036 *    <jv>client</jv>
037 *       .get(<jsf>URL</jsf>)
038 *       .run()
039 *       .assertDateHeader(<js>"Expires"</js>).isAfterNow();
040 * </p>
041 *
042 *
043 * <h5 class='section'>Test Methods:</h5>
044 * <p>
045 * <ul class='javatree'>
046 *    <li class='jc'>{@link FluentDateAssertion}
047 *    <ul class='javatreec'>
048 *       <li class='jm'>{@link FluentDateAssertion#is(Date,ChronoUnit) is(Date,ChronoUnit)}
049 *       <li class='jm'>{@link FluentDateAssertion#isAfter(Date) isAfter(Date)}
050 *       <li class='jm'>{@link FluentDateAssertion#isAfterNow() isAfterNow()}
051 *       <li class='jm'>{@link FluentDateAssertion#isBefore(Date) isBefore(Date)}
052 *       <li class='jm'>{@link FluentDateAssertion#isBeforeNow() isBeforeNow()}
053 *       <li class='jm'>{@link FluentDateAssertion#isBetween(Date,Date) isBetween(Date,Date)}
054 *    </ul>
055 *    <li class='jc'>{@link FluentComparableAssertion}
056 *    <ul class='javatreec'>
057 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
058 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
059 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
060 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
061 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
062  *   </ul>
063 *    <li class='jc'>{@link FluentObjectAssertion}
064 *    <ul class='javatreec'>
065 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
066 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
067 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
068 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
069 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
070 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
071 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
072 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
073 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
074 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
075 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
076 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
077 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
078 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
079 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
080 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
081 *    </ul>
082 * </ul>
083 *
084 * <h5 class='section'>Transform Methods:</h5>
085 * <p>
086 * <ul class='javatree'>
087 *    <li class='jc'>{@link FluentDateAssertion}
088 *    <ul class='javatreec'>
089 *       <li class='jm'>{@link FluentDateAssertion#asEpochMillis() asEpochMillis()}
090 *       <li class='jm'>{@link FluentDateAssertion#asEpochSeconds() asEpochSeconds()}
091 *    </ul>
092 *    <li class='jc'>{@link FluentObjectAssertion}
093 *    <ul class='javatreec'>
094 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
095 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
096 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
097 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
098 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
099 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
100 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
101 * </ul>
102 * </ul>
103 *
104 * <h5 class='section'>Configuration Methods:</h5>
105 * <p>
106 * <ul class='javatree'>
107 *    <li class='jc'>{@link Assertion}
108 *    <ul class='javatreec'>
109 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
110 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
111 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
112 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
113 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
114 *    </ul>
115 * </ul>
116 *
117 * <h5 class='section'>See Also:</h5><ul>
118 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a>
119 * </ul>
120 *
121 * @param <R> The return type.
122 */
123public class FluentDateAssertion<R> extends FluentComparableAssertion<Date,R> {
124
125   // @formatter:off
126   private static final Messages MESSAGES = Messages.of(FluentDateAssertion.class, "Messages");
127   private static final String
128      MSG_unexpectedValue = MESSAGES.getString("unexpectedValue"),
129      MSG_valueWasNotAfterExpected = MESSAGES.getString("valueWasNotAfterExpected"),
130      MSG_valueWasNotBeforeExpected = MESSAGES.getString("valueWasNotBeforeExpected");
131   // @formatter:on
132
133   /**
134    * Chained constructor.
135    *
136    * <p>
137    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
138    *
139    * @param creator
140    *    The assertion that created this assertion.
141    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
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 FluentDateAssertion(Assertion creator, Date value, R returns) {
151      super(creator, value, returns);
152   }
153
154   /**
155    * Constructor.
156    *
157    * @param value
158    *    The object being tested.
159    *    <br>Can be <jk>null</jk>.
160    * @param returns
161    *    The object to return after a test method is called.
162    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
163    * used on the same assertion.
164    */
165   public FluentDateAssertion(Date value, R returns) {
166      this(null, value, returns);
167   }
168
169   /**
170    * Returns an long assertion on the epoch milliseconds of this date.
171    *
172    * <p>
173    * If the date is <jk>null</jk>, the returned assertion is a null assertion
174    * (meaning {@link FluentLongAssertion#isExists()} returns <jk>false</jk>).
175    *
176    * @return A new assertion.
177    */
178   public FluentLongAssertion<R> asEpochMillis() {
179      return new FluentLongAssertion<>(this, valueIsNull() ? null : value().getTime(), returns());
180   }
181
182   /**
183    * Returns an long assertion on the epoch seconds of this date.
184    *
185    * <p>
186    * If the date is <jk>null</jk>, the returned assertion is a null assertion
187    * (meaning {@link FluentLongAssertion#isExists()} returns <jk>false</jk>).
188    *
189    * @return A new assertion.
190    */
191   public FluentLongAssertion<R> asEpochSeconds() {
192      return new FluentLongAssertion<>(this, valueIsNull() ? null : value().getTime() / 1000, returns());
193   }
194
195   /**
196    * Asserts that the value equals the specified value at the specified precision.
197    *
198    * @param value The value to check against.
199    * @param precision The precision (e.g. {@link ChronoUnit#SECONDS}).
200    * @return The fluent return object.
201    * @throws AssertionError If assertion failed.
202    */
203   public R is(Date value, ChronoUnit precision) throws AssertionError {
204      if (neq(value(), value, (x, y) -> x.toInstant().truncatedTo(precision).equals(y.toInstant().truncatedTo(precision))))
205         throw error(MSG_unexpectedValue, value, value());
206      return returns();
207   }
208
209   /**
210    * Asserts that the value is after the specified value.
211    *
212    * @param value The values to check against.
213    * @return The fluent return object.
214    * @throws AssertionError If assertion failed.
215    */
216   public R isAfter(Date value) throws AssertionError {
217      assertArgNotNull("value", value);
218      if (! (value().after(value)))
219         throw error(MSG_valueWasNotAfterExpected, value, value());
220      return returns();
221   }
222
223   /**
224    * Asserts that the value is after the current date.
225    *
226    * @return The fluent return object.
227    * @throws AssertionError If assertion failed.
228    */
229   public R isAfterNow() throws AssertionError { return isAfter(new Date()); }
230
231   /**
232    * Asserts that the value is before the specified value.
233    *
234    * @param value The values to check against.
235    * @return The fluent return object.
236    * @throws AssertionError If assertion failed.
237    */
238   public R isBefore(Date value) throws AssertionError {
239      assertArgNotNull("value", value);
240      if (! (value().before(value)))
241         throw error(MSG_valueWasNotBeforeExpected, value, value());
242      return returns();
243   }
244
245   /**
246    * Asserts that the value is before the current date.
247    *
248    * @return The fluent return object.
249    * @throws AssertionError If assertion failed.
250    */
251   public R isBeforeNow() throws AssertionError { return isBefore(new Date()); }
252
253   /**
254    * Asserts that the value is between (inclusive) the specified upper and lower values.
255    *
256    * @param lower The lower value to check against.
257    * @param upper The upper value to check against.
258    * @return The fluent return object.
259    * @throws AssertionError If assertion failed.
260    */
261   public R isBetween(Date lower, Date upper) throws AssertionError {
262      isExists();
263      assertArgNotNull("lower", lower);
264      assertArgNotNull("upper", upper);
265      isLte(upper);
266      isGte(lower);
267      return returns();
268   }
269
270   @Override /* Overridden from Assertion */
271   public FluentDateAssertion<R> setMsg(String msg, Object...args) {
272      super.setMsg(msg, args);
273      return this;
274   }
275
276   @Override /* Overridden from Assertion */
277   public FluentDateAssertion<R> setOut(PrintStream value) {
278      super.setOut(value);
279      return this;
280   }
281
282   @Override /* Overridden from Assertion */
283   public FluentDateAssertion<R> setSilent() {
284      super.setSilent();
285      return this;
286   }
287
288   @Override /* Overridden from Assertion */
289   public FluentDateAssertion<R> setStdOut() {
290      super.setStdOut();
291      return this;
292   }
293
294   @Override /* Overridden from Assertion */
295   public FluentDateAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
296      super.setThrowable(value);
297      return this;
298   }
299}