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