summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/test/ClientTestDriver.java
blob: 1a5553fb608cad54f94466c71886ffaeed010495 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.test;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.ning.http.util.AllowAllHostnameVerifier;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.http.client.HttpClient;
import com.yahoo.jdisc.http.client.HttpClientConfig;
import com.yahoo.jdisc.http.client.filter.ResponseFilter;
import com.yahoo.jdisc.service.CurrentContainer;
import com.yahoo.jdisc.test.TestDriver;

import javax.net.ssl.HostnameVerifier;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class ClientTestDriver {

    private final TestDriver driver;
    private final HttpClient client;
    private final RemoteServer server;

    private ClientTestDriver(TestDriver driver, HttpClient client) throws IOException {
        this.driver = driver;
        this.client = client;
        this.server = RemoteServer.newInstance();
    }

    public CurrentContainer currentContainer() {
        return driver;
    }

    public boolean close() {
        if (!server.close(60, TimeUnit.SECONDS)) {
            return false;
        }
        client.release();
        return driver.close();
    }

    public HttpClient client() {
        return client;
    }

    public RemoteServer server() {
        return server;
    }

    public static ClientTestDriver newInstance(Module... guiceModules) throws IOException {
        return newInstance(new HttpClientConfig.Builder().sslConnectionPoolEnabled(false),
                           guiceModules);
    }

    public static ClientTestDriver newInstance(HttpClientConfig.Builder config, Module... guiceModules)
            throws IOException {
        Module[] lst = new Module[guiceModules.length + 2];
        lst[0] = newDefaultModule();
        lst[lst.length - 1] = newConfigModule(config);
        System.arraycopy(guiceModules, 0, lst, 1, guiceModules.length);
        return newInstanceImpl(HttpClient.class, lst);
    }

    private static ClientTestDriver newInstanceImpl(Class<? extends HttpClient> clientClass,
                                                    Module... guiceModules) throws IOException {
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(guiceModules);
        ContainerBuilder builder = driver.newContainerBuilder();
        HttpClient client = builder.guiceModules().getInstance(clientClass);
        builder.serverBindings().bind("*://*/*", client);
        driver.activateContainer(builder);
        try {
            client.start();
        } catch (RuntimeException e) {
            client.release();
            driver.close();
            throw e;
        }
        return new ClientTestDriver(driver, client);
    }

    public static Module newDefaultModule() {
        return new AbstractModule() {

            @Override
            protected void configure() {
                bind(HostnameVerifier.class).to(AllowAllHostnameVerifier.class);
                bind(new TypeLiteral<List<ResponseFilter>>() { }).toInstance(Collections.<ResponseFilter>emptyList());
            }
        };
    }

    public static Module newConfigModule(final HttpClientConfig.Builder config) {
        return new AbstractModule() {

            @Override
            protected void configure() {
                bind(HttpClientConfig.class).toInstance(new HttpClientConfig(config));
            }
        };
    }

    public static Module newFilterModule(final ResponseFilter... filters) {
        return new AbstractModule() {

            @Override
            protected void configure() {
                bind(new TypeLiteral<List<ResponseFilter>>() { }).toInstance(Arrays.asList(filters));
            }
        };
    }
}