package com.resteasytokenbasedauth.test

import com.resteasytokenbasedauth.test.filter.CustomLogFilter
import org.apache.http.impl.client.HttpClients
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.jboss.resteasy.client.jaxrs.ResteasyClient
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine
import org.junit.After
import org.junit.Before
import javax.ws.rs.core.UriBuilder

/**
 * Created by Tharaka on 9/29/2017.
 * Project: RESTEasyTokenBasedAuth
 */
abstract class BaseRestServiceTest {

    protected lateinit var client: ResteasyClient
    protected lateinit var baseResource: String
    protected lateinit var target: ResteasyWebTarget

    @Before open fun setUp() {
        val connectionManager = PoolingHttpClientConnectionManager()
        val httpClient = HttpClients.custom().apply {
            setConnectionManager(connectionManager)
        }.build()
        val engine = ApacheHttpClient4Engine(httpClient)
        client = ResteasyClientBuilder().apply {
            httpEngine(engine)
        }.build()
        client.register(CustomLogFilter::class.java)
        baseResource = "$PROTOCOL://$HOSTNAME:$PORT/rest"
        target = client.target(UriBuilder.fromPath(baseResource))
    }

    @After
    fun cleanUp() {
        client.close()
    }

    companion object {

        private val PROTOCOL = "http"
        private val HOSTNAME = "localhost"
        private val PORT = "8080"
    }
}
