package io.dropwizard.client;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URI;
import java.util.concurrent.Executors;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Optional;
import com.google.common.io.Resources;

import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.DropwizardAppRule;
import io.dropwizard.util.Duration;

public class JerseyIgnoreRequestUserAgentHeaderFilterTest {
    private static final int SLEEP_TIME_IN_MILLIS = 500;
    private static final int DEFAULT_CONNECT_TIMEOUT_IN_MILLIS = 200;

    @ClassRule
    public static DropwizardAppRule<Configuration> APP_RULE =
            new DropwizardAppRule<>(TestApplication.class, Resources.getResource("yaml/jerseyIgnoreRequestUserAgentHeaderFilterTest.yml").getPath());
    
    public final URI testUri = URI.create("http://localhost:" + APP_RULE.getLocalPort());

    private JerseyClientBuilder clientBuilder;
    
    private JerseyClientConfiguration clientConfiguration;

    @Before
    public void setup() {
        clientConfiguration = new JerseyClientConfiguration();
        clientConfiguration.setConnectionTimeout(Duration.milliseconds(SLEEP_TIME_IN_MILLIS / 2));
        clientConfiguration.setTimeout(Duration.milliseconds(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS));
        clientBuilder = new JerseyClientBuilder(new MetricRegistry())
            .using(clientConfiguration)
            .using(Executors.newSingleThreadExecutor(), Jackson.newObjectMapper());
    }
    
    @Test
    public void clientIsSetRequestIsNotSet() {
        clientConfiguration.setUserAgent(Optional.of("ClientUserAgentHeaderValue"));
        assertThat(
                clientBuilder.using(clientConfiguration).
                build("ClientName").target(testUri + "/user_agent")
                        .request()
                        .get(String.class)
        ).isEqualTo("ClientUserAgentHeaderValue");
    }
    
    @Test
    public void clientIsNotSetRequestIsSet() {
        assertThat(
                clientBuilder.build("ClientName").target(testUri + "/user_agent")
                        .request().header("User-Agent", "RequestUserAgentHeaderValue")
                        .get(String.class)
        ).isEqualTo("RequestUserAgentHeaderValue");
    }

    @Test
    public void clientIsNotSetRequestIsNotSet() {
        assertThat(false);        
        assertThat(
                clientBuilder.build("ClientName").target(testUri + "/user_agent")
                        .request()
                        .get(String.class)
        ).isEqualTo("ClientName");
    }

    @Test
    public void clientIsSetRequestIsSet() {
        clientConfiguration.setUserAgent(Optional.of("ClientUserAgentHeaderValue"));
        assertThat(
                clientBuilder.build("ClientName").target(testUri + "/user_agent")
                        .request().header("User-Agent", "RequestUserAgentHeaderValue")
                        .get(String.class)
        ).isEqualTo("RequestUserAgentHeaderValue");
    }
    
    
    @Path("/")
    public static class TestResource {

       @GET
        @Path("user_agent")
        public String getReturnUserAgentHeader(@HeaderParam("User-Agent") String userAgentHeader) {
            return userAgentHeader;
        }
        
    }

    public static class TestApplication extends Application<Configuration> {
        public static void main(String[] args) throws Exception {
            new TestApplication().run(args);
        }

        @Override
        public void run(Configuration configuration, Environment environment) throws Exception {
            environment.jersey().register(TestResource.class);
        }
    }



}
@@ -730,136 +730,8 @@
 t %7B%0A
-    private static final int SLEEP_TIME_IN_MILLIS = 500;%0A    private static final int DEFAULT_CONNECT_TIMEOUT_IN_MILLIS = 200;%0A%0A
     
@@ -741,16 +741,16 @@
 assRule%0A
+
     publ
@@ -963,21 +963,22 @@
   %0A    p
-ublic
+rivate
  final U
@@ -1049,17 +1049,16 @@
 ort());%0A
-%0A
     priv
@@ -1096,21 +1096,16 @@
 uilder;%0A
-    %0A
     priv
@@ -1332,32 +1332,13 @@
 nds(
-SLEEP_TIME_IN_MILLIS / 2
+1000L
 ));%0A
@@ -1402,41 +1402,13 @@
 nds(
-DEFAULT_CONNECT_TIMEOUT_IN_MILLIS
+2500L
 ));%0A
@@ -3134,25 +3134,16 @@
 ;%0A    %7D%0A
-    %0A    
 %0A    @Pa
@@ -3189,17 +3189,16 @@
 ource %7B%0A
-%0A
        @
@@ -3375,25 +3375,16 @@
       %7D%0A
-        %0A
     %7D%0A%0A 
@@ -3775,12 +3775,9 @@
 %7D%0A    %7D%0A
-%0A%0A%0A
 %7D

Increase client timeouts in JerseyIgnoreRequestUserAgentHeaderFilterTest