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.*;
023import java.time.temporal.*;
024import java.util.function.*;
025
026import org.apache.juneau.commons.function.*;
027
028
029/**
030 * Used for fluent assertion calls against {@link ZonedDateTime} objects.
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 *       .getHeader(<js>"Expires"</js>).asDateHeader().assertZonedDateTime().isAfterNow();
039 * </p>
040 *
041 *
042 * <h2 class='section'>Test Methods:</h2>
043 * <ul class='javatree'>
044 *    <li class='jc'>{@link FluentZonedDateTimeAssertion}
045 *    <ul class='javatreec'>
046 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#is(ZonedDateTime,ChronoUnit) is(ZonedDateTime,ChronoUnit)}
047 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#isAfter(ZonedDateTime) isAfter(ZonedDateTime)}
048 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#isAfterNow() isAfterNow()}
049 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#isBefore(ZonedDateTime) isBefore(ZonedDateTime)}
050 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#isBeforeNow() isBeforeNow()}
051 *       <li class='jm'>{@link FluentZonedDateTimeAssertion#isBetween(ZonedDateTime,ZonedDateTime) isBetween(ZonedDateTime,ZonedDateTime)}
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 FluentObjectAssertion}
081 *    <ul class='javatreec'>
082 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
083 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
084 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
085 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
086 * </ul>
087 * </ul>
088 *
089 * <h2 class='section'>Configuration Methods:</h2>
090 * <ul class='javatree'>
091 *    <li class='jc'>{@link Assertion}
092 *    <ul class='javatreec'>
093 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
094 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
095 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
096 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
097 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
098 *    </ul>
099 * </ul>
100 *
101 * <h2 class='section'>See Also:</h2><ul>
102 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
103 * </ul>
104 *
105 * @param <R> The return type.
106 */
107@SuppressWarnings({
108   "java:S115", // Constants use UPPER_snakeCase convention
109   "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants
110   "java:S8688" // isAfterNow()/isBeforeNow() compare against the current instant by design; ZonedDateTime.now() is the intended system-clock seam and a ZoneId would not change the instant comparison.
111})
112public class FluentZonedDateTimeAssertion<R> extends FluentComparableAssertion<ZonedDateTime,R> {
113
114   private static final Messages MESSAGES = Messages.of(FluentZonedDateTimeAssertion.class, "Messages");
115   private static final String MSG_unexpectedValue = MESSAGES.getString("unexpectedValue");
116   private static final String MSG_valueWasNotAfterExpected = MESSAGES.getString("valueWasNotAfterExpected");
117   private static final String MSG_valueWasNotBeforeExpected = MESSAGES.getString("valueWasNotBeforeExpected");
118
119   /**
120    * Chained constructor.
121    *
122    * <p>
123    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
124    *
125    * @param creator
126    *    The assertion that created this assertion.
127    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
128    * @param value
129    *    The object being tested.
130    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
131    * @param returns
132    *    The object to return after a test method is called.
133    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
134    * used on the same assertion.
135    */
136   public FluentZonedDateTimeAssertion(Assertion creator, ZonedDateTime value, R returns) {
137      super(creator, value, returns);
138   }
139
140   /**
141    * Constructor.
142    *
143    * @param value
144    *    The object being tested.
145    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
146    * @param returns
147    *    The object to return after a test method is called.
148    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
149    * used on the same assertion.
150    */
151   public FluentZonedDateTimeAssertion(ZonedDateTime value, R returns) {
152      this(null, value, returns);
153   }
154
155   /**
156    * Asserts that the value equals the specified value at the specified precision.
157    *
158    * @param value The value to check against.
159    * @param precision The precision (e.g. {@link ChronoUnit#SECONDS}.
160    * @return The fluent return object.
161    * @throws AssertionError If assertion failed.
162    */
163   public R is(ZonedDateTime value, ChronoUnit precision) throws AssertionError {
164      reqnn("precision", precision);
165      var v = orElse(null);
166      if (valueIsNull() && value == null)
167         return returns();
168      if (valueIsNotNull() && nn(value)) {
169         var d = Duration.between(value(), value);
170         if (d.compareTo(precision.getDuration()) <= 0)
171            return returns();
172      }
173      throw error(MSG_unexpectedValue, value, v);
174   }
175
176   /**
177    * Asserts that the value is after the specified value.
178    *
179    * @param value The values to check against.
180    * @return The fluent return object.
181    * @throws AssertionError If assertion failed.
182    */
183   public R isAfter(ZonedDateTime value) throws AssertionError {
184      reqnn("value", value);
185      if (! (value().isAfter(value)))
186         throw error(MSG_valueWasNotAfterExpected, value, value());
187      return returns();
188   }
189
190   /**
191    * Asserts that the value is after the current date.
192    *
193    * @return The fluent return object.
194    * @throws AssertionError If assertion failed.
195    */
196   public R isAfterNow() throws AssertionError { return isAfter(ZonedDateTime.now()); }
197
198   /**
199    * Asserts that the value is before the specified value.
200    *
201    * @param value The values to check against.
202    * @return The fluent return object.
203    * @throws AssertionError If assertion failed.
204    */
205   public R isBefore(ZonedDateTime value) throws AssertionError {
206      reqnn("value", value);
207      if (! (value().isBefore(value)))
208         throw error(MSG_valueWasNotBeforeExpected, value, value());
209      return returns();
210   }
211
212   /**
213    * Asserts that the value is before the current date.
214    *
215    * @return The fluent return object.
216    * @throws AssertionError If assertion failed.
217    */
218   public R isBeforeNow() throws AssertionError { return isBefore(ZonedDateTime.now()); }
219
220   /**
221    * Asserts that the value is between (inclusive) the specified upper and lower values.
222    *
223    * @param lower The lower value to check against.
224    * @param upper The upper value to check against.
225    * @return The fluent return object.
226    * @throws AssertionError If assertion failed.
227    */
228   public R isBetween(ZonedDateTime lower, ZonedDateTime upper) throws AssertionError {
229      isExists();
230      reqnn("lower", lower);
231      reqnn("upper", upper);
232      isLte(upper);
233      isGte(lower);
234      return returns();
235   }
236
237   @Override /* Overridden from Assertion */
238   public FluentZonedDateTimeAssertion<R> setMsg(String msg, Object...args) {
239      super.setMsg(msg, args);
240      return this;
241   }
242
243   @Override /* Overridden from Assertion */
244   public FluentZonedDateTimeAssertion<R> setOut(PrintStream value) {
245      super.setOut(value);
246      return this;
247   }
248
249   @Override /* Overridden from Assertion */
250   public FluentZonedDateTimeAssertion<R> setSilent() {
251      super.setSilent();
252      return this;
253   }
254
255   @Override /* Overridden from Assertion */
256   public FluentZonedDateTimeAssertion<R> setStdOut() {
257      super.setStdOut();
258      return this;
259   }
260
261   @Override /* Overridden from Assertion */
262   public FluentZonedDateTimeAssertion<R> setThrowable(Class<? extends RuntimeException> value) {
263      super.setThrowable(value);
264      return this;
265   }
266}