Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jc.pico.controller.admin;
- import com.jc.pico.bean.SvcTable;
- import com.jc.pico.bean.SvcTableStatus;
- import com.jc.pico.configuration.MainConfiguration;
- import com.jc.pico.configuration.MybatisConfiguration;
- import com.jc.pico.controller.AbstractTableControllerTest;
- import com.jc.pico.service.TableService;
- import com.jc.pico.service.TableStatusService;
- import com.jc.pico.service.app.models.CustomSvcStore;
- import org.hamcrest.core.IsInstanceOf;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.security.test.context.support.WithMockUser;
- 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.MvcResult;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Collections;
- import java.util.Date;
- import java.util.List;
- import static org.hamcrest.Matchers.hasItem;
- import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
- import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
- import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(classes = {MybatisConfiguration.class, MainConfiguration.class})
- @WebAppConfiguration
- @Transactional
- public class AdminTableControllerTest extends AbstractTableControllerTest {
- @Autowired
- protected TableService tableService;
- @Autowired
- protected TableStatusService tableStatusService;
- @Override
- protected String getControllerName() {
- return "adminTableController";
- }
- private Long storeId;
- @Before
- public void setUp() {
- this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
- CustomSvcStore tableStore = createStore();
- this.storeId = tableStore.getId();
- this.section = createSection(storeId, tableStore.getBrandId());
- this.tables = createTables(section);
- }
- @Override
- @Test
- public void redirectUnauthorized() throws Exception {
- mockMvc.perform(get("/admin/model/table/" + storeId))
- .andDo(print())
- .andExpect(status().is(302))
- .andExpect(redirectedUrlPattern(redirectUrlPattern))
- .andReturn();
- }
- @Override
- @Test
- @WithMockUser(username = ASPECT_USERNAME, password = ASPECT_PASSWORD)
- public void accessDenied() throws Exception {
- mockMvc.perform(get(String.format("/admin/model/table/image/%d/%d/%d",
- storeId, tables.get(0).getId(), tables.get(0).getImgId())))
- .andDo(print())
- .andExpect(status().isForbidden());
- }
- @Test
- public void returnNullFalseSuccessBodyJsonAdminModelTable0() throws Exception {
- MvcResult result = mockMvc.perform(get("/admin/model/table/0")
- .session(createSessionAdminMock()))
- .andDo(print())
- .andExpect(jsonPath("$.body").doesNotExist())
- .andExpect(jsonPath("$.success").value(false))
- .andReturn();
- Assert.assertEquals("application/json", result.getResponse().getContentType());
- }
- @Test
- public void requestTablesList() throws Exception {
- MvcResult mvcResult = this.mockMvc.perform(get("/admin/model/table/" + storeId)
- .session(createSessionAdminMock()))
- .andDo(print())
- .andExpect(status().isOk())
- .andReturn();
- Assert.assertEquals("application/json",
- mvcResult.getResponse().getContentType());
- }
- @Test
- public void requestAddTables() throws Exception {
- MvcResult mvcResult = this.mockMvc.perform(post("/admin/model/table/" + storeId)
- .param("amount", "10")
- .session(createSessionAdminMock()))
- .andDo(print())
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.body[*]", hasSize(10)))
- .andExpect(jsonPath("$.body[9].id").value(new IsInstanceOf(Integer.class)))
- .andExpect(jsonPath("$.body[9].brandId").value(new IsInstanceOf(Integer.class)))
- .andReturn();
- Assert.assertEquals("application/json",
- mvcResult.getResponse().getContentType());
- }
- @Test
- public void requestAddTable() throws Exception {
- MvcResult mvcResult = this.mockMvc.perform(post("/admin/model/table/" + storeId)
- .session(createSessionAdminMock()))
- .andDo(print())
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.body[0].id").value(new IsInstanceOf(Integer.class)))
- .andExpect(jsonPath("$.body[0].brandId").value(new IsInstanceOf(Integer.class)))
- .andReturn();
- Assert.assertEquals("application/json",
- mvcResult.getResponse().getContentType());
- }
- @Test
- public void requestUpdateTable() throws Exception {
- SvcTable table = tables.get(0);
- String tableName = String.format("table_test_1_%d", System.currentTimeMillis());
- table.setName(tableName);
- table.setDeposit(666.66);
- table.setMapX((short) 123);
- table.setMapY((short) 321);
- MvcResult mvcResult = this.mockMvc.perform(put("/admin/model/table/" + storeId)
- .session(createSessionAdminMock())
- .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(table))))
- .andDo(print())
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.body[*].name", hasItem(tableName)))
- .andExpect(jsonPath("$.body[*].deposit", hasItem(666.66)))
- .andExpect(jsonPath("$.body[*].mapX", hasItem(123)))
- .andExpect(jsonPath("$.body[*].mapY", hasItem(321)))
- .andReturn();
- Assert.assertEquals("application/json", mvcResult.getResponse().getContentType());
- }
- @Test
- public void requestUpdateTable_WithOrders() throws Exception {
- List<SvcTable> tables = tableService.create(6L, 1).getObject();
- Assert.assertNotNull(tables);
- Assert.assertTrue(tables.size() == 1);
- SvcTable table = tables.get(0);
- SvcTableStatus svcTableStatus = tableStatusService.createTableStatus(createTableStatus(table.getId())).getObject();
- Assert.assertNotNull(svcTableStatus);
- Assert.assertNotNull(svcTableStatus.getId());
- table.setEnabled(false);
- MvcResult mvcResult = this.mockMvc.perform(put("/admin/model/table/" + storeId)
- .session(createSessionAdminMock())
- .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(table))))
- .andDo(print())
- .andExpect(status().isOk())
- .andExpect(jsonPath("$.success").value(Boolean.FALSE))
- .andReturn();
- Assert.assertEquals("application/json",
- mvcResult.getResponse().getContentType());
- }
- @Test
- public void requestDeleteTable() throws Exception {
- Long tableId = 958L;
- MvcResult mvcResult = this.mockMvc.perform(delete(String.format("/admin/model/table/%d", storeId))
- .session(createSessionAdminMock())
- .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(tableId))))
- .andDo(print())
- .andExpect(status().isOk())
- .andReturn();
- Assert.assertEquals("application/json",
- mvcResult.getResponse().getContentType());
- }
- private SvcTableStatus createTableStatus(Long tableId) {
- Long millis = System.currentTimeMillis();
- SvcTableStatus svcTableStatus = new SvcTableStatus();
- svcTableStatus.setUserId(1L);
- svcTableStatus.setTableId(tableId);
- svcTableStatus.setOrderBegin(new Date(millis + HOUR));
- svcTableStatus.setOrderEnd(new Date(millis + 2 * HOUR));
- return svcTableStatus;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement