package org.dstadler.commons.testing;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.InetAddress;
import java.util.logging.Logger;

import org.dstadler.commons.http.NanoHTTPD;
import org.dstadler.commons.logging.jdk.LoggerFactory;
import org.dstadler.commons.net.UrlUtils;
import org.junit.Test;


/**
 * Just verify that the host where the tests are run does what we
 * expect it to in respect to hostname resolution/DNS/...
 *
 * @author cwat-dstadler
 */
public class MockRESTServerTest {
    private static final Logger log = LoggerFactory.make();

	@Test
	public void testLocalhost() throws IOException {
		runWithHostname("localhost");
	}

	@Test
	public void testLocalhostIP() throws IOException {
		runWithHostname("127.0.0.1");
	}

	@Test
	public void testIP() throws IOException {
		InetAddress localHost = java.net.InetAddress.getLocalHost();
		assertNotNull("Should get a local address", localHost);
		String ipaddress = localHost.getHostAddress();

		log.info("Had hostname: " + ipaddress + ", address-info: " + localHost);

		assertNotNull("Should get a local ip-address", ipaddress);
		assertFalse("Local ip-address should not equal localhost", ipaddress.equals("localhost"));
		// cannot assert on startsWith("127.0.0") as e.g. lab13 reports an ip-address of 127.0.0.2
		assertFalse("Local ip-address should not equal 127.0.0.1", ipaddress.equals("127.0.0.1"));

		runWithHostname(ipaddress);
	}

	@Test
	public void testHostname() throws IOException {
		assertNotNull(java.net.InetAddress.getLocalHost());
		String hostname = java.net.InetAddress.getLocalHost().getHostName();
		assertNotNull(hostname);
		assertFalse("Local hostname should not equal localhost", hostname.equals("localhost"));
		assertFalse("Local hostname should not start with 127.0.0", hostname.startsWith("127.0.0"));

		runWithHostname(hostname);
	}

	@Test
	public void testCanonicalHostname() throws IOException {
		assertNotNull(java.net.InetAddress.getLocalHost());
		String hostname = java.net.InetAddress.getLocalHost().getCanonicalHostName();
		assertNotNull(hostname);
		assertFalse(hostname.equals("localhost"));
		assertFalse(hostname.startsWith("127.0.0"));

		runWithHostname(hostname);
	}

	private void runWithHostname(String hostname) throws IOException {
		try (MockRESTServer server = new MockRESTServer(NanoHTTPD.HTTP_OK,  NanoHTTPD.MIME_PLAINTEXT, "OK")) {
			boolean check = UrlUtils.isAvailable("http://" + hostname + ":" + server.getPort(), false, 500);
			assertTrue("Host: " + hostname + ":" + server.getPort() + ": Had: " + check, check);

			check = UrlUtils.isAvailable("http://" + hostname + ":" + server.getPort(), true, 500);
			assertTrue("Host: " + hostname + ":" + server.getPort() + ": Had: " + check, check);

			String checkStr = UrlUtils.retrieveData("http://" + hostname + ":" + server.getPort(), 500);
			assertTrue("Host: " + hostname + ":" + server.getPort() + ": Had: " + check, checkStr.length() > 0);

			String data = UrlUtils.retrieveData("http://" + hostname + ":" + server.getPort(), 500);
			assertEquals("Host: " + hostname + ":" + server.getPort() + ": Had: " + data, "OK", data);
		}
	}

	@SuppressWarnings("deprecation")
	@Test
	public void testStop() throws IOException {
		@SuppressWarnings("resource")
		MockRESTServer server = new MockRESTServer(NanoHTTPD.HTTP_OK,  NanoHTTPD.MIME_PLAINTEXT, "OK");
		try {
			boolean check = UrlUtils.isAvailable("http://localhost:" + server.getPort(), false, 500);
			assertTrue("Host: localhost: Had: " + check, check);
		} finally {
			server.stop();
		}
	}
}

@@ -306,24 +306,49 @@
 .UrlUtils;%0D%0A
+import org.junit.After;%0D%0A
 import org.j
@@ -623,24 +623,158 @@
 .make();%0D%0A%0D%0A
+%09@After%0D%0A%09public void tearDown() throws InterruptedException %7B%0D%0A%09%09ThreadTestHelper.waitForThreadToFinishSubstring(%22NanoHTTP%22);%0D%0A%09%7D%0D%0A%0D%0A
 %09@Test%0D%0A%09pub
@@ -3824,12 +3824,637 @@
 %09%09%7D%0D%0A%09%7D%0D
+%0A%0D%0A%09@Test%0D%0A%09public void testStartupTwice() throws IOException %7B%0D%0A%09%09try (MockRESTServer server = new MockRESTServer(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, %22%3Chtml%3E1%3C/html%3E%22)) %7B%0D%0A%09%09%09try (MockRESTServer server2 = new MockRESTServer(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, %22%3Chtml%3E2%3C/html%3E%22)) %7B%0D%0A%09%09%09%09assertTrue(server.getPort() != server2.getPort());%0D%0A%0D%0A%09%09%09%09String data = UrlUtils.retrieveData(%22http://localhost:%22 + server.getPort(), 10_000);%0D%0A%09%09%09%09assertEquals(%22%3Chtml%3E1%3C/html%3E%22, data);%0D%0A%0D%0A%09%09%09%09data = UrlUtils.retrieveData(%22http://localhost:%22 + server2.getPort(), 10_000);%0D%0A%09%09%09%09assertEquals(%22%3Chtml%3E2%3C/html%3E%22, data);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%7D%0D
 %0A%7D%0D%0A

Add test with more than one MockRESTServer and verify that no NanoHTTPD threads are left after shutdown