Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 KB | None | 0 0
  1. package de.hhn.smartlogistics;
  2.  
  3. import de.hhn.smartlogistics.user.User;
  4. import de.hhn.smartlogistics.user.UserDao;
  5. import de.hhn.smartlogistics.user.UserRole;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.test.annotation.Rollback;
  12. import org.springframework.test.context.ContextConfiguration;
  13. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  14. import org.springframework.test.context.web.WebAppConfiguration;
  15. import org.springframework.test.web.servlet.MockMvc;
  16. import org.springframework.web.context.WebApplicationContext;
  17.  
  18. import java.security.NoSuchAlgorithmException;
  19.  
  20. import static org.hamcrest.CoreMatchers.is;
  21. import static org.hamcrest.MatcherAssert.assertThat;
  22. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  23. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
  24. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  25. import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
  26.  
  27. @RunWith(SpringJUnit4ClassRunner.class)
  28. @WebAppConfiguration
  29. @ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
  30. public class UserControllerTest {
  31.     @SuppressWarnings("SpringJavaAutowiringInspection")
  32.     @Autowired
  33.     protected WebApplicationContext wac;
  34.     private MockMvc mockMvc;
  35.  
  36.     private UserDao userDao;
  37.     private User dummyUser;
  38.  
  39.     @Before
  40.     public void setup() throws NoSuchAlgorithmException {
  41.         this.mockMvc = webAppContextSetup(this.wac).build();
  42.  
  43.         userDao = (UserDao) wac.getBean("userDao");
  44.  
  45.         dummyUser = new User();
  46.         dummyUser.setFirstName("John");
  47.         dummyUser.setSurname("Smith");
  48.         dummyUser.setRole(UserRole.CUSTOMER);
  49.         dummyUser.setEmail("johnsmith@web.de");
  50.         dummyUser.setPassword("johnsmith");
  51.         dummyUser.encryptPassword();
  52.         dummyUser.generateIdentification();
  53.         userDao.create(dummyUser);
  54.  
  55.         // fetch user again to get the generated id
  56.         dummyUser = userDao.findByEmail("johnsmith@web.de");
  57.     }
  58.  
  59.     @After
  60.     public void tearDown() {
  61.         userDao.delete(dummyUser.getId());
  62.     }
  63.  
  64.     @Test
  65.     @Rollback
  66.     public void should_createUser_when_valid() throws Exception {
  67.         mockMvc.perform(post("/users")
  68.                 .param("firstName", "john2")
  69.                 .param("surname", "smith")
  70.                 .param("email", "john2smith@web.de")
  71.                 .param("role", "DRIVER")
  72.                 .param("password", "john2smith"))
  73.                 .andExpect(status().is3xxRedirection());
  74.  
  75.         assertThat(userDao.findByEmail("john2smith@web.de").getFirstName(), is("john2"));
  76.     }
  77.  
  78.     @Test
  79.     @Rollback
  80.     public void should_not_createUser_when_emailMissing() throws Exception {
  81.         mockMvc.perform(post("/users")
  82.                 .param("firstName", "john2")
  83.                 .param("surname", "smith")
  84.                 .param("role", "DRIVER")
  85.                 .param("password", "john2smith"))
  86.                 .andExpect(status().isOk())
  87.                 .andExpect(model().hasErrors());
  88.     }
  89.  
  90.     @Test
  91.     @Rollback
  92.     public void should_not_createUser_when_emailIsInvalid() throws Exception {
  93.         mockMvc.perform(post("/users")
  94.                 .param("firstName", "john2")
  95.                 .param("surname", "smith")
  96.                 .param("email", "smithweb.de")
  97.                 .param("role", "DRIVER")
  98.                 .param("password", "john2smith"))
  99.                 .andExpect(status().isOk())
  100.                 .andExpect(model().hasErrors());
  101.     }
  102.  
  103.     @Test
  104.     @Rollback
  105.     public void should_not_createUser_when_passwordMissing() throws Exception {
  106.         mockMvc.perform(post("/users")
  107.                 .param("firstName", "john2")
  108.                 .param("surname", "smith")
  109.                 .param("email", "john2smith@web.de")
  110.                 .param("role", "DRIVER"))
  111.                 .andExpect(status().isOk())
  112.                 .andExpect(model().hasErrors());
  113.     }
  114.  
  115.     @Test
  116.     @Rollback
  117.     public void should_not_createUser_when_firstNameMissing() throws Exception {
  118.         mockMvc.perform(post("/users")
  119.                 .param("surname", "smith")
  120.                 .param("email", "john2smith@web.de")
  121.                 .param("role", "DRIVER")
  122.                 .param("password", "john2smith"))
  123.                 .andExpect(status().isOk())
  124.                 .andExpect(model().hasErrors());
  125.     }
  126.  
  127.     @Test
  128.     @Rollback
  129.     public void should_not_createUser_when_surnameMissing() throws Exception {
  130.         mockMvc.perform(post("/users")
  131.                 .param("firstName", "john2")
  132.                 .param("email", "john2smith@web.de")
  133.                 .param("role", "DRIVER")
  134.                 .param("password", "john2smith"))
  135.                 .andExpect(status().isOk())
  136.                 .andExpect(model().hasErrors());
  137.     }
  138.  
  139.     @Test
  140.     @Rollback
  141.     public void should_not_createUser_when_roleMissing() throws Exception {
  142.         mockMvc.perform(post("/users")
  143.                 .param("firstName", "john2")
  144.                 .param("surname", "smith")
  145.                 .param("email", "john2smith@web.de")
  146.                 .param("password", "john2smith"))
  147.                 .andExpect(status().isOk())
  148.                 .andExpect(model().hasErrors());
  149.     }
  150.  
  151.     @Test
  152.     @Rollback
  153.     public void should_not_createUser_when_roleIsInvalid() throws Exception {
  154.         mockMvc.perform(post("/users")
  155.                 .param("firstName", "john2")
  156.                 .param("surname", "smith")
  157.                 .param("email", "john2smith@web.de")
  158.                 .param("role", "PROGRAMMER")
  159.                 .param("password", "john2smith"))
  160.                 .andExpect(status().isOk())
  161.                 .andExpect(model().hasErrors());
  162.     }
  163.  
  164.     @Test
  165.     @Rollback
  166.     public void should_updateUser_when_valid() throws Exception {
  167.         mockMvc.perform(put("/users/{id}", dummyUser.getId())
  168.                 .param("firstName", "walter")
  169.                 .param("surname", "white")
  170.                 .param("email", "heisenberg@gmail.com")
  171.                 .param("role", UserRole.ADMIN.toString()))
  172.                 .andExpect(status().is3xxRedirection())
  173.                 .andExpect(model().hasNoErrors());
  174.  
  175.         User user = userDao.find(dummyUser.getId());
  176.  
  177.         assertThat(user.getFirstName(), is("walter"));
  178.         assertThat(user.getSurname(), is("white"));
  179.         assertThat(user.getEmail(), is("heisenberg@gmail.com"));
  180.         assertThat(user.getRole(), is(UserRole.ADMIN));
  181.     }
  182.  
  183.     @Test
  184.     @Rollback
  185.     public void should_not_updateUser_when_invalidFirstname() throws Exception {
  186.         mockMvc.perform(put("/users/{id}", dummyUser.getId())
  187.                 .param("firstName", "")
  188.                 .param("surname", dummyUser.getSurname())
  189.                 .param("email", dummyUser.getEmail())
  190.                 .param("role", dummyUser.getRole().toString()))
  191.                 .andExpect(model().hasErrors());
  192.     }
  193.  
  194.     @Test
  195.     @Rollback
  196.     public void should_not_updateUser_when_invalidSurname() throws Exception {
  197.         mockMvc.perform(put("/users/{id}", dummyUser.getId())
  198.                 .param("firstName", dummyUser.getFirstName())
  199.                 .param("surname", "")
  200.                 .param("email", dummyUser.getEmail())
  201.                 .param("role", dummyUser.getRole().toString()))
  202.                 .andExpect(model().hasErrors());
  203.     }
  204.  
  205.     @Test
  206.     @Rollback
  207.     public void should_not_updateUser_when_invalidEmail() throws Exception {
  208.         mockMvc.perform(put("/users/{id}", dummyUser.getId())
  209.                 .param("firstName", dummyUser.getFirstName())
  210.                 .param("surname", dummyUser.getSurname())
  211.                 .param("email", "@.de")
  212.                 .param("role", dummyUser.getRole().toString()))
  213.                 .andExpect(model().hasErrors());
  214.     }
  215.  
  216.     @Test
  217.     @Rollback
  218.     public void should_not_updateUser_when_invalidRole() throws Exception {
  219.         mockMvc.perform(put("/users/{id}", dummyUser.getId())
  220.                 .param("firstName", dummyUser.getFirstName())
  221.                 .param("surname", dummyUser.getSurname())
  222.                 .param("email", dummyUser.getEmail())
  223.                 .param("role", "PROGRAMMER"))
  224.                 .andExpect(model().hasErrors());
  225.     }
  226.  
  227.     @Test
  228.     @Rollback
  229.     public void should_deleteUser_when_userExists() throws Exception {
  230.         mockMvc.perform(delete("/users/{id}", dummyUser.getId()))
  231.                 .andExpect(status().is3xxRedirection());
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement