Convert test to BrambleMockTestCase.

This commit is contained in:
akwizgran
2018-05-08 15:02:07 +01:00
parent 5320737d49
commit 3c4513b9c7

View File

@@ -8,11 +8,9 @@ import org.briarproject.bramble.api.sync.Ack;
import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.SyncRecordWriter; import org.briarproject.bramble.api.sync.SyncRecordWriter;
import org.briarproject.bramble.api.transport.StreamWriter; import org.briarproject.bramble.api.transport.StreamWriter;
import org.briarproject.bramble.test.BrambleTestCase; import org.briarproject.bramble.test.BrambleMockTestCase;
import org.briarproject.bramble.test.ImmediateExecutor; import org.briarproject.bramble.test.ImmediateExecutor;
import org.briarproject.bramble.test.TestUtils;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
@@ -20,36 +18,26 @@ import java.util.Collections;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS; import static org.briarproject.bramble.api.sync.SyncConstants.MAX_MESSAGE_IDS;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
// TODO: Convert to BrambleMockTestCase public class SimplexOutgoingSessionTest extends BrambleMockTestCase {
public class SimplexOutgoingSessionTest extends BrambleTestCase {
private final Mockery context; private static final int MAX_LATENCY = Integer.MAX_VALUE;
private final DatabaseComponent db;
private final Executor dbExecutor;
private final EventBus eventBus;
private final ContactId contactId;
private final MessageId messageId;
private final int maxLatency;
private final StreamWriter streamWriter;
private final SyncRecordWriter recordWriter;
public SimplexOutgoingSessionTest() { private final DatabaseComponent db = context.mock(DatabaseComponent.class);
context = new Mockery(); private final EventBus eventBus = context.mock(EventBus.class);
db = context.mock(DatabaseComponent.class); private final StreamWriter streamWriter = context.mock(StreamWriter.class);
dbExecutor = new ImmediateExecutor(); private final SyncRecordWriter recordWriter =
eventBus = context.mock(EventBus.class); context.mock(SyncRecordWriter.class);
streamWriter = context.mock(StreamWriter.class);
recordWriter = context.mock(SyncRecordWriter.class); private final Executor dbExecutor = new ImmediateExecutor();
contactId = new ContactId(234); private final ContactId contactId = new ContactId(234);
messageId = new MessageId(TestUtils.getRandomId()); private final MessageId messageId = new MessageId(getRandomId());
maxLatency = Integer.MAX_VALUE;
}
@Test @Test
public void testNothingToSend() throws Exception { public void testNothingToSend() throws Exception {
SimplexOutgoingSession session = new SimplexOutgoingSession(db, SimplexOutgoingSession session = new SimplexOutgoingSession(db,
dbExecutor, eventBus, contactId, maxLatency, streamWriter, dbExecutor, eventBus, contactId, MAX_LATENCY, streamWriter,
recordWriter); recordWriter);
Transaction noAckTxn = new Transaction(null, false); Transaction noAckTxn = new Transaction(null, false);
Transaction noMsgTxn = new Transaction(null, false); Transaction noMsgTxn = new Transaction(null, false);
@@ -68,7 +56,7 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(noMsgTxn)); will(returnValue(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId), oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(maxLatency)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null)); will(returnValue(null));
oneOf(db).commitTransaction(noMsgTxn); oneOf(db).commitTransaction(noMsgTxn);
oneOf(db).endTransaction(noMsgTxn); oneOf(db).endTransaction(noMsgTxn);
@@ -79,8 +67,6 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
}}); }});
session.run(); session.run();
context.assertIsSatisfied();
} }
@Test @Test
@@ -88,7 +74,7 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
Ack ack = new Ack(Collections.singletonList(messageId)); Ack ack = new Ack(Collections.singletonList(messageId));
byte[] raw = new byte[1234]; byte[] raw = new byte[1234];
SimplexOutgoingSession session = new SimplexOutgoingSession(db, SimplexOutgoingSession session = new SimplexOutgoingSession(db,
dbExecutor, eventBus, contactId, maxLatency, streamWriter, dbExecutor, eventBus, contactId, MAX_LATENCY, streamWriter,
recordWriter); recordWriter);
Transaction ackTxn = new Transaction(null, false); Transaction ackTxn = new Transaction(null, false);
Transaction noAckTxn = new Transaction(null, false); Transaction noAckTxn = new Transaction(null, false);
@@ -110,7 +96,7 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(msgTxn)); will(returnValue(msgTxn));
oneOf(db).generateBatch(with(msgTxn), with(contactId), oneOf(db).generateBatch(with(msgTxn), with(contactId),
with(any(int.class)), with(maxLatency)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(Arrays.asList(raw))); will(returnValue(Arrays.asList(raw)));
oneOf(db).commitTransaction(msgTxn); oneOf(db).commitTransaction(msgTxn);
oneOf(db).endTransaction(msgTxn); oneOf(db).endTransaction(msgTxn);
@@ -126,7 +112,7 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
oneOf(db).startTransaction(false); oneOf(db).startTransaction(false);
will(returnValue(noMsgTxn)); will(returnValue(noMsgTxn));
oneOf(db).generateBatch(with(noMsgTxn), with(contactId), oneOf(db).generateBatch(with(noMsgTxn), with(contactId),
with(any(int.class)), with(maxLatency)); with(any(int.class)), with(MAX_LATENCY));
will(returnValue(null)); will(returnValue(null));
oneOf(db).commitTransaction(noMsgTxn); oneOf(db).commitTransaction(noMsgTxn);
oneOf(db).endTransaction(noMsgTxn); oneOf(db).endTransaction(noMsgTxn);
@@ -137,7 +123,5 @@ public class SimplexOutgoingSessionTest extends BrambleTestCase {
}}); }});
session.run(); session.run();
context.assertIsSatisfied();
} }
} }