From f534b70bd25a4fc52ee2f6722302a10320c27ea7 Mon Sep 17 00:00:00 2001 From: djmil Date: Fri, 1 Sep 2023 16:20:40 +0200 Subject: [PATCH] SpringBoot: test reorganization --- backend/build.gradle | 12 +++ .../cordaclient/CordaClient.java | 56 +++++++++++--- .../cordaclient/HoldingIdentityResolver.java | 4 +- .../cordaclient/dao/flow/RequestBody.java | 7 ++ .../flow/arguments/CreateGameProposal.java | 5 ++ .../cordaclient/dao/flow/arguments/Empty.java | 8 ++ .../djmil/cordacheckers/CordaClientTest.java | 33 -------- .../CordacheckersApplicationTests.java | 76 ------------------- .../cordaclient/CordaClientTest.java | 40 ++++++++++ .../dao}/HoldingIdentityTest.java | 4 +- .../cordaclient/dao/flow/RequestBodyTest.java | 31 ++++++++ .../dao/flow/requestBody/ListFlow.json | 5 ++ 12 files changed, 158 insertions(+), 123 deletions(-) create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBody.java create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/CreateGameProposal.java create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/Empty.java delete mode 100644 backend/src/test/java/djmil/cordacheckers/CordaClientTest.java create mode 100644 backend/src/test/java/djmil/cordacheckers/cordaclient/CordaClientTest.java rename backend/src/test/java/djmil/cordacheckers/{ => cordaclient/dao}/HoldingIdentityTest.java (88%) create mode 100644 backend/src/test/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBodyTest.java create mode 100644 backend/src/test/resources/djmil/cordacheckers/cordaclient/dao/flow/requestBody/ListFlow.json diff --git a/backend/build.gradle b/backend/build.gradle index 9063fa1..b2199a9 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -29,3 +29,15 @@ dependencies { tasks.named('test') { useJUnitPlatform() } + +// Mostly necessary for VsCode IDE +sourceSets { + test { + java { + srcDirs = ['src/test/java'] + } + resources { + srcDirs = ['src/test/resources'] + } + } +} \ No newline at end of file diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java index 7e0f047..61d7281 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -1,5 +1,7 @@ package djmil.cordacheckers.cordaclient; +import static java.util.Objects.requireNonNull; + import java.util.List; import org.springframework.http.HttpEntity; @@ -10,10 +12,14 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; import djmil.cordacheckers.cordaclient.dao.VirtualNode; import djmil.cordacheckers.cordaclient.dao.VirtualNodeList; - -import static java.util.Objects.requireNonNull; +import djmil.cordacheckers.cordaclient.dao.flow.RequestBody; +import djmil.cordacheckers.cordaclient.dao.flow.arguments.Empty; @Service public class CordaClient { @@ -49,14 +55,46 @@ public class CordaClient { .virtualNodes(); } - // public String getGemeProposals(String ) { - // // Request authorization header - // HttpHeaders headers = basicAuthorizationHeader(); - - // // Request - // final HttpEntity request = new HttpEntity<>(headers); + /** + * Obtain list of unconsumed (active) GameProposals + * @param holdingIdentity + * @return GameProposals list in JSON form + * @throws JsonProcessingException + */ + public String listGameProposals(HoldingIdentity holdingIdentity) throws JsonProcessingException { + // Request authorization header + HttpHeaders headers = basicAuthorizationHeader(); + - // } + + RequestBody body = new RequestBody("list-2", "djmil.cordacheckers.gameproposal.ListFlow", new Empty()); + + ObjectMapper mapper = new ObjectMapper(); + + String json = mapper.writeValueAsString(body); + + if (json != null) + return json; + // new String("{\n" + // + // " \"clientRequestId\": \"list-1\",\n" + // + // " \"flowClassName\": \"djmil.cordacheckers.gameproposal.ListFlow\",\n" + // + // " \"requestBody\": {}\n" + // + // "}"); + + System.out.println("HiH "+holdingIdentity.shortHash()+"\nRequst JSON = "+json); + + + // Request + final HttpEntity request = new HttpEntity<>(json, headers); + + ResponseEntity resp = this.restTemplate.exchange( + "https://localhost:8888/api/v1/flow/"+holdingIdentity.shortHash(), + HttpMethod.POST, + request, + String.class); + + return resp.getBody(); + } private HttpHeaders basicAuthorizationHeader() { HttpHeaders headers = new HttpHeaders(); diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/HoldingIdentityResolver.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/HoldingIdentityResolver.java index fd9b5d3..d16801a 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/HoldingIdentityResolver.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/HoldingIdentityResolver.java @@ -30,9 +30,9 @@ public class HoldingIdentityResolver { private static Map setCache(CordaClient cordaClient) { Map map = new HashMap<>(); - List vNodeList = cordaClient.getVirtualNodeList(); - try { + List vNodeList = cordaClient.getVirtualNodeList(); + for (VirtualNode vNode : vNodeList) { var identity = vNode.holdingIdentity(); diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBody.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBody.java new file mode 100644 index 0000000..d60680d --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBody.java @@ -0,0 +1,7 @@ +package djmil.cordacheckers.cordaclient.dao.flow; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize +public record RequestBody (String clientRequestId, String flowClassName, Object requestBody) { +} diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/CreateGameProposal.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/CreateGameProposal.java new file mode 100644 index 0000000..9856d33 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/CreateGameProposal.java @@ -0,0 +1,5 @@ +package djmil.cordacheckers.cordaclient.dao.flow.arguments; + +public record CreateGameProposal(String opponentName, String opponentColor, String additionalMessage) { + +} diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/Empty.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/Empty.java new file mode 100644 index 0000000..9af8db8 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/flow/arguments/Empty.java @@ -0,0 +1,8 @@ +package djmil.cordacheckers.cordaclient.dao.flow.arguments; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +@JsonSerialize +public class Empty { + +} diff --git a/backend/src/test/java/djmil/cordacheckers/CordaClientTest.java b/backend/src/test/java/djmil/cordacheckers/CordaClientTest.java deleted file mode 100644 index 99bbf23..0000000 --- a/backend/src/test/java/djmil/cordacheckers/CordaClientTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package djmil.cordacheckers; - -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import djmil.cordacheckers.cordaclient.CordaClient; -import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; -import djmil.cordacheckers.cordaclient.dao.VirtualNode; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.List; - -import javax.naming.InvalidNameException; - -@SpringBootTest -public class CordaClientTest { - @Autowired - CordaClient cordaclient; - - @Test - void whenGetVirtualnode_thenListHoldingIdentity() throws GeneralSecurityException, IOException, InvalidNameException { - List vNodes = cordaclient.getVirtualNodeList(); - - HoldingIdentity identity = vNodes.get(0).holdingIdentity(); - assertThat(identity.getName()).isEqualTo("Bob"); - } - -} diff --git a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java index 3a6a0b7..daa5018 100644 --- a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java +++ b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java @@ -1,36 +1,9 @@ package djmil.cordacheckers; -import java.io.IOException; -import java.security.GeneralSecurityException; - -import javax.net.ssl.SSLContext; - -import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.HttpClients; -import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; -import org.apache.hc.client5.http.socket.ConnectionSocketFactory; -import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; -import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; -import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; -import org.apache.hc.core5.http.config.Registry; -import org.apache.hc.core5.http.config.RegistryBuilder; -import org.apache.hc.core5.ssl.SSLContexts; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.Resource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.web.client.RestTemplate; - -import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; -import djmil.cordacheckers.cordaclient.dao.VirtualNodeList; - -import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @@ -46,53 +19,4 @@ class CordacheckersApplicationTests { void contextLoads() { } - @Test - void givenAcceptOnlyCACertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException { - final SSLContext sslContext = SSLContexts.custom() - .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()) - .build(); - final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); - final Registry socketFactoryRegistry = RegistryBuilder. create() - .register("https", sslsf) - .register("http", new PlainConnectionSocketFactory()) - .build(); - - final BasicHttpClientConnectionManager connectionManager = - new BasicHttpClientConnectionManager(socketFactoryRegistry); - final CloseableHttpClient httpClient = HttpClients.custom() - .setConnectionManager(connectionManager) - .build(); - - final HttpComponentsClientHttpRequestFactory requestFactory = - new HttpComponentsClientHttpRequestFactory(httpClient); - //requestFactory.setReadTimeout(readTimeout); - //requestFactory.setConnectTimeout(connectTimeout); - - - // Request authorization header - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth("admin", "admin"); - - // String authStr = "username:password"; - // String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes()); - // headers.add("Authorization", "Basic " + base64Creds); - - // Request - final HttpEntity request = new HttpEntity<>(headers); - - final ResponseEntity response = new RestTemplate(requestFactory) - .exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, VirtualNodeList.class); - - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.hasBody()); - - VirtualNodeList vNodeList = response.getBody(); - assertThat(vNodeList).isNotNull(); - if (vNodeList != null) { - assertThat(vNodeList.virtualNodes().size() == 5); - } - - HoldingIdentity identity = vNodeList.virtualNodes().get(0).holdingIdentity(); - assertThat(identity.x500Name().contains("NotaryRep1")); - } } diff --git a/backend/src/test/java/djmil/cordacheckers/cordaclient/CordaClientTest.java b/backend/src/test/java/djmil/cordacheckers/cordaclient/CordaClientTest.java new file mode 100644 index 0000000..1166498 --- /dev/null +++ b/backend/src/test/java/djmil/cordacheckers/cordaclient/CordaClientTest.java @@ -0,0 +1,40 @@ +package djmil.cordacheckers.cordaclient; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import djmil.cordacheckers.cordaclient.dao.VirtualNode; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import javax.naming.InvalidNameException; + +@SpringBootTest +public class CordaClientTest { + @Autowired + CordaClient cordaClient; + + @Autowired + HoldingIdentityResolver holdingIdentityResolver; + + @Test + void testGetVirtualNodeList() throws InvalidNameException { + List vNodes = cordaClient.getVirtualNodeList(); + + assertThat(vNodes.size()).isEqualTo(5); // default vNode config for CSDE + } + + @Test + void testListGameProposals() throws JsonProcessingException { + String resp = cordaClient.listGameProposals(holdingIdentityResolver.getByCommonName("alice")); + + System.out.println("testListGameProposals "+ resp); + } + +} diff --git a/backend/src/test/java/djmil/cordacheckers/HoldingIdentityTest.java b/backend/src/test/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentityTest.java similarity index 88% rename from backend/src/test/java/djmil/cordacheckers/HoldingIdentityTest.java rename to backend/src/test/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentityTest.java index 5bda5b1..c69cc1a 100644 --- a/backend/src/test/java/djmil/cordacheckers/HoldingIdentityTest.java +++ b/backend/src/test/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentityTest.java @@ -1,9 +1,7 @@ -package djmil.cordacheckers; +package djmil.cordacheckers.cordaclient.dao; import org.junit.jupiter.api.Test; -import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; - import static org.assertj.core.api.Assertions.assertThat; import javax.naming.InvalidNameException; diff --git a/backend/src/test/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBodyTest.java b/backend/src/test/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBodyTest.java new file mode 100644 index 0000000..049d86e --- /dev/null +++ b/backend/src/test/java/djmil/cordacheckers/cordaclient/dao/flow/RequestBodyTest.java @@ -0,0 +1,31 @@ +package djmil.cordacheckers.cordaclient.dao.flow; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.json.JacksonTester; + +import djmil.cordacheckers.cordaclient.dao.flow.arguments.Empty; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; + +@JsonTest +public class RequestBodyTest { + + @Autowired + private JacksonTester json; + + @Test + void listFlowTest() throws IOException { + + RequestBody requestBody = new RequestBody( + "list-2", + "djmil.cordacheckers.gameproposal.ListFlow", + new Empty() + ); + + assertThat(json.write(requestBody)).isEqualToJson("requestBody/ListFlow.json"); + } +} diff --git a/backend/src/test/resources/djmil/cordacheckers/cordaclient/dao/flow/requestBody/ListFlow.json b/backend/src/test/resources/djmil/cordacheckers/cordaclient/dao/flow/requestBody/ListFlow.json new file mode 100644 index 0000000..bc1eea0 --- /dev/null +++ b/backend/src/test/resources/djmil/cordacheckers/cordaclient/dao/flow/requestBody/ListFlow.json @@ -0,0 +1,5 @@ +{ + "clientRequestId": "list-2", + "flowClassName": "djmil.cordacheckers.gameproposal.ListFlow", + "requestBody": {} +} \ No newline at end of file