package ca.uhn.example;

import static org.junit.Assert.assertEquals;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.AfterClass;
import org.junit.Test;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.rest.client.IGenericClient;

public class ExampleTest {

	private static Integer ourPort;
	private static Server ourServer;
	private static FhirContext ourCtx;
	private static IGenericClient ourClient;

	@AfterClass
	public static void afterClass() throws Exception {
		ourServer.stop();
		
		System.clearProperty("ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS");
		
	}

	   public static boolean isWindows()
	   {
	      return System.getProperty("os.name").startsWith("Windows");
	   }
	
	@Test
	public void test01Search() throws Exception {
		if (isWindows()) {
			/*
			 * Tests here have some weird windows inconsistency relating to the path for finding the WAR file.
			 * Since this test isn't really important to work multiplatform, we can skip it
			 */
			return;
		}
		
		beforeClass();
		
		Bundle results = ourClient.search().forResource(Patient.class).execute();
		assertEquals(1, results.size());
	
		
	}
	
	/**
	 * Not annotated with @BeforeClass so that we can skip if we're not running tests here
	 */
	public static void beforeClass() throws Exception {
		if (ourPort != null) {
			return;
		}
		
		ourPort = RandomServerPortProvider.findFreePort();
		ourServer = new Server(ourPort);

		String base = "http://localhost:" + ourPort+"/fhir";
		System.setProperty("ca.uhn.fhir.to.TesterConfig_SYSPROP_FORCE_SERVERS", "example , Restful Server Example , " + base);		
		
		WebAppContext root = new WebAppContext();
		root.setAllowDuplicateFragmentNames(true);
		
		root.setWar("file:../restful-server-example/target/restful-server-example.war");
		root.setContextPath("/");
		root.setAttribute(WebAppContext.BASETEMPDIR, "target/tempextrtact");
		root.setParentLoaderPriority(false);
		root.setCopyWebInf(true);
		root.setCopyWebDir(true);

		ourServer.setHandler(root);

		ourServer.start();

		ourCtx = new FhirContext();
		ourClient = ourCtx.newRestfulGenericClient(base);


	}
	
}

@@ -616,16 +616,44 @@
 ption %7B%0A
+%09%09if (ourServer != null) %7B%0A%09
 %09%09ourSer
@@ -660,24 +660,28 @@
 ver.stop();%0A
+%09%09%7D%0A
 %09%09%0A%09%09System.

Fix test yet again!