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 java.io.*;
020import java.util.function.*;
021
022import org.apache.juneau.commons.function.*;
023import org.apache.juneau.commons.lang.*;
024
025/**
026 * Used for fluent assertion calls against {@link Version} objects.
027 *
028 * <h2 class='section'>Example:</h2>
029 * <p class='bjava'>
030 *    <jc>// Validates the response expiration is after the current date.</jc>
031 *    <jv>client</jv>
032 *       .get(<jsf>URL</jsf>)
033 *       .run()
034 *       .getHeader(ClientVersion.<jk>class</jk>).assertVersion().asMajor().isGreaterThanOrEqual(2);
035 * </p>
036 *
037 *
038 * <h2 class='section'>Test Methods:</h2>
039 * <ul class='javatree'>
040 *    <li class='jc'>{@link FluentComparableAssertion}
041 *    <ul class='javatreec'>
042 *       <li class='jm'>{@link FluentComparableAssertion#isGt(Comparable) isGt(Comparable)}
043 *       <li class='jm'>{@link FluentComparableAssertion#isGte(Comparable) isGte(Comparable)}
044 *       <li class='jm'>{@link FluentComparableAssertion#isLt(Comparable) isLt(Comparable)}
045 *       <li class='jm'>{@link FluentComparableAssertion#isLte(Comparable) isLte(Comparable)}
046 *       <li class='jm'>{@link FluentComparableAssertion#isBetween(Comparable,Comparable) isBetween(Comparable,Comparable)}
047  *   </ul>
048 *    <li class='jc'>{@link FluentObjectAssertion}
049 *    <ul class='javatreec'>
050 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
051 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
052 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
055 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
056 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
057 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
058 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
061 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
062 *    </ul>
063 * </ul>
064 *
065 * <h2 class='section'>Transform Methods:</h2>
066 * <ul class='javatree'>
067 *    <li class='jc'>{@link FluentVersionAssertion}
068 *    <ul class='javatreec'>
069 *       <li class='jm'>{@link FluentVersionAssertion#asPart(int) asPart(int)}
070 *       <li class='jm'>{@link FluentVersionAssertion#asMajor() asMajor()}
071 *       <li class='jm'>{@link FluentVersionAssertion#asMinor() asMinor()}
072 *       <li class='jm'>{@link FluentVersionAssertion#asMaintenance() asMaintenance()}
073 *    </ul>
074 *    <li class='jc'>{@link FluentObjectAssertion}
075 *    <ul class='javatreec'>
076 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
077 *       <li class='jm'>{@link FluentObjectAssertion#asString(ThrowingFunction) asString(ThrowingFunction)}
078 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(UnaryOperator) asTransformed(UnaryOperator)}
079 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
080 * </ul>
081 * </ul>
082 *
083 * <h2 class='section'>Configuration Methods:</h2>
084 * <ul class='javatree'>
085 *    <li class='jc'>{@link Assertion}
086 *    <ul class='javatreec'>
087 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
088 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
089 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
090 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
091 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
092 *    </ul>
093 * </ul>
094 *
095 * <h2 class='section'>See Also:</h2><ul>
096 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem</a>
097 * </ul>
098 *
099 * @param <R> The return type.
100 */
101public class FluentVersionAssertion<R> extends FluentComparableAssertion<Version,R> {
102
103   /**
104    * Chained constructor.
105    *
106    * <p>
107    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
108    *
109    * @param creator
110    *    The assertion that created this assertion.
111    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
112    * @param value
113    *    The object being tested.
114    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
115    * @param returns
116    *    The object to return after a test method is called.
117    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
118    * used on the same assertion.
119    */
120   public FluentVersionAssertion(Assertion creator, Version value, R returns) {
121      super(creator, value, returns);
122   }
123
124   /**
125    * Constructor.
126    *
127    * @param value
128    *    The object being tested.
129    *    <br>Can be <jk>null</jk> (assertions such as {@code isNull()} then operate on the absence of a value).
130    * @param returns
131    *    The object to return after a test method is called.
132    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
133    * used on the same assertion.
134    */
135   public FluentVersionAssertion(Version value, R returns) {
136      this(null, value, returns);
137   }
138
139   /**
140    * Extracts the maintenance part of the version string (index position 2).
141    *
142    * @return This object.
143    */
144   public FluentIntegerAssertion<R> asMaintenance() {
145      return asPart(2);
146   }
147
148   /**
149    * Extracts the major part of the version string (index position 0).
150    *
151    * @return This object.
152    */
153   public FluentIntegerAssertion<R> asMajor() {
154      return asPart(0);
155   }
156
157   /**
158    * Extracts the minor part of the version string (index position 1).
159    *
160    * @return This object.
161    */
162   public FluentIntegerAssertion<R> asMinor() {
163      return asPart(1);
164   }
165
166   /**
167    * Extracts the specified version part (zero-indexed position).
168    *
169    * @param index The index of the version part to extract.
170    * @return This object.
171    */
172   public FluentIntegerAssertion<R> asPart(int index) {
173      return new FluentIntegerAssertion<>(this, valueIsNull() ? null : value().getPart(index).orElse(null), returns());
174   }
175
176   @Override /* Overridden from Assertion */
177   public FluentVersionAssertion<R> setMsg(String msg, Object...args) {
178      super.setMsg(msg, args);
179      return this;
180   }
181
182   @Override /* Overridden from Assertion */
183   public FluentVersionAssertion<R> setOut(PrintStream value) {
184      super.setOut(value);
185      return this;
186   }
187
188   @Override /* Overridden from Assertion */
189   public FluentVersionAssertion<R> setSilent() {
190      super.setSilent();
191      return this;
192   }
193
194   @Override /* Overridden from Assertion */
195   public FluentVersionAssertion<R> setStdOut() {
196      super.setStdOut();
197      return this;
198   }
199
200   @Override /* Overridden from Assertion */
201   public FluentVersionAssertion<R> setThrowable(Class<? extends RuntimeException> value) {
202      super.setThrowable(value);
203      return this;
204   }
205}