Search in sources :

Example 11 with Message

use of codeu.model.data.Message in project CodeU-Spring-2018 by maksymko.

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", "test_passord", 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("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 12 with Message

use of codeu.model.data.Message in project CodeU-Spring-2018 by maksymko.

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");
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation) Test(org.junit.Test)

Example 13 with Message

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

the class DefaultDataStore method addRandomMessages.

private void addRandomMessages() {
    for (int i = 0; i < DEFAULT_MESSAGE_COUNT; i++) {
        Conversation conversation = getRandomElement(conversations);
        User author = getRandomElement(users);
        String content = getRandomMessageContent();
        Message message = new Message(UUID.randomUUID(), conversation.getId(), author.getId(), content, Instant.now());
        PersistentStorageAgent.getInstance().writeThrough(message);
        messages.add(message);
    }
}
Also used : User(codeu.model.data.User) Message(codeu.model.data.Message) Conversation(codeu.model.data.Conversation)

Example 14 with Message

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

the class MessageStore method getMessagesInConversationSorted.

/**
 * Access the current set of Messages within the given Conversation sorted with newest messages
 * first.
 */
public List<Message> getMessagesInConversationSorted(UUID conversationId) {
    List<Message> messagesInConversation = new ArrayList<>();
    for (Message message : messages) {
        if (message.getConversationId().equals(conversationId)) {
            messagesInConversation.add(message);
        }
    }
    messagesInConversation.sort(msgComparator);
    return messagesInConversation;
}
Also used : Message(codeu.model.data.Message) ArrayList(java.util.ArrayList)

Example 15 with Message

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

the class MessageStore method getMessagesByAuthorSorted.

/**
 * Retrieves a list of messages belonging to a user with a specified ID sorted with newest
 * messages first
 */
public List<Message> getMessagesByAuthorSorted(UUID authorId) {
    List<Message> messagesWrittenByAuthor = new ArrayList<>();
    for (Message message : messages) {
        if (message.getAuthorId().equals(authorId)) {
            messagesWrittenByAuthor.add(message);
        }
    }
    messagesWrittenByAuthor.sort(msgComparator);
    return messagesWrittenByAuthor;
}
Also used : Message(codeu.model.data.Message) ArrayList(java.util.ArrayList)

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