}
}
+ /**
+ * Creates a random name.
+ *
+ * @return A random name
+ * @throws IOException if an I/O error occurs
+ * @throws FcpException if an FCP error occurs
+ */
+ public String getRandomName() throws IOException, FcpException {
+ Map<String, String> replies = fcpClient.sendPluginMessage(webOfTrustPluginName, createParameters("Message", "RandomName"));
+ if (!replies.get("Message").equals("Name")) {
+ throw new FcpException("WebOfTrust Plugin did not reply with “Name” message!");
+ }
+ return replies.get("Name");
+ }
+
//
// PRIVATE METHODS
//
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
}
+ public static class GetRandomNameTests extends Common {
+
+ @Test
+ public void getRandomNameSendsTheCorrectCommand() throws Exception {
+ TestFcpConnection fcpConnection = createConnectionThatSendsName();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ webOfTrustPlugin.getRandomName();
+ assertThat(fcpConnection.sentMessages, contains(allOf(
+ isNamed(equalTo("FCPPluginMessage")),
+ hasField("Identifier", notNullValue()),
+ hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")),
+ hasField("Param.Message", equalTo("RandomName"))
+ )));
+ }
+
+ @Test
+ public void getRandomNameReturnsCorrectName() throws Exception {
+ TestFcpConnection fcpConnection = createConnectionThatSendsName();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ String name = webOfTrustPlugin.getRandomName();
+ assertThat(name, equalTo("R. A. Ndom"));
+ }
+
+ @Test
+ public void getRandomNameThrowsExceptionWhenDifferentReplyIsSentByPlugin() {
+ FcpConnection fcpConnection = createConnectionThatSendsOtherMessage();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ assertThrows(FcpException.class, () -> webOfTrustPlugin.getRandomName());
+ }
+
+ private TestFcpConnection createConnectionThatSendsName() {
+ return createConnection("Name", entries("Name", "R. A. Ndom"));
+ }
+
+ }
+
private static class Common {
@SafeVarargs