Search in sources :

Example 6 with Message

use of tech.aroma.thrift.Message in project aroma-data-operations by RedRoma.

the class MemoryInboxRepositoryTest method testSaveMessageForUserWithBadArguments.

@DontRepeat
public void testSaveMessageForUserWithBadArguments() throws Exception {
    assertThrows(() -> instance.saveMessageForUser(null, message)).isInstanceOf(InvalidArgumentException.class);
    assertThrows(() -> instance.saveMessageForUser(user, null)).isInstanceOf(InvalidArgumentException.class);
    assertThrows(() -> instance.saveMessageForUser(new User(), message)).isInstanceOf(InvalidArgumentException.class);
    assertThrows(() -> instance.saveMessageForUser(user, new Message())).isInstanceOf(InvalidArgumentException.class);
}
Also used : User(tech.aroma.thrift.User) Message(tech.aroma.thrift.Message) DontRepeat(tech.sirwellington.alchemy.test.junit.runners.DontRepeat)

Example 7 with Message

use of tech.aroma.thrift.Message in project aroma-data-operations by RedRoma.

the class MemoryMessageRepository method deleteMessage.

@Override
public void deleteMessage(String applicationId, String messageId) throws TException {
    checkThat(applicationId, messageId).throwing(InvalidArgumentException.class).are(nonEmptyString());
    if (!messages.containsKey(messageId)) {
        return;
    }
    Message message = messages.remove(messageId);
    String appId = message.applicationId;
    Set<String> appMessages = messagesByApplication.getOrDefault(appId, Sets.create());
    appMessages.remove(messageId);
    messagesByApplication.put(appId, appMessages);
}
Also used : InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) RequestAssertions.validMessage(tech.aroma.data.assertions.RequestAssertions.validMessage) Message(tech.aroma.thrift.Message) StringAssertions.nonEmptyString(tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString)

Example 8 with Message

use of tech.aroma.thrift.Message in project aroma-data-operations by RedRoma.

the class MemoryMessageRepository method getMessage.

@Override
public Message getMessage(String applicationId, String messageId) throws TException {
    checkThat(applicationId, messageId).throwing(InvalidArgumentException.class).are(nonEmptyString());
    checkThat(messageId).throwing(MessageDoesNotExistException.class).is(keyInMap(messages));
    Message message = messages.get(messageId);
    checkThat(message.applicationId).throwing(MessageDoesNotExistException.class).usingMessage("Message does not correspond to the given App ID").is(equalTo(applicationId));
    return message;
}
Also used : InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) RequestAssertions.validMessage(tech.aroma.data.assertions.RequestAssertions.validMessage) Message(tech.aroma.thrift.Message) MessageDoesNotExistException(tech.aroma.thrift.exceptions.MessageDoesNotExistException)

Example 9 with Message

use of tech.aroma.thrift.Message in project aroma-data-operations by RedRoma.

the class CassandraInboxRepository method getMessagesForUser.

@Override
public List<Message> getMessagesForUser(String userId) throws TException {
    checkThat(userId).throwing(InvalidArgumentException.class).is(validUserId());
    Statement query = createQueryToGetMessagesFor(userId);
    ResultSet results;
    try {
        results = cassandra.execute(query);
    } catch (Exception ex) {
        LOG.error("Failed to query for Messages in Inbox for User [{}]", userId, ex);
        throw new OperationFailedException("Could not fetch inbox: " + ex.getMessage());
    }
    checkThat(results).throwing(OperationFailedException.class).usingMessage("Cassandra returned null results").is(notNull());
    List<Message> messages = Lists.create();
    for (Row row : results) {
        Message message = messageMapper.apply(row);
        messages.add(message);
    }
    return messages;
}
Also used : InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) RequestAssertions.validMessage(tech.aroma.data.assertions.RequestAssertions.validMessage) Message(tech.aroma.thrift.Message) Statement(com.datastax.driver.core.Statement) ResultSet(com.datastax.driver.core.ResultSet) OperationFailedException(tech.aroma.thrift.exceptions.OperationFailedException) Row(com.datastax.driver.core.Row) TException(org.apache.thrift.TException) InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) OperationFailedException(tech.aroma.thrift.exceptions.OperationFailedException)

Example 10 with Message

use of tech.aroma.thrift.Message in project aroma-data-operations by RedRoma.

the class CassandraInboxRepository method saveMessageForUser.

@Override
public void saveMessageForUser(@Required User user, @Required Message message, @Required LengthOfTime lifetime) throws TException {
    checkThat(message).throwing(ex -> new InvalidArgumentException(ex.getMessage())).is(validMessage());
    checkThat(user).throwing(ex -> new InvalidArgumentException(ex.getMessage())).is(validUser());
    Statement insertStatement = createStatementToSaveMessage(message, user, lifetime);
    try {
        cassandra.execute(insertStatement);
    } catch (Exception ex) {
        LOG.error("Failed to save Message in Cassandra Inbox. User [{}] Message [{}]", user.userId, message, ex);
        throw new OperationFailedException("Could not save message in Inbox: " + ex.getMessage());
    }
}
Also used : TimeFunctions(tech.aroma.thrift.functions.TimeFunctions) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) LengthOfTime(tech.aroma.thrift.LengthOfTime) Row(com.datastax.driver.core.Row) LoggerFactory(org.slf4j.LoggerFactory) Required(tech.sirwellington.alchemy.annotations.arguments.Required) Function(java.util.function.Function) Arguments.checkThat(tech.sirwellington.alchemy.arguments.Arguments.checkThat) Inject(javax.inject.Inject) ResultSet(com.datastax.driver.core.ResultSet) RequestAssertions.validUserId(tech.aroma.data.assertions.RequestAssertions.validUserId) Session(com.datastax.driver.core.Session) Logger(org.slf4j.Logger) QueryBuilder.desc(com.datastax.driver.core.querybuilder.QueryBuilder.desc) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) RequestAssertions.validMessage(tech.aroma.data.assertions.RequestAssertions.validMessage) TException(org.apache.thrift.TException) UUID(java.util.UUID) InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) RequestAssertions.validUser(tech.aroma.data.assertions.RequestAssertions.validUser) Assertions.notNull(tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull) List(java.util.List) OperationFailedException(tech.aroma.thrift.exceptions.OperationFailedException) Inbox(tech.aroma.data.cassandra.Tables.Inbox) InboxRepository(tech.aroma.data.InboxRepository) User(tech.aroma.thrift.User) Statement(com.datastax.driver.core.Statement) Message(tech.aroma.thrift.Message) QueryBuilder.ttl(com.datastax.driver.core.querybuilder.QueryBuilder.ttl) Lists(sir.wellington.alchemy.collections.lists.Lists) RequestAssertions.validMessageId(tech.aroma.data.assertions.RequestAssertions.validMessageId) InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) Statement(com.datastax.driver.core.Statement) OperationFailedException(tech.aroma.thrift.exceptions.OperationFailedException) TException(org.apache.thrift.TException) InvalidArgumentException(tech.aroma.thrift.exceptions.InvalidArgumentException) OperationFailedException(tech.aroma.thrift.exceptions.OperationFailedException)

Aggregations

Message (tech.aroma.thrift.Message)22 Test (org.junit.Test)12 Statement (com.datastax.driver.core.Statement)8 Row (com.datastax.driver.core.Row)7 ResultSet (com.datastax.driver.core.ResultSet)6 RequestAssertions.validMessage (tech.aroma.data.assertions.RequestAssertions.validMessage)5 InvalidArgumentException (tech.aroma.thrift.exceptions.InvalidArgumentException)5 DontRepeat (tech.sirwellington.alchemy.test.junit.runners.DontRepeat)5 TException (org.apache.thrift.TException)3 User (tech.aroma.thrift.User)3 Session (com.datastax.driver.core.Session)2 List (java.util.List)2 Function (java.util.function.Function)2 LengthOfTime (tech.aroma.thrift.LengthOfTime)2 OperationFailedException (tech.aroma.thrift.exceptions.OperationFailedException)2 IntegrationTest (tech.sirwellington.alchemy.annotations.testing.IntegrationTest)2 QueryBuilder (com.datastax.driver.core.querybuilder.QueryBuilder)1 QueryBuilder.desc (com.datastax.driver.core.querybuilder.QueryBuilder.desc)1 QueryBuilder.eq (com.datastax.driver.core.querybuilder.QueryBuilder.eq)1 QueryBuilder.ttl (com.datastax.driver.core.querybuilder.QueryBuilder.ttl)1