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

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

There is a newer version: 1.14
Show 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.test.MockServer;
import io.mstream.trader.commons.test.Socket;
import io.mstream.trader.commons.test.TestContext;
import io.mstream.trader.commons.test.util.date.DateModule;
import io.restassured.RestAssured;
import org.mockserver.client.server.MockServerClient;

import static io.mstream.trader.commons.utils.Preconditions.checkNotEmpty;
import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;


public class CommonsModule 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 final String mockServerAddress;
    private final String mockServerPort;

    public CommonsModule() {
        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);

        RestAssured.baseURI = String.format(
                "http://%s:%d",
                serviceAddress,
                parseInt(servicePort));
    }

    @Override
    protected void configure() {
        binder().requireExplicitBindings();

        install(new DateModule());

        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);


    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy