package org.dcache.ldap4testing;

import com.google.common.io.Resources;
import com.google.common.util.concurrent.Service;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;

public class EmbeddedServerTest {

    EmbeddedServer server;

    @After
    public void shutdown() {
        if (server != null && server.state() != Service.State.TERMINATED) {
            server.stopAsync().awaitTerminated();
            assertNotFailed(server);
        }
    }

    @Test
    public void testStaringAndStopServer() {
        server = new EmbeddedServer(0);

        server.startAsync().awaitRunning();
        assertNotFailed(server);
        assertEquals("Embedded LDAP server failed to start", Service.State.RUNNING, server.state());

        server.stopAsync().awaitTerminated();
        assertNotFailed(server);
        assertEquals("Embedded LDAP server failed to stop", Service.State.TERMINATED, server.state());
    }


    @Test
    public void testServerWithLdiffFile() {
        InputStream initialLdif = ClassLoader.getSystemResourceAsStream("org/dcache/ldap4testing/init.ldif");
        server = new EmbeddedServer(0, initialLdif);

        server.startAsync().awaitRunning();
        assertNotFailed(server);
        assertEquals("Embedded LDAP server failed to start", Service.State.RUNNING, server.state());
    }

    private void assertNotFailed(EmbeddedServer server) {
        if (server.state() == Service.State.FAILED) {
            fail(server.failureCause().toString());
        }
    }
}

@@ -31,47 +31,8 @@
 g;%0A%0A
-import com.google.common.io.Resources;%0A
 impo
@@ -109,65 +109,8 @@
 am;%0A
-import java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 impo

remove unused imports