Browse Source

Before remove FsMQManager.getIdentityAddresses() / not needed / identityExisting() is sufficient.

JNI-96
heck 5 years ago
parent
commit
d480c35514
  1. 12
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java
  2. 97
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  3. 95
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

12
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() { public String getqDir() {
return qDir; return qDir;
} }
public void setqDir(String qDir) {
this.qDir = qDir;
}
public String toString(){
String ret = "";
ret += "Address: '" + address + "'\n";
ret += "qDir : '" + qDir + "'";
return ret;
}
} }

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

@ -30,36 +30,43 @@ public class FsMQManager {
// - true for added // - true for added
// - false for updated // - false for updated
public boolean addOrUpdateIdentity(FsMQIdentity ident) { public boolean addOrUpdateIdentity(FsMQIdentity ident) {
try { boolean ret = false;
getIdentityForAddress(ident.getAddress()); if(!identityExists(ident.getAddress())) {
} catch (UnknownIdentityException e) {
// Good, add new ident // Good, add new ident
addIdent(ident); addIdent(ident);
return true; ret = true;
} } else{
// Ok, update ident // Ok, update ident
removeIdent(ident); removeIdentity(ident.getAddress());
addIdent(ident); addIdent(ident);
return false; ret = false;
}
return ret;
} }
public void sendMsgToIdentity(FsMQIdentity ident, String msg) throws UnknownIdentityException, IOException { // 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 sendMessage(String address, String msg) throws UnknownIdentityException, IOException {
FsMQMessage mqMsg = new FsMQMessage(self, msg); FsMQMessage mqMsg = new FsMQMessage(self, msg);
String serializedStr = mqMsg.serialize(); String serializedStr = mqMsg.serialize();
getQueueForIdentity(ident).add(serializedStr); getQueueForIdentity(address).add(serializedStr);
} }
public void clearOwnQueue() { public void clearOwnQueue() {
getQueueForIdentity(self).clear(); getQueueForIdentity(self.getAddress()).clear();
} }
public String waitForMsg() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException { public String receiveMessage() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException {
return waitForMsg(0); 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; String ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self); FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress());
FsMQMessage mqMsg = null; FsMQMessage mqMsg = null;
int pollInterval = 100; int pollInterval = 100;
int pollRepeats = timeoutSec * 1000 / pollInterval; int pollRepeats = timeoutSec * 1000 / pollInterval;
@ -79,42 +86,60 @@ public class FsMQManager {
return ret; return ret;
} }
// True if existing
// False if not
// Exception on not unique
public boolean identityExists(String address) {
boolean ret = false;
List<FsMQIdentity> 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<FsMQIdentity> getIdentities() {
return new ArrayList<FsMQIdentity>(identities);
}
public List<String> getIdentityAddresses() {
List <String> ret = new ArrayList<>();
for(FsMQIdentity i : identities) {
ret.add(i.getAddress());
}
return ret;
}
// undefined behaviour if already existing // undefined behaviour if already existing
private void addIdent(FsMQIdentity ident) { private void addIdent(FsMQIdentity ident) {
identities.add(ident); identities.add(ident);
createQueueForIdent(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) { private void createQueueForIdent(FsMQIdentity ident) {
FsMsgQueue q = new FsMsgQueue(ident.getqDir()); FsMsgQueue q = new FsMsgQueue(ident.getqDir());
identityAddressQueues.put(ident.getAddress(), q); identityAddressQueues.put(ident.getAddress(), q);
} }
private FsMsgQueue getQueueForIdentity(FsMQIdentity ident) throws UnknownIdentityException { private FsMsgQueue getQueueForIdentity(String address) throws UnknownIdentityException {
FsMsgQueue ret = null; FsMsgQueue ret = null;
ret = identityAddressQueues.get(ident.getAddress()); ret = identityAddressQueues.get(address);
if (ret == null) { if (ret == null) {
throw new UnknownIdentityException("Unknown identity address: " + ident.getAddress()); throw new UnknownIdentityException("Unknown identity address: " + address);
}
return ret;
}
public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException {
FsMQIdentity ret = null;
List<FsMQIdentity> 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);
} }
ret = matches.get(0);
return ret; return ret;
} }

95
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.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@ -14,7 +15,10 @@ class FsMQManagerTestContext extends AbstractTestContext {
String ownAddress = "Alice"; String ownAddress = "Alice";
String ownQDir = "../resources/fsmsgqueue-test/alice"; String ownQDir = "../resources/fsmsgqueue-test/alice";
String bobAddress = "Bob"; String bobAddress = "Bob";
String bobQDirWrong = "../resources/fsmsgqueue-test/Wr0ngD1r3ct0ry";
String bobQDir = "../resources/fsmsgqueue-test/bob"; String bobQDir = "../resources/fsmsgqueue-test/bob";
String carolAddress = "Carol";
String carolQDir = "../resources/fsmsgqueue-test/carol";
int msgCount = 10; int msgCount = 10;
ArrayList<String> messages; ArrayList<String> messages;
@ -22,6 +26,7 @@ class FsMQManagerTestContext extends AbstractTestContext {
FsMQManager qm; FsMQManager qm;
FsMQIdentity self = null; FsMQIdentity self = null;
FsMQIdentity bob = null; FsMQIdentity bob = null;
FsMQIdentity carol = null;
@Override @Override
public void init() throws Throwable { public void init() throws Throwable {
@ -58,6 +63,18 @@ class TestMain {
assert ctx.qm != null : "null"; assert ctx.qm != null : "null";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("identExists" + testCtx.ownAddress, testCtx, ctx -> {
assert ctx.qm.identityExists(ctx.ownAddress);
}).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>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress); FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress);
assert self != null : "null"; assert self != null : "null";
@ -67,19 +84,15 @@ class TestMain {
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("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 != null : "null";
assert ctx.bob.getAddress().equals(ctx.bobAddress) : "Address mismatch"; 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(); }).add();
new TestUnit<FsMQManagerTestContext>("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> {
try { FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress);
FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.bobAddress); assert bob == null : "Ident is known but shouldnt";
} catch (UnknownIdentityException e) {
return;
}
assert false : "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 -> {
@ -91,10 +104,70 @@ class TestMain {
assert bob.equals(ctx.bob) : "Obj mismatch"; assert bob.equals(ctx.bob) : "Obj mismatch";
}).add(); }).add();
new TestUnit<FsMQManagerTestContext>("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<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>("getIdents is copy", testCtx, ctx -> {
List<FsMQIdentity> 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<FsMQManagerTestContext>("Update ident " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("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"; assert !ctx.qm.addOrUpdateIdentity(ctx.bob) : "Ident got added but should have been updated";
}).add(); }).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>("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<FsMQManagerTestContext>("getIdentities", testCtx, ctx -> {
List<FsMQIdentity> idents = ctx.qm.getIdentities();
for (FsMQIdentity i : idents) {
log(i.toString());
}
assert idents.size() == 2 : "identity count wrong";
}).add();
// new TestUnit<FsMQManagerTestContext>("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<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {
ctx.qm.clearOwnQueue(); ctx.qm.clearOwnQueue();
}).add(); }).add();
@ -102,7 +175,7 @@ class TestMain {
new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> {
log("waitForMessage with timeout..."); log("waitForMessage with timeout...");
try { try {
ctx.qm.waitForMsg(3); ctx.qm.receiveMessage(3);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.toString()); throw new RuntimeException(e.toString());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -116,7 +189,7 @@ class TestMain {
String msg = ctx.messages.get(0); String msg = ctx.messages.get(0);
log("TX MSG: " + msg); log("TX MSG: " + msg);
try { try {
ctx.qm.sendMsgToIdentity(ctx.self, msg); ctx.qm.sendMessage(ctx.self.getAddress(), msg);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.toString()); throw new RuntimeException(e.toString());
} }
@ -125,7 +198,7 @@ class TestMain {
new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> { new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> {
String msg = null; String msg = null;
try { try {
msg = ctx.qm.waitForMsg(10); msg = ctx.qm.receiveMessage(10);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e.toString()); throw new RuntimeException(e.toString());
} }

Loading…
Cancel
Save