Commit 015f5005 authored by Torsten Grote's avatar Torsten Grote

Merge branch '1232-get-pending-contact' into 'master'

Add method to get a pending contact

See merge request !1110
parents 91c5ec6f 79632908
Pipeline #3393 passed with stage
in 7 minutes and 41 seconds
......@@ -87,6 +87,12 @@ public interface ContactManager {
PendingContact addPendingContact(String link, String alias)
throws DbException, FormatException;
/**
* Returns the pending contact with the given ID.
*/
PendingContact getPendingContact(Transaction txn, PendingContactId p)
throws DbException;
/**
* Returns a list of {@link PendingContact PendingContacts} and their
* {@link PendingContactState states}.
......
......@@ -411,6 +411,14 @@ public interface DatabaseComponent extends TransactionManager {
*/
long getNextSendTime(Transaction txn, ContactId c) throws DbException;
/**
* Returns the pending contact with the given ID.
* <p/>
* Read-only.
*/
PendingContact getPendingContact(Transaction txn, PendingContactId p)
throws DbException;
/**
* Returns all pending contacts.
* <p/>
......
......@@ -127,6 +127,12 @@ class ContactManagerImpl implements ContactManager {
return p;
}
@Override
public PendingContact getPendingContact(Transaction txn, PendingContactId p)
throws DbException {
return db.getPendingContact(txn, p);
}
@Override
public Collection<Pair<PendingContact, PendingContactState>> getPendingContacts()
throws DbException {
......
......@@ -496,6 +496,14 @@ interface Database<T> {
*/
long getNextSendTime(T txn, ContactId c) throws DbException;
/**
* Returns the pending contact with the given ID.
* <p/>
* Read-only.
*/
PendingContact getPendingContact(T txn, PendingContactId p)
throws DbException;
/**
* Returns all pending contacts.
* <p/>
......
......@@ -712,6 +712,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
return db.getNextSendTime(txn, c);
}
@Override
public PendingContact getPendingContact(Transaction transaction,
PendingContactId p) throws DbException {
T txn = unbox(transaction);
if (!db.containsPendingContact(txn, p))
throw new NoSuchPendingContactException();
return db.getPendingContact(txn, p);
}
@Override
public Collection<PendingContact> getPendingContacts(
Transaction transaction) throws DbException {
......
......@@ -2205,6 +2205,30 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
@Override
public PendingContact getPendingContact(Connection txn, PendingContactId p)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT publicKey, alias, timestamp"
+ " FROM pendingContacts"
+ " WHERE pendingContactId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, p.getBytes());
rs = ps.executeQuery();
if (!rs.next()) throw new DbStateException();
PublicKey publicKey = new AgreementPublicKey(rs.getBytes(1));
String alias = rs.getString(2);
long timestamp = rs.getLong(3);
return new PendingContact(p, publicKey, alias, timestamp);
} catch (SQLException e) {
tryToClose(rs, LOG, WARNING);
tryToClose(ps, LOG, WARNING);
throw new DbException(e);
}
}
@Override
public Collection<PendingContact> getPendingContacts(Connection txn)
throws DbException {
......
......@@ -763,12 +763,12 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
throws Exception {
context.checking(new Expectations() {{
// Check whether the pending contact is in the DB (which it's not)
exactly(3).of(database).startTransaction();
exactly(4).of(database).startTransaction();
will(returnValue(txn));
exactly(3).of(database).containsPendingContact(txn,
exactly(4).of(database).containsPendingContact(txn,
pendingContactId);
will(returnValue(false));
exactly(3).of(database).abortTransaction(txn);
exactly(4).of(database).abortTransaction(txn);
}});
DatabaseComponent db = createDatabaseComponent(database, eventBus,
eventExecutor, shutdownManager);
......@@ -791,6 +791,14 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
// Expected
}
try {
db.transaction(false, transaction ->
db.getPendingContact(transaction, pendingContactId));
fail();
} catch (NoSuchPendingContactException expected) {
// Expected
}
try {
db.transaction(false, transaction ->
db.removePendingContact(transaction, pendingContactId));
......
......@@ -2204,14 +2204,14 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
assertEquals(emptyList(), db.getPendingContacts(txn));
db.addPendingContact(txn, pendingContact);
PendingContact retrieved =
db.getPendingContact(txn, pendingContact.getId());
assertPendingContactEquals(pendingContact, retrieved);
Collection<PendingContact> pendingContacts = db.getPendingContacts(txn);
assertEquals(1, pendingContacts.size());
PendingContact retrieved = pendingContacts.iterator().next();
assertEquals(pendingContact.getId(), retrieved.getId());
assertEquals(pendingContact.getAlias(), retrieved.getAlias());
assertArrayEquals(pendingContact.getPublicKey().getEncoded(),
retrieved.getPublicKey().getEncoded());
assertEquals(pendingContact.getTimestamp(), retrieved.getTimestamp());
retrieved = pendingContacts.iterator().next();
assertPendingContactEquals(pendingContact, retrieved);
db.removePendingContact(txn, pendingContact.getId());
assertEquals(emptyList(), db.getPendingContacts(txn));
......@@ -2220,6 +2220,15 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
db.close();
}
private void assertPendingContactEquals(PendingContact expected,
PendingContact actual) {
assertEquals(expected.getId(), actual.getId());
assertArrayEquals(expected.getPublicKey().getEncoded(),
actual.getPublicKey().getEncoded());
assertEquals(expected.getAlias(), actual.getAlias());
assertEquals(expected.getTimestamp(), actual.getTimestamp());
}
@Test
public void testTransferKeys() throws Exception {
boolean alice = random.nextBoolean();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment