Add test for freenet interface
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 16 Feb 2016 21:11:44 +0000 (22:11 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 16 Feb 2016 21:11:44 +0000 (22:11 +0100)
pom.xml
src/main/java/de/todesbaum/jsite/application/Freenet7Interface.java
src/test/java/de/todesbaum/jsite/application/Freenet7InterfaceTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 718759a..d0546a8 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                        <version>1.3</version>
                        <scope>test</scope>
                </dependency>
+               <dependency>
+                       <groupId>org.mockito</groupId>
+                       <artifactId>mockito-all</artifactId>
+                       <version>1.10.19</version>
+                       <scope>test</scope>
+               </dependency>
        </dependencies>
        <repositories>
                <repository>
index dae72fb..9263010 100644 (file)
@@ -21,6 +21,7 @@ package de.todesbaum.jsite.application;
 import java.io.IOException;
 
 import de.todesbaum.util.freenet.fcp2.Client;
+import de.todesbaum.util.freenet.fcp2.Command;
 import de.todesbaum.util.freenet.fcp2.Connection;
 import de.todesbaum.util.freenet.fcp2.GenerateSSK;
 import de.todesbaum.util.freenet.fcp2.Message;
@@ -39,12 +40,26 @@ public class Freenet7Interface {
        /** Counter. */
        private static int counter = 0;
 
+       private final NodeSupplier nodeSupplier;
+       private final ConnectionSupplier connectionSupplier;
+       private final ClientSupplier clientSupplier;
+
        /** The node to connect to. */
        private Node node;
 
        /** The connection to the node. */
        private Connection connection;
 
+       public Freenet7Interface() {
+               this(new DefaultNodeSupplier(), new DefaultConnectionSupplier(), new DefaultClientSupplier());
+       }
+
+       Freenet7Interface(NodeSupplier nodeSupplier, ConnectionSupplier connectionSupplier, ClientSupplier clientSupplier) {
+               this.nodeSupplier = nodeSupplier;
+               this.connectionSupplier = connectionSupplier;
+               this.clientSupplier = clientSupplier;
+       }
+
        /**
         * Sets hostname and port from the given node.
         *
@@ -53,8 +68,8 @@ public class Freenet7Interface {
         */
        public void setNode(de.todesbaum.jsite.application.Node node) {
                if (node != null) {
-                       this.node = new Node(node.getHostname(), node.getPort());
-                       connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
+                       this.node = nodeSupplier.supply(node.getHostname(), node.getPort());
+                       connection = connectionSupplier.supply(node, "jSite-" + number + "-connection-" + counter++);
                } else {
                        this.node = null;
                        connection = null;
@@ -78,7 +93,7 @@ public class Freenet7Interface {
         * @return The connection to the node
         */
        public Connection getConnection(String identifier) {
-               return new Connection(node, identifier);
+               return connectionSupplier.supply(node, identifier);
        }
 
        /**
@@ -111,7 +126,7 @@ public class Freenet7Interface {
                        throw new IOException("Node is offline.");
                }
                GenerateSSK generateSSK = new GenerateSSK();
-               Client client = new Client(connection, generateSSK);
+               Client client = clientSupplier.supply(connection, generateSSK);
                Message keypairMessage = client.readMessage();
                return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
        }
@@ -126,4 +141,49 @@ public class Freenet7Interface {
                return (node != null) && (connection != null);
        }
 
+       public interface NodeSupplier {
+
+               Node supply(String hostname, int port);
+
+       }
+
+       public static class DefaultNodeSupplier implements NodeSupplier {
+
+               @Override
+               public Node supply(String hostname, int port) {
+                       return new Node(hostname, port);
+               }
+
+       }
+
+       public interface ConnectionSupplier {
+
+               Connection supply(Node node, String identifier);
+
+       }
+
+       public static class DefaultConnectionSupplier implements ConnectionSupplier {
+
+               @Override
+               public Connection supply(Node node, String identifier) {
+                       return new Connection(node, identifier);
+               }
+
+       }
+
+       public interface ClientSupplier {
+
+               Client supply(Connection connection, Command command) throws IOException;
+
+       }
+
+       public static class DefaultClientSupplier implements ClientSupplier {
+
+               @Override
+               public Client supply(Connection connection, Command command) throws IOException {
+                       return new Client(connection, command);
+               }
+
+       }
+
 }
diff --git a/src/test/java/de/todesbaum/jsite/application/Freenet7InterfaceTest.java b/src/test/java/de/todesbaum/jsite/application/Freenet7InterfaceTest.java
new file mode 100644 (file)
index 0000000..54de0a5
--- /dev/null
@@ -0,0 +1,158 @@
+package de.todesbaum.jsite.application;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+
+import de.todesbaum.jsite.application.Freenet7Interface.ClientSupplier;
+import de.todesbaum.jsite.application.Freenet7Interface.ConnectionSupplier;
+import de.todesbaum.jsite.application.Freenet7Interface.NodeSupplier;
+import de.todesbaum.util.freenet.fcp2.Client;
+import de.todesbaum.util.freenet.fcp2.Connection;
+import de.todesbaum.util.freenet.fcp2.GenerateSSK;
+import de.todesbaum.util.freenet.fcp2.Message;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+/**
+ * Unit test for {@link Freenet7Interface}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Freenet7InterfaceTest {
+
+       private static final String NODE_ADDRESS = "node-address";
+       private static final int NODE_PORT = 12345;
+       private static final String IDENTIFIER = "connection-identifier";
+       private static final String INSERT_URI = "insert-uri";
+       private static final String REQUEST_URI = "request-uri";
+
+       @Rule
+       public final ExpectedException expectedException = ExpectedException.none();
+
+       private final NodeSupplier nodeSupplier = mock(NodeSupplier.class);
+       private final ConnectionSupplier connectionSupplier = mock(ConnectionSupplier.class);
+       private final ClientSupplier clientSupplier = mock(ClientSupplier.class);
+       private final Freenet7Interface freenet7Interface = new Freenet7Interface(nodeSupplier, connectionSupplier, clientSupplier);
+
+       @Test
+       public void defaultConstructorCanBeCalled() {
+               new Freenet7Interface();
+       }
+
+       @Test
+       public void settingNodeAddressUsesNodeAndConnectionSuppliers() {
+               Node node = new Node(NODE_ADDRESS, NODE_PORT);
+               freenet7Interface.setNode(node);
+               verify(nodeSupplier).supply(NODE_ADDRESS, NODE_PORT);
+               verify(connectionSupplier).supply(eq(node), anyString());
+       }
+
+       @Test
+       public void settingNodeRetainsNodeCorrectly() {
+               Node node = new Node(NODE_ADDRESS, NODE_PORT);
+               Node realNode = mock(Node.class);
+               when(nodeSupplier.supply(NODE_ADDRESS, NODE_PORT)).thenReturn(realNode);
+               freenet7Interface.setNode(node);
+               assertThat(freenet7Interface.getNode(), is(realNode));
+       }
+
+       @Test
+       public void newConnectionCanBeCreated() {
+               Connection connection = mock(Connection.class);
+               when(connectionSupplier.supply(any(Node.class), eq(IDENTIFIER))).thenReturn(connection);
+               Connection returnedConnection = freenet7Interface.getConnection(IDENTIFIER);
+               assertThat(returnedConnection, is(connection));
+       }
+
+       @Test
+       public void interfaceHasNoNodeBeforeNodeIsSet() {
+               assertThat(freenet7Interface.hasNode(), is(false));
+       }
+
+       @Test
+       public void interfaceHasNodeOnceANodeWasSet() {
+               Connection connection = mock(Connection.class);
+               when(nodeSupplier.supply(anyString(), anyInt())).thenReturn(mock(Node.class));
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               assertThat(freenet7Interface.hasNode(), is(true));
+       }
+
+       @Test
+       public void interfaceHasNoNodeIfNodeIsSetToNull() {
+               Connection connection = mock(Connection.class);
+               when(nodeSupplier.supply(anyString(), anyInt())).thenReturn(mock(Node.class));
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               freenet7Interface.setNode(null);
+               assertThat(freenet7Interface.hasNode(), is(false));
+       }
+
+
+       @Test
+       public void nodeIsPresentIfConnectionIsConnected() throws IOException {
+               Connection connection = mock(Connection.class);
+               when(connection.isConnected()).thenReturn(true);
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               assertThat(freenet7Interface.isNodePresent(), is(true));
+       }
+
+       @Test
+       public void nodeIsPresentIfConnectionCanBeCreated() throws IOException {
+               Connection connection = mock(Connection.class);
+               when(connection.connect()).thenReturn(true);
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               assertThat(freenet7Interface.isNodePresent(), is(true));
+       }
+
+       @Test
+       public void exceptionIsThrownIfNodeIsNotPresentAndConnectionCanNotBeCreated() throws IOException {
+               Connection connection = mock(Connection.class);
+               doThrow(IOException.class).when(connection).connect();
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               expectedException.expect(IOException.class);
+               freenet7Interface.isNodePresent();
+       }
+
+       @Test
+       public void keyPairIsNotGeneratedIfNodeIsNotPresent() throws IOException {
+               Connection connection = mock(Connection.class);
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               expectedException.expect(IOException.class);
+               freenet7Interface.generateKeyPair();
+       }
+
+       @Test
+       public void keyPairIsGeneratedSuccessfully() throws IOException {
+               Connection connection = mock(Connection.class);
+               when(connection.isConnected()).thenReturn(true);
+               when(connectionSupplier.supply(any(Node.class), anyString())).thenReturn(connection);
+               freenet7Interface.setNode(mock(Node.class));
+               Message message = new Message("SSKKeyPair");
+               message.put("InsertURI", INSERT_URI);
+               message.put("RequestURI", REQUEST_URI);
+               Client client = mock(Client.class);
+               when(client.readMessage()).thenReturn(message);
+               when(clientSupplier.supply(eq(connection), Mockito.any(GenerateSSK.class))).thenReturn(client);
+               String[] keyPair = freenet7Interface.generateKeyPair();
+               assertThat(keyPair[0], is(INSERT_URI));
+               assertThat(keyPair[1], is(REQUEST_URI));
+       }
+
+}