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);
}
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");
}
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);
}
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());
}
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);
}
}
Aggregations