use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
the class PersistentStorageAgentTest method testWriteThroughConversation.
@Test
public void testWriteThroughConversation() {
Conversation conversation = new Conversation(UUID.randomUUID(), UUID.randomUUID(), "test_conversation", Instant.now());
persistentStorageAgent.writeThrough(conversation);
Mockito.verify(mockPersistentDataStore).writeThrough(conversation);
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
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.Conversation in project CodeU-Spring-2018 by maksymko.
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);
}
}
use of codeu.model.data.Conversation in project CodeU-Spring-2018 by maksymko.
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.Conversation in project CodeU-Spring-2018 by maksymko.
the class ChatServlet method doGet.
/**
* This function fires when a user navigates to the chat page. It gets the conversation title from
* the URL, finds the corresponding Conversation, and fetches the messages in that Conversation.
* It then forwards to chat.jsp for rendering.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String requestUrl = request.getRequestURI();
String conversationTitle = requestUrl.substring("/chat/".length());
Conversation conversation = conversationStore.getConversationWithTitle(conversationTitle);
if (conversation == null) {
// couldn't find conversation, redirect to conversation list
System.out.println("Conversation was null: " + conversationTitle);
response.sendRedirect("/conversations");
return;
}
UUID conversationId = conversation.getId();
List<Message> messages = messageStore.getMessagesInConversation(conversationId);
request.setAttribute("conversation", conversation);
request.setAttribute("messages", messages);
request.getRequestDispatcher("/WEB-INF/view/chat.jsp").forward(request, response);
}
Aggregations