HoldingIdentity Tests

This commit is contained in:
djmil 2023-09-22 14:45:37 +02:00
parent d3f5499ad8
commit ada353ce2f
4 changed files with 51 additions and 25 deletions

View File

@ -40,7 +40,7 @@ public class CordaClient {
this.jsonMapper = jsonMapper;
}
public List<VirtualNode> getVirtualNodeList() {
public List<VirtualNode> fetchVirtualNodeList() {
ResponseEntity<VirtualNodeList> resp = this.restTemplate.exchange(
"/virtualnode",
HttpMethod.GET,

View File

@ -34,4 +34,28 @@ public record HoldingIdentity(String x500Name, String shortHash) implements Seri
return false;
}
public boolean isCustodian() throws InvalidNameException {
LdapName ln = new LdapName(x500Name);
for(Rdn rdn : ln.getRdns()) {
if(rdn.getType().equalsIgnoreCase("OU")) {
return rdn.getValue().toString().equalsIgnoreCase("Custodian");
}
}
return false;
}
public boolean isCordaCheckers() throws InvalidNameException {
LdapName ln = new LdapName(x500Name);
for(Rdn rdn : ln.getRdns()) {
if(rdn.getType().equalsIgnoreCase("O")) {
return rdn.getValue().toString().equalsIgnoreCase("Checkers");
}
}
return false;
}
}

View File

@ -5,8 +5,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.naming.InvalidNameException;
import org.springframework.stereotype.Service;
import djmil.cordacheckers.cordaclient.CordaClient;
@ -22,45 +20,49 @@ public class HoldingIdentityResolver {
* Ideally, we want to be able to force update cache, had the corda
* cluster configuration changed (aka in case of a cache miss)
*/
final Map<String, HoldingIdentity> cache; // PlayerName to ShortHash
Map<String, HoldingIdentity> player; // PlayerName to ShortHash
HoldingIdentity custodian;
HoldingIdentityResolver(CordaClient cordaClient) {
this.cache = setCache(cordaClient);
this.player = new HashMap<>();
fetchVNodeList(cordaClient);
}
private static Map<String, HoldingIdentity> setCache(CordaClient cordaClient) {
Map<String, HoldingIdentity> map = new HashMap<>();
private void fetchVNodeList(CordaClient cordaClient) {
final List<VirtualNode> vnodeList = cordaClient.fetchVirtualNodeList();
for (VirtualNode vNode : vnodeList) {
final var identity = vNode.holdingIdentity();
try {
List<VirtualNode> vNodeList = cordaClient.getVirtualNodeList();
if (!identity.isCordaCheckers())
continue;
for (VirtualNode vNode : vNodeList) {
var identity = vNode.holdingIdentity();
if (identity.isPlayer())
player.put(identity.getName().toLowerCase(locale), identity);
if (identity.isPlayer()) {
map.put(
identity.getName().toLowerCase(locale),
identity
);
if (identity.isCustodian())
custodian = identity; // <<-- there shall be only one custodian
}
catch (Exception e) {
System.err.println("Incomprehensible VirtualNode identity " +identity);
}
}
} catch (InvalidNameException e) {
// TODO: logs
System.out.println("Unable to get ShorHash list of Corda VirtualNodes: "+e.getMessage());
}
return map;
}
/*
* @param playerName
* HoldingIdentity x500 name typically looks like
* "CN=Bob, OU=Player, O=Checkers, L=Kviv, C=UA"
* "CN=Bob, OU=Player, O=Checkers, L=Kyiv, C=UA"
* CN - is a common name, expected to be unique for CordaCheckers setup.
*
* @return HoldingIdentity
*/
public HoldingIdentity getByUsername(String userName) {
return this.cache.get(userName.toLowerCase(locale));
return this.player.get(userName.toLowerCase(locale));
}
public HoldingIdentity getCustodian() {
return this.custodian;
}
}

View File

@ -12,7 +12,7 @@
"cpi" : "CordaCheckers"
},
{
"x500Name" : "CN=Kumar, OU=Referee, O=Checkers, L=Mumbai, C=IN",
"x500Name" : "CN=Kumar, OU=Custodian, O=Checkers, L=Mumbai, C=IN",
"cpi" : "CordaCheckers"
},
{