import static java.util.Collections.emptySet;
import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts;
import static net.pterodactylus.fcp.test.IdentityMatchers.hasProperties;
+import static net.pterodactylus.fcp.test.IdentityMatchers.isIdentity;
import static net.pterodactylus.fcp.test.IdentityMatchers.isOwnIdentity;
import static net.pterodactylus.fcp.test.Matchers.hasField;
import static net.pterodactylus.fcp.test.Matchers.isNamed;
}
+ public static class AddIdentityTests extends Common {
+
+ @Test
+ public void addIdentitySendsCorrectMessage() throws Exception {
+ TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+ assertThat(fcpConnection.sentMessages.get(0), allOf(
+ isNamed(equalTo("FCPPluginMessage")),
+ hasField("Identifier", not(nullValue())),
+ hasField("PluginName", equalTo("plugins.WebOfTrust.WebOfTrust")),
+ hasField("Param.RequestURI", equalTo("USK@foo,bar/baz/123"))
+ ));
+ }
+
+ @Test
+ public void addIdentityParsesIdentityWithNicknameCorrectly() throws Exception {
+ TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithNickname();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+ assertThat(addedIdentity, isIdentity(equalTo("new-id"), equalTo("new-nick"), equalTo("USK@foo,bar/baz/123")));
+ }
+
+ @Test
+ public void addIdentityParsesIdentityWithoutNicknameCorrectly() throws Exception {
+ TestFcpConnection fcpConnection = createConnectionThatConfirmsAddedIdentityWithoutNickname();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ Identity addedIdentity = webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123");
+ assertThat(addedIdentity, isIdentity(equalTo("new-id"), nullValue(), equalTo("USK@foo,bar/baz/123")));
+ }
+
+ @Test
+ public void addIdentityFailsWhenDifferentReplyIsSentByPlugin() {
+ FcpConnection fcpConnection = createConnectionThatSendsOtherMessage();
+ WebOfTrustPlugin webOfTrustPlugin = createWebOfTrustPlugin(fcpConnection);
+ assertThrows(FcpException.class, () -> webOfTrustPlugin.addIdentity("USK@foo,bar/baz/123"));
+ }
+
+ private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithNickname() {
+ return createConnection("IdentityAdded", entries("ID", "new-id", "Nickname", "new-nick"));
+ }
+
+ private TestFcpConnection createConnectionThatConfirmsAddedIdentityWithoutNickname() {
+ return createConnection("IdentityAdded", entries("ID", "new-id"));
+ }
+
+ }
+
private static class Common {
@SafeVarargs
};
}
+ public static Matcher<Identity> isIdentity(Matcher<? super String> identifier, Matcher<? super String> nickname, Matcher<? super String> requestUri) {
+ return new TypeSafeDiagnosingMatcher<Identity>(Identity.class) {
+ @Override
+ protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+ if (!identifier.matches(identity.getIdentifier())) {
+ identifier.describeMismatch(identity.getIdentifier(), mismatchDescription);
+ return false;
+ }
+ if (!nickname.matches(identity.getNickname())) {
+ nickname.describeMismatch(identity.getNickname(), mismatchDescription);
+ return false;
+ }
+ if (!requestUri.matches(identity.getRequestUri())) {
+ requestUri.describeMismatch(identity.getRequestUri(), mismatchDescription);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is identity (").appendValue(identifier).appendText(", nicknamed ").appendValue(nickname)
+ .appendText(", public uri ").appendValue(requestUri).appendText(")");
+ }
+
+ };
+ }
+
}