package com.hantsylabs.example.spring.dao;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import com.hantsylabs.example.spring.config.JpaConfig;
import com.hantsylabs.example.spring.config.WebConfig;
import com.hantsylabs.example.spring.jpa.TaskRepository;
import com.hantsylabs.example.spring.model.Task;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { JpaConfig.class, WebConfig.class })
@WebAppConfiguration
public class TaskRepositoryImplTest {
	private static final Logger log = LoggerFactory
			.getLogger(TaskRepositoryImplTest.class);

	private MockMvc mvc;
	
	@Inject
	private WebApplicationContext ctx;

	@Autowired
	private TaskRepository taskRepository;

	@PersistenceContext
	EntityManager em;

	private Task newTask() {
		Task conf = new Task();
		conf.setName("test");
		conf.setDescription("Your first test task");
		return conf;
	}

	@BeforeClass
	public static void init() {
		log.debug("==================before class=========================");

	}

	@Before
	@Transactional
	public void beforeTestCase() {
		log.debug("==================before test case=========================");
		taskRepository.save(newTask());
		mvc = webAppContextSetup(ctx).build();
		
	}

	@After
	@Transactional
	public void afterTestCase() {
		log.debug("==================after test case=========================");
		taskRepository.deleteAll();
	}

	@Test
	public void retrieveTasks() throws Exception {

		mvc.perform(get("/api/tasks")).andExpect(status().isOk());

	}
	
	@Test
	public void retrieveSingleTask() throws Exception {
		mvc.perform(
				get("/api/tasks/{id}", 1L)
				.accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType("application/json;charset=UTF-8"))
		.andExpect(jsonPath("id").value(1));

	}

}

@@ -37,16 +37,106 @@
 g.dao;%0A%0A
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;%0A
 import s
@@ -2708,17 +2708,16 @@
 ption %7B%0A
-%0A
 %09%09mvc.pe
@@ -2769,17 +2769,16 @@
 sOk());%0A
-%0A
 %09%7D%0A%09%0A%09@T
@@ -3060,15 +3060,286 @@
 ue(1));%0A
+%09%7D%0A%09%0A%09@Test%0A%09public void removeTask() throws Exception %7B%0A%09%09mvc.perform(%0A%09%09%09%09delete(%22/api/tasks/%7Bid%7D%22, 1L))%0A%09%09.andExpect(status().isNoContent());%0A%09%09%0A%09%09mvc.perform(%0A%09%09%09%09get(%22/api/tasks/%7Bid%7D%22, 1L)%0A%09%09%09%09.accept(MediaType.APPLICATION_JSON))%0A%09%09.andExpect(status().isNotFound());
 %0A%09%7D%0A%0A%7D%0A

clean the test codes.