How to use hasDigest method of org.assertj.core.api.AbstractInputStreamAssert class

Best Assertj code snippet using org.assertj.core.api.AbstractInputStreamAssert.hasDigest

Source:AbstractInputStreamAssert.java Github

copy

Full Screen

...206 * <pre><code class="java"> // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar207 * InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));208 *209 * // The following assertions succeed:210 * assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44});211 * assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25});212 *213 * // The following assertions fail:214 * assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes());215 * assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f".getBytes());</code></pre>216 *217 * @param digest the MessageDigest used to calculate the digests.218 * @param expected the expected binary content to compare the actual {@code InputStream}'s digest to.219 * @return {@code this} assertion object.220 * @throws NullPointerException if the given algorithm is {@code null}.221 * @throws NullPointerException if the given digest is {@code null}.222 * @throws AssertionError if the actual {@code InputStream} is {@code null}.223 * @throws AssertionError if the actual {@code InputStream} is not readable.224 * @throws InputStreamsException if an I/O error occurs.225 * @throws AssertionError if the content of the tested {@code InputStream}'s digest is not equal to the given one.226 * @since 3.11.0227 */228 public SELF hasDigest(MessageDigest digest, byte[] expected) {229 inputStreams.assertHasDigest(info, actual, digest, expected);230 return myself;231 }232 /**233 * Verifies that the tested {@link InputStream} digest (calculated with the specified {@link MessageDigest}) is equal to the given one.234 * <p>235 * Examples:236 * <pre><code class="java"> // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar237 * InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));238 *239 * // The following assertions succeed:240 * assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "5c5ae45b58f12023817abe492447cdc7912c1a2c");241 * assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "dcb3015cd28447644c810af352832c19");242 *243 * // The following assertions fail:244 * assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad");245 * assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f");</code></pre>246 *247 * @param digest the MessageDigest used to calculate the digests.248 * @param expected the expected binary content to compare the actual {@code InputStream}'s digest to.249 * @return {@code this} assertion object.250 * @throws NullPointerException if the given algorithm is {@code null}.251 * @throws NullPointerException if the given digest is {@code null}.252 * @throws AssertionError if the actual {@code InputStream} is {@code null}.253 * @throws AssertionError if the actual {@code InputStream} is not readable.254 * @throws InputStreamsException if an I/O error occurs.255 * @throws AssertionError if the content of the tested {@code InputStream}'s digest is not equal to the given one.256 * @since 3.11.0257 */258 public SELF hasDigest(MessageDigest digest, String expected) {259 inputStreams.assertHasDigest(info, actual, digest, expected);260 return myself;261 }262 /**263 * Verifies that the tested {@link InputStream} digest (calculated with the specified algorithm) is equal to the given one.264 * <p>265 * Examples:266 * <pre><code class="java"> // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar267 * InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));268 *269 * // The following assertion succeeds:270 * assertThat(tested).hasDigest("SHA1", new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44});271 * assertThat(tested).hasDigest("MD5", new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25});272 *273 * // The following assertion fails:274 * assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes());275 * assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f".getBytes()); </code></pre>276 *277 * @param algorithm the algorithm used to calculate the digests.278 * @param expected the expected binary content to compare the actual {@code InputStream}'s content to.279 * @return {@code this} assertion object.280 * @throws NullPointerException if the given algorithm is {@code null}.281 * @throws NullPointerException if the given digest is {@code null}.282 * @throws AssertionError if the actual {@code InputStream} is {@code null}.283 * @throws AssertionError if the actual {@code InputStream} is not readable.284 * @throws InputStreamsException if an I/O error occurs.285 * @throws AssertionError if the content of the tested {@code InputStream}'s digest is not equal to the given one.286 * @since 3.11.0287 */288 public SELF hasDigest(String algorithm, byte[] expected) {289 inputStreams.assertHasDigest(info, actual, algorithm, expected);290 return myself;291 }292 /**293 * Verifies that the tested {@link InputStream} digest (calculated with the specified algorithm) is equal to the given one.294 * <p>295 * Examples:296 * <pre><code class="java"> // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar297 * InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));298 *299 * // The following assertion succeeds:300 * assertThat(tested).hasDigest("SHA1", "5c5ae45b58f12023817abe492447cdc7912c1a2c");301 * assertThat(tested).hasDigest("MD5", "dcb3015cd28447644c810af352832c19");302 *303 * // The following assertion fails:304 * assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad");305 * assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f"); </code></pre>306 *307 * @param algorithm the algorithm used to calculate the digests.308 * @param expected the expected binary content to compare the actual {@code InputStream}'s content to.309 * @return {@code this} assertion object.310 * @throws NullPointerException if the given algorithm is {@code null}.311 * @throws NullPointerException if the given digest is {@code null}.312 * @throws AssertionError if the actual {@code InputStream} is {@code null}.313 * @throws AssertionError if the actual {@code InputStream} is not readable.314 * @throws InputStreamsException if an I/O error occurs.315 * @throws AssertionError if the content of the tested {@code InputStream}'s digest is not equal to the given one.316 * @since 3.11.0317 */318 public SELF hasDigest(String algorithm, String expected) {319 inputStreams.assertHasDigest(info, actual, algorithm, expected);320 return myself;321 }322 private String readString(Charset charset) {323 try {324 ByteArrayOutputStream buffer = new ByteArrayOutputStream();325 int read;326 byte[] data = new byte[1024];327 while ((read = actual.read(data, 0, data.length)) != -1) {328 buffer.write(data, 0, read);329 }330 buffer.flush();331 return new String(buffer.toByteArray(), charset);332 } catch (IOException e) {...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful