Test fixes and improvements

This commit is contained in:
Christian Basler 2017-04-02 21:02:20 +02:00
parent 016b4f80ba
commit 841fb7eccd
11 changed files with 78 additions and 56 deletions

View File

@ -439,9 +439,7 @@ public class Plaintext implements Streamable {
public void addLabels(Collection<Label> labels) {
if (labels != null) {
for (Label label : labels) {
this.labels.add(label);
}
this.labels.addAll(labels);
}
}
@ -540,7 +538,7 @@ public class Plaintext implements Streamable {
private byte[] ackData;
private byte[] ackMessage;
private byte[] signature;
private long sent;
private Long sent;
private Long received;
private Status status;
private Set<Label> labels = new LinkedHashSet<>();
@ -665,12 +663,12 @@ public class Plaintext implements Streamable {
return this;
}
public Builder sent(long sent) {
public Builder sent(Long sent) {
this.sent = sent;
return this;
}
public Builder received(long received) {
public Builder received(Long received) {
this.received = received;
return this;
}

View File

@ -25,12 +25,11 @@ import ch.dissem.bitmessage.entity.payload.ObjectType;
import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.testutils.TestInventory;
import ch.dissem.bitmessage.utils.MessageMatchers;
import ch.dissem.bitmessage.utils.Singleton;
import ch.dissem.bitmessage.utils.TTL;
import ch.dissem.bitmessage.utils.TestUtils;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
@ -51,15 +50,18 @@ import static org.mockito.Mockito.*;
public class BitmessageContextTest {
private BitmessageContext ctx;
private BitmessageContext.Listener listener;
private TestInventory testInventory;
@Before
public void setUp() throws Exception {
Singleton.initialize(null);
listener = mock(BitmessageContext.Listener.class);
Singleton.initialize(new BouncyCryptography());
testInventory = new TestInventory();
ctx = new BitmessageContext.Builder()
.addressRepo(mock(AddressRepository.class))
.cryptography(new BouncyCryptography())
.inventory(mock(Inventory.class))
.cryptography(cryptography())
.inventory(spy(testInventory))
.listener(listener)
.messageRepo(mock(MessageRepository.class))
.networkHandler(mock(NetworkHandler.class))
@ -134,11 +136,19 @@ public class BitmessageContextTest {
@Test
public void ensureV2PubkeyIsNotRequestedIfItExistsInInventory() throws Exception {
testInventory.init(
"V1Msg.payload",
"V2GetPubkey.payload",
"V2Pubkey.payload",
"V3GetPubkey.payload",
"V3Pubkey.payload",
"V4Broadcast.payload",
"V4GetPubkey.payload",
"V4Pubkey.payload",
"V5Broadcast.payload"
);
BitmessageAddress contact = new BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT");
when(ctx.internals().getInventory().getObjects(anyLong(), anyLong(), any(ObjectType.class)))
.thenReturn(Collections.singletonList(
TestUtils.loadObjectMessage(2, "V2Pubkey.payload")
));
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(contact);
ctx.addContact(contact);
@ -150,11 +160,18 @@ public class BitmessageContextTest {
@Test
public void ensureV4PubkeyIsNotRequestedIfItExistsInInventory() throws Exception {
testInventory.init(
"V1Msg.payload",
"V2GetPubkey.payload",
"V2Pubkey.payload",
"V3GetPubkey.payload",
"V3Pubkey.payload",
"V4Broadcast.payload",
"V4GetPubkey.payload",
"V4Pubkey.payload",
"V5Broadcast.payload"
);
BitmessageAddress contact = new BitmessageAddress("BM-2cXxfcSetKnbHJX2Y85rSkaVpsdNUZ5q9h");
when(ctx.internals().getInventory().getObjects(anyLong(), anyLong(), any(ObjectType.class)))
.thenReturn(Collections.singletonList(
TestUtils.loadObjectMessage(2, "V4Pubkey.payload")
));
final BitmessageAddress stored = new BitmessageAddress(contact.getAddress());
stored.setAlias("Test");
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(stored);
@ -170,13 +187,12 @@ public class BitmessageContextTest {
public void ensureSubscriptionIsAddedAndExistingBroadcastsRetrieved() throws Exception {
BitmessageAddress address = new BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ");
List<ObjectMessage> objects = new LinkedList<>();
objects.add(TestUtils.loadObjectMessage(4, "V4Broadcast.payload"));
objects.add(TestUtils.loadObjectMessage(5, "V5Broadcast.payload"));
when(ctx.internals().getInventory().getObjects(eq(address.getStream()), anyLong(), any(ObjectType.class)))
.thenReturn(objects);
when(ctx.addresses().getSubscriptions(anyLong())).thenReturn(Collections.singletonList(address));
testInventory.init(
"V4Broadcast.payload",
"V5Broadcast.payload"
);
when(ctx.addresses().getSubscriptions(anyLong())).thenReturn(Collections.singletonList(address));
ctx.addSubscribtion(address);
verify(ctx.addresses(), atLeastOnce()).save(address);

View File

@ -31,6 +31,7 @@ import java.util.ArrayList;
import java.util.Collections;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
public class SerializationTest extends TestBase {
@ -78,7 +79,7 @@ public class SerializationTest extends TestBase {
@Test
public void ensurePlaintextIsSerializedAndDeserializedCorrectly() throws Exception {
Plaintext p1 = new Plaintext.Builder(MSG)
Plaintext expected = new Plaintext.Builder(MSG)
.from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.to(TestUtils.loadContact())
.message("Subject", "Message")
@ -86,21 +87,21 @@ public class SerializationTest extends TestBase {
.signature(new byte[0])
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
p1.write(out);
expected.write(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Plaintext p2 = Plaintext.read(MSG, in);
Plaintext actual = Plaintext.read(MSG, in);
// Received is automatically set on deserialization, so we'll need to set it to 0
// Received is automatically set on deserialization, so we'll need to set it to null
Field received = Plaintext.class.getDeclaredField("received");
received.setAccessible(true);
received.set(p2, 0L);
received.set(actual, null);
assertEquals(p1, p2);
assertThat(expected, is(actual));
}
@Test
public void ensurePlaintextWithExtendedEncodingIsSerializedAndDeserializedCorrectly() throws Exception {
Plaintext p1 = new Plaintext.Builder(MSG)
Plaintext expected = new Plaintext.Builder(MSG)
.from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.to(TestUtils.loadContact())
.message(new Message.Builder()
@ -111,42 +112,42 @@ public class SerializationTest extends TestBase {
.signature(new byte[0])
.build();
ByteArrayOutputStream out = new ByteArrayOutputStream();
p1.write(out);
expected.write(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Plaintext p2 = Plaintext.read(MSG, in);
Plaintext actual = Plaintext.read(MSG, in);
// Received is automatically set on deserialization, so we'll need to set it to 0
// Received is automatically set on deserialization, so we'll need to set it to null
Field received = Plaintext.class.getDeclaredField("received");
received.setAccessible(true);
received.set(p2, 0L);
received.set(actual, null);
assertEquals(p1, p2);
assertEquals(expected, actual);
}
@Test
public void ensurePlaintextWithAckMessageIsSerializedAndDeserializedCorrectly() throws Exception {
Plaintext p1 = new Plaintext.Builder(MSG)
Plaintext expected = new Plaintext.Builder(MSG)
.from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"))
.to(TestUtils.loadContact())
.message("Subject", "Message")
.ackData("ackMessage".getBytes())
.signature(new byte[0])
.build();
ObjectMessage ackMessage1 = p1.getAckMessage();
ObjectMessage ackMessage1 = expected.getAckMessage();
assertNotNull(ackMessage1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
p1.write(out);
expected.write(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Plaintext p2 = Plaintext.read(MSG, in);
Plaintext actual = Plaintext.read(MSG, in);
// Received is automatically set on deserialization, so we'll need to set it to 0
// Received is automatically set on deserialization, so we'll need to set it to null
Field received = Plaintext.class.getDeclaredField("received");
received.setAccessible(true);
received.set(p2, 0L);
received.set(actual, null);
assertEquals(p1, p2);
assertEquals(ackMessage1, p2.getAckMessage());
assertEquals(expected, actual);
assertEquals(ackMessage1, actual.getAckMessage());
}
@Test

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package ch.dissem.bitmessage.networking;
package ch.dissem.bitmessage.testutils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.ObjectType;

View File

@ -119,7 +119,7 @@ public class ConversationServiceTest {
.message(content)
.status(status);
if (status != Plaintext.Status.DRAFT && status != Plaintext.Status.DOING_PROOF_OF_WORK) {
builder.received(5 * ++timer - RANDOM.nextInt(10));
builder.received(5L * ++timer - RANDOM.nextInt(10));
}
return builder.build();
}

View File

@ -12,7 +12,7 @@ uploadArchives {
dependencies {
compile project(':core')
compile 'org.bouncycastle:bcprov-jdk15on:1.52'
compile 'org.bouncycastle:bcprov-jdk15on:1.56'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
}

View File

@ -24,6 +24,7 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.exception.NodeException;
import ch.dissem.bitmessage.networking.nio.NioNetworkHandler;
import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.testutils.TestInventory;
import ch.dissem.bitmessage.utils.Property;
import org.junit.After;
import org.junit.Before;

View File

@ -32,6 +32,11 @@ class TestNodeRegistry implements NodeRegistry {
this.nodes = Arrays.asList(nodes);
}
@Override
public void clear() {
// no op
}
@Override
public List<NetworkAddress> getKnownAddresses(int limit, long... streams) {
return nodes;

View File

@ -16,7 +16,7 @@ dependencies {
compile project(':core')
compile 'org.flywaydb:flyway-core:4.0.3'
testCompile 'junit:junit:4.12'
testCompile 'com.h2database:h2:1.4.192'
testCompile 'com.h2database:h2:1.4.194'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile project(path: ':core', configuration: 'testArtifacts')
testCompile project(':cryptography-bc')

View File

@ -114,13 +114,13 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
builder.from(ctx.getAddressRepository().getAddress(rs.getString("sender")));
builder.to(ctx.getAddressRepository().getAddress(rs.getString("recipient")));
builder.ackData(rs.getBytes("ack_data"));
builder.sent(rs.getLong("sent"));
builder.received(rs.getLong("received"));
builder.sent(rs.getObject("sent", Long.class));
builder.received(rs.getObject("received", Long.class));
builder.status(Plaintext.Status.valueOf(rs.getString("status")));
builder.ttl(rs.getLong("ttl"));
builder.retries(rs.getInt("retries"));
builder.nextTry(rs.getLong("next_try"));
builder.conversation((UUID) rs.getObject("conversation"));
builder.nextTry(rs.getObject("next_try", Long.class));
builder.conversation(rs.getObject("conversation", UUID.class));
builder.labels(findLabels(connection,
"id IN (SELECT label_id FROM Message_Label WHERE message_id=" + id + ") ORDER BY ord"));
Plaintext message = builder.build();
@ -232,8 +232,8 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
ps.setString(4, message.getTo() == null ? null : message.getTo().getAddress());
writeBlob(ps, 5, message);
ps.setBytes(6, message.getAckData());
ps.setLong(7, message.getSent());
ps.setLong(8, message.getReceived());
ps.setObject(7, message.getSent());
ps.setObject(8, message.getReceived());
ps.setString(9, message.getStatus() == null ? null : message.getStatus().name());
ps.setBytes(10, message.getInitialHash());
ps.setLong(11, message.getTTL());
@ -261,8 +261,8 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
ps.setString(4, message.getTo() == null ? null : message.getTo().getAddress());
writeBlob(ps, 5, message);
ps.setBytes(6, message.getAckData());
ps.setLong(7, message.getSent());
ps.setLong(8, message.getReceived());
ps.setObject(7, message.getSent());
ps.setObject(8, message.getReceived());
ps.setString(9, message.getStatus() == null ? null : message.getStatus().name());
ps.setBytes(10, message.getInitialHash());
ps.setLong(11, message.getTTL());

View File

@ -218,6 +218,7 @@ public class JdbcMessageRepositoryTest extends TestBase {
.from(identity)
.to(contactA)
.message("Subject", "Message")
.sent(UnixTime.now())
.status(Plaintext.Status.SENT)
.ttl(2)
.build();