Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.76 KB | None | 0 0
  1. package com.jc.pico.controller.admin;
  2.  
  3. import com.jc.pico.bean.SvcTable;
  4. import com.jc.pico.bean.SvcTableStatus;
  5. import com.jc.pico.configuration.MainConfiguration;
  6. import com.jc.pico.configuration.MybatisConfiguration;
  7. import com.jc.pico.controller.AbstractTableControllerTest;
  8. import com.jc.pico.service.TableService;
  9. import com.jc.pico.service.TableStatusService;
  10. import com.jc.pico.service.app.models.CustomSvcStore;
  11. import org.hamcrest.core.IsInstanceOf;
  12. import org.junit.Assert;
  13. import org.junit.Before;
  14. import org.junit.Test;
  15. import org.junit.runner.RunWith;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.security.test.context.support.WithMockUser;
  19. import org.springframework.test.context.ContextConfiguration;
  20. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  21. import org.springframework.test.context.web.WebAppConfiguration;
  22. import org.springframework.test.web.servlet.MvcResult;
  23. import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  24. import org.springframework.transaction.annotation.Transactional;
  25.  
  26. import java.util.Collections;
  27. import java.util.Date;
  28. import java.util.List;
  29.  
  30. import static org.hamcrest.Matchers.hasItem;
  31. import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
  32. import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
  33. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  34. import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  35. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  36. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  37.  
  38. @RunWith(SpringJUnit4ClassRunner.class)
  39. @ContextConfiguration(classes = {MybatisConfiguration.class, MainConfiguration.class})
  40. @WebAppConfiguration
  41. @Transactional
  42. public class AdminTableControllerTest extends AbstractTableControllerTest {
  43.  
  44.     @Autowired
  45.     protected TableService tableService;
  46.  
  47.     @Autowired
  48.     protected TableStatusService tableStatusService;
  49.  
  50.     @Override
  51.     protected String getControllerName() {
  52.         return "adminTableController";
  53.     }
  54.  
  55.     private Long storeId;
  56.  
  57.     @Before
  58.     public void setUp() {
  59.         this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
  60.  
  61.         CustomSvcStore tableStore = createStore();
  62.         this.storeId = tableStore.getId();
  63.         this.section = createSection(storeId, tableStore.getBrandId());
  64.         this.tables = createTables(section);
  65.     }
  66.  
  67.     @Override
  68.     @Test
  69.     public void redirectUnauthorized() throws Exception {
  70.         mockMvc.perform(get("/admin/model/table/" + storeId))
  71.                 .andDo(print())
  72.                 .andExpect(status().is(302))
  73.                 .andExpect(redirectedUrlPattern(redirectUrlPattern))
  74.                 .andReturn();
  75.     }
  76.  
  77.     @Override
  78.     @Test
  79.     @WithMockUser(username = ASPECT_USERNAME, password = ASPECT_PASSWORD)
  80.     public void accessDenied() throws Exception {
  81.         mockMvc.perform(get(String.format("/admin/model/table/image/%d/%d/%d",
  82.                 storeId, tables.get(0).getId(), tables.get(0).getImgId())))
  83.                 .andDo(print())
  84.                 .andExpect(status().isForbidden());
  85.     }
  86.  
  87.     @Test
  88.     public void returnNullFalseSuccessBodyJsonAdminModelTable0() throws Exception {
  89.         MvcResult result = mockMvc.perform(get("/admin/model/table/0")
  90.                 .session(createSessionAdminMock()))
  91.                 .andDo(print())
  92.                 .andExpect(jsonPath("$.body").doesNotExist())
  93.                 .andExpect(jsonPath("$.success").value(false))
  94.                 .andReturn();
  95.         Assert.assertEquals("application/json", result.getResponse().getContentType());
  96.     }
  97.  
  98.     @Test
  99.     public void requestTablesList() throws Exception {
  100.         MvcResult mvcResult = this.mockMvc.perform(get("/admin/model/table/" + storeId)
  101.                 .session(createSessionAdminMock()))
  102.                 .andDo(print())
  103.                 .andExpect(status().isOk())
  104.                 .andReturn();
  105.         Assert.assertEquals("application/json",
  106.                 mvcResult.getResponse().getContentType());
  107.     }
  108.  
  109.     @Test
  110.     public void requestAddTables() throws Exception {
  111.         MvcResult mvcResult = this.mockMvc.perform(post("/admin/model/table/" + storeId)
  112.                 .param("amount", "10")
  113.                 .session(createSessionAdminMock()))
  114.                 .andDo(print())
  115.                 .andExpect(status().isOk())
  116.                 .andExpect(jsonPath("$.body[*]", hasSize(10)))
  117.                 .andExpect(jsonPath("$.body[9].id").value(new IsInstanceOf(Integer.class)))
  118.                 .andExpect(jsonPath("$.body[9].brandId").value(new IsInstanceOf(Integer.class)))
  119.                 .andReturn();
  120.         Assert.assertEquals("application/json",
  121.                 mvcResult.getResponse().getContentType());
  122.     }
  123.  
  124.     @Test
  125.     public void requestAddTable() throws Exception {
  126.         MvcResult mvcResult = this.mockMvc.perform(post("/admin/model/table/" + storeId)
  127.                 .session(createSessionAdminMock()))
  128.                 .andDo(print())
  129.                 .andExpect(status().isOk())
  130.                 .andExpect(jsonPath("$.body[0].id").value(new IsInstanceOf(Integer.class)))
  131.                 .andExpect(jsonPath("$.body[0].brandId").value(new IsInstanceOf(Integer.class)))
  132.                 .andReturn();
  133.         Assert.assertEquals("application/json",
  134.                 mvcResult.getResponse().getContentType());
  135.     }
  136.  
  137.     @Test
  138.     public void requestUpdateTable() throws Exception {
  139.         SvcTable table = tables.get(0);
  140.         String tableName = String.format("table_test_1_%d", System.currentTimeMillis());
  141.         table.setName(tableName);
  142.         table.setDeposit(666.66);
  143.         table.setMapX((short) 123);
  144.         table.setMapY((short) 321);
  145.         MvcResult mvcResult = this.mockMvc.perform(put("/admin/model/table/" + storeId)
  146.                 .session(createSessionAdminMock())
  147.                 .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(table))))
  148.                 .andDo(print())
  149.                 .andExpect(status().isOk())
  150.                 .andExpect(jsonPath("$.body[*].name", hasItem(tableName)))
  151.                 .andExpect(jsonPath("$.body[*].deposit", hasItem(666.66)))
  152.                 .andExpect(jsonPath("$.body[*].mapX", hasItem(123)))
  153.                 .andExpect(jsonPath("$.body[*].mapY", hasItem(321)))
  154.                 .andReturn();
  155.         Assert.assertEquals("application/json", mvcResult.getResponse().getContentType());
  156.     }
  157.  
  158.     @Test
  159.     public void requestUpdateTable_WithOrders() throws Exception {
  160.         List<SvcTable> tables = tableService.create(6L, 1).getObject();
  161.         Assert.assertNotNull(tables);
  162.         Assert.assertTrue(tables.size() == 1);
  163.         SvcTable table = tables.get(0);
  164.         SvcTableStatus svcTableStatus = tableStatusService.createTableStatus(createTableStatus(table.getId())).getObject();
  165.         Assert.assertNotNull(svcTableStatus);
  166.         Assert.assertNotNull(svcTableStatus.getId());
  167.         table.setEnabled(false);
  168.         MvcResult mvcResult = this.mockMvc.perform(put("/admin/model/table/" + storeId)
  169.                 .session(createSessionAdminMock())
  170.                 .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(table))))
  171.                 .andDo(print())
  172.                 .andExpect(status().isOk())
  173.                 .andExpect(jsonPath("$.success").value(Boolean.FALSE))
  174.                 .andReturn();
  175.         Assert.assertEquals("application/json",
  176.                 mvcResult.getResponse().getContentType());
  177.     }
  178.  
  179.     @Test
  180.     public void requestDeleteTable() throws Exception {
  181.         Long tableId = 958L;
  182.         MvcResult mvcResult = this.mockMvc.perform(delete(String.format("/admin/model/table/%d", storeId))
  183.                 .session(createSessionAdminMock())
  184.                 .contentType(MediaType.APPLICATION_JSON).content(asJson(Collections.singletonList(tableId))))
  185.                 .andDo(print())
  186.                 .andExpect(status().isOk())
  187.                 .andReturn();
  188.         Assert.assertEquals("application/json",
  189.                 mvcResult.getResponse().getContentType());
  190.     }
  191.  
  192.     private SvcTableStatus createTableStatus(Long tableId) {
  193.         Long millis = System.currentTimeMillis();
  194.         SvcTableStatus svcTableStatus = new SvcTableStatus();
  195.         svcTableStatus.setUserId(1L);
  196.         svcTableStatus.setTableId(tableId);
  197.         svcTableStatus.setOrderBegin(new Date(millis + HOUR));
  198.         svcTableStatus.setOrderEnd(new Date(millis + 2 * HOUR));
  199.         return svcTableStatus;
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement