All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.artipie.asto.test.ContentIs Maven / Gradle / Ivy

/*
 * The MIT License (MIT) Copyright (c) 2020-2023 artipie.com
 * https://github.com/artipie/artipie/blob/master/LICENSE.txt
 */
package com.artipie.asto.test;

import com.artipie.asto.Content;
import com.google.common.util.concurrent.Uninterruptibles;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;

import java.nio.charset.Charset;
import java.util.concurrent.ExecutionException;

/**
 * Matcher for {@link Content}.
 * @since 0.24
 */
public final class ContentIs extends TypeSafeMatcher {

    /**
     * Byte array matcher.
     */
    private final Matcher matcher;

    /**
     * Content is a string with encoding.
     * @param expected String
     * @param enc Encoding charset
     */
    public ContentIs(final String expected, final Charset enc) {
        this(expected.getBytes(enc));
    }

    /**
     * Content is a byte array.
     * @param expected Byte array
     */
    public ContentIs(final byte[] expected) {
        this(Matchers.equalTo(expected));
    }

    /**
     * Content matches for byte array matcher.
     * @param matcher Byte array matcher
     */
    public ContentIs(final Matcher matcher) {
        this.matcher = matcher;
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("has bytes ").appendValue(this.matcher);
    }

    @Override
    public boolean matchesSafely(final Content item) {
        try {
            return this.matcher.matches(
                Uninterruptibles.getUninterruptibly(item.asBytesFuture())
            );
        } catch (final ExecutionException err) {
            throw new IllegalStateException("Failed to read content", err);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy