Test fixes and improvements

This commit is contained in:
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

@ -0,0 +1,81 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.testutils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.ObjectType;
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
import ch.dissem.bitmessage.ports.Inventory;
import ch.dissem.bitmessage.utils.TestUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestInventory implements Inventory {
private final Map<InventoryVector, ObjectMessage> inventory;
public TestInventory() {
this.inventory = new HashMap<>();
}
@Override
public List<InventoryVector> getInventory(long... streams) {
return new ArrayList<>(inventory.keySet());
}
@Override
public List<InventoryVector> getMissing(List<InventoryVector> offer, long... streams) {
return offer;
}
@Override
public ObjectMessage getObject(InventoryVector vector) {
return inventory.get(vector);
}
@Override
public List<ObjectMessage> getObjects(long stream, long version, ObjectType... types) {
return new ArrayList<>(inventory.values());
}
@Override
public void storeObject(ObjectMessage object) {
inventory.put(object.getInventoryVector(), object);
}
@Override
public boolean contains(ObjectMessage object) {
return inventory.containsKey(object.getInventoryVector());
}
@Override
public void cleanup() {
}
public void init(String... resources) throws IOException {
inventory.clear();
for (String resource : resources) {
int version = Integer.parseInt(resource.substring(1, 2));
ObjectMessage obj = TestUtils.loadObjectMessage(version, resource);
inventory.put(obj.getInventoryVector(), obj);
}
}
}

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();
}