package test.mock;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/spring/applicationContext.xml")
@WebAppConfiguration
public class MockMVCTest {
	private MockMvc mockMvc;

	@Autowired
	private WebApplicationContext webApplicationContext;

	@Before
	public void setUp() throws Exception {
		mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
	}

	/**
	 * 查询每个省的项目数和每个省的建筑面积的单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void getmapdatalist() throws Exception {
		String contentAsString = mockMvc
				.perform(post("/api/service/data/getmapdatalist").param("organcode", "01").param("commid", "185301") // 添加参数
				// .accept(MediaType.parseMediaType("application/json;charset=UTF-8"))
				).andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString();
		System.out.println("contentAsString" + contentAsString);
		// .andExpect(status().isOk())
		// .andExpect(content().contentType("application/json;charset=UTF-8"))
		// .andExpect(content().json("{'foo':'bar'}"))
		;
	}

	/**
	 * 查询本日实收，昨日追缴新增，昨日车位收入新增，这个需要每次都查询。<br>
	 * 单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void queryBrssZrzjZrcwsrData() throws Exception {
		String contentAsString = mockMvc
				.perform(post("/api/service/data/queryBrssZrzjZrcwsrData").param("organcode", "01").param("commid",
						"185301"))
				.andExpect(status().isOk())// 加上断言判断状态是否OK
				.andDo(print())// 增加打印信息
				.andReturn().getResponse().getContentAsString();
		System.out.println("contentAsString:" + contentAsString);
	}

	/**
	 * 获取基础数据<br>
	 * 单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void getcommondatalist() throws Exception {
		String contentAsString = mockMvc.perform(post("/api/service/data/getcommondatalist")// url
				.param("organcode", "01").param("commid", "185301")// 参数
		).andExpect(status().isOk()).andDo(print())// 打印
				.andReturn().getResponse().getContentAsString();
		System.out.println("contentAsString:" + contentAsString);
	}

	/**
	 * 查询房屋状态<br>
	 * 单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void queryRoomState() throws Exception {
		String contentAsString = mockMvc.perform(post("/api/service/data/queryRoomState")// url
				.param("organcode", "01").param("commid", "185301")// 参数
		).andExpect(status().isOk()).andDo(print())// 打印
				.andReturn().getResponse().getContentAsString();
		System.out.println(contentAsString);
	}

	/**
	 * 查询本年累计追缴率,本年综合收费率,本年欠费追缴率<br>
	 * 单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void queryMultiRate() throws Exception {
		String contentAsString = mockMvc.perform(post("/api/service/data/queryMultiRate")// url
				.param("organcode", "01").param("commid", "185301")// params
		).andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString();
		System.out.println("contentAsString:" + contentAsString);
	}

	/**
	 * 查询接管面积一览表<br>
	 * 单元测试
	 * 
	 * @throws Exception
	 */
	@Test
	public void queryTakeOverArea() throws Exception {
		String contentAsString = mockMvc.perform(post("/api/service/data/queryTakeOverArea")// url
				.param("organcode", "01").param("commid", "185301")// params
		).andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString();
		System.out.println("queryTakeOverArea_result:" + contentAsString);

	}
	
	
}
