use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class ChatServletTest method testDoPost_StoresMessage.
@Test
public void testDoPost_StoresMessage() throws IOException, ServletException {
Mockito.when(mockRequest.getRequestURI()).thenReturn("/chat/test_conversation");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
Conversation fakeConversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
Mockito.when(mockConversationStore.getConversationWithTitle("test_conversation")).thenReturn(fakeConversation);
Mockito.when(mockRequest.getParameter("message")).thenReturn("Test message.");
chatServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
Mockito.verify(mockMessageStore).addMessage(messageArgumentCaptor.capture());
Assert.assertEquals("Test message.", messageArgumentCaptor.getValue().getContent());
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class ConversationServletTest method testDoPost_NewConversation.
@Test
public void testDoPost_NewConversation() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("conversationTitle")).thenReturn("test_conversation");
Mockito.when(mockSession.getAttribute("user")).thenReturn("test_username");
User fakeUser = new User(UUID.randomUUID(), "test_username", "test_password", Instant.now());
Mockito.when(mockUserStore.getUser("test_username")).thenReturn(fakeUser);
Mockito.when(mockConversationStore.isTitleTaken("test_conversation")).thenReturn(false);
conversationServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<Conversation> conversationArgumentCaptor = ArgumentCaptor.forClass(Conversation.class);
Mockito.verify(mockConversationStore).addConversation(conversationArgumentCaptor.capture());
Assert.assertEquals(conversationArgumentCaptor.getValue().getTitle(), "test_conversation");
Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class LoginServletTest method testDoPost_ExistingUser.
@Test
public void testDoPost_ExistingUser() throws IOException, ServletException {
User mockUser = Mockito.mock(User.class);
Mockito.when(mockUser.getName()).thenReturn("username_test");
Mockito.when(mockUser.getPassword()).thenReturn("valid_password");
Mockito.when(mockRequest.getParameter("username")).thenReturn("username_test");
Mockito.when(mockRequest.getParameter("password")).thenReturn("valid_password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("username_test")).thenReturn(true);
Mockito.when(mockUserStore.getUser("username_test")).thenReturn(mockUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockResponse).sendRedirect("/conversations");
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class LoginServletTest method testDoPost_WrongPassword.
@Test
public void testDoPost_WrongPassword() throws IOException, ServletException {
User mockUser = Mockito.mock(User.class);
Mockito.when(mockUser.getName()).thenReturn("username_test");
Mockito.when(mockUser.getPassword()).thenReturn("valid_password");
Mockito.when(mockRequest.getParameter("username")).thenReturn("username_test");
Mockito.when(mockRequest.getParameter("password")).thenReturn("invalid_password");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("username_test")).thenReturn(true);
Mockito.when(mockUserStore.getUser("username_test")).thenReturn(mockUser);
loginServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
loginServlet.doPost(mockRequest, mockResponse);
Mockito.verify(mockRequest).setAttribute("error", "Invalid password.");
Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
use of codeu.model.data.User in project CodeU-Spring-2018 by jwang281.
the class RegisterServletTest method testDoPost_NewUser.
@Test
public void testDoPost_NewUser() throws IOException, ServletException {
Mockito.when(mockRequest.getParameter("username")).thenReturn("test username");
UserStore mockUserStore = Mockito.mock(UserStore.class);
Mockito.when(mockUserStore.isUserRegistered("test username")).thenReturn(false);
registerServlet.setUserStore(mockUserStore);
HttpSession mockSession = Mockito.mock(HttpSession.class);
Mockito.when(mockRequest.getSession()).thenReturn(mockSession);
registerServlet.doPost(mockRequest, mockResponse);
ArgumentCaptor<User> userArgumentCaptor = ArgumentCaptor.forClass(User.class);
Mockito.verify(mockUserStore).addUser(userArgumentCaptor.capture());
Assert.assertEquals(userArgumentCaptor.getValue().getName(), "test username");
Mockito.verify(mockUserStore, Mockito.atLeastOnce()).addUser(Mockito.any(User.class));
Mockito.verify(mockResponse).sendRedirect("/login");
}
Aggregations