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.IoUtils.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import java.io.*;
023import java.nio.charset.*;
024import java.util.function.*;
025
026import org.apache.juneau.serializer.*;
027
028/**
029 * Used for fluent assertion calls against byte arrays.
030 *
031 * <h5 class='section'>Test Methods:</h5>
032 * <p>
033 * <ul class='javatree'>
034 *    <li class='jc'>{@link FluentPrimitiveArrayAssertion}
035 *    <ul class='javatreec'>
036 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isHas(Object...) isHas(Object...)}
037 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#is(Predicate) is(Predicate)}
038 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAny(Predicate) isAny(Predicate)}
039 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isAll(Predicate) isAll(Predicate)}
040 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isEmpty() isEmpty()}
041 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotEmpty() isNotEmpty()}
042 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isSize(int) isSize(int)}
043 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isContains(Object) isContains(Object)}
044 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#isNotContains(Object) isNotContains(Object)}
045 *    </ul>
046 *    <li class='jc'>{@link FluentObjectAssertion}
047 *    <ul class='javatreec'>
048 *       <li class='jm'>{@link FluentObjectAssertion#isExists() isExists()}
049 *       <li class='jm'>{@link FluentObjectAssertion#is(Object) is(Object)}
050 *       <li class='jm'>{@link FluentObjectAssertion#is(Predicate) is(Predicate)}
051 *       <li class='jm'>{@link FluentObjectAssertion#isNot(Object) isNot(Object)}
052 *       <li class='jm'>{@link FluentObjectAssertion#isAny(Object...) isAny(Object...)}
053 *       <li class='jm'>{@link FluentObjectAssertion#isNotAny(Object...) isNotAny(Object...)}
054 *       <li class='jm'>{@link FluentObjectAssertion#isNull() isNull()}
055 *       <li class='jm'>{@link FluentObjectAssertion#isNotNull() isNotNull()}
056 *       <li class='jm'>{@link FluentObjectAssertion#isString(String) isString(String)}
057 *       <li class='jm'>{@link FluentObjectAssertion#isJson(String) isJson(String)}
058 *       <li class='jm'>{@link FluentObjectAssertion#isSame(Object) isSame(Object)}
059 *       <li class='jm'>{@link FluentObjectAssertion#isSameJsonAs(Object) isSameJsonAs(Object)}
060 *       <li class='jm'>{@link FluentObjectAssertion#isSameSortedJsonAs(Object) isSameSortedJsonAs(Object)}
061 *       <li class='jm'>{@link FluentObjectAssertion#isSameSerializedAs(Object, WriterSerializer) isSameSerializedAs(Object, WriterSerializer)}
062 *       <li class='jm'>{@link FluentObjectAssertion#isType(Class) isType(Class)}
063 *       <li class='jm'>{@link FluentObjectAssertion#isExactType(Class) isExactType(Class)}
064 *    </ul>
065 * </ul>
066 *
067 * <h5 class='section'>Transform Methods:</h5>
068 * <p>
069 * <ul class='javatree'>
070 *    <li class='jc'>{@link FluentByteArrayAssertion}
071 *    <ul class='javatreec'>
072 *       <li class='jm'>{@link FluentByteArrayAssertion#asString() asString()}
073 *       <li class='jm'>{@link FluentByteArrayAssertion#asString(Charset) asString(Charset)}
074 *       <li class='jm'>{@link FluentByteArrayAssertion#asBase64() asBase64()}
075 *       <li class='jm'>{@link FluentByteArrayAssertion#asHex() asHex()}
076 *       <li class='jm'>{@link FluentByteArrayAssertion#asSpacedHex() asSpacedHex()}
077 *    </ul>
078 *    <li class='jc'>{@link FluentPrimitiveArrayAssertion}
079 *    <ul class='javatreec'>
080 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#asItem(int) asItem(int)}
081 *       <li class='jm'>{@link FluentPrimitiveArrayAssertion#asLength() asLength()}
082 *    </ul>
083 *    <li class='jc'>{@link FluentObjectAssertion}
084 *    <ul class='javatreec'>
085 *       <li class='jm'>{@link FluentObjectAssertion#asString() asString()}
086 *       <li class='jm'>{@link FluentObjectAssertion#asString(WriterSerializer) asString(WriterSerializer)}
087 *       <li class='jm'>{@link FluentObjectAssertion#asString(Function) asString(Function)}
088 *       <li class='jm'>{@link FluentObjectAssertion#asJson() asJson()}
089 *       <li class='jm'>{@link FluentObjectAssertion#asJsonSorted() asJsonSorted()}
090 *       <li class='jm'>{@link FluentObjectAssertion#asTransformed(Function) asApplied(Function)}
091 *       <li class='jm'>{@link FluentObjectAssertion#asAny() asAny()}
092 * </ul>
093 * </ul>
094 *
095 * <h5 class='section'>Configuration Methods:</h5>
096 * <p>
097 * <ul class='javatree'>
098 *    <li class='jc'>{@link Assertion}
099 *    <ul class='javatreec'>
100 *       <li class='jm'>{@link Assertion#setMsg(String, Object...) setMsg(String, Object...)}
101 *       <li class='jm'>{@link Assertion#setOut(PrintStream) setOut(PrintStream)}
102 *       <li class='jm'>{@link Assertion#setSilent() setSilent()}
103 *       <li class='jm'>{@link Assertion#setStdOut() setStdOut()}
104 *       <li class='jm'>{@link Assertion#setThrowable(Class) setThrowable(Class)}
105 *    </ul>
106 * </ul>
107 *
108 * <h5 class='section'>See Also:</h5><ul>
109 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauEcosystemOverview">Juneau Ecosystem Overview</a>
110 * </ul>
111 *
112 * @param <R> The return type.
113 */
114public class FluentByteArrayAssertion<R> extends FluentPrimitiveArrayAssertion<Byte,byte[],R> {
115
116   /**
117    * Chained constructor.
118    *
119    * <p>
120    * Used when transforming one assertion into another so that the assertion config can be used by the new assertion.
121    *
122    * @param creator
123    *    The assertion that created this assertion.
124    *    <br>Should be <jk>null</jk> if this is the top-level assertion.
125    * @param value
126    *    The object being tested.
127    *    <br>Can be <jk>null</jk>.
128    * @param returns
129    *    The object to return after a test method is called.
130    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
131    * used on the same assertion.
132    */
133   public FluentByteArrayAssertion(Assertion creator, byte[] value, R returns) {
134      super(creator, value, returns);
135   }
136
137   /**
138    * Constructor.
139    *
140    * @param value
141    *    The object being tested.
142    *    <br>Can be <jk>null</jk>.
143    * @param returns
144    *    The object to return after a test method is called.
145    *    <br>If <jk>null</jk>, the test method returns this object allowing multiple test method calls to be
146    * used on the same assertion.
147    */
148   public FluentByteArrayAssertion(byte[] value, R returns) {
149      this(null, value, returns);
150   }
151
152   /**
153    * Converts this byte array to a base-64 encoded string and returns it as a new assertion.
154    *
155    * <h5 class='section'>Example:</h5>
156    * <p class='bjava'>
157    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
158    *    <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asBase64().is(<js>"Zm9v"</js>);
159    * </p>
160    *
161    * @return A new fluent string assertion.
162    */
163   public FluentStringAssertion<R> asBase64() {
164      return new FluentStringAssertion<>(this, valueIsNull() ? null : base64Encode(value()), returns());
165   }
166
167   /**
168    * Converts this byte array to hexadecimal and returns it as a new assertion.
169    *
170    * <h5 class='section'>Example:</h5>
171    * <p class='bjava'>
172    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
173    *    <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asHex().is(<js>"666F6F"</js>);
174    * </p>
175    *
176    * @return A new string consisting of hexadecimal characters.
177    */
178   public FluentStringAssertion<R> asHex() {
179      return new FluentStringAssertion<>(this, valueIsNull() ? null : toHex(value()), returns());
180   }
181
182   /**
183    * Converts this byte array to spaced hexadecimal and returns it as a new assertion.
184    *
185    * <h5 class='section'>Example:</h5>
186    * <p class='bjava'>
187    *    <jc>// Validates that the specified byte array contains the string "foo".</jc>
188    *    <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asSpacedHex().is(<js>"66 6F 6F"</js>);
189    * </p>
190    *
191    * @return A new string consisting of hexadecimal characters.
192    */
193   public FluentStringAssertion<R> asSpacedHex() {
194      return new FluentStringAssertion<>(this, valueIsNull() ? null : toSpacedHex(value()), returns());
195   }
196
197   /**
198    * Converts this byte array to a UTF-8 encoded string and returns it as a new assertion.
199    *
200    * <h5 class='section'>Example:</h5>
201    * <p class='bjava'>
202    *    <jc>// Validates that the specified byte array contains the string "foobar".</jc>
203    *    <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString().is(<js>"foobar"</js>);
204    * </p>
205    *
206    * @return A new fluent string assertion.
207    */
208   @Override
209   public FluentStringAssertion<R> asString() {
210      return asString(UTF8);
211   }
212
213   /**
214    * Converts this byte array to a string and returns it as a new assertion.
215    *
216    * <h5 class='section'>Example:</h5>
217    * <p class='bjava'>
218    *    <jc>// Validates that the specified byte array contains the string "foobar" encoded in ASCII.</jc>
219    *    <jsm>assertBytes</jsm>(<jv>myByteArray</jv>).asString(<js>"iso8859-1"</js>).is(<js>"foobar"</js>);
220    * </p>
221    *
222    * @param cs The charset to use to decode the string.
223    * @return A new fluent string assertion.
224    */
225   public FluentStringAssertion<R> asString(Charset cs) {
226      return new FluentStringAssertion<>(this, valueIsNull() ? null : new String(value(), cs), returns());
227   }
228
229   @Override /* Overridden from Assertion */
230   public FluentByteArrayAssertion<R> setMsg(String msg, Object...args) {
231      super.setMsg(msg, args);
232      return this;
233   }
234
235   @Override /* Overridden from Assertion */
236   public FluentByteArrayAssertion<R> setOut(PrintStream value) {
237      super.setOut(value);
238      return this;
239   }
240
241   @Override /* Overridden from Assertion */
242   public FluentByteArrayAssertion<R> setSilent() {
243      super.setSilent();
244      return this;
245   }
246
247   @Override /* Overridden from Assertion */
248   public FluentByteArrayAssertion<R> setStdOut() {
249      super.setStdOut();
250      return this;
251   }
252
253   @Override /* Overridden from Assertion */
254   public FluentByteArrayAssertion<R> setThrowable(Class<? extends java.lang.RuntimeException> value) {
255      super.setThrowable(value);
256      return this;
257   }
258}