Search in sources :

Example 1 with ReceiveRequest

use of com.quorum.tessera.api.ReceiveRequest in project tessera by ConsenSys.

the class SendReceiveBesuIT method sendAndReceivePrivacyGroup.

@Test
public void sendAndReceivePrivacyGroup() {
    final Party a = partyHelper.findByAlias("A");
    final Party b = partyHelper.findByAlias("B");
    final String output = privacyGroupTestUtil.create("A", "B");
    final JsonObject jsonObj = Json.createReader(new StringReader(output)).readObject();
    final String groupId = jsonObj.getString("privacyGroupId");
    byte[] transactionData = utils.createTransactionData();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPrivacyGroupId(groupId);
    sendRequest.setPayload(transactionData);
    final Response response = client.target(partyHelper.findByAlias("A").getQ2TUri()).path("/send").request().post(Entity.entity(sendRequest, MediaType.APPLICATION_JSON));
    final SendResponse result = response.readEntity(SendResponse.class);
    final String hash = result.getKey();
    assertThat(hash).isNotNull().isNotBlank();
    // Hash length = 32 bytes
    assertThat(Base64.getDecoder().decode(hash)).hasSize(32);
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(hash);
    final Response receiveResponse = client.target(a.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResult = receiveResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResult.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResult.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResult.getPrivacyGroupId()).isEqualTo(groupId);
    final Response receiveResponseOnB = client.target(b.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponseOnB).isNotNull();
    assertThat(receiveResponseOnB.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResultOnB = receiveResponseOnB.readEntity(ReceiveResponse.class);
    assertThat(receiveResultOnB.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResultOnB.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResultOnB.getPrivacyGroupId()).isEqualTo(groupId);
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Example 2 with ReceiveRequest

use of com.quorum.tessera.api.ReceiveRequest in project tessera by ConsenSys.

the class BesuTransactionResourceTest method receive.

@Test
public void receive() {
    String key = Base64.getEncoder().encodeToString("KEY".getBytes());
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(key);
    String recipient = Base64.getEncoder().encodeToString("Bobby Sixkiller".getBytes());
    receiveRequest.setTo(recipient);
    ReceiveResponse receiveResponse = mock(ReceiveResponse.class);
    when(receiveResponse.getAffectedTransactions()).thenReturn(Set.of());
    when(receiveResponse.getUnencryptedTransactionData()).thenReturn("Result".getBytes());
    when(receiveResponse.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(receiveResponse.sender()).thenReturn(PublicKey.from("sender".getBytes()));
    when(receiveResponse.getPrivacyGroupId()).thenReturn(Optional.of(PrivacyGroup.Id.fromBytes("group".getBytes())));
    when(transactionManager.receive(any(com.quorum.tessera.transaction.ReceiveRequest.class))).thenReturn(receiveResponse);
    BesuTransactionResource resource = new BesuTransactionResource(transactionManager, privacyGroupManager);
    final Response result = resource.receive(receiveRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    BesuReceiveResponse resultResponse = (BesuReceiveResponse) result.getEntity();
    assertThat(resultResponse.getPayload()).isEqualTo("Result".getBytes());
    assertThat(resultResponse.getSenderKey()).isEqualTo(PublicKey.from("sender".getBytes()).encodeToBase64());
    assertThat(resultResponse.getPrivacyGroupId()).isEqualTo(PublicKey.from("group".getBytes()).encodeToBase64());
    verify(transactionManager).receive(any(com.quorum.tessera.transaction.ReceiveRequest.class));
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Example 3 with ReceiveRequest

use of com.quorum.tessera.api.ReceiveRequest in project tessera by ConsenSys.

the class BesuTransactionResourceTest method receiveWithRecipient.

@Test
public void receiveWithRecipient() {
    String key = Base64.getEncoder().encodeToString("KEY".getBytes());
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(key);
    receiveRequest.setTo(Base64.getEncoder().encodeToString("Reno Raynes".getBytes()));
    ReceiveResponse receiveResponse = mock(ReceiveResponse.class);
    when(receiveResponse.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
    when(transactionManager.receive(any())).thenReturn(receiveResponse);
    when(receiveResponse.getUnencryptedTransactionData()).thenReturn("Result".getBytes());
    when(receiveResponse.sender()).thenReturn(PublicKey.from("sender".getBytes()));
    when(receiveResponse.getPrivacyGroupId()).thenReturn(Optional.of(PrivacyGroup.Id.fromBytes("group".getBytes())));
    BesuTransactionResource resource = new BesuTransactionResource(transactionManager, privacyGroupManager);
    final Response result = resource.receive(receiveRequest);
    assertThat(result.getStatus()).isEqualTo(200);
    BesuReceiveResponse resultResponse = (BesuReceiveResponse) result.getEntity();
    assertThat(resultResponse.getPayload()).isEqualTo("Result".getBytes());
    assertThat(resultResponse.getSenderKey()).isEqualTo(PublicKey.from("sender".getBytes()).encodeToBase64());
    assertThat(resultResponse.getPrivacyGroupId()).isEqualTo(PublicKey.from("group".getBytes()).encodeToBase64());
    verify(transactionManager).receive(any(com.quorum.tessera.transaction.ReceiveRequest.class));
}
Also used : SendResponse(com.quorum.tessera.api.SendResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Example 4 with ReceiveRequest

use of com.quorum.tessera.api.ReceiveRequest in project tessera by ConsenSys.

the class SendReceiveBesuIT method sendAndReceivePrivacyFor.

@Test
public void sendAndReceivePrivacyFor() throws InterruptedException {
    final Party a = partyHelper.findByAlias("A");
    final Party d = partyHelper.findByAlias("D");
    byte[] transactionData = utils.createTransactionData();
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(transactionData);
    sendRequest.setTo(d.getPublicKey());
    final Response response = client.target(a.getQ2TUri()).path("/send").request().post(Entity.entity(sendRequest, MediaType.APPLICATION_JSON));
    assertThat(response).isNotNull();
    assertThat(response.getStatus()).isEqualTo(200);
    final SendResponse result = response.readEntity(SendResponse.class);
    final String hash = result.getKey();
    // Hash length = 32 bytes
    assertThat(Base64.getDecoder().decode(hash)).hasSize(32);
    String findOutput = privacyGroupTestUtil.find("A", "A", "D");
    final JsonArray json = Json.createReader(new StringReader(findOutput)).readArray();
    Optional<JsonObject> legacyGroup = json.getValuesAs(JsonObject.class).stream().filter(v -> v.getString("type").equals("LEGACY")).findAny();
    // Legacy privacy group was created
    assertThat(legacyGroup).isPresent();
    final String groupId = legacyGroup.get().getString("privacyGroupId");
    ReceiveRequest receiveRequest = new ReceiveRequest();
    receiveRequest.setKey(hash);
    final Response receiveResponse = client.target(a.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponse).isNotNull();
    assertThat(receiveResponse.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResult = receiveResponse.readEntity(ReceiveResponse.class);
    assertThat(receiveResult.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResult.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResult.getPrivacyGroupId()).isEqualTo(groupId);
    final Response receiveResponseOnB = client.target(d.getQ2TUri()).path("/receive").request().post(Entity.entity(receiveRequest, MediaType.APPLICATION_JSON));
    // validate result
    assertThat(receiveResponseOnB).isNotNull();
    assertThat(receiveResponseOnB.getStatus()).isEqualTo(200);
    final ReceiveResponse receiveResultOnB = receiveResponseOnB.readEntity(ReceiveResponse.class);
    assertThat(receiveResultOnB.getPayload()).isEqualTo(transactionData);
    assertThat(receiveResultOnB.getSenderKey()).isEqualTo(a.getPublicKey());
    assertThat(receiveResultOnB.getPrivacyGroupId()).isEqualTo(groupId);
}
Also used : Client(jakarta.ws.rs.client.Client) SendResponse(com.quorum.tessera.api.SendResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) PartyHelper(com.quorum.tessera.test.PartyHelper) Json(jakarta.json.Json) Entity(jakarta.ws.rs.client.Entity) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Response(jakarta.ws.rs.core.Response) Base64(java.util.Base64) SendRequest(com.quorum.tessera.api.SendRequest) MediaType(jakarta.ws.rs.core.MediaType) StringReader(java.io.StringReader) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) JsonObject(jakarta.json.JsonObject) Optional(java.util.Optional) Party(com.quorum.tessera.test.Party) JsonArray(jakarta.json.JsonArray) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) SendRequest(com.quorum.tessera.api.SendRequest) SendResponse(com.quorum.tessera.api.SendResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) JsonObject(jakarta.json.JsonObject) SendResponse(com.quorum.tessera.api.SendResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) JsonArray(jakarta.json.JsonArray) Party(com.quorum.tessera.test.Party) StringReader(java.io.StringReader) ReceiveRequest(com.quorum.tessera.api.ReceiveRequest) Test(org.junit.Test)

Aggregations

ReceiveRequest (com.quorum.tessera.api.ReceiveRequest)4 SendResponse (com.quorum.tessera.api.SendResponse)4 Response (jakarta.ws.rs.core.Response)4 Test (org.junit.Test)4 BesuReceiveResponse (com.quorum.tessera.api.BesuReceiveResponse)2 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)2 SendRequest (com.quorum.tessera.api.SendRequest)2 Party (com.quorum.tessera.test.Party)2 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)2 JsonObject (jakarta.json.JsonObject)2 StringReader (java.io.StringReader)2 PartyHelper (com.quorum.tessera.test.PartyHelper)1 Json (jakarta.json.Json)1 JsonArray (jakarta.json.JsonArray)1 Client (jakarta.ws.rs.client.Client)1 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)1 Entity (jakarta.ws.rs.client.Entity)1 MediaType (jakarta.ws.rs.core.MediaType)1 Base64 (java.util.Base64)1 Optional (java.util.Optional)1