mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
[bramble] Add transactionless method for retrieving AuthorInfo to ContactManager
This commit is contained in:
@@ -114,6 +114,11 @@ public interface ContactManager {
|
|||||||
boolean contactExists(AuthorId remoteAuthorId, AuthorId localAuthorId)
|
boolean contactExists(AuthorId remoteAuthorId, AuthorId localAuthorId)
|
||||||
throws DbException;
|
throws DbException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@link AuthorInfo} for the given author.
|
||||||
|
*/
|
||||||
|
AuthorInfo getAuthorInfo(AuthorId a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link AuthorInfo} for the given author.
|
* Returns the {@link AuthorInfo} for the given author.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -201,6 +201,11 @@ class ContactManagerImpl implements ContactManager {
|
|||||||
db.removeContact(txn, c);
|
db.removeContact(txn, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthorInfo getAuthorInfo(AuthorId a) throws DbException {
|
||||||
|
return db.transactionWithResult(true, txn -> getAuthorInfo(txn, a));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthorInfo getAuthorInfo(Transaction txn, AuthorId authorId)
|
public AuthorInfo getAuthorInfo(Transaction txn, AuthorId authorId)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|||||||
@@ -224,7 +224,28 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAuthorStatus() throws DbException {
|
public void testGetAuthorStatus() throws Exception {
|
||||||
|
Transaction txn = new Transaction(null, true);
|
||||||
|
Collection<Contact> contacts = singletonList(
|
||||||
|
new Contact(new ContactId(1), remote, localAuthor.getId(),
|
||||||
|
alias, false, true));
|
||||||
|
|
||||||
|
context.checking(new DbExpectations() {{
|
||||||
|
oneOf(db).transactionWithResult(with(equal(true)),
|
||||||
|
withDbCallable(txn));
|
||||||
|
oneOf(identityManager).getLocalAuthor(txn);
|
||||||
|
will(returnValue(localAuthor));
|
||||||
|
oneOf(db).getContactsByAuthorId(txn, remote.getId());
|
||||||
|
will(returnValue(contacts));
|
||||||
|
}});
|
||||||
|
AuthorInfo authorInfo =
|
||||||
|
contactManager.getAuthorInfo(txn, remote.getId());
|
||||||
|
assertEquals(UNVERIFIED, authorInfo.getStatus());
|
||||||
|
assertEquals(alias, contact.getAlias());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAuthorStatusTransaction() throws DbException {
|
||||||
Transaction txn = new Transaction(null, true);
|
Transaction txn = new Transaction(null, true);
|
||||||
|
|
||||||
// check unknown author
|
// check unknown author
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.bramble.test;
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.db.DbCallable;
|
||||||
import org.briarproject.bramble.api.db.DbRunnable;
|
import org.briarproject.bramble.api.db.DbRunnable;
|
||||||
import org.briarproject.bramble.api.db.Transaction;
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
@@ -13,4 +14,11 @@ public class DbExpectations extends Expectations {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected <R, E extends Exception> DbCallable<R, E> withDbCallable(
|
||||||
|
Transaction txn) {
|
||||||
|
addParameterMatcher(any(DbCallable.class));
|
||||||
|
currentBuilder().setAction(new RunTransactionWithResultAction(txn));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.briarproject.bramble.test;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.db.DbCallable;
|
||||||
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
|
import org.hamcrest.Description;
|
||||||
|
import org.jmock.api.Action;
|
||||||
|
import org.jmock.api.Invocation;
|
||||||
|
|
||||||
|
public class RunTransactionWithResultAction implements Action {
|
||||||
|
|
||||||
|
private final Transaction txn;
|
||||||
|
|
||||||
|
public RunTransactionWithResultAction(Transaction txn) {
|
||||||
|
this.txn = txn;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object invoke(Invocation invocation) throws Throwable {
|
||||||
|
DbCallable task = (DbCallable) invocation.getParameter(1);
|
||||||
|
return task.call(txn);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void describeTo(Description description) {
|
||||||
|
description.appendText("runs a task inside a database transaction");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user