Browse Source

More tests and improvements FsMQManager

JNI-96
heck 5 years ago
parent
commit
0d80e48f05
  1. 120
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  2. 90
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

120
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java

@ -12,7 +12,6 @@ import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class FsMQManager { public class FsMQManager {
private FsMQIdentity self = null; private FsMQIdentity self = null;
private List<FsMQIdentity> identities = new ArrayList<>(); private List<FsMQIdentity> identities = new ArrayList<>();
private Map<String, FsMsgQueue> identityAddressQueues = new HashMap<String, FsMsgQueue>(); private Map<String, FsMsgQueue> identityAddressQueues = new HashMap<String, FsMsgQueue>();
@ -20,40 +19,112 @@ public class FsMQManager {
private static String SYNACKMSG = "SYNACK"; private static String SYNACKMSG = "SYNACK";
private static String ACKMSG = "ACK"; private static String ACKMSG = "ACK";
public FsMQManager(FsMQIdentity self) { public FsMQManager(FsMQIdentity self) throws NullPointerException {
if (self != null) {
this.self = self; this.self = self;
addOrUpdateIdentity(self); addOrUpdateIdentity(self);
} else {
throw new NullPointerException("self cant be null");
}
} }
// Identity address must be unique // Identity address must be unique
// Returns // Returns
// - true for added // - true for added
// - false for updated // - false for updated or own ident (which cant be updated)
public boolean addOrUpdateIdentity(FsMQIdentity ident) { public boolean addOrUpdateIdentity(FsMQIdentity ident) throws NullPointerException {
boolean ret = false; boolean ret = false;
if(!identityExists(ident.getAddress())) { if (ident != null) {
if (addIdent(ident)) {
// Good, add new ident // Good, add new ident
addIdent(ident);
ret = true; ret = true;
} else { } else {
// Ok, update ident // Ok, update ident
updateIdentity(ident);
ret = false;
}
} else {
throw new NullPointerException("ident cant be null");
}
return ret;
}
// cant update own identity
// True - Success
// False - ident not existing or own identity
public boolean updateIdentity(FsMQIdentity ident) throws NullPointerException {
boolean ret = false;
if (ident != null) {
if (!isOwnIdentity(ident.getAddress()) && identityExists(ident.getAddress())) {
removeIdentity(ident.getAddress()); removeIdentity(ident.getAddress());
addIdent(ident); addIdent(ident);
ret = false; ret = true;
}
} else {
throw new NullPointerException("ident cant be null");
} }
return ret; return ret;
} }
// Removes the identity from identities and identityQueues by address // Removes the identity from identities and identityQueues by address
public void removeIdentity(String address) { public boolean removeIdentity(String address) throws NullPointerException {
boolean ret = false;
if (address != null) {
if (identityExists(address) && !isOwnIdentity(address)) {
identities.removeIf(i -> i.getAddress().equals(address)); identities.removeIf(i -> i.getAddress().equals(address));
identityAddressQueues.entrySet().removeIf(iq -> iq.getKey().equals(address)); identityAddressQueues.entrySet().removeIf(iq -> iq.getKey().equals(address));
ret = true;
}
} else {
throw new NullPointerException("address cant be null");
}
return ret;
} }
public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException { // cant fail haha
public void removeAllIdentities() {
for (FsMQIdentity i : getIdentities()) {
removeIdentity(i.getAddress());
}
}
// Returns number of identities added
public int addIdentities(List<FsMQIdentity> idents) throws NullPointerException {
int ret = 0;
if (idents != null) {
for (FsMQIdentity i : idents) {
if (addOrUpdateIdentity(i)) {
ret++;
}
}
} else {
throw new NullPointerException("idents cant be null");
}
return ret;
}
public boolean isOwnIdentity(String address) throws NullPointerException {
boolean ret = false;
if (address != null) {
ret = self.getAddress() == address;
} else {
throw new NullPointerException("address cant be null");
}
return ret;
}
public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException, NullPointerException {
if (address != null) {
if (msg != null) {
FsMQMessage mqMsg = new FsMQMessage(self, msg); FsMQMessage mqMsg = new FsMQMessage(self, msg);
String serializedStr = mqMsg.serialize(); String serializedStr = mqMsg.serialize();
getQueueForIdentity(address).add(serializedStr); getQueueForIdentity(address).add(serializedStr);
} else {
throw new NullPointerException("msg cant be null");
}
} else {
throw new NullPointerException("address cant be null");
}
} }
public void clearOwnQueue() { public void clearOwnQueue() {
@ -64,6 +135,7 @@ public class FsMQManager {
return receiveMessage(0); return receiveMessage(0);
} }
// Blocks until timeout
public String receiveMessage(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { public String receiveMessage(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException {
String ret = null; String ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress()); FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress());
@ -91,6 +163,7 @@ public class FsMQManager {
// Exception on not unique // Exception on not unique
public boolean identityExists(String address) { public boolean identityExists(String address) {
boolean ret = false; boolean ret = false;
if (address != null) {
List<FsMQIdentity> matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()); List<FsMQIdentity> matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList());
if (matches.size() > 1) { if (matches.size() > 1) {
throw new IllegalStateException("Internal Error: Identity address not unique: " + address); throw new IllegalStateException("Internal Error: Identity address not unique: " + address);
@ -98,18 +171,21 @@ public class FsMQManager {
if (matches.size() == 1) { if (matches.size() == 1) {
ret = true; ret = true;
} }
} else {
throw new NullPointerException("address cant be null");
}
return ret; return ret;
} }
// Return null if not existing // // Return null if not existing
public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException { // public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException {
FsMQIdentity ret = null; // FsMQIdentity ret = null;
if (identityExists(address)) { // if (identityExists(address)) {
ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0); // ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0);
} // }
return ret; // return ret;
} // }
public List<FsMQIdentity> getIdentities() { public List<FsMQIdentity> getIdentities() {
return new ArrayList<FsMQIdentity>(identities); return new ArrayList<FsMQIdentity>(identities);
@ -123,10 +199,16 @@ public class FsMQManager {
return ret; return ret;
} }
// undefined behaviour if already existing // True - success
private void addIdent(FsMQIdentity ident) { // False - already existing
private boolean addIdent(FsMQIdentity ident) {
boolean ret = false;
if (!identityExists(ident.getAddress())) {
identities.add(ident); identities.add(ident);
createQueueForIdent(ident); createQueueForIdent(ident);
ret = true;
}
return ret;
} }
private void createQueueForIdent(FsMQIdentity ident) { private void createQueueForIdent(FsMQIdentity ident) {

90
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

@ -18,6 +18,7 @@ class FsMQManagerTestContext extends AbstractTestContext {
String bobQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry"; String bobQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry";
String bobQDir = "../resources/fsmsgqueue-test/bob"; String bobQDir = "../resources/fsmsgqueue-test/bob";
String carolAddress = "Carol"; String carolAddress = "Carol";
String carolQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry";
String carolQDir = "../resources/fsmsgqueue-test/carol"; String carolQDir = "../resources/fsmsgqueue-test/carol";
int msgCount = 10; int msgCount = 10;
@ -28,6 +29,8 @@ class FsMQManagerTestContext extends AbstractTestContext {
FsMQIdentity bob = null; FsMQIdentity bob = null;
FsMQIdentity carol = null; FsMQIdentity carol = null;
List<FsMQIdentity> identList = null;
@Override @Override
public void init() throws Throwable { public void init() throws Throwable {
messages = createTestMessages(msgCount); messages = createTestMessages(msgCount);
@ -63,7 +66,7 @@ class TestMain {
assert ctx.qm != null : "null"; assert ctx.qm != null : "null";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("identExists" + testCtx.ownAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
assert ctx.qm.identityExists(ctx.ownAddress); assert ctx.qm.identityExists(ctx.ownAddress);
}).add(); }).add();
@ -76,11 +79,7 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress); assert ctx.qm.identityExists(ctx.self.getAddress()) : "Own identity uknown";
assert self != null : "null";
assert self.equals(ctx.self) : "Obj mismatch";
log("Address: " + self.getAddress());
log("qDir: " + self.getqDir());
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> {
@ -91,8 +90,7 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> {
FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress); assert !ctx.qm.identityExists(ctx.bobAddress) : "Ident is known but shouldnt";
assert bob == null : "Ident is known but shouldnt";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Add ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Add ident " + testCtx.bobAddress, testCtx, ctx -> {
@ -100,18 +98,18 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.bobAddress, testCtx, ctx -> {
FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress); assert ctx.qm.identityExists(ctx.bobAddress) : "Ident is not known";
assert bob.equals(ctx.bob) : "Obj mismatch";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Create/Add Ident " + testCtx.carolAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Create/Add Ident " + testCtx.carolAddress, testCtx, ctx -> {
ctx.carol = new FsMQIdentity(ctx.carolAddress, ctx.carolQDir); ctx.carol = new FsMQIdentity(ctx.carolAddress, ctx.carolQDirWrong);
assert ctx.carol != null : "null"; assert ctx.carol != null : "null";
assert ctx.carol.getAddress().equals(ctx.carolAddress) : "Address mismatch"; assert ctx.carol.getAddress().equals(ctx.carolAddress) : "Address mismatch";
assert ctx.carol.getqDir().equals(ctx.carolQDir) : "qDir mismatch"; assert ctx.carol.getqDir().equals(ctx.carolQDirWrong) : "qDir mismatch";
assert ctx.qm.addOrUpdateIdentity(ctx.carol) : "Ident got updated but should have been added"; assert ctx.qm.addOrUpdateIdentity(ctx.carol) : "Ident got updated but should have been added";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities(); List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) { for (FsMQIdentity i : idents) {
@ -128,12 +126,21 @@ class TestMain {
assert !ctx.qm.identityExists("Eve") : "Identity Eve should not be known"; assert !ctx.qm.identityExists("Eve") : "Identity Eve should not be known";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Update ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("AddOrUpdate ident " + testCtx.bobAddress, testCtx, ctx -> {
ctx.bob.setqDir(ctx.bobQDir); ctx.bob.setqDir(ctx.bobQDir);
assert ctx.bob.getqDir().equals(ctx.bobQDir); assert ctx.bob.getqDir().equals(ctx.bobQDir);
assert !ctx.qm.addOrUpdateIdentity(ctx.bob) : "Ident got added but should have been updated"; assert !ctx.qm.addOrUpdateIdentity(ctx.bob) : "Ident got added but should have been updated";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Update ident " + testCtx.carolAddress, testCtx, ctx -> {
ctx.carol.setqDir(ctx.carolQDir);
assert ctx.qm.updateIdentity(ctx.carol): "Error updating ident";
}).add();
new TestUnit<FsMQManagerTestContext>("Update ownIdent Fails " + testCtx.carolAddress, testCtx, ctx -> {
assert !ctx.qm.updateIdentity(ctx.self): "upadted own ident";
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities(); List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) { for (FsMQIdentity i : idents) {
@ -142,6 +149,47 @@ class TestMain {
assert idents.size() == 3 : "identity count wrong"; assert idents.size() == 3 : "identity count wrong";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("removeAllIdents", testCtx, ctx -> {
ctx.qm.removeAllIdentities();
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) {
log(i.toString());
}
assert idents.size() == 1 : "identity count wrong";
}).add();
new TestUnit<FsMQManagerTestContext>("addIdentities", testCtx, ctx -> {
ctx.identList = new ArrayList<>();
ctx.identList.add(ctx.self);
ctx.identList.add(ctx.bob);
ctx.identList.add(ctx.carol);
assert ctx.qm.addIdentities(ctx.identList) == 2 : "indents added count wrong";
}).add();
new TestUnit<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) {
log(i.toString());
}
assert idents.size() == 3 : "identity count wrong";
}).add();
new TestUnit<FsMQManagerTestContext>("isOwnIdent", testCtx, ctx -> {
for (FsMQIdentity i : ctx.qm.getIdentities()) {
if (ctx.qm.isOwnIdentity(i.getAddress())) {
log("isOwnIdent: " + i.getAddress() + "... YES");
assert i.getAddress().equals(ctx.self.getAddress()) : "should be own ident";
} else {
log("isOwnIdent: " + i.getAddress() + "... NO");
assert !i.getAddress().equals(ctx.self.getAddress()) : "shouldnt be own ident";
}
}
}).add();
new TestUnit<FsMQManagerTestContext>("removeIdent" + testCtx.carolAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("removeIdent" + testCtx.carolAddress, testCtx, ctx -> {
ctx.qm.removeIdentity(ctx.carol.getAddress()); ctx.qm.removeIdentity(ctx.carol.getAddress());
assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; assert ctx.qm.getIdentities().size() == 2 : "identity count wrong";
@ -156,16 +204,12 @@ class TestMain {
assert idents.size() == 2 : "identity count wrong"; assert idents.size() == 2 : "identity count wrong";
}).add(); }).add();
// new TestUnit<FsMQManagerTestContext>("cant remove own ident", testCtx, ctx -> {
// ctx.qm.removeIdentity(ctx.self.getAddress()); new TestUnit<FsMQManagerTestContext>("cant remove own ident", testCtx, ctx -> {
// assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; ctx.qm.removeIdentity(ctx.self.getAddress());
// try { assert ctx.qm.getIdentities().size() == 2 : "identity count wrong";
// ctx.qm.getIdentityForAddress(ctx.carol.getAddress()); assert ctx.qm.identityExists(ctx.self.getAddress()) : "removed own identity";
// } catch (UnknownIdentityException e) { }).add();
// return;
// }
// assert false : "Identity Eve should not be known";
// }).add();
new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {

Loading…
Cancel
Save