Browse Source

Test: PityTest - Make model generic

android-nettle-update
heck 1 year ago
parent
commit
fbade42ae9
4 changed files with 137 additions and 119 deletions
  1. +28
    -58
      test/java/foundation/pEp/jniadapter/test/utils/model/TestIdentity.java
  2. +21
    -19
      test/java/foundation/pEp/jniadapter/test/utils/model/TestModel.java
  3. +27
    -42
      test/java/foundation/pEp/jniadapter/test/utils/model/TestNode.java
  4. +61
    -0
      test/java/foundation/pEp/jniadapter/test/utils/model/pEpTestIdentity.java

+ 28
- 58
test/java/foundation/pEp/jniadapter/test/utils/model/TestIdentity.java View File

@ -1,39 +1,37 @@
package foundation.pEp.jniadapter.test.utils.model;
import foundation.pEp.jniadapter.Identity;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.FsMQIdentity;
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestIdentity {
private TestModel model = null;
private Role role = null;
private TestModel model = null; // Belongs to a TestModel (Tree struture)
private Role role = null; // Assumes a Role
private Role defaultPartner = null;
public Identity pEpIdent = null;
private List<TestKeyPair> keys = new ArrayList<>();
private TestKeyPair defaultKey = null;
private TestKeyPair defaultKeyPP = null;
private Map<NodeName,FsMQIdentity> transportIdents = new HashMap<>();
private Map<NodeName, FsMQIdentity> transportIdents = new HashMap<>();
private boolean isInitialized = false;
public TestIdentity(TestModel model, Role role) {
this.model = model;
this.role = role;
pEpIdent = new Identity();
pEpIdent.username = role.toString();
pEpIdent.address = role + "@peptest.org";
this.model.addIdent(this);
public TestIdentity() {
}
// public TestModel getModel() {
// return model;
// }
// this method has to be called before you can do ANYTHING with this object
public void initialize( TestModel model, Role role) {
this.role = role;
this.model = model;
isInitialized = true;
}
public Role getRole() {
ensureInitialized();
return role;
}
public TestIdentity getDefaultPartner() {
ensureInitialized();
return model.getIdent(defaultPartner);
}
@ -46,10 +44,11 @@ public class TestIdentity {
}
public void addNode(TestNode node) {
ensureInitialized();
FsMQIdentity tmp = createTransportIdentity(node);
transportIdents.put(node.getName(),tmp);
transportIdents.put(node.getName(), tmp);
if (node.getIdent().getRole() != getRole()) {
node.setRole(getRole());
node.setDefaultRole(getRole());
}
}
@ -61,45 +60,16 @@ public class TestIdentity {
return transportIdents.get(nodeName);
}
public void addKey(TestKeyPair kp, boolean isDefault) {
keys.add(kp);
if (isDefault) {
if (kp.getType() == KeyType.NORMAL) {
defaultKey = kp;
} else {
defaultKeyPP = kp;
}
}
}
public TestKeyPair getDefaultKey(boolean passphrase) {
if (!passphrase) {
return defaultKey;
} else {
return defaultKeyPP;
}
}
public List<TestKeyPair> getAllKeys() {
return keys;
}
public List<TestKeyPair> getNormalKeys() {
return keys.stream().filter(i -> {
return i.getType().equals(KeyType.NORMAL);
}).collect(Collectors.toList());
}
public List<TestKeyPair> getPassphraseKeys() {
return keys.stream().filter(i -> {
return i.getType().equals(KeyType.PASSPHRASE);
}).collect(Collectors.toList());
}
private FsMQIdentity createTransportIdentity(TestNode node) {
ensureInitialized();
String transportAddress = node.getName().toString() + getRole().toString();
String transportDir = node.getTransportDir() + getRole().toString();
return new FsMQIdentity(transportAddress, transportDir);
}
private void ensureInitialized() {
if (!isInitialized) {
throw new IllegalStateException("not initialized");
}
}
}

+ 21
- 19
test/java/foundation/pEp/jniadapter/test/utils/model/TestModel.java View File

@ -1,53 +1,55 @@
package foundation.pEp.jniadapter.test.utils.model;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.FsMQIdentity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
public class TestModel {
private Map<Role, TestIdentity> idents = new HashMap<>();
private Map<NodeName, TestNode> nodes = new HashMap<>();
public class TestModel<IdentityType extends TestIdentity, NodeType extends TestNode> {
private Map<Role, IdentityType> idents = new HashMap<>();
private Map<NodeName, NodeType> nodes = new HashMap<>();
public String dataDir = "../resources/";;
public String dataDir = "../resources/";
public String nodesDir = dataDir + "nodes/";
public TestModel() {
public TestModel(Supplier<IdentityType> identityTypeSupplier, Supplier<NodeType> nodeTypeSupplier) {
// Creating all Roles
for (Role r : Role.values()) {
new TestIdentity(this, r);
IdentityType tmp = identityTypeSupplier.get();
tmp.initialize(this,r);
addIdent(tmp);
}
// Creating all Nodes
for (NodeName n : NodeName.values()) {
new TestNode(this, n);
NodeType tmp = nodeTypeSupplier.get();
tmp.initialize(this, n);
addNode(tmp);
}
}
public void addIdent(TestIdentity ident) {
public void addIdent(IdentityType ident) {
idents.put(ident.getRole(), ident);
}
public TestIdentity getIdent(Role name) {
public IdentityType getIdent(Role name) {
return idents.get(name);
}
public List<TestIdentity> getAllIdents() {
return new ArrayList<TestIdentity>(idents.values());
public List<IdentityType> getAllIdents() {
return new ArrayList<>(idents.values());
}
public void addNode(TestNode node) {
public void addNode(NodeType node) {
nodes.put(node.getName(), node);
}
public TestNode getNode(NodeName name) {
TestNode re = nodes.get(name);
return re;
public NodeType getNode(NodeName name) {
return nodes.get(name);
}
public List<TestNode> getAllNodes() {
return new ArrayList<TestNode>(nodes.values());
public List<NodeType> getAllNodes() {
return new ArrayList<>(nodes.values());
}
}

+ 27
- 42
test/java/foundation/pEp/jniadapter/test/utils/model/TestNode.java View File

@ -1,74 +1,59 @@
package foundation.pEp.jniadapter.test.utils.model;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.FsMQIdentity;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class TestNode {
public class TestNode<IdentityType extends TestIdentity> {
private NodeName name = null;
private TestModel model = null;
private TestModel<IdentityType,TestNode<IdentityType>> model = null;
// TODO: Just one role per node for now
// private Set<Role> ownRoles = new HashSet();
private Role defaultRole = null;
private String homeDir = null;
private String transportDir = null;
private boolean isInitialized = false;
public TestNode(TestModel model, NodeName name) {
this.model = model;
public TestNode() {
}
// this method has to be called before you can do ANYTHING with this object
public void initialize(TestModel model, NodeName name) {
this.name = name;
homeDir = model.nodesDir + name.toString() + "/";
transportDir = homeDir + "inboxes/";
this.model.addNode(this);
this.model = model;
this.homeDir = model.nodesDir + name.toString() + "/";
this.transportDir = homeDir + "inboxes/";
this.isInitialized = true;
}
public NodeName getName() {
ensureInitialized();
return name;
}
// public TestModel getModel() {
// return model;
// }
public void setRole(Role role) {
public void setDefaultRole(Role role) {
ensureInitialized();
this.defaultRole = role;
TestIdentity ident = model.getIdent(role);
if(!ident.hasNode(getName())) {
IdentityType ident = model.getIdent(role);
if (!ident.hasNode(getName())) {
ident.addNode(this);
}
}
public TestIdentity getIdent() {
public IdentityType getIdent() {
ensureInitialized();
return model.getIdent(defaultRole);
}
// public void addRole(Role role) {
// this.ownRoles.add(role);
// TestIdentity ident = model.getIdent(role);
// if(!ident.hasNode(getName())) {
// ident.addNode(this);
// }
// }
// public boolean hasRole(Role role) {
// return ownRoles.contains(role);
// }
// public Set<TestIdentity> getIdents() {
// Set<TestIdentity> ret = new HashSet();
// for(Role r : ownRoles) {
// ret.add(model.getIdent(r));
// }
// return ret;
// }
public String getHomeDir() {
ensureInitialized();
return homeDir;
}
public String getTransportDir() {
ensureInitialized();
return transportDir;
}
private void ensureInitialized() {
if (!isInitialized) {
throw new IllegalStateException("Not initialized");
}
}
}

+ 61
- 0
test/java/foundation/pEp/jniadapter/test/utils/model/pEpTestIdentity.java View File

@ -0,0 +1,61 @@
package foundation.pEp.jniadapter.test.utils.model;
import foundation.pEp.jniadapter.Identity;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class pEpTestIdentity extends TestIdentity {
private List<pEpTestKeyPair> keys = new ArrayList<>();
public Identity pEpIdent = null;
private pEpTestKeyPair defaultKey = null;
private pEpTestKeyPair defaultKeyPP = null;
public pEpTestIdentity() {
super();
}
@Override
public void initialize(TestModel model, Role role) {
super.initialize(model, role);
pEpIdent = new Identity();
pEpIdent.username = role.toString();
pEpIdent.address = role + "@peptest.org";
}
public void addKey(pEpTestKeyPair kp, boolean isDefault) {
keys.add(kp);
if (isDefault) {
if (kp.getType() == KeyType.NORMAL) {
defaultKey = kp;
} else {
defaultKeyPP = kp;
}
}
}
public pEpTestKeyPair getDefaultKey(boolean passphrase) {
if (!passphrase) {
return defaultKey;
} else {
return defaultKeyPP;
}
}
public List<pEpTestKeyPair> getAllKeys() {
return keys;
}
public List<pEpTestKeyPair> getNormalKeys() {
return keys.stream().filter(i -> {
return i.getType().equals(KeyType.NORMAL);
}).collect(Collectors.toList());
}
public List<pEpTestKeyPair> getPassphraseKeys() {
return keys.stream().filter(i -> {
return i.getType().equals(KeyType.PASSPHRASE);
}).collect(Collectors.toList());
}
}

Loading…
Cancel
Save