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