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