From 0d80e48f0552c285a65a2da67ea9650037cc35d5 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 3 Jun 2020 19:35:04 +0200 Subject: [PATCH] More tests and improvements FsMQManager --- .../transport/fsmqmanager/FsMQManager.java | 166 +++++++++++++----- .../fsmqmanager/test/regression/TestMain.java | 90 +++++++--- 2 files changed, 191 insertions(+), 65 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java index acdb3e4..f5204db 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java +++ b/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 { private FsMQIdentity self = null; - private List identities = new ArrayList<>(); private Map identityAddressQueues = new HashMap(); @@ -20,40 +19,112 @@ public class FsMQManager { private static String SYNACKMSG = "SYNACK"; private static String ACKMSG = "ACK"; - public FsMQManager(FsMQIdentity self) { - this.self = self; - addOrUpdateIdentity(self); + public FsMQManager(FsMQIdentity self) throws NullPointerException { + if (self != null) { + this.self = self; + addOrUpdateIdentity(self); + } else { + throw new NullPointerException("self cant be null"); + } } // Identity address must be unique // Returns // - true for added - // - false for updated - public boolean addOrUpdateIdentity(FsMQIdentity ident) { + // - false for updated or own ident (which cant be updated) + public boolean addOrUpdateIdentity(FsMQIdentity ident) throws NullPointerException { boolean ret = false; - if(!identityExists(ident.getAddress())) { - // Good, add new ident - addIdent(ident); - ret = true; - } else{ - // Ok, update ident - removeIdentity(ident.getAddress()); - addIdent(ident); - ret = false; + if (ident != null) { + if (addIdent(ident)) { + // Good, add new ident + ret = true; + } else { + // 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()); + addIdent(ident); + ret = true; + } + } else { + throw new NullPointerException("ident cant be null"); } return ret; } // Removes the identity from identities and identityQueues by address - public void removeIdentity(String address) { - identities.removeIf(i -> i.getAddress().equals(address)); - identityAddressQueues.entrySet().removeIf(iq -> iq.getKey().equals(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)); + 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 { - FsMQMessage mqMsg = new FsMQMessage(self, msg); - String serializedStr = mqMsg.serialize(); - getQueueForIdentity(address).add(serializedStr); + // cant fail haha + public void removeAllIdentities() { + for (FsMQIdentity i : getIdentities()) { + removeIdentity(i.getAddress()); + } + } + + // Returns number of identities added + public int addIdentities(List 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); + String serializedStr = mqMsg.serialize(); + getQueueForIdentity(address).add(serializedStr); + } else { + throw new NullPointerException("msg cant be null"); + } + } else { + throw new NullPointerException("address cant be null"); + } } public void clearOwnQueue() { @@ -64,6 +135,7 @@ public class FsMQManager { return receiveMessage(0); } + // Blocks until timeout public String receiveMessage(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { String ret = null; FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress()); @@ -91,42 +163,52 @@ public class FsMQManager { // Exception on not unique public boolean identityExists(String address) { boolean ret = false; - List matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()); - if (matches.size() > 1) { - throw new IllegalStateException("Internal Error: Identity address not unique: " + address); - } - if (matches.size() == 1) { - ret = true; + if (address != null) { + List matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()); + if (matches.size() > 1) { + throw new IllegalStateException("Internal Error: Identity address not unique: " + address); + } + if (matches.size() == 1) { + ret = true; + } + } else { + throw new NullPointerException("address cant be null"); } return ret; } - // Return null if not existing - public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException { - FsMQIdentity ret = null; - if (identityExists(address)) { - ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0); - } - return ret; - } +// // Return null if not existing +// public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException { +// FsMQIdentity ret = null; +// if (identityExists(address)) { +// ret = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()).get(0); +// } +// return ret; +// } public List getIdentities() { return new ArrayList(identities); } public List getIdentityAddresses() { - List ret = new ArrayList<>(); - for(FsMQIdentity i : identities) { + List ret = new ArrayList<>(); + for (FsMQIdentity i : identities) { ret.add(i.getAddress()); } return ret; } - // undefined behaviour if already existing - private void addIdent(FsMQIdentity ident) { - identities.add(ident); - createQueueForIdent(ident); + // True - success + // False - already existing + private boolean addIdent(FsMQIdentity ident) { + boolean ret = false; + if (!identityExists(ident.getAddress())) { + identities.add(ident); + createQueueForIdent(ident); + ret = true; + } + return ret; } private void createQueueForIdent(FsMQIdentity ident) { diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java index ad6ebf7..60924f4 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java +++ b/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 bobQDir = "../resources/fsmsgqueue-test/bob"; String carolAddress = "Carol"; + String carolQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry"; String carolQDir = "../resources/fsmsgqueue-test/carol"; int msgCount = 10; @@ -28,6 +29,8 @@ class FsMQManagerTestContext extends AbstractTestContext { FsMQIdentity bob = null; FsMQIdentity carol = null; + List identList = null; + @Override public void init() throws Throwable { messages = createTestMessages(msgCount); @@ -63,7 +66,7 @@ class TestMain { assert ctx.qm != null : "null"; }).add(); - new TestUnit("identExists" + testCtx.ownAddress, testCtx, ctx -> { + new TestUnit("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { assert ctx.qm.identityExists(ctx.ownAddress); }).add(); @@ -76,11 +79,7 @@ class TestMain { }).add(); new TestUnit("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { - FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress); - assert self != null : "null"; - assert self.equals(ctx.self) : "Obj mismatch"; - log("Address: " + self.getAddress()); - log("qDir: " + self.getqDir()); + assert ctx.qm.identityExists(ctx.self.getAddress()) : "Own identity uknown"; }).add(); new TestUnit("Create ident " + testCtx.bobAddress, testCtx, ctx -> { @@ -91,8 +90,7 @@ class TestMain { }).add(); new TestUnit("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> { - FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress); - assert bob == null : "Ident is known but shouldnt"; + assert !ctx.qm.identityExists(ctx.bobAddress) : "Ident is known but shouldnt"; }).add(); new TestUnit("Add ident " + testCtx.bobAddress, testCtx, ctx -> { @@ -100,18 +98,18 @@ class TestMain { }).add(); new TestUnit("Ident known: " + testCtx.bobAddress, testCtx, ctx -> { - FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress); - assert bob.equals(ctx.bob) : "Obj mismatch"; + assert ctx.qm.identityExists(ctx.bobAddress) : "Ident is not known"; }).add(); new TestUnit("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.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"; }).add(); + new TestUnit("getIdentities", testCtx, ctx -> { List idents = ctx.qm.getIdentities(); for (FsMQIdentity i : idents) { @@ -128,12 +126,49 @@ class TestMain { assert !ctx.qm.identityExists("Eve") : "Identity Eve should not be known"; }).add(); - new TestUnit("Update ident " + testCtx.bobAddress, testCtx, ctx -> { + new TestUnit("AddOrUpdate ident " + testCtx.bobAddress, testCtx, ctx -> { ctx.bob.setqDir(ctx.bobQDir); assert ctx.bob.getqDir().equals(ctx.bobQDir); assert !ctx.qm.addOrUpdateIdentity(ctx.bob) : "Ident got added but should have been updated"; }).add(); + new TestUnit("Update ident " + testCtx.carolAddress, testCtx, ctx -> { + ctx.carol.setqDir(ctx.carolQDir); + assert ctx.qm.updateIdentity(ctx.carol): "Error updating ident"; + }).add(); + + new TestUnit("Update ownIdent Fails " + testCtx.carolAddress, testCtx, ctx -> { + assert !ctx.qm.updateIdentity(ctx.self): "upadted own ident"; + }).add(); + + new TestUnit("getIdentities", testCtx, ctx -> { + List idents = ctx.qm.getIdentities(); + for (FsMQIdentity i : idents) { + log(i.toString()); + } + assert idents.size() == 3 : "identity count wrong"; + }).add(); + + new TestUnit("removeAllIdents", testCtx, ctx -> { + ctx.qm.removeAllIdentities(); + }).add(); + + new TestUnit("getIdentities", testCtx, ctx -> { + List idents = ctx.qm.getIdentities(); + for (FsMQIdentity i : idents) { + log(i.toString()); + } + assert idents.size() == 1 : "identity count wrong"; + }).add(); + + new TestUnit("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("getIdentities", testCtx, ctx -> { List idents = ctx.qm.getIdentities(); for (FsMQIdentity i : idents) { @@ -142,6 +177,19 @@ class TestMain { assert idents.size() == 3 : "identity count wrong"; }).add(); + + new TestUnit("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("removeIdent" + testCtx.carolAddress, testCtx, ctx -> { ctx.qm.removeIdentity(ctx.carol.getAddress()); assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; @@ -156,16 +204,12 @@ class TestMain { assert idents.size() == 2 : "identity count wrong"; }).add(); -// new TestUnit("cant remove own ident", testCtx, ctx -> { -// ctx.qm.removeIdentity(ctx.self.getAddress()); -// assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; -// try { -// ctx.qm.getIdentityForAddress(ctx.carol.getAddress()); -// } catch (UnknownIdentityException e) { -// return; -// } -// assert false : "Identity Eve should not be known"; -// }).add(); + + new TestUnit("cant remove own ident", testCtx, ctx -> { + ctx.qm.removeIdentity(ctx.self.getAddress()); + assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; + assert ctx.qm.identityExists(ctx.self.getAddress()) : "removed own identity"; + }).add(); new TestUnit("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {