package org.jboss.resteasy.test.client;

import java.io.ByteArrayInputStream;
import java.util.Locale;

import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

import org.jboss.logging.Logger;
import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.test.client.resource.ClientCustomException;
import org.jboss.resteasy.test.client.resource.RequestFilterAbortWith;
import org.jboss.resteasy.test.client.resource.RequestFilterAcceptLanguage;
import org.jboss.resteasy.test.client.resource.RequestFilterAnnotation;
import org.jboss.resteasy.test.client.resource.RequestFilterGetEntity;
import org.jboss.resteasy.test.client.resource.RequestFilterSetEntity;
import org.jboss.resteasy.test.client.resource.RequestFilterThrowCustomException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
 * @tpSubChapter Resteasy-client
 * @tpChapter Client tests
 * @tpSince RESTEasy 3.0.16
 */
public class RequestFilterTest {

    protected static final Logger logger = Logger.getLogger(RequestFilterTest.class.getName());

    static Client client;
    String dummyUrl = "dummyUrl";

    @BeforeAll
    public static void setupClient() {
        client = ClientBuilder.newClient();

    }

    @AfterAll
    public static void close() {
        client.close();
    }

    /**
     * @tpTestDetails Client registers implementation of ClientRequestFilter and sends GET request. The request is
     *                processed by registered filter before sending it to the server. Filter aborts processing and sends its own
     *                response
     * @tpPassCrit Expected String is returned from the ClientRequestFilter
     * @tpSince RESTEasy 3.0.16
     */
    @Test
    public void AbortWithTest() {
        Response response = client.target(dummyUrl).register(RequestFilterAbortWith.class).request().get();
        String str = response.readEntity(String.class);
        Assertions.assertEquals("42", str,
                "String returned from ClientRequest filter doesn't contain the expected value");
    }

    /**
     * @tpTestDetails Client registers implementation of ClientRequestFilter and sends GET request.
     *                The request has preset accepted language types. The request is processed by registered filter before
     *                it is send to the server. Filter aborts processing and sends its own response with list of acceptable
     *                languages
     *                from the request.
     * @tpPassCrit Expected String is returned from the ClientRequestFilter
     * @tpSince RESTEasy 3.0.16
     */
    @Test
    public void AcceptLanguagesTest() {
        Response response = client.target(dummyUrl).register(RequestFilterAcceptLanguage.class).request()
                .acceptLanguage(Locale.CANADA_FRENCH)
                .acceptLanguage(Locale.PRC).get();
        String str = response.readEntity(String.class);
        logger.info("The locale from the response: " + str);
        logger.info("The expected locale: " + Locale.CANADA_FRENCH.toString());
        Assertions.assertTrue(str.contains(Locale.CANADA_FRENCH.toString()),
                "String returned from ClientRequest filter doesn't contain accepted locale");

    }

    /**
     * @tpTestDetails Client registers implementations of ClientRequestFilters and sends POST request.
     *                The request is processed by both filters one setting request entity and the other checking the entity is
     *                the same
     *                entity set up in the first filter. Second Filter aborts processing and sends its own response.
     *                from the request.
     * @tpPassCrit The request is processed by both filters.
     * @tpSince RESTEasy 3.0.16
     */
    @Test
    public void MultipleFiltersTest() {
        Entity<ByteArrayInputStream> entity = Entity.entity(new ByteArrayInputStream(
                "test".getBytes()), MediaType.WILDCARD_TYPE);
        Response response = client.target(dummyUrl).register(RequestFilterSetEntity.class)
                .register(RequestFilterGetEntity.class).request().post(entity);
        Assertions.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
    }

    /**
     * @tpTestDetails Client registers implementation of ClientRequestFilter and sends POST request.
     *                The request is processed by registered filter before it is send to the server. Filter aborts processing
     *                and sends its own response with request entity annotation.
     * @tpPassCrit Expected Annotation is returned from the ClientRequestFilter
     * @tpSince RESTEasy 3.0.16
     */
    @Test
    public void AnnotationFilterTest() {
        Entity<String> post = Entity.entity("test", MediaType.WILDCARD_TYPE,
                RequestFilterAnnotation.class.getAnnotations());
        Response response = client.target(dummyUrl).register(RequestFilterAnnotation.class).request().post(post);
        Assertions.assertEquals(Provider.class.getName(), response.readEntity(String.class),
                "The response doesn't contain the expexted provider name");
    }

    /**
     * @tpTestDetails Client registers implementation of ClientRequestFilter and sends GET request.
     *                The request is processed by registered filter before it is send to the server. Filter aborts processing
     *                by throwing a custom exception, which should not be wrapped in a ProcessingException. [RESTEASY-1591]
     * @tpPassCrit Expected Exception is thrown from the Client
     * @tpSince RESTEasy 3.0.21
     */
    @Test
    public void ThrowCustomExceptionFilterTest() {
        try {
            client.target(dummyUrl).register(RequestFilterThrowCustomException.class).request().get();
            Assertions.fail();
        } catch (ProcessingException pe) {
            Assertions.assertEquals(ClientCustomException.class, pe.getCause().getClass());
        }
    }
}
