@Test public void testUpdate() throws Exception { // Create item Location storedItem = _postMockModel(); LOG.info("stored item: " + storedItem); // Update by id String resourcePathWithId = MvcControllerUtil.resourcePathWithId(storedItem); storedItem.changeName("a new name"); String putJsonStr = JackSONUtils.toJSON(storedItem); LOG.info("location putJsonStr: " + putJsonStr + " resourcePathWithId: " + resourcePathWithId); ResultActions resultActions = mockMvc .perform( put(resourcePathWithId) .content(putJsonStr) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location updatedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); Assert.assertEquals(storedItem, updatedItem); // debug the result ResultActionsUtil.log(resultActions); }
@Test public void testRemove() throws Exception { // Create item Location storedItem = _postMockModel(); LOG.info("stored item: " + storedItem); // Remove by id String resourcePathWithId = MvcControllerUtil.resourcePathWithId(storedItem); LOG.info("fetch with resourcePathWithId: " + resourcePathWithId); ResultActions resultActions = mockMvc.perform(delete(resourcePathWithId).accept(MediaType.APPLICATION_JSON)); resultActions .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location removedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); Assert.assertEquals(storedItem, removedItem); // debug the result // ResultActionsUtil.log(resultActions); }
/** * Tests all the LocationController<Location> methods using the {@link MockMvc} object. * * <p>Test as a "Standalone" meaning that the obtained MockMvc has "access" to the service methods * of the specified controller(s) and can perform a REST request simulating the {@link * DispatcherServlet} * * <p>Dependencies are loaded through: {@link AppContextCommonConfigRIAK} * * <ol> * <li>create - creates a {@link Customer } * <li>fetch - returns a {@link Customer } * <li>fetchAll - returns a {@link List<Customer> } * <li>update - updates a {@link Customer } * <li>remove - removes a {@link Customer } * <li>removeAll - removes all {@link Customer } * </ol> * * @author fredrik.karbing */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AppContextWebConfig.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class LocationControllerMockMvcStandaloneTest { private static Logger LOG = LoggerFactory.getLogger(LocationControllerMockMvcStandaloneTest.class); // Resource path private final String resourcePath = MvcControllerUtil.resourcePath(Location.class); @Autowired private LocationController locationController; private MockMvc mockMvc; @Before public void before() { mockMvc = MockMvcBuilders.standaloneSetup(locationController).build(); } // TODO: verify location! @Test public void testCreate() throws Exception { _postMockModel(); // ResultActionsUtil.log(resultActions); } @Test public void testCreateInvalidModel() throws Exception { Location location = new Location(null, null); String json = JackSONUtils.toJSON(location); LOG.info("location json: " + json); // -------- Request --------// MockHttpServletRequestBuilder requestBuilder = post(resourcePath).content(json); ResultActions resultActions = mockMvc.perform( requestBuilder .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)); // -------- Response --------// // Check: Status == 400 (bad request) resultActions.andExpect(status().isBadRequest()); ResultActionsUtil.log(resultActions); } @Test public void testUpdate() throws Exception { // Create item Location storedItem = _postMockModel(); LOG.info("stored item: " + storedItem); // Update by id String resourcePathWithId = MvcControllerUtil.resourcePathWithId(storedItem); storedItem.changeName("a new name"); String putJsonStr = JackSONUtils.toJSON(storedItem); LOG.info("location putJsonStr: " + putJsonStr + " resourcePathWithId: " + resourcePathWithId); ResultActions resultActions = mockMvc .perform( put(resourcePathWithId) .content(putJsonStr) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location updatedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); Assert.assertEquals(storedItem, updatedItem); // debug the result ResultActionsUtil.log(resultActions); } // TODO: Get location from Response and use that id! @Test public void testFetch() throws Exception { // Create item Location storedItem = _postMockModel(); LOG.info("stored item: " + storedItem); // Fetch by id String resourcePathWithId = MvcControllerUtil.resourcePathWithId(storedItem); LOG.info("fetch with resourcePathWithId: " + resourcePathWithId); ResultActions resultActions = mockMvc.perform(get(resourcePathWithId).accept(MediaType.APPLICATION_JSON)); resultActions .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location fetchedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); Assert.assertEquals(storedItem, fetchedItem); // debug the result // ResultActionsUtil.log(resultActions); } @Test public void testFetchAll() throws Exception { // Create 3 for (int i = 0; i < 3; i++) { String json = JackSONUtils.toJSON(new Location(null, "karbing")); LOG.info("location json: " + json); mockMvc.perform( post(resourcePath) .content(json) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)); } // Fetch MockHttpServletRequestBuilder requestBuilder = get(resourcePath); ResultActions resultActions = mockMvc.perform(requestBuilder.accept(MediaType.APPLICATION_JSON)); resultActions .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); // debug the result // ResultActionsUtil.log(resultActions); } @Test public void testRemove() throws Exception { // Create item Location storedItem = _postMockModel(); LOG.info("stored item: " + storedItem); // Remove by id String resourcePathWithId = MvcControllerUtil.resourcePathWithId(storedItem); LOG.info("fetch with resourcePathWithId: " + resourcePathWithId); ResultActions resultActions = mockMvc.perform(delete(resourcePathWithId).accept(MediaType.APPLICATION_JSON)); resultActions .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location removedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); Assert.assertEquals(storedItem, removedItem); // debug the result // ResultActionsUtil.log(resultActions); } @Test public void testRemoveAll() throws Exception { // Create 3 for (int i = 0; i < 3; i++) { String json = JackSONUtils.toJSON(new Location(null, "karbing")); LOG.info("location json: " + json); mockMvc .perform( delete(resourcePath) .content(json) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } // RemoveAll MockHttpServletRequestBuilder requestBuilder = delete(resourcePath).accept(MediaType.APPLICATION_JSON); ResultActions resultActions = mockMvc.perform(requestBuilder); resultActions .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); // debug the result // ResultActionsUtil.log(resultActions); } /** * Helper method to create (persist) a model. * * @return * @throws Exception */ private Location _postMockModel() throws Exception { String jsonStr = JackSONUtils.toJSON(LocationMock.newIntance()); LOG.info("location json: " + jsonStr); // Post request ResultActions resultActions = mockMvc.perform( post(resourcePath) .content(jsonStr) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)); // Validate response resultActions .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); Location storedItem = ResultActionsUtil.contentAsJson(resultActions, Location.class); LOG.info("storedItem: " + storedItem); return storedItem; } }