All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mstream.trader.commons.test.guice.TestCommonsModule Maven / Gradle / Ivy

The newest version!
package io.mstream.trader.commons.test.guice;


import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import cucumber.runtime.java.guice.ScenarioScoped;
import freemarker.template.Configuration;
import io.mstream.trader.commons.config.Application;
import io.mstream.trader.commons.config.ApplicationName;
import io.mstream.trader.commons.config.ApplicationVersionProvider;
import io.mstream.trader.commons.config.model.Version;
import io.mstream.trader.commons.test.MockServer;
import io.mstream.trader.commons.test.Socket;
import io.mstream.trader.commons.test.TestContext;
import io.mstream.trader.commons.test.cucumber.CachingStepdefs;
import io.mstream.trader.commons.test.cucumber.CommonStepDefs;
import io.mstream.trader.commons.test.cucumber.MonitoringStepDefs;
import io.mstream.trader.commons.test.util.date.DateModule;
import io.mstream.trader.commons.test.zookeeper.ZookeeperBootstrap;
import io.restassured.RestAssured;
import io.restassured.config.HttpClientConfig;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;
import org.mockserver.client.server.MockServerClient;

import static com.google.inject.Scopes.SINGLETON;
import static io.mstream.trader.commons.utils.Preconditions.checkNotEmpty;
import static java.lang.Integer.parseInt;
import static java.lang.String.format;
import static java.lang.System.getProperty;


public class TestCommonsModule
        extends AbstractModule {
    
    private static final String SERVICE_ADDRESS_PROPERTY_NAME =
            "SERVICE_ADDRESS";
    
    private static final String SERVICE_PORT_PROPERTY_NAME =
            "SERVICE_PORT";
    
    private static final String MOCK_ADDRESS_PROPERTY_NAME =
            "MOCK_ADDRESS";
    
    private static final String MOCK_SERVER_PORT_PROPERTY_NAME =
            "MOCK_SERVER_PORT";
    
    private static final String ZK_CONNECTION_STRING_PROPERTY_NAME =
            "ZK_CONNECTION_STRING";
    
    private final String applicationName;
    
    private final String testType;
    
    private final String mockServerAddress;
    
    private final String mockServerPort;
    
    public TestCommonsModule(
            String applicationName, String testType
    ) {
        
        this.applicationName = applicationName;
        this.testType = testType;
        String serviceAddress = getProperty(SERVICE_ADDRESS_PROPERTY_NAME);
        String servicePort = getProperty(SERVICE_PORT_PROPERTY_NAME);
        mockServerAddress = getProperty(MOCK_ADDRESS_PROPERTY_NAME);
        mockServerPort = getProperty(MOCK_SERVER_PORT_PROPERTY_NAME);
        
        checkNotEmpty(SERVICE_ADDRESS_PROPERTY_NAME, serviceAddress);
        checkNotEmpty(SERVICE_PORT_PROPERTY_NAME, servicePort);
        checkNotEmpty(MOCK_ADDRESS_PROPERTY_NAME, mockServerAddress);
        checkNotEmpty(MOCK_SERVER_PORT_PROPERTY_NAME, mockServerPort);
    
        int httpTimeout = 5000;
    
        RequestConfig requestConfig =
                RequestConfig
                        .custom()
                        .setConnectionRequestTimeout(httpTimeout)
                        .setConnectTimeout(httpTimeout)
                        .setSocketTimeout(httpTimeout)
                        .build();
        
        HttpClient httpClient = HttpClients
                .custom()
                .setDefaultRequestConfig(requestConfig)
                .build();
        
        RestAssured.baseURI = format(
                "http://%s:%d",
                serviceAddress,
                parseInt(servicePort)
        );
        
        RestAssured
                .config()
                .httpClient(
                        HttpClientConfig
                                .httpClientConfig()
                                .httpClientFactory(() -> httpClient)
                                .reuseHttpClientInstance()
                );
    }
    
    @Override
    protected void configure() {
        
        final String zkConnectionString =
                getProperty(ZK_CONNECTION_STRING_PROPERTY_NAME);
        
        checkNotEmpty(
                ZK_CONNECTION_STRING_PROPERTY_NAME,
                zkConnectionString
        );
        
        binder().requireExplicitBindings();
        
        install(new DateModule());
        
        CuratorFramework curator = CuratorFrameworkFactory.newClient(
                zkConnectionString,
                new ExponentialBackoffRetry(1000, 3)
        );
        curator.start();
        
        bind(CuratorFramework.class)
                .toInstance(curator);
        
        bind(ZookeeperBootstrap.class).asEagerSingleton();
        
        bind(TestContext.class).in(ScenarioScoped.class);
        
        bind(Socket.class)
                .annotatedWith(MockServer.class)
                .toInstance(new Socket(
                        mockServerAddress,
                        parseInt(mockServerPort)
                ));
        
        bind(MockServerClient.class)
                .toProvider(MockServerClientProvider.class)
                .in(ScenarioScoped.class);
        
        bind(Configuration.class)
                .toProvider(FreemakerConfigProvider.class)
                .in(Scopes.SINGLETON);
        
        bind(String.class)
                .annotatedWith(ApplicationName.class)
                .toInstance(applicationName);
        
        bind(Version.class)
                .annotatedWith(Application.class)
                .toProvider(new ApplicationVersionProvider(
                        format(
                                "%s-%s-tests",
                                applicationName,
                                testType
                        )
                ))
                .in(SINGLETON);
    
        bind(CommonStepDefs.class).in(ScenarioScoped.class);
        bind(MonitoringStepDefs.class).in(ScenarioScoped.class);
        bind(CachingStepdefs.class).in(ScenarioScoped.class);
    
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy