diff --git a/bramble-core/src/test/java/org/briarproject/bramble/mailbox/ContactMailboxClientTest.java b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/ContactMailboxClientTest.java new file mode 100644 index 000000000..a264b7650 --- /dev/null +++ b/bramble-core/src/test/java/org/briarproject/bramble/mailbox/ContactMailboxClientTest.java @@ -0,0 +1,136 @@ +package org.briarproject.bramble.mailbox; + +import org.briarproject.bramble.api.contact.ContactId; +import org.briarproject.bramble.api.mailbox.MailboxFolderId; +import org.briarproject.bramble.api.mailbox.MailboxProperties; +import org.briarproject.bramble.test.BrambleMockTestCase; +import org.jmock.Expectations; +import org.junit.Before; +import org.junit.Test; + +import static org.briarproject.bramble.api.mailbox.MailboxConstants.CLIENT_SUPPORTS; +import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull; +import static org.briarproject.bramble.test.TestUtils.getContactId; +import static org.briarproject.bramble.test.TestUtils.getMailboxProperties; + +public class ContactMailboxClientTest extends BrambleMockTestCase { + + private final MailboxWorkerFactory workerFactory = + context.mock(MailboxWorkerFactory.class); + private final ConnectivityChecker connectivityChecker = + context.mock(ConnectivityChecker.class); + private final TorReachabilityMonitor reachabilityMonitor = + context.mock(TorReachabilityMonitor.class); + private final MailboxWorker uploadWorker = + context.mock(MailboxWorker.class, "uploadWorker"); + private final MailboxWorker downloadWorker = + context.mock(MailboxWorker.class, "downloadWorker"); + + private final MailboxProperties properties = + getMailboxProperties(false, CLIENT_SUPPORTS); + private final MailboxFolderId inboxId = + requireNonNull(properties.getInboxId()); + private final MailboxFolderId outboxId = + requireNonNull(properties.getOutboxId()); + private final ContactId contactId = getContactId(); + + private ContactMailboxClient client; + + @Before + public void setUp() { + client = new ContactMailboxClient(workerFactory, connectivityChecker, + reachabilityMonitor); + } + + @Test + public void testStartAndDestroyWithNoContactsAssigned() { + client.start(); + client.destroy(); + } + + @Test + public void testAssignContactForUploadAndDestroyClient() { + client.start(); + + // When the contact is assigned, the worker should be created and + // started + expectCreateAndStartUploadWorker(); + client.assignContactForUpload(contactId, properties, outboxId); + + // When the client is destroyed, the worker should be destroyed + expectDestroyWorker(uploadWorker); + client.destroy(); + } + + @Test + public void testAssignAndDeassignContactForUpload() { + client.start(); + + // When the contact is assigned, the worker should be created and + // started + expectCreateAndStartUploadWorker(); + client.assignContactForUpload(contactId, properties, outboxId); + + // When the contact is deassigned, the worker should be destroyed + expectDestroyWorker(uploadWorker); + client.deassignContactForUpload(contactId); + context.assertIsSatisfied(); + + client.destroy(); + } + + @Test + public void assignContactForDownloadAndDestroyClient() { + client.start(); + + // When the contact is assigned, the worker should be created and + // started + expectCreateAndStartDownloadWorker(); + client.assignContactForDownload(contactId, properties, inboxId); + + // When the client is destroyed, the worker should be destroyed + expectDestroyWorker(downloadWorker); + client.destroy(); + } + + @Test + public void assignAndDeassignContactForDownload() { + client.start(); + + // When the contact is assigned, the worker should be created and + // started + expectCreateAndStartDownloadWorker(); + client.assignContactForDownload(contactId, properties, inboxId); + + // When the contact is deassigned, the worker should be destroyed + expectDestroyWorker(downloadWorker); + client.deassignContactForDownload(contactId); + context.assertIsSatisfied(); + + client.destroy(); + } + + private void expectCreateAndStartUploadWorker() { + context.checking(new Expectations() {{ + oneOf(workerFactory).createUploadWorker(connectivityChecker, + properties, outboxId, contactId); + will(returnValue(uploadWorker)); + oneOf(uploadWorker).start(); + }}); + } + + private void expectCreateAndStartDownloadWorker() { + context.checking(new Expectations() {{ + oneOf(workerFactory).createDownloadWorkerForContactMailbox( + connectivityChecker, reachabilityMonitor, properties); + will(returnValue(downloadWorker)); + oneOf(downloadWorker).start(); + }}); + } + + private void expectDestroyWorker(MailboxWorker worker) { + context.checking(new Expectations() {{ + oneOf(worker).destroy(); + }}); + } +}