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.jsonPath;
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.junit.Test;
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();

	}

	@Test
	public void unitTest_createGhost() throws Exception {

		// Given
		Coordinate location = randomCoordinateList.get(0);
		String path =
				BASE_MAPPING + "/" +
				location.getLatitude() + "/" +
				location.getLongitude();

		// When
		mockMvc
				.perform(post(path))

		// Then
				.andExpect(status().isOk())
				.andExpect(jsonPath("$.id").exists());

	}

	@Test
	public void unitTest_createGhost_sameLocation() throws Exception {

		// Given
		Coordinate location = randomCoordinateList.get(0);
		String path =
				BASE_MAPPING + "/" +
				location.getLatitude() + "/" +
				location.getLongitude();

		mockMvc
				.perform(post(path))
				.andExpect(status().isOk());

		// When
		mockMvc
				.perform(post(path))

		// Then
				.andExpect(status().isOk())
				.andExpect(jsonPath("$.id").exists());

	}

	@Test
	public void unitTest_createGhost_notANumber() throws Exception {

		// Given
		Coordinate location = randomCoordinateList.get(0);
		String path =
				BASE_MAPPING + "/" +
				location.getLatitude() + "/" +
				"longitude";

		// When
		mockMvc
				.perform(post(path))

		// Then
				.andExpect(status().isBadRequest());

	}

	// 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");

	}

}

@@ -378,16 +378,44 @@
 tatus;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -535,24 +535,48 @@
 g4j.Logger;%0A
+import org.junit.After;%0A
 import org.j
@@ -1082,16 +1082,100 @@
 teImpl;%0A
+import com.pm.server.player.Ghost;%0Aimport com.pm.server.repository.GhostRepository;%0A
 public c
@@ -1233,16 +1233,71 @@
 late %7B%0A%0A
+%09@Autowired%0A%09private GhostRepository ghostRepository;%0A%0A
 %09private
@@ -1838,32 +1838,474 @@
 %09.build();%0A%0A%09%7D%0A%0A
+%09@After%0A%09public void cleanUp() %7B%0A%0A%09%09List%3CGhost%3E ghostList = ghostRepository.getAllPlayers();%0A%0A%09%09List%3CInteger%3E ghostIdList = new ArrayList%3CInteger%3E();%0A%09%09for(Ghost ghost : ghostList) %7B%0A%09%09%09ghostIdList.add(ghost.getId());%0A%09%09%7D%0A%0A%09%09for(Integer id : ghostIdList) %7B%0A%09%09%09try %7B%0A%09%09%09%09ghostRepository.deletePlayerById(id);%0A%09%09%09%7D%0A%09%09%09catch(Exception e) %7B%0A%09%09%09%09log.error(e.getMessage());%0A%09%09%09%09fail();%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09assert(ghostRepository.numOfPlayers() == 0);%0A%0A%09%7D%0A%0A
 %09@Test%0A%09public v
@@ -2342,33 +2342,32 @@
 ows Exception %7B%0A
-%0A
 %09%09// Given%0A%09%09Coo

Add cleanup operation for GhostControllerTest