SpringBoot: test reorganization

This commit is contained in:
djmil 2023-09-01 16:20:40 +02:00
parent c8d733dfac
commit f534b70bd2
12 changed files with 158 additions and 123 deletions

View File

@ -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']
}
}
}

View File

@ -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();
/**
* 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();
// // Request
// final HttpEntity<String> request = new HttpEntity<>(headers);
// }
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<String> request = new HttpEntity<>(json, headers);
ResponseEntity<String> 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();

View File

@ -30,9 +30,9 @@ public class HoldingIdentityResolver {
private static Map<String, HoldingIdentity> setCache(CordaClient cordaClient) {
Map<String, HoldingIdentity> map = new HashMap<>();
List<VirtualNode> vNodeList = cordaClient.getVirtualNodeList();
try {
List<VirtualNode> vNodeList = cordaClient.getVirtualNodeList();
for (VirtualNode vNode : vNodeList) {
var identity = vNode.holdingIdentity();

View File

@ -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) {
}

View File

@ -0,0 +1,5 @@
package djmil.cordacheckers.cordaclient.dao.flow.arguments;
public record CreateGameProposal(String opponentName, String opponentColor, String additionalMessage) {
}

View File

@ -0,0 +1,8 @@
package djmil.cordacheckers.cordaclient.dao.flow.arguments;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize
public class Empty {
}

View File

@ -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<VirtualNode> vNodes = cordaclient.getVirtualNodeList();
HoldingIdentity identity = vNodes.get(0).holdingIdentity();
assertThat(identity.getName()).isEqualTo("Bob");
}
}

View File

@ -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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> 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<String> request = new HttpEntity<>(headers);
final ResponseEntity<VirtualNodeList> 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"));
}
}

View File

@ -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<VirtualNode> 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);
}
}

View File

@ -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;

View File

@ -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<RequestBody> 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");
}
}

View File

@ -0,0 +1,5 @@
{
"clientRequestId": "list-2",
"flowClassName": "djmil.cordacheckers.gameproposal.ListFlow",
"requestBody": {}
}