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