Search in sources :

Example 1 with Message

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

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 2 with Message

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

Example 3 with Message

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

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)

Example 4 with Message

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

the class PersistentDataStoreTest method testSaveAndLoadMessages.

@Test
public void testSaveAndLoadMessages() throws PersistentDataStoreException {
    UUID idOne = UUID.randomUUID();
    UUID conversationOne = UUID.randomUUID();
    UUID authorOne = UUID.randomUUID();
    String contentOne = "test content one";
    Instant creationOne = Instant.ofEpochMilli(1000);
    Message inputMessageOne = new Message(idOne, conversationOne, authorOne, contentOne, creationOne);
    UUID idTwo = UUID.randomUUID();
    UUID conversationTwo = UUID.randomUUID();
    UUID authorTwo = UUID.randomUUID();
    String contentTwo = "test content one";
    Instant creationTwo = Instant.ofEpochMilli(2000);
    Message inputMessageTwo = new Message(idTwo, conversationTwo, authorTwo, contentTwo, creationTwo);
    // save
    persistentDataStore.writeThrough(inputMessageOne);
    persistentDataStore.writeThrough(inputMessageTwo);
    // load
    List<Message> resultMessages = persistentDataStore.loadMessages();
    // confirm that what we saved matches what we loaded
    Message resultMessageOne = resultMessages.get(0);
    Assert.assertEquals(idOne, resultMessageOne.getId());
    Assert.assertEquals(conversationOne, resultMessageOne.getConversationId());
    Assert.assertEquals(authorOne, resultMessageOne.getAuthorId());
    Assert.assertEquals(contentOne, resultMessageOne.getContent());
    Assert.assertEquals(creationOne, resultMessageOne.getCreationTime());
    Message resultMessageTwo = resultMessages.get(1);
    Assert.assertEquals(idTwo, resultMessageTwo.getId());
    Assert.assertEquals(conversationTwo, resultMessageTwo.getConversationId());
    Assert.assertEquals(authorTwo, resultMessageTwo.getAuthorId());
    Assert.assertEquals(contentTwo, resultMessageTwo.getContent());
    Assert.assertEquals(creationTwo, resultMessageTwo.getCreationTime());
}
Also used : Message(codeu.model.data.Message) Instant(java.time.Instant) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with Message

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

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)

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