Skip to content

Commit 8fe4339

Browse files
committed
asap session mocking implemented
1 parent 52d836d commit 8fe4339

File tree

7 files changed

+332
-27
lines changed

7 files changed

+332
-27
lines changed

src/net/sharksystem/asap/ASAPChunkFS.java

-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.sharksystem.asap;
22

33
import net.sharksystem.asap.util.Helper;
4-
import net.sharksystem.asap.util.Log;
54

65
import java.io.*;
76
import java.util.*;
@@ -421,29 +420,4 @@ public int getEra() throws IOException {
421420
return this.era;
422421
}
423422

424-
private class MessageIter implements Iterator {
425-
private final List<byte[]> byteMessages;
426-
private int nextIndex;
427-
private String nextString;
428-
429-
430-
MessageIter(List<byte[]> byteMessages) throws FileNotFoundException {
431-
this.byteMessages = byteMessages;
432-
this.nextIndex = 0;
433-
}
434-
435-
@Override
436-
public boolean hasNext() {
437-
return this.byteMessages.size() > nextIndex;
438-
}
439-
440-
@Override
441-
public String next() {
442-
if(!this.hasNext()) {
443-
throw new NoSuchElementException("no more messages");
444-
}
445-
446-
return new String(this.byteMessages.get(nextIndex++));
447-
}
448-
}
449423
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.sharksystem.asap;
2+
3+
import java.io.FileNotFoundException;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.NoSuchElementException;
7+
8+
public class MessageIter implements Iterator {
9+
private final List<byte[]> byteMessages;
10+
private int nextIndex;
11+
private String nextString;
12+
13+
14+
public MessageIter(List<byte[]> byteMessages) throws FileNotFoundException {
15+
this.byteMessages = byteMessages;
16+
this.nextIndex = 0;
17+
}
18+
19+
@Override
20+
public boolean hasNext() {
21+
return this.byteMessages.size() > nextIndex;
22+
}
23+
24+
@Override
25+
public String next() {
26+
if (!this.hasNext()) {
27+
throw new NoSuchElementException("no more messages");
28+
}
29+
30+
return new String(this.byteMessages.get(nextIndex++));
31+
}
32+
}

src/net/sharksystem/asap/apps/ASAPMessageReceivedListener.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import net.sharksystem.asap.ASAPMessages;
44

5+
import java.io.IOException;
6+
57
public interface ASAPMessageReceivedListener {
6-
void asapMessagesReceived(ASAPMessages messages);
8+
void asapMessagesReceived(ASAPMessages messages) throws IOException;
79
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.sharksystem.asap.apps;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
5+
public interface ASAPMessageSender {
6+
/**
7+
* Send a message
8+
* @param appName
9+
* @param uri
10+
* @param message
11+
* @throws ASAPException
12+
*/
13+
void sendASAPMessage(CharSequence appName, CharSequence uri,
14+
byte[] message) throws ASAPException;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.sharksystem.asap.apps.mock;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPMessages;
5+
import net.sharksystem.asap.MessageIter;
6+
7+
import java.io.IOException;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
11+
class ASAPMessagesMock implements ASAPMessages {
12+
private final CharSequence appName;
13+
private final CharSequence uri;
14+
private final List<byte[]> serializedAppPDUs;
15+
16+
ASAPMessagesMock(CharSequence appName, CharSequence uri, List<byte[]> serializedAppPDUs) {
17+
this.appName = appName;
18+
this.uri = uri;
19+
this.serializedAppPDUs = serializedAppPDUs;
20+
}
21+
22+
@Override
23+
public int size() throws IOException {
24+
return this.serializedAppPDUs.size();
25+
}
26+
27+
@Override
28+
public CharSequence getURI() {
29+
return this.uri;
30+
}
31+
32+
@Override
33+
public CharSequence getFormat() {
34+
return this.appName;
35+
}
36+
37+
@Override
38+
public Iterator<CharSequence> getMessagesAsCharSequence() throws IOException {
39+
return new MessageIter(this.serializedAppPDUs);
40+
}
41+
42+
@Override
43+
public Iterator<byte[]> getMessages() throws IOException {
44+
return this.serializedAppPDUs.iterator();
45+
}
46+
47+
@Override
48+
public CharSequence getMessageAsCharSequence(int position, boolean chronologically) throws ASAPException, IOException {
49+
throw new ASAPException("not implemented in mocking class");
50+
}
51+
52+
@Override
53+
public byte[] getMessage(int position, boolean chronologically) throws ASAPException, IOException {
54+
throw new ASAPException("not implemented in mocking class");
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package net.sharksystem.asap.apps.mock;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPMessages;
5+
import net.sharksystem.asap.apps.ASAPMessageReceivedListener;
6+
import net.sharksystem.asap.apps.ASAPMessageSender;
7+
8+
import java.io.IOException;
9+
import java.util.*;
10+
11+
public class ASAPSessionMock implements ASAPMessageSender {
12+
private Map<CharSequence, Map<CharSequence, List<byte[]>>> appMsgStorage = new HashMap<>();
13+
private Map<CharSequence, List<ASAPMessageReceivedListener>> listenerMap = new HashMap<>();
14+
private boolean connected = false;
15+
16+
private List<byte[]> getStorage(CharSequence appName, CharSequence uri) {
17+
Map<CharSequence, List<byte[]>> charSequenceListMap = this.appMsgStorage.get(appName);
18+
if (charSequenceListMap == null) {
19+
charSequenceListMap = new HashMap<>();
20+
this.appMsgStorage.put(appName, charSequenceListMap);
21+
}
22+
23+
List<byte[]> byteMessageList = charSequenceListMap.get(uri);
24+
if (byteMessageList == null) {
25+
byteMessageList = new ArrayList<>();
26+
charSequenceListMap.put(uri, byteMessageList);
27+
}
28+
29+
return byteMessageList;
30+
}
31+
32+
@Override
33+
public void sendASAPMessage(CharSequence appName, CharSequence uri, byte[] message) throws ASAPException {
34+
synchronized(this.appMsgStorage) {
35+
List<byte[]> storage = this.getStorage(appName, uri);
36+
storage.add(message);
37+
}
38+
39+
if(this.connected) this.notifyListeners();
40+
}
41+
42+
public void addASAPMessageReceivedListener(CharSequence format, ASAPMessageReceivedListener listener) {
43+
List<ASAPMessageReceivedListener> asapMessageReceivedListeners = this.listenerMap.get(format);
44+
if(asapMessageReceivedListeners == null) {
45+
asapMessageReceivedListeners = new ArrayList<>();
46+
this.listenerMap.put(format, asapMessageReceivedListeners);
47+
}
48+
49+
asapMessageReceivedListeners.add(listener);
50+
}
51+
52+
/**
53+
* Simulate a connection - this mock will notify all registered listeners and remove messages.
54+
* After connected - any sendASAPMessage call will immediately lead to a listener call.
55+
*/
56+
public void connect() {
57+
this.connected = true;
58+
this.notifyListeners();
59+
}
60+
61+
private void notifyListeners() {
62+
Map<CharSequence, Map<CharSequence, List<byte[]>>> appUriMessages = null;
63+
synchronized(this.appMsgStorage) {
64+
if(this.appMsgStorage.isEmpty()) return;
65+
66+
// else copy
67+
appUriMessages = this.appMsgStorage;
68+
69+
// create empty
70+
this.appMsgStorage = new HashMap<>();
71+
}
72+
73+
// send
74+
for(CharSequence appName : appUriMessages.keySet()) {
75+
Map<CharSequence, List<byte[]>> appMap = appUriMessages.get(appName);
76+
if(appMap != null) {
77+
List<ASAPMessageReceivedListener> asapMessageReceivedListeners = this.listenerMap.get(appName);
78+
if(asapMessageReceivedListeners != null && !asapMessageReceivedListeners.isEmpty()) {
79+
Set<CharSequence> uris = appMap.keySet();
80+
for(CharSequence uri : uris) {
81+
List<byte[]> serializedAppPDUs = appMap.get(uri);
82+
for(ASAPMessageReceivedListener listener : asapMessageReceivedListeners) {
83+
// create a thread for each listener
84+
new Thread(new Runnable() {
85+
@Override
86+
public void run() {
87+
ASAPMessages messagesMock = new ASAPMessagesMock(appName, uri, serializedAppPDUs);
88+
try {
89+
listener.asapMessagesReceived(messagesMock);
90+
} catch (IOException e) {
91+
e.printStackTrace();
92+
}
93+
}
94+
}).start();
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
/**
103+
* disconnect - opposite of connect
104+
*/
105+
public void disconnect() {
106+
this.connected = false;
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package net.sharksystem.asap.mock;
2+
3+
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPMessages;
5+
import net.sharksystem.asap.apps.ASAPMessageSender;
6+
import net.sharksystem.asap.apps.ASAPMessageReceivedListener;
7+
import net.sharksystem.asap.apps.mock.ASAPSessionMock;
8+
import org.junit.Test;
9+
10+
import java.io.*;
11+
import java.util.Iterator;
12+
13+
/**
14+
* How to mock ASAP communication
15+
*/
16+
public class ASAPMockUsage {
17+
18+
private static final CharSequence YOUR_APP_NAME = "yourAppName";
19+
private static final CharSequence YOUR_URI = "yourSchema://example";
20+
21+
/**
22+
* a serialization example
23+
* @param exampleLong
24+
* @param exampleString
25+
* @param exampleBoolean
26+
* @return
27+
*/
28+
private static byte[] serializeExample(long exampleLong, String exampleString, boolean exampleBoolean) throws IOException {
29+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
30+
DataOutputStream daos = new DataOutputStream(baos);
31+
32+
// serialize
33+
daos.writeLong(exampleLong);
34+
daos.writeUTF(exampleString);
35+
daos.writeBoolean(exampleBoolean);
36+
37+
return baos.toByteArray();
38+
}
39+
40+
/**
41+
* a deserialization example
42+
*/
43+
private static void deserializeExample(byte[] serializedData) throws IOException {
44+
ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
45+
DataInputStream dais = new DataInputStream(bais);
46+
47+
// deserialize
48+
long exampleLong = dais.readLong();
49+
String exampleString = dais.readUTF();
50+
boolean exampleBoolean = dais.readBoolean();
51+
52+
// call a methode in your app
53+
54+
// here: just print
55+
System.out.println("received: " + exampleLong + " | " + exampleString + " | " + exampleBoolean);
56+
}
57+
58+
@Test
59+
public void usageTest1() throws IOException, ASAPException, InterruptedException {
60+
/* Imagine we are here inside your application code. Data are to be transmitted. You implemented
61+
a methode that serializes your data (PDU) into an array of bytes
62+
*/
63+
64+
// example - this should be produced by your application
65+
byte[] serializedData = ASAPMockUsage.serializeExample(42, "don't panic", true);
66+
67+
// now: ASAP is used to deliver those data - we mock it
68+
ASAPSessionMock asapSessionMock = new ASAPSessionMock();
69+
70+
ASAPMessageSender asapMessageSender = asapSessionMock;
71+
72+
asapMessageSender.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, serializedData);
73+
74+
// we simulated a sender - now, we need to simulate recipient
75+
76+
// this should be replaced with your code - you must implement a listener.
77+
ASAPMessageReceivedListenerExample asapMessageReceivedListenerExample =
78+
new ASAPMessageReceivedListenerExample();
79+
80+
// register your listener (or that mock) with asap connection mock
81+
asapSessionMock.addASAPMessageReceivedListener(YOUR_APP_NAME, asapMessageReceivedListenerExample);
82+
83+
// simulate ASAP encounter
84+
asapSessionMock.connect();
85+
86+
// give your app a moment to process
87+
Thread.sleep(1000);
88+
89+
// add another message while still connected
90+
asapMessageSender.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
91+
ASAPMockUsage.serializeExample(43, "second message", false));
92+
93+
asapSessionMock.disconnect();
94+
System.out.println("send message without connection");
95+
asapMessageSender.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
96+
ASAPMockUsage.serializeExample(44, "third message", false));
97+
asapMessageSender.sendASAPMessage(YOUR_APP_NAME, YOUR_URI,
98+
ASAPMockUsage.serializeExample(45, "forth message", false));
99+
Thread.sleep(1000);
100+
101+
System.out.println("re-connect");
102+
asapSessionMock.connect();
103+
Thread.sleep(1000);
104+
}
105+
106+
private class ASAPMessageReceivedListenerExample implements ASAPMessageReceivedListener {
107+
@Override
108+
public void asapMessagesReceived(ASAPMessages messages) throws IOException {
109+
CharSequence format = messages.getFormat();
110+
CharSequence uri = messages.getURI();
111+
System.out.println("asap message received (" + format + " | " + uri + "). size == " + messages.size());
112+
Iterator<byte[]> yourPDUIter = messages.getMessages();
113+
while (yourPDUIter.hasNext()) {
114+
ASAPMockUsage.deserializeExample(yourPDUIter.next());
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)