Small improvements to DB interface.

This commit is contained in:
akwizgran
2016-01-19 10:55:46 +00:00
parent 5355951466
commit 77054cbae7
9 changed files with 99 additions and 79 deletions

View File

@@ -9,6 +9,7 @@ import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.MessageExistsException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.NoSuchContactException;
import org.briarproject.api.db.NoSuchLocalAuthorException;
@@ -166,7 +167,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
oneOf(database).containsGroup(txn, groupId);
will(returnValue(true));
// getGroups()
oneOf(database).getGroups(txn);
oneOf(database).getGroups(txn, clientId);
will(returnValue(Collections.singletonList(group)));
// removeGroup()
oneOf(database).containsGroup(txn, groupId);
@@ -205,7 +206,7 @@ public class DatabaseComponentImplTest extends BriarTestCase {
db.getRemoteProperties(transportId));
db.addGroup(group); // First time - listeners called
db.addGroup(group); // Second time - not called
assertEquals(Collections.singletonList(group), db.getGroups());
assertEquals(Collections.singletonList(group), db.getGroups(clientId));
db.removeGroup(group);
db.removeContact(contactId);
db.removeLocalAuthor(localAuthorId);
@@ -226,14 +227,17 @@ public class DatabaseComponentImplTest extends BriarTestCase {
will(returnValue(txn));
oneOf(database).containsMessage(txn, messageId);
will(returnValue(true));
oneOf(database).containsGroup(txn, groupId);
will(returnValue(true));
oneOf(database).commitTransaction(txn);
oneOf(database).abortTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
db.addLocalMessage(message, clientId, metadata);
try {
db.addLocalMessage(message, clientId, metadata);
fail();
} catch (MessageExistsException expected) {
// Expected
}
context.assertIsSatisfied();
}
@@ -253,12 +257,17 @@ public class DatabaseComponentImplTest extends BriarTestCase {
will(returnValue(false));
oneOf(database).containsGroup(txn, groupId);
will(returnValue(false));
oneOf(database).commitTransaction(txn);
oneOf(database).abortTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown);
db.addLocalMessage(message, clientId, metadata);
try {
db.addLocalMessage(message, clientId, metadata);
fail();
} catch (NoSuchSubscriptionException expected) {
// Expected
}
context.assertIsSatisfied();
}

View File

@@ -58,6 +58,7 @@ public class H2DatabaseTest extends BriarTestCase {
private final File testDir = TestUtils.getTestDirectory();
private final Random random = new Random();
private final ClientId clientId;
private final GroupId groupId;
private final Group group;
private final Author author;
@@ -72,8 +73,8 @@ public class H2DatabaseTest extends BriarTestCase {
private final ContactId contactId;
public H2DatabaseTest() throws Exception {
clientId = new ClientId(TestUtils.getRandomId());
groupId = new GroupId(TestUtils.getRandomId());
ClientId clientId = new ClientId(TestUtils.getRandomId());
byte[] descriptor = new byte[MAX_GROUP_DESCRIPTOR_LENGTH];
group = new Group(groupId, clientId, descriptor);
AuthorId authorId = new AuthorId(TestUtils.getRandomId());
@@ -901,31 +902,34 @@ public class H2DatabaseTest extends BriarTestCase {
db.setGroups(txn, contactId1, Collections.singletonList(group), 1);
// The group should be available
assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getGroups(txn, clientId));
assertEquals(Collections.singletonList(group),
db.getAvailableGroups(txn));
db.getAvailableGroups(txn, clientId));
// Subscribe to the group - it should no longer be available
db.addGroup(txn, group);
assertEquals(Collections.singletonList(group), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getAvailableGroups(txn));
assertEquals(Collections.singletonList(group),
db.getGroups(txn, clientId));
assertEquals(Collections.emptyList(),
db.getAvailableGroups(txn, clientId));
// Unsubscribe from the group - it should be available again
db.removeGroup(txn, groupId);
assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getGroups(txn, clientId));
assertEquals(Collections.singletonList(group),
db.getAvailableGroups(txn));
db.getAvailableGroups(txn, clientId));
// The first contact unsubscribes - it should still be available
db.setGroups(txn, contactId, Collections.<Group>emptyList(), 2);
assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getGroups(txn, clientId));
assertEquals(Collections.singletonList(group),
db.getAvailableGroups(txn));
db.getAvailableGroups(txn, clientId));
// The second contact unsubscribes - it should no longer be available
db.setGroups(txn, contactId1, Collections.<Group>emptyList(), 2);
assertEquals(Collections.emptyList(), db.getGroups(txn));
assertEquals(Collections.emptyList(), db.getAvailableGroups(txn));
assertEquals(Collections.emptyList(), db.getGroups(txn, clientId));
assertEquals(Collections.emptyList(),
db.getAvailableGroups(txn, clientId));
db.commitTransaction(txn);
db.close();