From e3e13592274853ec9336fb9b837d86762a5f8396 Mon Sep 17 00:00:00 2001 From: namtruong Date: Mon, 11 Nov 2019 14:50:11 +0000 Subject: [PATCH 1/3] Expose GET partyinfo API on ThirdParty server --- .../tessera/thirdparty/PartyInfoResource.java | 54 ++++++++++++ .../tessera/thirdparty/ThirdPartyRestApp.java | 17 +++- .../thirdparty/PartyInfoResourceTest.java | 86 +++++++++++++++++++ .../thirdparty/ThirdPartyRestAppTest.java | 5 +- 4 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java create mode 100644 tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java new file mode 100644 index 0000000000..573311000b --- /dev/null +++ b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java @@ -0,0 +1,54 @@ +package com.quorum.tessera.thirdparty; + +import com.quorum.tessera.partyinfo.PartyInfoService; +import com.quorum.tessera.partyinfo.model.PartyInfo; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; + +import javax.json.Json; +import javax.json.JsonArrayBuilder; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import static java.util.Objects.requireNonNull; + +@Path("/partyinfo") +public class PartyInfoResource { + + private final PartyInfoService partyInfoService; + + public PartyInfoResource(final PartyInfoService partyInfoService) { + this.partyInfoService = requireNonNull(partyInfoService, "partyInfoService must not be null"); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Fetch network/peer information", produces = "public list of peers/publickey mappings") + @ApiResponses({@ApiResponse(code = 200, message = "Peer/Network information", response = PartyInfo.class)}) + public Response getPartyInfo() { + + final PartyInfo current = this.partyInfoService.getPartyInfo(); + + final JsonArrayBuilder recipientBuilder = Json.createArrayBuilder(); + current.getRecipients().stream() + .map( + recipient -> + Json.createObjectBuilder() + .add("key", recipient.getKey().encodeToBase64()) + .build()) + .forEach(recipientBuilder::add); + + final String output = + Json.createObjectBuilder() + .add("url", current.getUrl()) + .add("keys", recipientBuilder.build()) + .build() + .toString(); + + return Response.status(Response.Status.OK).entity(output).build(); + } +} diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/ThirdPartyRestApp.java b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/ThirdPartyRestApp.java index 758ca40ea9..ef2a0a5b8e 100644 --- a/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/ThirdPartyRestApp.java +++ b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/ThirdPartyRestApp.java @@ -3,6 +3,9 @@ import com.quorum.tessera.api.filter.IPWhitelistFilter; import com.quorum.tessera.app.TesseraRestApplication; import com.quorum.tessera.config.AppType; +import com.quorum.tessera.core.api.ServiceFactory; +import com.quorum.tessera.partyinfo.PartyInfoService; + import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -13,13 +16,21 @@ @ApplicationPath("/") public class ThirdPartyRestApp extends TesseraRestApplication { + private final PartyInfoService partyInfoService; + + public ThirdPartyRestApp() { + final ServiceFactory serviceFactory = ServiceFactory.create(); + this.partyInfoService = serviceFactory.partyInfoService(); + } + @Override public Set getSingletons() { - IPWhitelistFilter iPWhitelistFilter = new IPWhitelistFilter(); - RawTransactionResource rawTransactionResource = new RawTransactionResource(); + final IPWhitelistFilter iPWhitelistFilter = new IPWhitelistFilter(); + final RawTransactionResource rawTransactionResource = new RawTransactionResource(); + final PartyInfoResource partyInfoResource = new PartyInfoResource(partyInfoService); - return Stream.of(iPWhitelistFilter, rawTransactionResource).collect(Collectors.toSet()); + return Stream.of(iPWhitelistFilter, rawTransactionResource, partyInfoResource).collect(Collectors.toSet()); } @Override diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java new file mode 100644 index 0000000000..ae17e364a2 --- /dev/null +++ b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java @@ -0,0 +1,86 @@ +package com.quorum.tessera.thirdparty; + +import com.quorum.tessera.encryption.PublicKey; +import com.quorum.tessera.partyinfo.PartyInfoService; +import com.quorum.tessera.partyinfo.model.Party; +import com.quorum.tessera.partyinfo.model.PartyInfo; +import com.quorum.tessera.partyinfo.model.Recipient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.json.Json; +import javax.json.JsonReader; +import javax.ws.rs.core.Response; +import java.io.StringReader; +import java.time.Instant; +import java.util.Arrays; +import java.util.Base64; +import java.util.HashSet; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +public class PartyInfoResourceTest { + + private PartyInfoService partyInfoService; + + private PartyInfoResource partyInfoResource; + + @Before + public void onSetup() { + this.partyInfoService = mock(PartyInfoService.class); + + this.partyInfoResource = new PartyInfoResource(partyInfoService); + } + + @After + public void onTearDown() { + verifyNoMoreInteractions(partyInfoService); + } + + @Test + public void partyInfoGet() { + + final String partyInfoJson = + "{\"url\":\"http://localhost:9001/\",\"keys\":[{\"key\":\"BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=\"},{\"key\":\"QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=\"}]}"; + + final Party partyWithoutTimestamp = new Party("http://localhost:9006/"); + final Party partyWithTimestamp = new Party("http://localhost:9005/"); + partyWithTimestamp.setLastContacted(Instant.parse("2019-01-02T15:03:22.875Z")); + + final PartyInfo partyInfo = + new PartyInfo( + "http://localhost:9001/", + new HashSet<>( + Arrays.asList( + new Recipient( + PublicKey.from( + Base64.getDecoder() + .decode( + "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=")), + "http://localhost:9001/"), + new Recipient( + PublicKey.from( + Base64.getDecoder() + .decode( + "QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=")), + "http://localhost:9002/"))), + new HashSet<>(Arrays.asList(partyWithTimestamp, partyWithoutTimestamp))); + + when(partyInfoService.getPartyInfo()).thenReturn(partyInfo); + + final Response response = partyInfoResource.getPartyInfo(); + + assertThat(response).isNotNull(); + assertThat(response.getStatus()).isEqualTo(200); + + final String output = response.getEntity().toString(); + final JsonReader expected = Json.createReader(new StringReader(partyInfoJson)); + final JsonReader actual = Json.createReader(new StringReader(output)); + + assertThat(expected.readObject()).isEqualTo(actual.readObject()); + + verify(partyInfoService).getPartyInfo(); + } +} diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/ThirdPartyRestAppTest.java b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/ThirdPartyRestAppTest.java index e1a60a37ac..a8bc82a763 100644 --- a/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/ThirdPartyRestAppTest.java +++ b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/ThirdPartyRestAppTest.java @@ -1,9 +1,9 @@ package com.quorum.tessera.thirdparty; -import com.quorum.tessera.thirdparty.ThirdPartyRestApp; import com.jpmorgan.quorum.mock.servicelocator.MockServiceLocator; import com.quorum.tessera.admin.ConfigService; import com.quorum.tessera.config.AppType; +import com.quorum.tessera.partyinfo.PartyInfoService; import com.quorum.tessera.service.locator.ServiceLocator; import com.quorum.tessera.transaction.TransactionManager; import java.util.HashSet; @@ -34,6 +34,7 @@ public void setUp() throws Exception { Set services = new HashSet(); services.add(mock(ConfigService.class)); services.add(mock(TransactionManager.class)); + services.add(mock(PartyInfoService.class)); serviceLocator.setServices(services); @@ -62,7 +63,7 @@ public void getSingletons() { Set results = thirdParty.getSingletons(); - assertThat(results).hasSize(2); + assertThat(results).hasSize(3); } @Test From 8b74891517fe50b2ef9da404dc6f712c7559cdfe Mon Sep 17 00:00:00 2001 From: namtruong Date: Mon, 11 Nov 2019 16:03:58 +0000 Subject: [PATCH 2/3] also remove p2p url from the json result --- .../java/com/quorum/tessera/thirdparty/PartyInfoResource.java | 1 - 1 file changed, 1 deletion(-) diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java index 573311000b..b70b55f879 100644 --- a/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java +++ b/tessera-jaxrs/thirdparty-jaxrs/src/main/java/com/quorum/tessera/thirdparty/PartyInfoResource.java @@ -44,7 +44,6 @@ public Response getPartyInfo() { final String output = Json.createObjectBuilder() - .add("url", current.getUrl()) .add("keys", recipientBuilder.build()) .build() .toString(); From 01e4e95377b1faf2b46f9067bf738a2b12e688cf Mon Sep 17 00:00:00 2001 From: namtruong Date: Mon, 11 Nov 2019 16:04:55 +0000 Subject: [PATCH 3/3] also remove p2p url from the json result --- .../com/quorum/tessera/thirdparty/PartyInfoResourceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java index ae17e364a2..522b364b70 100644 --- a/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java +++ b/tessera-jaxrs/thirdparty-jaxrs/src/test/java/com/quorum/tessera/thirdparty/PartyInfoResourceTest.java @@ -43,7 +43,7 @@ public void onTearDown() { public void partyInfoGet() { final String partyInfoJson = - "{\"url\":\"http://localhost:9001/\",\"keys\":[{\"key\":\"BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=\"},{\"key\":\"QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=\"}]}"; + "{\"keys\":[{\"key\":\"BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=\"},{\"key\":\"QfeDAys9MPDs2XHExtc84jKGHxZg/aj52DTh0vtA3Xc=\"}]}"; final Party partyWithoutTimestamp = new Party("http://localhost:9006/"); final Party partyWithTimestamp = new Party("http://localhost:9005/");