Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hhn.smartlogistics;
- import de.hhn.smartlogistics.user.User;
- import de.hhn.smartlogistics.user.UserDao;
- import de.hhn.smartlogistics.user.UserRole;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.annotation.Rollback;
- 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.MockMvc;
- import org.springframework.web.context.WebApplicationContext;
- import java.security.NoSuchAlgorithmException;
- import static org.hamcrest.CoreMatchers.is;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
- @RunWith(SpringJUnit4ClassRunner.class)
- @WebAppConfiguration
- @ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
- public class UserControllerTest {
- @SuppressWarnings("SpringJavaAutowiringInspection")
- @Autowired
- protected WebApplicationContext wac;
- private MockMvc mockMvc;
- private UserDao userDao;
- private User dummyUser;
- @Before
- public void setup() throws NoSuchAlgorithmException {
- this.mockMvc = webAppContextSetup(this.wac).build();
- userDao = (UserDao) wac.getBean("userDao");
- dummyUser = new User();
- dummyUser.setFirstName("John");
- dummyUser.setSurname("Smith");
- dummyUser.setRole(UserRole.CUSTOMER);
- dummyUser.setEmail("johnsmith@web.de");
- dummyUser.setPassword("johnsmith");
- dummyUser.encryptPassword();
- dummyUser.generateIdentification();
- userDao.create(dummyUser);
- // fetch user again to get the generated id
- dummyUser = userDao.findByEmail("johnsmith@web.de");
- }
- @After
- public void tearDown() {
- userDao.delete(dummyUser.getId());
- }
- @Test
- @Rollback
- public void should_createUser_when_valid() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("email", "john2smith@web.de")
- .param("role", "DRIVER")
- .param("password", "john2smith"))
- .andExpect(status().is3xxRedirection());
- assertThat(userDao.findByEmail("john2smith@web.de").getFirstName(), is("john2"));
- }
- @Test
- @Rollback
- public void should_not_createUser_when_emailMissing() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("role", "DRIVER")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_emailIsInvalid() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("email", "smithweb.de")
- .param("role", "DRIVER")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_passwordMissing() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("email", "john2smith@web.de")
- .param("role", "DRIVER"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_firstNameMissing() throws Exception {
- mockMvc.perform(post("/users")
- .param("surname", "smith")
- .param("email", "john2smith@web.de")
- .param("role", "DRIVER")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_surnameMissing() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("email", "john2smith@web.de")
- .param("role", "DRIVER")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_roleMissing() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("email", "john2smith@web.de")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_createUser_when_roleIsInvalid() throws Exception {
- mockMvc.perform(post("/users")
- .param("firstName", "john2")
- .param("surname", "smith")
- .param("email", "john2smith@web.de")
- .param("role", "PROGRAMMER")
- .param("password", "john2smith"))
- .andExpect(status().isOk())
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_updateUser_when_valid() throws Exception {
- mockMvc.perform(put("/users/{id}", dummyUser.getId())
- .param("firstName", "walter")
- .param("surname", "white")
- .param("email", "heisenberg@gmail.com")
- .param("role", UserRole.ADMIN.toString()))
- .andExpect(status().is3xxRedirection())
- .andExpect(model().hasNoErrors());
- User user = userDao.find(dummyUser.getId());
- assertThat(user.getFirstName(), is("walter"));
- assertThat(user.getSurname(), is("white"));
- assertThat(user.getEmail(), is("heisenberg@gmail.com"));
- assertThat(user.getRole(), is(UserRole.ADMIN));
- }
- @Test
- @Rollback
- public void should_not_updateUser_when_invalidFirstname() throws Exception {
- mockMvc.perform(put("/users/{id}", dummyUser.getId())
- .param("firstName", "")
- .param("surname", dummyUser.getSurname())
- .param("email", dummyUser.getEmail())
- .param("role", dummyUser.getRole().toString()))
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_updateUser_when_invalidSurname() throws Exception {
- mockMvc.perform(put("/users/{id}", dummyUser.getId())
- .param("firstName", dummyUser.getFirstName())
- .param("surname", "")
- .param("email", dummyUser.getEmail())
- .param("role", dummyUser.getRole().toString()))
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_updateUser_when_invalidEmail() throws Exception {
- mockMvc.perform(put("/users/{id}", dummyUser.getId())
- .param("firstName", dummyUser.getFirstName())
- .param("surname", dummyUser.getSurname())
- .param("email", "@.de")
- .param("role", dummyUser.getRole().toString()))
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_not_updateUser_when_invalidRole() throws Exception {
- mockMvc.perform(put("/users/{id}", dummyUser.getId())
- .param("firstName", dummyUser.getFirstName())
- .param("surname", dummyUser.getSurname())
- .param("email", dummyUser.getEmail())
- .param("role", "PROGRAMMER"))
- .andExpect(model().hasErrors());
- }
- @Test
- @Rollback
- public void should_deleteUser_when_userExists() throws Exception {
- mockMvc.perform(delete("/users/{id}", dummyUser.getId()))
- .andExpect(status().is3xxRedirection());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement