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