JdbcMessageRepository extends AbstractMessageRepository implements MessageRepository { @Override public void save(Plaintext message) { safeSenderIfNecessary(message); try (Connection connection = config.getConnection()) { try { connection.setAutoCommit(false); save(connection, message); updateLabels(connection, message); connection.commit(); } catch (IOException | SQLException e) { connection.rollback(); throw e; } } catch (IOException | SQLException e) { throw new ApplicationException(e); } } JdbcMessageRepository(JdbcConfig config); @Override int countUnread(Label label); @Override void save(Plaintext message); @Override void remove(Plaintext message);  }
@Test public void ensureMessageCanBeRetrievedByInitialHash() { byte[] initialHash = new byte[64]; Plaintext message = repo.findMessages(contactA).get(0); message.setInitialHash(initialHash); repo.save(message); Plaintext other = repo.getMessage(initialHash); assertThat(other, is(message)); }
@Test public void ensureAckMessageCanBeUpdatedAndRetrieved() { byte[] initialHash = new byte[64]; Plaintext message = repo.findMessages(contactA).get(0); message.setInitialHash(initialHash); ObjectMessage ackMessage = message.getAckMessage(); repo.save(message); Plaintext other = repo.getMessage(initialHash); assertThat(other, is(message)); assertThat(other.getAckMessage(), is(ackMessage)); }
@Test public void testSave() throws Exception { Plaintext message = new Plaintext.Builder(MSG) .IV(new InventoryVector(cryptography().randomBytes(32))) .from(identity) .to(contactA) .message("Subject", "Message") .status(Plaintext.Status.DOING_PROOF_OF_WORK) .build(); repo.save(message); assertNotNull(message.getId()); message.addLabels(inbox); repo.save(message); List<Plaintext> messages = repo.findMessages(Plaintext.Status.DOING_PROOF_OF_WORK); assertEquals(1, messages.size()); assertNotNull(messages.get(0).getInventoryVector()); }
@Test public void ensureUnacknowledgedMessagesAreFoundForResend() throws Exception { Plaintext message = new Plaintext.Builder(MSG) .IV(new InventoryVector(cryptography().randomBytes(32))) .from(identity) .to(contactA) .message("Subject", "Message") .status(Plaintext.Status.SENT) .ttl(2) .build(); message.updateNextTry(); assertThat(message.getRetries(), is(1)); assertThat(message.getNextTry(), greaterThan(UnixTime.now())); assertThat(message.getNextTry(), lessThanOrEqualTo(UnixTime.now(+2))); repo.save(message); Thread.sleep(4100); List<Plaintext> messagesToResend = repo.findMessagesToResend(); assertThat(messagesToResend, hasSize(1)); message.updateNextTry(); assertThat(message.getRetries(), is(2)); assertThat(message.getNextTry(), greaterThan(UnixTime.now())); repo.save(message); messagesToResend = repo.findMessagesToResend(); assertThat(messagesToResend, empty()); }