From d480c35514e26e124737c3d850876ba9d42ace99 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 3 Jun 2020 18:14:55 +0200 Subject: [PATCH] Before remove FsMQManager.getIdentityAddresses() / not needed / identityExisting() is sufficient. --- .../transport/fsmqmanager/FsMQIdentity.java | 12 ++ .../transport/fsmqmanager/FsMQManager.java | 99 ++++++++++------ .../fsmqmanager/test/regression/TestMain.java | 107 +++++++++++++++--- 3 files changed, 164 insertions(+), 54 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java index ff9caff..1155439 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java @@ -16,4 +16,16 @@ public class FsMQIdentity implements java.io.Serializable { public String getqDir() { return qDir; } + + public void setqDir(String qDir) { + this.qDir = qDir; + } + + public String toString(){ + String ret = ""; + + ret += "Address: '" + address + "'\n"; + ret += "qDir : '" + qDir + "'"; + return ret; + } } \ No newline at end of file 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 1fd5290..acdb3e4 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 @@ -30,36 +30,43 @@ public class FsMQManager { // - true for added // - false for updated public boolean addOrUpdateIdentity(FsMQIdentity ident) { - try { - getIdentityForAddress(ident.getAddress()); - } catch (UnknownIdentityException e) { + boolean ret = false; + if(!identityExists(ident.getAddress())) { // Good, add new ident addIdent(ident); - return true; + ret = true; + } else{ + // Ok, update ident + removeIdentity(ident.getAddress()); + addIdent(ident); + ret = false; } - // Ok, update ident - removeIdent(ident); - addIdent(ident); - return false; + 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 void sendMsgToIdentity(FsMQIdentity ident, String msg) throws UnknownIdentityException, IOException { + public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException { FsMQMessage mqMsg = new FsMQMessage(self, msg); String serializedStr = mqMsg.serialize(); - getQueueForIdentity(ident).add(serializedStr); + getQueueForIdentity(address).add(serializedStr); } public void clearOwnQueue() { - getQueueForIdentity(self).clear(); + getQueueForIdentity(self.getAddress()).clear(); } - public String waitForMsg() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { - return waitForMsg(0); + public String receiveMessage() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { + return receiveMessage(0); } - public String waitForMsg(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { + public String receiveMessage(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { String ret = null; - FsMsgQueue onwQueue = getQueueForIdentity(self); + FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress()); FsMQMessage mqMsg = null; int pollInterval = 100; int pollRepeats = timeoutSec * 1000 / pollInterval; @@ -79,42 +86,60 @@ public class FsMQManager { return ret; } + // True if existing + // False if not + // 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; + } + 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) { + ret.add(i.getAddress()); + } + return ret; + } + // undefined behaviour if already existing private void addIdent(FsMQIdentity ident) { identities.add(ident); createQueueForIdent(ident); } - // Removes the identity from identities and identityQueues by address - private void removeIdent(FsMQIdentity ident) { - identities.removeIf(i -> i.getAddress().equals(ident.getAddress())); - identityAddressQueues.entrySet().removeIf(iq -> iq.getKey().equals(ident.getAddress())); - } - private void createQueueForIdent(FsMQIdentity ident) { FsMsgQueue q = new FsMsgQueue(ident.getqDir()); identityAddressQueues.put(ident.getAddress(), q); } - private FsMsgQueue getQueueForIdentity(FsMQIdentity ident) throws UnknownIdentityException { + private FsMsgQueue getQueueForIdentity(String address) throws UnknownIdentityException { FsMsgQueue ret = null; - ret = identityAddressQueues.get(ident.getAddress()); + ret = identityAddressQueues.get(address); if (ret == null) { - throw new UnknownIdentityException("Unknown identity address: " + ident.getAddress()); - } - return ret; - } - - public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException { - FsMQIdentity ret = null; - List matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList()); - if (matches.size() <= 0) { - throw new UnknownIdentityException("No identity with address:" + address); - } - if (matches.size() > 1) { - throw new IllegalStateException("Identity address not unique: " + address); + throw new UnknownIdentityException("Unknown identity address: " + address); } - ret = matches.get(0); return ret; } 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 5959924..ad6ebf7 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 @@ -7,6 +7,7 @@ import foundation.pEp.jniadapter.test.framework.*; import java.io.IOException; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeoutException; @@ -14,7 +15,10 @@ class FsMQManagerTestContext extends AbstractTestContext { String ownAddress = "Alice"; String ownQDir = "../resources/fsmsgqueue-test/alice"; String bobAddress = "Bob"; + String bobQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry"; String bobQDir = "../resources/fsmsgqueue-test/bob"; + String carolAddress = "Carol"; + String carolQDir = "../resources/fsmsgqueue-test/carol"; int msgCount = 10; ArrayList messages; @@ -22,6 +26,7 @@ class FsMQManagerTestContext extends AbstractTestContext { FsMQManager qm; FsMQIdentity self = null; FsMQIdentity bob = null; + FsMQIdentity carol = null; @Override public void init() throws Throwable { @@ -49,8 +54,8 @@ class TestMain { new TestUnit("Create own ident: " + testCtx.ownAddress, testCtx, ctx -> { ctx.self = new FsMQIdentity(ctx.ownAddress, ctx.ownQDir); assert ctx.self != null : "null"; - assert ctx.self.getAddress().equals(ctx.ownAddress): "Address mismatch"; - assert ctx.self.getqDir().equals(ctx.ownQDir): "qDir mismatch"; + assert ctx.self.getAddress().equals(ctx.ownAddress) : "Address mismatch"; + assert ctx.self.getqDir().equals(ctx.ownQDir) : "qDir mismatch"; }).add(); new TestUnit("Constructor with: " + testCtx.ownAddress, testCtx, ctx -> { @@ -58,6 +63,18 @@ class TestMain { assert ctx.qm != null : "null"; }).add(); + new TestUnit("identExists" + testCtx.ownAddress, testCtx, ctx -> { + assert ctx.qm.identityExists(ctx.ownAddress); + }).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("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress); assert self != null : "null"; @@ -67,19 +84,15 @@ class TestMain { }).add(); new TestUnit("Create ident " + testCtx.bobAddress, testCtx, ctx -> { - ctx.bob = new FsMQIdentity(ctx.bobAddress, ctx.bobQDir); + ctx.bob = new FsMQIdentity(ctx.bobAddress, ctx.bobQDirWrong); assert ctx.bob != null : "null"; assert ctx.bob.getAddress().equals(ctx.bobAddress) : "Address mismatch"; - assert ctx.bob.getqDir().equals(ctx.bobQDir) : "qDir mismatch"; + assert ctx.bob.getqDir().equals(ctx.bobQDirWrong) : "qDir mismatch"; }).add(); new TestUnit("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> { - try { - FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.bobAddress); - } catch (UnknownIdentityException e) { - return; - } - assert false : "Ident is known but shouldnt"; + FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress); + assert bob == null : "Ident is known but shouldnt"; }).add(); new TestUnit("Add ident " + testCtx.bobAddress, testCtx, ctx -> { @@ -91,10 +104,70 @@ class TestMain { assert bob.equals(ctx.bob) : "Obj mismatch"; }).add(); + new TestUnit("Create/Add Ident " + testCtx.carolAddress, testCtx, ctx -> { + ctx.carol = new FsMQIdentity(ctx.carolAddress, ctx.carolQDir); + 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.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) { + log(i.toString()); + } + assert idents.size() == 3 : "identity count wrong"; + }).add(); + + new TestUnit("getIdents is copy", testCtx, ctx -> { + List idents = ctx.qm.getIdentities(); + int identSize = idents.size(); + idents.add(new FsMQIdentity("Eve", "EvilEveDir")); + assert identSize == ctx.qm.getIdentities().size() : "ident count wrong"; + assert !ctx.qm.identityExists("Eve") : "Identity Eve should not be known"; + }).add(); + new TestUnit("Update 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("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("removeIdent" + testCtx.carolAddress, testCtx, ctx -> { + ctx.qm.removeIdentity(ctx.carol.getAddress()); + assert ctx.qm.getIdentities().size() == 2 : "identity count wrong"; + assert !ctx.qm.identityExists(ctx.carol.getAddress()) : "Remove failed"; + }).add(); + + new TestUnit("getIdentities", testCtx, ctx -> { + List idents = ctx.qm.getIdentities(); + for (FsMQIdentity i : idents) { + log(i.toString()); + } + 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("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> { ctx.qm.clearOwnQueue(); }).add(); @@ -102,10 +175,10 @@ class TestMain { new TestUnit("waitForMsg timeout", testCtx, ctx -> { log("waitForMessage with timeout..."); try { - ctx.qm.waitForMsg(3); - } catch(IOException e) { + ctx.qm.receiveMessage(3); + } catch (IOException e) { throw new RuntimeException(e.toString()); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException(e.toString()); } catch (TimeoutException e) { return; @@ -116,8 +189,8 @@ class TestMain { String msg = ctx.messages.get(0); log("TX MSG: " + msg); try { - ctx.qm.sendMsgToIdentity(ctx.self, msg); - } catch(IOException e) { + ctx.qm.sendMessage(ctx.self.getAddress(), msg); + } catch (IOException e) { throw new RuntimeException(e.toString()); } }).add(); @@ -125,8 +198,8 @@ class TestMain { new TestUnit("waitForMsg", testCtx, ctx -> { String msg = null; try { - msg = ctx.qm.waitForMsg(10); - } catch(Exception e) { + msg = ctx.qm.receiveMessage(10); + } catch (Exception e) { throw new RuntimeException(e.toString()); } log("RX MSG: " + msg);