|
| 1 | +package net.sharksystem.asap.mock; |
| 2 | + |
| 3 | +import net.sharksystem.asap.ASAPException; |
| 4 | +import net.sharksystem.asap.apps.ASAPPeerServices; |
| 5 | +import net.sharksystem.asap.apps.mock.ASAPPeerMock; |
| 6 | +import net.sharksystem.asap.apps.mock.ASAPSimplePeer; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.*; |
| 10 | + |
| 11 | +import static net.sharksystem.asap.mock.TestUtils.*; |
| 12 | + |
| 13 | +/** |
| 14 | + * An ASAP app communicates by sending messages. First, you must ensure stability in your application. |
| 15 | + * Implement methods that serialized and deserialize messages. Implement a listener. Test your app |
| 16 | + * by testing scenarios. This class comprises a scenario in two steps. |
| 17 | + * |
| 18 | + * One tests uses an asap mock. It simulates a message exchange but does not use ASAP at all. |
| 19 | + * |
| 20 | + * The asapTestExamples is nearly identical with one important difference: The ASAP engines are used. |
| 21 | + * |
| 22 | + * Note: The test scenarios do not differ at all. Your application and test logic is written once and is tested |
| 23 | + * against a mock and later against ASAP. Same interfaces are also available in Android. YOu can spent some |
| 24 | + * times by implementing test scenarios. Makes app coding on your target platform much faster. |
| 25 | + * |
| 26 | + * Test your app first with the mock and afterwards with the ASAP protocol stack. If anything runs smoothly - |
| 27 | + * you will have a stable Android or Java app in no time. |
| 28 | + */ |
| 29 | +public class UseThisAsTemplate4YourAppTests { |
| 30 | + private static final int PORT = 7777; |
| 31 | + |
| 32 | + private static int port = 0; |
| 33 | + static int getPortNumber() { |
| 34 | + if(UseThisAsTemplate4YourAppTests.port == 0) { |
| 35 | + UseThisAsTemplate4YourAppTests.port = PORT; |
| 36 | + } else { |
| 37 | + UseThisAsTemplate4YourAppTests.port++; |
| 38 | + } |
| 39 | + |
| 40 | + return UseThisAsTemplate4YourAppTests.port; |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void mockTestExample() throws IOException, ASAPException, InterruptedException { |
| 45 | + ///////////////// ALICE ////////////////////////////////////////////////////////////// |
| 46 | + // setup mocked peer / asap application and activity in android |
| 47 | + ASAPPeerMock aliceMockPeer = new ASAPPeerMock(ALICE); |
| 48 | + ASAPPeerMock bobMockPeer = new ASAPPeerMock(BOB); |
| 49 | + |
| 50 | + // 1st encounter |
| 51 | + this.scenarioPart1(aliceMockPeer, bobMockPeer); |
| 52 | + |
| 53 | + aliceMockPeer.startEncounter(bobMockPeer); |
| 54 | + // give your app a moment to process |
| 55 | + Thread.sleep(1000); |
| 56 | + // stop encounter |
| 57 | + bobMockPeer.stopEncounter(aliceMockPeer); |
| 58 | + // give your app a moment to process |
| 59 | + Thread.sleep(1000); |
| 60 | + |
| 61 | + // 2nd encounter |
| 62 | + this.scenarioPart2(aliceMockPeer, bobMockPeer); |
| 63 | + |
| 64 | + bobMockPeer.startEncounter(bobMockPeer); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void asapTestExample() throws IOException, ASAPException, InterruptedException { |
| 69 | + ///////////////// ALICE ////////////////////////////////////////////////////////////// |
| 70 | + // setup mocked peer / asap application and activity in android |
| 71 | + ASAPSimplePeer aliceSimplePeer = new ASAPSimplePeer(ALICE); |
| 72 | + ASAPSimplePeer bobSimplePeer = new ASAPSimplePeer(BOB); |
| 73 | + |
| 74 | + // 1st encounter |
| 75 | + this.scenarioPart1(aliceSimplePeer, bobSimplePeer); |
| 76 | + |
| 77 | + aliceSimplePeer.startEncounter(getPortNumber(), bobSimplePeer); |
| 78 | + // give your app a moment to process |
| 79 | + Thread.sleep(1000); |
| 80 | + // stop encounter |
| 81 | + bobSimplePeer.stopEncounter(aliceSimplePeer); |
| 82 | + // give your app a moment to process |
| 83 | + Thread.sleep(1000); |
| 84 | + |
| 85 | + // 2nd encounter |
| 86 | + this.scenarioPart2(aliceSimplePeer, bobSimplePeer); |
| 87 | + |
| 88 | + aliceSimplePeer.startEncounter(getPortNumber(), bobSimplePeer); |
| 89 | + } |
| 90 | + |
| 91 | + public void scenarioPart1(ASAPPeerServices alicePeer, ASAPPeerServices bobPeer) |
| 92 | + throws IOException, ASAPException, InterruptedException { |
| 93 | + // simulate ASAP first encounter with full ASAP protocol stack and engines |
| 94 | + System.out.println("+++++++++++++++++++ 1st encounter starts soon ++++++++++++++++++++"); |
| 95 | + Thread.sleep(50); |
| 96 | + |
| 97 | + // setup message received listener - this should be replaced with your code - you implement a listener. |
| 98 | + ASAPMessageReceivedListenerExample aliceMessageReceivedListenerExample = |
| 99 | + new ASAPMessageReceivedListenerExample(); |
| 100 | + |
| 101 | + alicePeer.addASAPMessageReceivedListener(YOUR_APP_NAME, aliceMessageReceivedListenerExample); |
| 102 | + |
| 103 | + // example - this should be produced by your application |
| 104 | + byte[] serializedData = TestUtils.serializeExample(42, "from alice", true); |
| 105 | + |
| 106 | + alicePeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, serializedData); |
| 107 | + |
| 108 | + ///////////////// BOB ////////////////////////////////////////////////////////////// |
| 109 | + |
| 110 | + // this should be replaced with your code - you must implement a listener. |
| 111 | + ASAPMessageReceivedListenerExample asapMessageReceivedListenerExample = |
| 112 | + new ASAPMessageReceivedListenerExample(); |
| 113 | + |
| 114 | + // register your listener (or that mock) with asap connection mock |
| 115 | + bobPeer.addASAPMessageReceivedListener(YOUR_APP_NAME, asapMessageReceivedListenerExample); |
| 116 | + |
| 117 | + // bob writes something |
| 118 | + bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, |
| 119 | + TestUtils.serializeExample(43, "from bob", false)); |
| 120 | + bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, |
| 121 | + TestUtils.serializeExample(44, "from bob again", false)); |
| 122 | + |
| 123 | + |
| 124 | + // give your app a moment to process |
| 125 | + Thread.sleep(500); |
| 126 | + } |
| 127 | + |
| 128 | + public void scenarioPart2(ASAPPeerServices alicePeer, ASAPPeerServices bobPeer) |
| 129 | + throws IOException, ASAPException, InterruptedException { |
| 130 | + |
| 131 | + // bob writes something |
| 132 | + bobPeer.sendASAPMessage(YOUR_APP_NAME, YOUR_URI, |
| 133 | + TestUtils.serializeExample(43, "third message from bob", false)); |
| 134 | + |
| 135 | + // simulate second encounter |
| 136 | + System.out.println("+++++++++++++++++++ 2nd encounter starts soon ++++++++++++++++++++"); |
| 137 | + Thread.sleep(50); |
| 138 | + } |
| 139 | + |
| 140 | + public void testScenarioResults() { |
| 141 | + // TODO |
| 142 | + } |
| 143 | +} |
0 commit comments