|
| 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