package <%=@group_id%>.integration.<%=@model_name.downcase%>; /* * Copyright (c) 2015 <%=@user_name%>. * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static util.TestUtil.toJson; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNotNull; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.github.springtestdbunit.DbUnitTestExecutionListener; import com.github.springtestdbunit.annotation.DatabaseSetup; import <%=@group_id%>.model.<%=@model_name%>; import <%=@group_id%>.integration.<%=@model_name.downcase%>.<%=@model_name%>IntegrationTestConfig; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.MediaTypes; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.support.DirtiesContextTestExecutionListener; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; 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 util.TestUtil; import static org.hamcrest.Matchers.is; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static util.TestUtil.toJson; /** * A integration test for <%=@model_name%>s * @author <%=@user_name%> */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {<%=@model_name%>IntegrationTestConfig.class}) @WebAppConfiguration //TODO add custom sample-data in <%=@model_name.downcase%>SampleData.xml @DatabaseSetup("/sampledata/<%=@model_name.downcase%>SampleData.xml") @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class }) public class <%=@model_name%>IntegrationTest { private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; /** * sets up the test. */ @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testGet<%=@model_name%>s() throws Exception { mockMvc.perform(get("/<%=@model_name.downcase%>s")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON)); } @Test public void itShouldContainLinksToModulesOfficersAssistantsSelfAndRootModule() throws Exception { mockMvc.perform(get("/<%=@model_name.downcase%>s/1")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON)) .andExpect( jsonPath("$._links.self.href", is("http://localhost/<%=@model_name.downcase%>s/1"))); } @Test public void testCreate<%=@model_name%>() throws Exception { mockMvc.perform(post("/<%=@model_name.downcase%>s") .contentType(TestUtil.APPLICATION_JSON_UTF8) .content(toJson(new <%=@model_name%>()))) .andExpect(status().isCreated()) .andExpect(redirectedUrl("http://localhost/<%=@model_name.downcase%>s/11")); } }