package org.apache.onami.lifecycle.guava;



import com.google.inject.*;
import org.junit.Test;
import org.junit.Assert;

import com.google.common.util.concurrent.Service;

import static com.google.inject.Guice.createInjector;

public final class GuavaServiceObjectTestCase
{
    public static class TestModule
        extends AbstractModule
    {
        @Override
        protected void configure()
        {
            bind(GuavaServiceObject.class);
        }
    }

    @Test
    public void testGuavaServiceStartup()
    {
        GuavaServiceObject svc = createInjector( new GuavaServiceStartModule(), new TestModule() ).getInstance( GuavaServiceObject.class );
        //  startAsync call doStart
        Assert.assertEquals(svc.sb.toString(), "s");
    }

    @Test
    public void testGuavaServiceStop()
    {
        GuavaServiceStopModule mod = new GuavaServiceStopModule();
        Injector injector = createInjector( mod, new TestModule() );
        GuavaServiceObject  svc = injector.getInstance( GuavaServiceObject.class);

        mod.getStager().stage();

        //  stopAsync will NOT call doStop since it is NOT started yet
        Assert.assertEquals("", svc.sb.toString());
    }

    @Test
    public void testGuavaService()
    {
        GuavaServiceStopModule mod = new GuavaServiceStopModule();
        Injector injector = createInjector( new GuavaServiceStartModule(), mod, new TestModule() );
        GuavaServiceObject  svc = injector.getInstance( GuavaServiceObject.class);

        mod.getStager().stage();

        //  complete lifcycle for Guava service
        Assert.assertEquals(svc.sb.toString(), "sS");
    }


}
