@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); }
@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); }
/** * 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; }
@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); }