Search in sources :

Example 31 with Message

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

the class PersistentDataStore method loadMessages.

/**
 * Loads all Message objects from the Datastore service and returns them in a List.
 *
 * @throws codeu.model.store.persistence.PersistentDataStoreException if an error was detected
 *     during the load from the Datastore service
 */
public List<Message> loadMessages() throws PersistentDataStoreException {
    List<Message> messages = new ArrayList<>();
    // Retrieve all messages from the datastore.
    Query query = new Query("chat-messages");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            UUID conversationUuid = UUID.fromString((String) entity.getProperty("conv_uuid"));
            UUID authorUuid = UUID.fromString((String) entity.getProperty("author_uuid"));
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            String content = (String) entity.getProperty("content");
            Message message = new Message(uuid, conversationUuid, authorUuid, content, creationTime);
            messages.add(message);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return messages;
}
Also used : Message(codeu.model.data.Message) Instant(java.time.Instant) ArrayList(java.util.ArrayList) UUID(java.util.UUID)

Example 32 with Message

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

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

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

the class PersistentDataStore method loadMessages.

/**
 * Loads all Message objects from the Datastore service and returns them in a List.
 *
 * @throws PersistentDataStoreException if an error was detected during the load from the
 *     Datastore service
 */
public List<Message> loadMessages() throws PersistentDataStoreException {
    List<Message> messages = new ArrayList<>();
    // Retrieve all messages from the datastore.
    Query query = new Query("chat-messages");
    PreparedQuery results = datastore.prepare(query);
    for (Entity entity : results.asIterable()) {
        try {
            UUID uuid = UUID.fromString((String) entity.getProperty("uuid"));
            UUID conversationUuid = UUID.fromString((String) entity.getProperty("conv_uuid"));
            UUID authorUuid = UUID.fromString((String) entity.getProperty("author_uuid"));
            Instant creationTime = Instant.parse((String) entity.getProperty("creation_time"));
            String content = (String) entity.getProperty("content");
            Message message = new Message(uuid, conversationUuid, authorUuid, content, creationTime);
            messages.add(message);
        } catch (Exception e) {
            // database entity definition mismatches, or service mismatches.
            throw new PersistentDataStoreException(e);
        }
    }
    return messages;
}
Also used : Entity(com.google.appengine.api.datastore.Entity) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException) Message(codeu.model.data.Message) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Query(com.google.appengine.api.datastore.Query) Instant(java.time.Instant) ArrayList(java.util.ArrayList) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) UUID(java.util.UUID) PersistentDataStoreException(codeu.model.store.persistence.PersistentDataStoreException)

Example 34 with Message

use of codeu.model.data.Message 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);
}
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 35 with Message

use of codeu.model.data.Message 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);
    }
}
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