Search in sources :

Example 21 with Message

use of codeu.model.data.Message in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServletTest method testDoPost_UserJoinedConversation.

@Test
public void testDoPost_UserJoinedConversation() 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", "password", "testbio", 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);
    fakeConversation.getConversationUsers().add(fakeUser);
    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");
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 22 with Message

use of codeu.model.data.Message in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServletTest method testDoPost_CleansHtmlContent.

@Test
public void testDoPost_CleansHtmlContent() 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", "password", "test biography", 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);
    fakeConversation.getConversationUsers().add(fakeUser);
    Mockito.when(mockRequest.getParameter("message")).thenReturn("Contains <b>html</b> and <script>JavaScript</script> content.");
    chatServlet.doPost(mockRequest, mockResponse);
    ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(mockMessageStore).addMessage(messageArgumentCaptor.capture());
    Assert.assertEquals("Contains html and  content.", messageArgumentCaptor.getValue().getContent());
    Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 23 with Message

use of codeu.model.data.Message in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServletTest method testDoGet.

@Test
public void testDoGet() throws IOException, ServletException {
    Mockito.when(mockRequest.getRequestURI()).thenReturn("/chat/test_conversation");
    UUID fakeConversationId = UUID.randomUUID();
    Conversation fakeConversation = new Conversation(fakeConversationId, UUID.randomUUID(), "test_conversation", Instant.now());
    Mockito.when(mockConversationStore.getConversationWithTitle("test_conversation")).thenReturn(fakeConversation);
    List<Message> fakeMessageList = new ArrayList<>();
    fakeMessageList.add(new Message(UUID.randomUUID(), fakeConversationId, UUID.randomUUID(), "test message", Instant.now()));
    Mockito.when(mockMessageStore.getMessagesInConversation(fakeConversationId)).thenReturn(fakeMessageList);
    chatServlet.doGet(mockRequest, mockResponse);
    Mockito.verify(mockRequest).setAttribute("conversation", fakeConversation);
    Mockito.verify(mockRequest).setAttribute("messages", fakeMessageList);
    Mockito.verify(mockRequestDispatcher).forward(mockRequest, mockResponse);
}
Also used : Message(codeu.model.data.Message) ArrayList(java.util.ArrayList) Conversation(codeu.model.data.Conversation) UUID(java.util.UUID) Test(org.junit.Test)

Example 24 with Message

use of codeu.model.data.Message in project codeu-2018-team12 by codeu-2018-team12.

the class ChatServletTest method testDoPost_StoresActivity.

@Test
public void testDoPost_StoresActivity() 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", "password", "testbiography", 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);
    fakeConversation.getConversationUsers().add(fakeUser);
    Mockito.when(mockRequest.getParameter("message")).thenReturn("Test message.");
    Activity activity = new Activity(UUID.randomUUID(), fakeUser.getId(), fakeConversation.getId(), Instant.now(), "sentMessage", "test_activity_message" + "Test message.");
    mockActivityStore.addActivity(activity);
    Mockito.when(mockActivityStore.getActivityWithId(activity.getId())).thenReturn(activity);
    chatServlet.doPost(mockRequest, mockResponse);
    ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
    Mockito.verify(mockMessageStore).addMessage(messageArgumentCaptor.capture());
    Mockito.verify(mockActivityStore).addActivity(activity);
    Assert.assertEquals("Test message.", messageArgumentCaptor.getValue().getContent());
    Assert.assertTrue(mockActivityStore.getActivityWithId(activity.getId()).getActivityMessage().contains("Test message."));
    Mockito.verify(mockResponse).sendRedirect("/chat/test_conversation");
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Activity(codeu.model.data.Activity) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 25 with Message

use of codeu.model.data.Message in project codeu-2018-team12 by codeu-2018-team12.

the class MessageStoreTest method testAddMessage.

@Test
public void testAddMessage() {
    UUID inputConversationId = UUID.randomUUID();
    Message inputMessage = new Message(UUID.randomUUID(), inputConversationId, UUID.randomUUID(), "test message", Instant.now());
    messageStore.addMessage(inputMessage);
    Message resultMessage = messageStore.getMessagesInConversation(inputConversationId).get(0);
    assertEquals(inputMessage, resultMessage);
    Mockito.verify(mockPersistentStorageAgent).writeThrough(inputMessage);
}
Also used : Message(codeu.model.data.Message) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Message (codeu.model.data.Message)39 Test (org.junit.Test)23 Conversation (codeu.model.data.Conversation)22 User (codeu.model.data.User)19 UUID (java.util.UUID)15 ArrayList (java.util.ArrayList)9 Instant (java.time.Instant)6 Activity (codeu.model.data.Activity)2 PersistentDataStoreException (codeu.model.store.persistence.PersistentDataStoreException)2 Entity (com.google.appengine.api.datastore.Entity)2 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)2 Query (com.google.appengine.api.datastore.Query)2 OutputSettings (org.jsoup.nodes.Document.OutputSettings)1