diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java index ff3235fbc9e2e7b9fd6c24b0a7d41071118eaedf..b2e4842db87c3f0a6633f1196756be456b7671b9 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java @@ -47,6 +47,7 @@ import org.briarproject.bramble.api.transport.IncomingKeys; import org.briarproject.bramble.api.transport.OutgoingKeys; import org.briarproject.bramble.api.transport.TransportKeys; import org.briarproject.bramble.test.BrambleMockTestCase; +import org.briarproject.bramble.test.CaptureArgumentAction; import org.briarproject.bramble.test.TestUtils; import org.jmock.Expectations; import org.junit.Test; @@ -54,9 +55,12 @@ import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static org.briarproject.bramble.api.sync.Group.Visibility.INVISIBLE; import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED; import static org.briarproject.bramble.api.sync.Group.Visibility.VISIBLE; @@ -158,7 +162,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { ContactStatusChangedEvent.class))); // getContacts() oneOf(database).getContacts(txn); - will(returnValue(Collections.singletonList(contact))); + will(returnValue(singletonList(contact))); // addGroup() oneOf(database).containsGroup(txn, groupId); will(returnValue(false)); @@ -169,12 +173,12 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { will(returnValue(true)); // getGroups() oneOf(database).getGroups(txn, clientId); - will(returnValue(Collections.singletonList(group))); + will(returnValue(singletonList(group))); // removeGroup() oneOf(database).containsGroup(txn, groupId); will(returnValue(true)); oneOf(database).getGroupVisibility(txn, groupId); - will(returnValue(Collections.emptyMap())); + will(returnValue(emptyMap())); oneOf(database).removeGroup(txn, groupId); oneOf(eventBus).broadcast(with(any(GroupRemovedEvent.class))); oneOf(eventBus).broadcast(with(any( @@ -203,11 +207,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { db.addLocalAuthor(transaction, localAuthor); assertEquals(contactId, db.addContact(transaction, author, localAuthor.getId(), true, true)); - assertEquals(Collections.singletonList(contact), + assertEquals(singletonList(contact), db.getContacts(transaction)); db.addGroup(transaction, group); // First time - listeners called db.addGroup(transaction, group); // Second time - not called - assertEquals(Collections.singletonList(group), + assertEquals(singletonList(group), db.getGroups(transaction, clientId)); db.removeGroup(transaction, group); db.removeContact(transaction, contactId); @@ -389,7 +393,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { transaction = db.startTransaction(false); try { - Ack a = new Ack(Collections.singletonList(messageId)); + Ack a = new Ack(singletonList(messageId)); db.receiveAck(transaction, contactId, a); fail(); } catch (NoSuchContactException expected) { @@ -410,7 +414,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { transaction = db.startTransaction(false); try { - Offer o = new Offer(Collections.singletonList(messageId)); + Offer o = new Offer(singletonList(messageId)); db.receiveOffer(transaction, contactId, o); fail(); } catch (NoSuchContactException expected) { @@ -421,7 +425,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { transaction = db.startTransaction(false); try { - Request r = new Request(Collections.singletonList(messageId)); + Request r = new Request(singletonList(messageId)); db.receiveRequest(transaction, contactId, r); fail(); } catch (NoSuchContactException expected) { @@ -1016,7 +1020,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { Transaction transaction = db.startTransaction(false); try { - Ack a = new Ack(Collections.singletonList(messageId)); + Ack a = new Ack(singletonList(messageId)); db.receiveAck(transaction, contactId, a); db.commitTransaction(transaction); } finally { @@ -1186,7 +1190,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { Transaction transaction = db.startTransaction(false); try { - Request r = new Request(Collections.singletonList(messageId)); + Request r = new Request(singletonList(messageId)); db.receiveRequest(transaction, contactId, r); db.commitTransaction(transaction); } finally { @@ -1197,6 +1201,9 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { @Test public void testChangingVisibilityFromInvisibleToVisibleCallsListeners() throws Exception { + AtomicReference<GroupVisibilityUpdatedEvent> event = + new AtomicReference<>(); + context.checking(new Expectations() {{ oneOf(database).startTransaction(); will(returnValue(txn)); @@ -1205,11 +1212,13 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { oneOf(database).containsGroup(txn, groupId); will(returnValue(true)); oneOf(database).getGroupVisibility(txn, contactId, groupId); - will(returnValue(INVISIBLE)); // Not yet visible + will(returnValue(INVISIBLE)); oneOf(database).addGroupVisibility(txn, contactId, groupId, false); oneOf(database).commitTransaction(txn); oneOf(eventBus).broadcast(with(any( GroupVisibilityUpdatedEvent.class))); + will(new CaptureArgumentAction<>(event, + GroupVisibilityUpdatedEvent.class, 0)); }}); DatabaseComponent db = createDatabaseComponent(database, eventBus, shutdown); @@ -1221,11 +1230,18 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { } finally { db.endTransaction(transaction); } + + GroupVisibilityUpdatedEvent e = event.get(); + assertNotNull(e); + assertEquals(singletonList(contactId), e.getAffectedContacts()); } @Test public void testChangingVisibilityFromVisibleToInvisibleCallsListeners() throws Exception { + AtomicReference<GroupVisibilityUpdatedEvent> event = + new AtomicReference<>(); + context.checking(new Expectations() {{ oneOf(database).startTransaction(); will(returnValue(txn)); @@ -1234,11 +1250,13 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { oneOf(database).containsGroup(txn, groupId); will(returnValue(true)); oneOf(database).getGroupVisibility(txn, contactId, groupId); - will(returnValue(VISIBLE)); // Not yet visible + will(returnValue(VISIBLE)); oneOf(database).removeGroupVisibility(txn, contactId, groupId); oneOf(database).commitTransaction(txn); oneOf(eventBus).broadcast(with(any( GroupVisibilityUpdatedEvent.class))); + will(new CaptureArgumentAction<>(event, + GroupVisibilityUpdatedEvent.class, 0)); }}); DatabaseComponent db = createDatabaseComponent(database, eventBus, shutdown); @@ -1250,6 +1268,10 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { } finally { db.endTransaction(transaction); } + + GroupVisibilityUpdatedEvent e = event.get(); + assertNotNull(e); + assertEquals(singletonList(contactId), e.getAffectedContacts()); } @Test @@ -1281,8 +1303,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { @Test public void testTransportKeys() throws Exception { TransportKeys transportKeys = createTransportKeys(); - Map<ContactId, TransportKeys> keys = Collections.singletonMap( - contactId, transportKeys); + Map<ContactId, TransportKeys> keys = + singletonMap(contactId, transportKeys); context.checking(new Expectations() {{ // startTransaction() oneOf(database).startTransaction();