package com.pm.server.controller;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Arrays;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.jayway.jsonpath.JsonPath;
import com.pm.server.ControllerTestTemplate;
import com.pm.server.datatype.Coordinate;
import com.pm.server.datatype.CoordinateImpl;
public class GhostControllerTest extends ControllerTestTemplate {

	private static final List<Coordinate> randomCoordinateList = Arrays.asList(
			new CoordinateImpl(12345.54321, 95837.39821),
			new CoordinateImpl(49381.30982, 39399.49932)
	);

	private static final String BASE_MAPPING = "/ghost";

	private static final Logger log =
			LogManager.getLogger(GhostControllerTest.class.getName());

	@Autowired
	private WebApplicationContext webApplicationContext;

	private MockMvc mockMvc;

	@Before
	public void setUp() {

		mockMvc = MockMvcBuilders
				.webAppContextSetup(this.webApplicationContext)
				.build();

	}

	// Returns the ID of the created ghost
	private Integer createGhost_failUponException(Coordinate location) {

		String path =
				BASE_MAPPING + "/" +
				location.getLatitude() + "/" +
				location.getLongitude();
		String jsonContent = null;

		try {
			MvcResult result = mockMvc
					.perform(post(path))
					.andExpect(status().isOk())
					.andReturn();
			jsonContent = result.getResponse().getContentAsString();
		}
		catch(Exception e) {
			log.error(e.getMessage());
			fail();
		}

		assertNotNull(jsonContent);
		return JsonPath.read(jsonContent, "$.id");

	}

}

@@ -199,16 +199,106 @@
 s.post;%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;%0A
 import s
@@ -377,16 +377,16 @@
 status;%0A
-
 %0Aimport 
@@ -536,16 +536,39 @@
 Before;%0A
+import org.junit.Test;%0A
 import o
@@ -1636,16 +1636,16 @@
 ontext)%0A
-
 %09%09%09%09.bui
@@ -1655,16 +1655,1170 @@
 );%0A%0A%09%7D%0A%0A
+%09@Test%0A%09public void unitTest_createGhost() throws Exception %7B%0A%0A%09%09// Given%0A%09%09Coordinate location = randomCoordinateList.get(0);%0A%09%09String path =%0A%09%09%09%09BASE_MAPPING + %22/%22 +%0A%09%09%09%09location.getLatitude() + %22/%22 +%0A%09%09%09%09location.getLongitude();%0A%0A%09%09// When%0A%09%09mockMvc%0A%09%09%09%09.perform(post(path))%0A%0A%09%09// Then%0A%09%09%09%09.andExpect(status().isOk())%0A%09%09%09%09.andExpect(jsonPath(%22$.id%22).exists());%0A%0A%09%7D%0A%0A%09@Test%0A%09public void unitTest_createGhost_sameLocation() throws Exception %7B%0A%0A%09%09// Given%0A%09%09Coordinate location = randomCoordinateList.get(0);%0A%09%09String path =%0A%09%09%09%09BASE_MAPPING + %22/%22 +%0A%09%09%09%09location.getLatitude() + %22/%22 +%0A%09%09%09%09location.getLongitude();%0A%0A%09%09mockMvc%0A%09%09%09%09.perform(post(path))%0A%09%09%09%09.andExpect(status().isOk());%0A%0A%09%09// When%0A%09%09mockMvc%0A%09%09%09%09.perform(post(path))%0A%0A%09%09// Then%0A%09%09%09%09.andExpect(status().isOk())%0A%09%09%09%09.andExpect(jsonPath(%22$.id%22).exists());%0A%0A%09%7D%0A%0A%09@Test%0A%09public void unitTest_createGhost_notANumber() throws Exception %7B%0A%0A%09%09// Given%0A%09%09Coordinate location = randomCoordinateList.get(0);%0A%09%09String path =%0A%09%09%09%09BASE_MAPPING + %22/%22 +%0A%09%09%09%09location.getLatitude() + %22/%22 +%0A%09%09%09%09%22longitude%22;%0A%0A%09%09// When%0A%09%09mockMvc%0A%09%09%09%09.perform(post(path))%0A%0A%09%09// Then%0A%09%09%09%09.andExpect(status().isBadRequest());%0A%0A%09%7D%0A%0A
 %09// Retu

Write GhostControllerTest createGhost tests