use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class AsyncBatchPayloadPublisherTest method publishPayloadWrapsInterruptedException.
@Test
public void publishPayloadWrapsInterruptedException() throws InterruptedException {
EncodedPayload payload = mock(EncodedPayload.class);
PublicKey recipient = PublicKey.from("RECIPIENT".getBytes());
PublicKey otherRecipient = PublicKey.from("OTHERRECIPIENT".getBytes());
List<PublicKey> recipients = List.of(recipient, otherRecipient);
InterruptedException cause = new InterruptedException("some exception");
doThrow(cause).when(countDownLatch).await();
Throwable ex = catchThrowable(() -> asyncPublisher.publishPayload(payload, recipients));
assertThat(ex).isExactlyInstanceOf(BatchPublishPayloadException.class);
assertThat(ex).hasCause(cause);
verify(executorFactory).createCachedThreadPool();
verify(executor, times(2)).execute(any(Runnable.class));
verify(countDownLatchFactory).create(2);
verify(countDownLatch).await();
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class RestPayloadPublisherTest method publishMandatoryRecipientsToNodesThatDoNotSupport.
@Test
public void publishMandatoryRecipientsToNodesThatDoNotSupport() {
String targetUrl = "http://someplace.com";
EncodedPayload encodedPayload = mock(EncodedPayload.class);
when(encodedPayload.getPrivacyMode()).thenReturn(PrivacyMode.MANDATORY_RECIPIENTS);
byte[] payloadData = "Some Data".getBytes();
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
PublicKey recipientKey = mock(PublicKey.class);
NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeInfo.supportedApiVersions()).thenReturn(Set.of("v2", "2.1", "3.0"));
Recipient recipient = mock(Recipient.class);
when(recipient.getKey()).thenReturn(recipientKey);
when(recipient.getUrl()).thenReturn(targetUrl);
when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
assertThatExceptionOfType(MandatoryRecipientsNotSupportedException.class).isThrownBy(() -> payloadPublisher.publishPayload(encodedPayload, recipientKey)).withMessageContaining("Transactions with mandatory recipients are not currently supported on recipient");
verify(discovery).getRemoteNodeInfo(eq(recipientKey));
payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class RestPayloadPublisherTest method publish.
@Test
public void publish() {
final String targetUrl = "nodeUrl";
final EncodedPayload encodedPayload = mock(EncodedPayload.class);
final PublicKey publicKey = mock(PublicKey.class);
for (Response.Status expectedResponseStatus : Response.Status.values()) {
for (PrivacyMode privacyMode : PrivacyMode.values()) {
when(encodedPayload.getPrivacyMode()).thenReturn(privacyMode);
final NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeInfo.supportedApiVersions()).thenReturn(Set.of(EnhancedPrivacyVersion.API_VERSION_2, "2.1", "3.0", "4.0"));
when(nodeInfo.getUrl()).thenReturn(targetUrl);
when(discovery.getRemoteNodeInfo(publicKey)).thenReturn(nodeInfo);
final byte[] payloadData = "Payload".getBytes();
when(payloadEncoder.encode(encodedPayload)).thenReturn(payloadData);
WebTarget webTarget = mock(WebTarget.class);
when(client.target(targetUrl)).thenReturn(webTarget);
when(webTarget.path("/push")).thenReturn(webTarget);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
Response response = Response.status(expectedResponseStatus).build();
when(invocationBuilder.post(Entity.entity(payloadData, MediaType.APPLICATION_OCTET_STREAM_TYPE))).thenReturn(response);
when(webTarget.request()).thenReturn(invocationBuilder);
if (expectedResponseStatus == Response.Status.OK || expectedResponseStatus == Response.Status.CREATED) {
payloadPublisher.publishPayload(encodedPayload, publicKey);
} else {
PublishPayloadException publishPayloadException = Assertions.catchThrowableOfType(() -> payloadPublisher.publishPayload(encodedPayload, publicKey), PublishPayloadException.class);
assertThat(publishPayloadException).hasMessage(String.format("Unable to push payload to recipient url %s", targetUrl));
}
}
}
int iterations = Response.Status.values().length * PrivacyMode.values().length;
verify(client, times(iterations)).target(targetUrl);
verify(discovery, times(iterations)).getRemoteNodeInfo(publicKey);
verify(payloadEncoder, times(iterations)).encode(encodedPayload);
payloadEncoderFactoryFunction.verify(times(iterations), () -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class RestPayloadPublisherTest method handleConnectionError.
@Test
public void handleConnectionError() {
final String targetUri = "http://jimmywhite.com";
final PublicKey recipientKey = mock(PublicKey.class);
Recipient recipient = mock(Recipient.class);
when(recipient.getKey()).thenReturn(recipientKey);
when(recipient.getUrl()).thenReturn(targetUri);
NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
when(nodeInfo.getUrl()).thenReturn(targetUri);
when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
Client client = mock(Client.class);
when(client.target(targetUri)).thenThrow(ProcessingException.class);
final EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
when(payloadEncoder.encode(payload)).thenReturn("SomeData".getBytes());
RestPayloadPublisher restPayloadPublisher = new RestPayloadPublisher(client, discovery);
try {
restPayloadPublisher.publishPayload(payload, recipientKey);
failBecauseExceptionWasNotThrown(NodeOfflineException.class);
} catch (NodeOfflineException ex) {
assertThat(ex).hasMessageContaining(targetUri);
verify(client).target(targetUri);
verify(discovery).getRemoteNodeInfo(eq(recipientKey));
verify(payloadEncoder).encode(payload);
verify(discovery).getRemoteNodeInfo(eq(recipientKey));
payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
}
use of com.quorum.tessera.encryption.PublicKey in project tessera by ConsenSys.
the class RestPrivacyGroupPublisherTest method remoteNodeDoesNotSupportPrivacyGroup.
@Test
public void remoteNodeDoesNotSupportPrivacyGroup() {
String targetUrl = "url2.com";
final List<String> versions = List.of(MultiTenancyVersion.API_VERSION_2_1);
final NodeInfo oldNode = NodeInfo.Builder.create().withUrl(targetUrl).withRecipients(Collections.emptyList()).withSupportedApiVersions(versions).build();
PublicKey recipient = PublicKey.from("OLD_KEY".getBytes());
when(discovery.getRemoteNodeInfo(recipient)).thenReturn(oldNode);
final byte[] data = new byte[] { 15 };
try {
publisher.publishPrivacyGroup(data, recipient);
} catch (PrivacyGroupNotSupportedException privacyGroupNotSupportedException) {
assertThat(privacyGroupNotSupportedException).isNotNull().hasMessageContaining(recipient.encodeToBase64());
verify(discovery).getRemoteNodeInfo(recipient);
}
}
Aggregations