55import org .junit .Test ;
66
77import java .io .IOException ;
8+ import java .util .List ;
89
910public class MultihopTests {
1011 /**
@@ -92,7 +93,83 @@ public void twoHops() throws IOException, ASAPException, InterruptedException {
9293 Assert .assertTrue (same );
9394 }
9495
95- @ Test
96+ //@Test
97+ public void createNonOpenStorage () throws IOException , ASAPException , InterruptedException {
98+ CmdLineUI ui = new CmdLineUI (System .out );
99+
100+ ui .doResetASAPStorages ();
101+
102+ // Alice creates storage with three recipients
103+ ui .doCreateASAPStorage ("Alice nonOpen" );
104+ ui .doCreateASAPStorage ("Bob nonOpen" );
105+ ui .doCreateASAPStorage ("Clara nonOpen" );
106+
107+ ui .doAddRecipient ("Alice:nonOpen Bob" );
108+ ui .doAddRecipient ("Alice:nonOpen Clara" );
109+ ui .doAddRecipient ("Alice:nonOpen David" );
110+
111+ // message shall reach Bob and Clara but not David
112+ ui .doAddRecipient ("Bob:nonOpen Clara" );
113+
114+ ui .doSetSendReceivedMessage ("Alice:nonOpen on" );
115+ ui .doSetSendReceivedMessage ("Bob:nonOpen on" );
116+
117+ // add message to alice storage
118+ String messageAlice2Clara = "HiClara" ;
119+ String parameters = "Alice nonOpen abcChat " + messageAlice2Clara ;
120+ ui .doCreateASAPMessage (parameters );
121+
122+ // message is no in Alice storage
123+
124+ System .out .println ("**************************************************************************" );
125+ System .out .println ("** connect Alice with Bob **" );
126+ System .out .println ("**************************************************************************" );
127+ // connect alice with bob
128+ ui .doCreateASAPMultiEngine ("Alice" );
129+ ui .doOpen ("7070 Alice" );
130+ // wait a moment to give server socket time to be created
131+ Thread .sleep (10 );
132+ ui .doCreateASAPMultiEngine ("Bob" );
133+
134+ ui .doConnect ("7070 Bob" );
135+
136+ // alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
137+
138+ // wait a moment
139+ Thread .sleep (1000 );
140+
141+ // kill connections
142+ ui .doKill ("all" );
143+
144+ // alice should stay in era 1 (no content change), bob should be in era 1 received something
145+
146+ // wait a moment
147+ Thread .sleep (1000 );
148+
149+ // Bob should now have created an closed asap storage with three recipients
150+ ASAPStorage bobStorage = this .getFreshStorageByName (ui , "Bob:nonOpen" );
151+
152+ CharSequence uri = bobStorage .getChannelURIs ().get (0 );
153+ List <CharSequence > recipientsList = bobStorage .getRecipients ("nonOpen" );
154+ boolean aliceFound = false ;
155+ boolean bobFound = false ;
156+ boolean claraFound = false ;
157+ boolean davidFound = false ;
158+ for (CharSequence recipient : recipientsList ) {
159+ String recipientString = recipient .toString ();
160+ switch (recipient .toString ()) {
161+ case "Alice" : aliceFound = true ; break ;
162+ case "Bob" : bobFound = true ; break ;
163+ case "Clara" : claraFound = true ; break ;
164+ case "David" : davidFound = true ; break ;
165+ default : Assert .fail ("found unexpected recipient: " + recipient );
166+ }
167+ }
168+
169+ Assert .assertTrue (aliceFound && bobFound && claraFound && davidFound );
170+ }
171+
172+ //@Test
96173 public void twoHopsNonOpenStorage () throws IOException , ASAPException , InterruptedException {
97174 CmdLineUI ui = new CmdLineUI (System .out );
98175
@@ -102,15 +179,17 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
102179 ui .doCreateASAPStorage ("Alice twoHops" );
103180 ui .doCreateASAPStorage ("Bob twoHops" );
104181 ui .doCreateASAPStorage ("Clara twoHops" );
182+ ui .doCreateASAPStorage ("David twoHops" );
105183
106184 ui .doAddRecipient ("Alice:twoHops Bob" );
107185 ui .doAddRecipient ("Alice:twoHops Clara" );
186+ ui .doAddRecipient ("Alice:twoHops David" );
108187
109- Assert .fail ("test case implementation not yet finished" );
188+ // message shall reach Bob and Clara but not David
189+ ui .doAddRecipient ("Bob:twoHops Clara" );
110190
111191 ui .doSetSendReceivedMessage ("Alice:twoHops on" );
112192 ui .doSetSendReceivedMessage ("Bob:twoHops on" );
113- ui .doSetSendReceivedMessage ("Clara:twoHops on" );
114193
115194 // add message to alice storage
116195 String messageAlice2Clara = "HiClara" ;
@@ -142,6 +221,31 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
142221 // wait a moment
143222 Thread .sleep (1000 );
144223
224+ System .out .println ("**************************************************************************" );
225+ System .out .println ("** connect Bob with David **" );
226+ System .out .println ("**************************************************************************" );
227+ // connect alice with bob
228+ ui .doCreateASAPMultiEngine ("Bob" );
229+ ui .doOpen ("7070 Bob" );
230+ // wait a moment to give server socket time to be created
231+ Thread .sleep (10 );
232+ ui .doCreateASAPMultiEngine ("David" );
233+
234+ ui .doConnect ("7070 David" );
235+
236+ // alice should be in era 1 (content has changed before connection) and bob era is 0 - no changes
237+
238+ // wait a moment
239+ Thread .sleep (1000 );
240+
241+ // kill connections
242+ ui .doKill ("all" );
243+
244+ // alice should stay in era 1 (no content change), bob should be in era 1 received something
245+
246+ // wait a moment
247+ Thread .sleep (1000 );
248+
145249 System .out .println ("**************************************************************************" );
146250 System .out .println ("** connect Bob with Clara **" );
147251 System .out .println ("**************************************************************************" );
@@ -159,8 +263,7 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
159263 ui .doKill ("all" );
160264
161265 // get Clara storage
162- String rootFolder = ui .getEngineRootFolderByStorageName ("Clara:twoHops" );
163- ASAPStorage clara = ASAPEngineFS .getExistingASAPEngineFS (rootFolder );
266+ ASAPStorage clara = this .getFreshStorageByName (ui , "Clara:twoHops" );
164267
165268 /* message was actually from Bob but originated from Alice. It is put
166269 into a incoming folder as it would have been directly received from Alice.
@@ -175,4 +278,9 @@ public void twoHopsNonOpenStorage() throws IOException, ASAPException, Interrupt
175278 boolean same = messageAlice2Clara .equalsIgnoreCase (message .toString ());
176279 Assert .assertTrue (same );
177280 }
281+
282+ private ASAPStorage getFreshStorageByName (CmdLineUI ui , String storageName ) throws ASAPException , IOException {
283+ String rootFolder = ui .getEngineRootFolderByStorageName (storageName );
284+ return ASAPEngineFS .getExistingASAPEngineFS (rootFolder );
285+ }
178286}
0 commit comments