Fixed / improved message sending and added a system test (it's a start)

This commit is contained in:
2016-02-02 20:40:01 +01:00
parent 5f4dbfc985
commit edd8045327
7 changed files with 246 additions and 116 deletions

View File

@ -30,4 +30,5 @@ dependencies {
compile 'args4j:args4j:2.32'
compile 'com.h2database:h2:1.4.190'
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.10.19'
}

View File

@ -0,0 +1,79 @@
package ch.dissem.bitmessage;
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
import ch.dissem.bitmessage.entity.BitmessageAddress;
import ch.dissem.bitmessage.entity.Plaintext;
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
import ch.dissem.bitmessage.repository.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* @author Christian Basler
*/
public class SystemTest {
static BitmessageContext alice;
static TestListener aliceListener = new TestListener();
static BitmessageAddress aliceIdentity;
static BitmessageContext bob;
static TestListener bobListener = new TestListener();
static BitmessageAddress bobIdentity;
@BeforeClass
public static void setUp() {
JdbcConfig aliceDB = new JdbcConfig("jdbc:h2:mem:alice;DB_CLOSE_DELAY=-1", "sa", "");
alice = new BitmessageContext.Builder()
.addressRepo(new JdbcAddressRepository(aliceDB))
.inventory(new JdbcInventory(aliceDB))
.messageRepo(new JdbcMessageRepository(aliceDB))
.powRepo(new JdbcProofOfWorkRepository(aliceDB))
.port(6001)
.nodeRegistry(new TestNodeRegistry(6002))
.networkHandler(new DefaultNetworkHandler())
.cryptography(new BouncyCryptography())
.listener(aliceListener)
.build();
alice.startup();
aliceIdentity = alice.createIdentity(false);
JdbcConfig bobDB = new JdbcConfig("jdbc:h2:mem:bob;DB_CLOSE_DELAY=-1", "sa", "");
bob = new BitmessageContext.Builder()
.addressRepo(new JdbcAddressRepository(bobDB))
.inventory(new JdbcInventory(bobDB))
.messageRepo(new JdbcMessageRepository(bobDB))
.powRepo(new JdbcProofOfWorkRepository(bobDB))
.port(6002)
.nodeRegistry(new TestNodeRegistry(6001))
.networkHandler(new DefaultNetworkHandler())
.cryptography(new BouncyCryptography())
.listener(bobListener)
.build();
bob.startup();
bobIdentity = bob.createIdentity(false);
}
@AfterClass
public static void tearDown() {
alice.shutdown();
bob.shutdown();
}
@Test
public void ensureAliceCanSendMessageToBob() throws Exception {
bobListener.reset();
String originalMessage = UUID.randomUUID().toString();
alice.send(aliceIdentity, new BitmessageAddress(bobIdentity.getAddress()), "Subject", originalMessage);
Plaintext plaintext = bobListener.get(5, TimeUnit.MINUTES);
assertThat(plaintext.getText(), equalTo(originalMessage));
}
}

View File

@ -0,0 +1,26 @@
package ch.dissem.bitmessage;
import ch.dissem.bitmessage.entity.Plaintext;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* Created by chrig on 02.02.2016.
*/
public class TestListener implements BitmessageContext.Listener {
private CompletableFuture<Plaintext> future = new CompletableFuture<>();
@Override
public void receive(Plaintext plaintext) {
future.complete(plaintext);
}
public void reset() {
future = new CompletableFuture<>();
}
public Plaintext get(long timeout, TimeUnit unit) throws Exception {
return future.get(timeout, unit);
}
}

View File

@ -0,0 +1,51 @@
/*
* 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;
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.ports.NodeRegistry;
import java.util.LinkedList;
import java.util.List;
/**
* Empty {@link NodeRegistry} that doesn't do anything, but shouldn't break things either.
*/
class TestNodeRegistry implements NodeRegistry {
private List<NetworkAddress> nodes = new LinkedList<>();
public TestNodeRegistry(int... ports) {
for (int port : ports) {
nodes.add(
new NetworkAddress.Builder()
.ipv4(127, 0, 0, 1)
.port(port)
.build()
);
}
}
@Override
public List<NetworkAddress> getKnownAddresses(int limit, long... streams) {
return nodes;
}
@Override
public void offerAddresses(List<NetworkAddress> addresses) {
// Ignore
}
}