package org.jboss.resteasy.test.cdi.interceptors;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.swing.text.Utilities;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.logging.Logger;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBook;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookReader;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookReaderInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookReaderInterceptorInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookWriter;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookWriterInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorBookWriterInterceptorInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorClassBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorClassInterceptorStereotype;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorClassMethodInterceptorStereotype;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorFilterBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorFour;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorLifecycleBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorMethodBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorOne;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorPostConstructInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorPreDestroyInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorReaderBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorRequestFilter;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorRequestFilterInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorRequestFilterInterceptorBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorResource;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorResponseFilter;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorResponseFilterInterceptor;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorResponseFilterInterceptorBinding;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorStereotyped;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorThree;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorTwo;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorVisitList;
import org.jboss.resteasy.test.cdi.interceptors.resource.InterceptorWriterBinding;
import org.jboss.resteasy.test.cdi.util.Constants;
import org.jboss.resteasy.test.cdi.util.UtilityProducer;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
 * @tpSubChapter CDI
 * @tpChapter Integration tests
 * @tpTestCaseDetails Interceptors test.
 * @tpSince RESTEasy 3.0.16
 */
@ExtendWith(ArquillianExtension.class)
@RunAsClient
public class InterceptorTest {
    protected static final Logger log = Logger.getLogger(InterceptorTest.class.getName());

    @Deployment
    public static Archive<?> createTestArchive() {

        WebArchive war = TestUtil.prepareArchive(InterceptorTest.class.getSimpleName());
        war.addClasses(Constants.class, UtilityProducer.class, Utilities.class, InterceptorVisitList.class)
                .addClasses(InterceptorResource.class, InterceptorOne.class, InterceptorTwo.class)
                .addClasses(InterceptorClassBinding.class, InterceptorMethodBinding.class, InterceptorThree.class,
                        InterceptorFour.class)
                .addClasses(InterceptorFilterBinding.class, InterceptorRequestFilterInterceptorBinding.class)
                .addClasses(InterceptorResponseFilterInterceptorBinding.class)
                .addClasses(InterceptorRequestFilterInterceptor.class, InterceptorResponseFilterInterceptor.class,
                        InterceptorRequestFilter.class, InterceptorResponseFilter.class)
                .addClasses(InterceptorReaderBinding.class, InterceptorWriterBinding.class)
                .addClasses(InterceptorBook.class, InterceptorBookReader.class, InterceptorBookWriter.class)
                .addClasses(InterceptorBookReaderInterceptor.class, InterceptorBookWriterInterceptor.class)
                .addClasses(InterceptorBookReaderInterceptorInterceptor.class,
                        InterceptorBookWriterInterceptorInterceptor.class)
                .addClasses(InterceptorClassInterceptorStereotype.class, InterceptorClassMethodInterceptorStereotype.class,
                        InterceptorStereotyped.class)
                .addClasses(InterceptorLifecycleBinding.class, InterceptorPostConstructInterceptor.class,
                        InterceptorPreDestroyInterceptor.class)
                .addAsWebInfResource(InterceptorTest.class.getPackage(), "interceptorBeans.xml", "beans.xml");
        return war;
    }

    private String generateURL(String path) {
        return PortProviderUtil.generateURL(path, InterceptorTest.class.getSimpleName());
    }

    /**
     * @tpTestDetails One item is stored and load to collection in resources.
     * @tpSince RESTEasy 3.0.16
     */
    @Test
    public void testInterceptors() throws Exception {
        Client client = ClientBuilder.newClient();

        // Create book.
        InterceptorBook book = new InterceptorBook("RESTEasy: the Sequel");
        WebTarget base = client.target(generateURL("/create/"));
        Response response = base.request().post(Entity.entity(book, Constants.MEDIA_TYPE_TEST_XML));
        assertEquals(200, response.getStatus());
        int id = response.readEntity(int.class);
        Assertions.assertEquals(id, 0, "Id of stored book is wrong.");

        // Retrieve book.
        base = client.target(generateURL("/book/" + id));
        response = base.request().accept(Constants.MEDIA_TYPE_TEST_XML).get();
        assertEquals(200, response.getStatus());
        InterceptorBook result = response.readEntity(InterceptorBook.class);
        assertEquals(book, result, "Wrong book is received.");

        // check interceptors
        base = client.target(generateURL("/test/"));
        response = base.request().post(Entity.text(new String()));
        assertEquals(200, response.getStatus());

        client.close();
    }
}
