source: tmcsimulator/trunk/test/tmcsim/cadsimulator/CADSimulatorConsoleTest.java @ 228

Revision 228, 21.0 KB checked in by jdalbey, 8 years ago (diff)

System Tests updated to work with new integrated Traffic Mgr. All system tests now passing.

Line 
1package tmcsim.cadsimulator;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.io.PrintWriter;
7import java.io.StringWriter;
8import java.util.logging.Level;
9import java.util.logging.Logger;
10import junit.framework.TestCase;
11import static org.mockito.Mockito.mock;
12import tmcsim.cadsimulator.viewer.CADConsoleViewer;
13import tmcsim.common.CADEnums;
14import tmcsim.common.ScriptException;
15import tmcsim.common.SimulationException;
16import tmcsim.interfaces.CADClientInterface;
17import tmcsim.interfaces.SimulationManagerInterface;
18
19/**
20 * Test of CADSimulator Console
21 *
22 * @author jdalbey
23 */
24public class CADSimulatorConsoleTest extends TestCase
25{
26
27    private static CADServer app = null;
28    private CADConsoleViewer console;
29    private StringWriter sw;
30
31    public CADSimulatorConsoleTest(String testName)
32    {
33        super(testName);
34    }
35
36    public void setUp() throws Exception
37    {
38        super.setUp();
39        writeConfigData();
40        // Since CADSimulator has registry, we musn't instantiate it more than once
41        if (app == null)
42        {
43            try
44            {
45                app = new CADServer("config.txt");
46            } catch (Exception ex)
47            {
48                fail("Couldn't launch CADSimulator");
49            }
50        }
51        console = (CADConsoleViewer) app.theViewer;
52        sw = new StringWriter();
53        console.setWriter(sw);
54    }
55
56    @Override
57    public void tearDown() throws java.io.IOException
58    {
59        File removeMe = new File("config.txt");
60        removeMe.delete();
61        removeMe = new File("pconfig.txt");
62        removeMe.delete();
63        removeMe = new File("empty.txt");
64        removeMe.delete();
65    }
66
67    /**
68     * compare StringWriter contents against expected
69     */
70    private void verify(String msg, String expect)
71    {
72        String result = sw.toString().trim();
73        result = result.replaceAll("\n", ",");
74        String fullExpect = expect.trim();
75        fullExpect = fullExpect.replaceAll("\n", ",");
76//        System.out.println(fullExpect);
77//        System.out.println(result.substring(result.length() - fullExpect.length(), result.length()));
78//        String diff = StringUtils.difference(fullExpect, result);
79        boolean match = result.endsWith(fullExpect);
80        assertTrue(msg + ": " + result, match);
81    }
82
83    public static void pause(int millis)
84    {
85        try
86        {
87            Thread.sleep(millis);
88        } catch (InterruptedException ex)
89        {
90        }
91    }
92
93    /**
94     * The tests must be all run in one method, because the order matters. As
95     * separate methods, we couldn't control which is executed first.
96     *
97     * @throws java.io.IOException
98     * @throws SimulationException
99     * @throws ScriptException
100     */
101    public void testAll() throws java.io.IOException, SimulationException, ScriptException
102    {
103        CADConsoleViewer console = (CADConsoleViewer) app.theViewer;
104        String expected1 =
105                "--- CAD Simulator ---\n"
106                + "Elapsed Simulation Time     : 0:00:00\n"
107                + "Status                      : No Script\n"
108                + "Connected CAD Terminals     : 0\n"
109                + "Simulation Manager Connected: No\n"
110                + "Connected to Paramics       : No\n"
111                + "Network Loaded              : None\n"
112                + "-- Info Messages --\n\n"
113                + "-- Error Messages --";
114        System.out.println("setVisible");
115        console.setVisible(true);
116        verify("Initial output incorrect: ", expected1);
117        String expected2 =
118                "--- CAD Simulator ---\n"
119                + "Elapsed Simulation Time     : 0:00:00\n"
120                + "Status                      : No Script\n"
121                + "Connected CAD Terminals     : 1\n"
122                + "Simulation Manager Connected: No\n"
123                + "Connected to Paramics       : No\n"
124                + "Network Loaded              : None\n"
125                + "-- Info Messages --\n\n"
126                + "-- Error Messages --\n\n";
127
128        System.out.println("connect one");
129        CADClientInterface ci = mock(CADClientInterface.class);
130        app.theCoordinator.registerForCallback(ci);
131        verify("connected 1 terminal output incorrect: ", expected2);
132        String expected3 =
133                "--- CAD Simulator ---\n"
134                + "Elapsed Simulation Time     : 0:00:00\n"
135                + "Status                      : No Script\n"
136                + "Connected CAD Terminals     : 2\n"
137                + "Simulation Manager Connected: No\n"
138                + "Connected to Paramics       : No\n"
139                + "Network Loaded              : None\n"
140                + "-- Info Messages --\n\n"
141                + "-- Error Messages --\n\n";
142        System.out.println("connect two");
143        app.theCoordinator.registerForCallback(ci);
144        verify("connected 2 terminals output incorrect: ", expected3);
145        System.out.println("disconnect");
146        app.theCoordinator.unregisterForCallback(ci);
147        verify("disconnect terminal output incorrect: ", expected2);
148        app.theCoordinator.unregisterForCallback(ci);
149        String expected4 =
150                "--- CAD Simulator ---\n"
151                + "Elapsed Simulation Time     : 0:00:00\n"
152                + "Status                      : No Script\n"
153                + "Connected CAD Terminals     : 0\n"
154                + "Simulation Manager Connected: Yes\n"
155                + "Connected to Paramics       : No\n"
156                + "Network Loaded              : None\n"
157                + "-- Info Messages --\n\n"
158                + "-- Error Messages --\n\n";
159        System.out.println("sim mgr connect");
160        SimulationManagerInterface si = mock(SimulationManagerInterface.class);
161        app.theCoordinator.registerForCallback(si);
162        verify("sim mgr connected output incorrect: ", expected4);
163        String expected5 =
164                "--- CAD Simulator ---\n"
165                + "Elapsed Simulation Time     : 0:00:00\n"
166                + "Status                      : No Script\n"
167                + "Connected CAD Terminals     : 0\n"
168                + "Simulation Manager Connected: Yes\n"
169                + "Connected to Paramics       : No\n"
170                + "Network Loaded              : None\n"
171                + "-- Info Messages --\n"
172                + ". = Console Info Message.\n"
173                + "-- Error Messages --\n\n";
174        System.out.println("Info msg");
175        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
176        cadSimLogger.logp(Level.INFO, "", "", "Console Info Message.");
177        verify("Info message output incorrect: ", expected5);
178        String expected7 =
179                "--- CAD Simulator ---\n"
180                + "Elapsed Simulation Time     : 0:00:00\n"
181                + "Status                      : No Script\n"
182                + "Connected CAD Terminals     : 0\n"
183                + "Simulation Manager Connected: Yes\n"
184                + "Connected to Paramics       : No\n"
185                + "Network Loaded              : None\n"
186                + "-- Info Messages --\n"
187                + ". = Console Info Message.\n"
188                + "-- Error Messages --\n"
189                + "Someclass.Somemethod = Something bad happened.\n";
190        System.out.println("Error msg");
191        cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
192        cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Something bad happened.");
193        verify("Error message output incorrect: ", expected7);
194        String expected6 =
195                "--- CAD Simulator ---\n"
196                + "Elapsed Simulation Time     : 0:00:00\n"
197                + "Status                      : No Script\n"
198                + "Connected CAD Terminals     : 0\n"
199                + "Simulation Manager Connected: Yes\n"
200                + "Connected to Paramics       : Yes\n"
201                + "Network Loaded              : None\n"
202                + "-- Info Messages --\n"
203                + ". = Console Info Message.\n"
204                + "-- Error Messages --\n"
205                + "Someclass.Somemethod = Something bad happened.\n";
206        System.out.println("Paramics connect");
207        app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
208        pause(500);
209        pause(500);
210        verify("paramics connected should be yes", expected6);
211        String expected8 =
212                "--- CAD Simulator ---\n"
213                + "Elapsed Simulation Time     : 0:00:00\n"
214                + "Status                      : Ready\n"
215                + "Connected CAD Terminals     : 0\n"
216                + "Simulation Manager Connected: Yes\n"
217                + "Connected to Paramics       : Yes\n"
218                + "Network Loaded              : None\n"
219                + "-- Info Messages --\n"
220                + ". = Console Info Message.\n"
221                + "-- Error Messages --\n"
222                + "Someclass.Somemethod = Something bad happened.\n";
223        String expected9 =
224                "--- CAD Simulator ---\n"
225                + "Elapsed Simulation Time     : 0:00:01\n"
226                + "Status                      : Running\n"
227                + "Connected CAD Terminals     : 0\n"
228                + "Simulation Manager Connected: Yes\n"
229                + "Connected to Paramics       : Yes\n"
230                + "Network Loaded              : None\n"
231                + "-- Info Messages --\n"
232                + ". = Console Info Message.\n"
233                + "-- Error Messages --\n"
234                + "Someclass.Somemethod = Something bad happened.\n";
235        System.out.println("Sim status");
236        // Load a script file - to put status at Ready
237        String autoloadScriptname = "scripts/one-incident.xml";
238        app.theCoordinator.loadScriptFile(new File(autoloadScriptname));
239        pause(500);
240        verify("Status should be 'ready'", expected8);
241        // startSimulation to put status at Running
242        app.theCoordinator.startSimulation();
243        pause(500);
244        verify("Status should be 'running', time should be 0:01", expected9);
245    }
246//    public void testNetworkID()
247//    {
248//        String expected10 =
249//                "--- CAD Simulator ---\n"
250//                + "Elapsed Simulation Time     : 0:00:00\n"
251//                + "Status                      : No Script\n"
252//                + "Connected CAD Terminals     : 0\n"
253//                + "Simulation Manager Connected: No\n"
254//                + "Connected to Paramics       : No\n"
255//                + "Network Loaded              : 17\n"
256//                + "-- Info Messages --\n\n"
257//                + "-- Error Messages --\n\n";
258//        // this will tell the model it has a new network ID
259//        cadmodel.setParamicsNetworkLoaded("17");
260//        cadmodel.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED);
261//        pause(500);
262//        verify("network id should be 17", expected10);
263//    }
264//    ByteArrayOutputStream bos;
265//    PrintStream ps;
266    static final String configData =
267            "CADClientPort          = 4444 \n"
268            + "CoordinatorRMIPort     = 4445 \n"
269            + "CADRmiPort             = 4446 \n"
270            + "UserInterface          = tmcsim.cadsimulator.viewer.CADConsoleViewer\n"
271            + "ParamicsProperties     = pconfig.txt\n"
272            + "ATMSProperties         = empty.txt\n"
273            + "TrafficMgrProperties   = tconfig.txt\n"
274            + "MediaProperties        = empty.txt\n";
275    static final String trafficMgrData = "Highways_Map_File = config/vds_data/highways_fullmap.txt\n"
276            +"Events_File = config/vds_data/atmsBatchEvents.txt\n"
277            +"FEPSim_IP_addr = localhost\n"
278            +"Output_Destination = Console\n";
279    static final String paramicsData = "ParamicsCommHost = 127.0.0.1\n"
280            + "ParamicsCommPort       = 4450\n"
281            + "IncidentUpdateInterval = 30\n"
282            + "IncidentUpdateFile     = exchange.xml\n"
283            + "ParamicsStatusInterval = 15\n"
284            + "ParamicsStatusFile     = paramics_status.xml\n"
285            + "CameraStatusInterval   = 30\n"
286            + "CameraStatusFile       = camera_status.xml\n";
287    static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7";
288    static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD";
289    /*
290     * Creating instance of app must be done only once or you get registry
291     * bind problems, and code Written in Constructor is Executed
292     * before each Test Method
293     */
294
295    private void writeConfigData()
296    {
297        // Declare a stream to the output
298//        bos = new ByteArrayOutputStream();
299//        ps = new PrintStream(bos);
300        // Redirect the standard output
301//        System.setOut(ps);
302        File cf = new File ("config.txt");
303        cf.delete();
304        writedata("config.txt", configData);
305        writedata("tconfig.txt", trafficMgrData);
306        writedata("pconfig.txt", paramicsData);
307        writedata("empty.txt", "");
308        writeScriptfiles();
309    }
310
311    private void writeScriptfiles()
312    {
313        new File("scripts").mkdir();
314        writedata("scripts/Cardfile.xml", cardfileData);
315        writedata("scripts/one-incident.xml", oneincidentXML);
316    }
317
318    // Write the test data to a file
319    private void writedata(String filename, String data)
320    {
321        File cardFile = new File(filename);
322        // If a cardfile exists, leave it
323        if (!cardFile.exists())
324        {
325            PrintWriter writer = null;
326            try
327            {
328                writer = new PrintWriter(new FileWriter(filename));
329                writer.println(data);
330                writer.close();
331            } catch (Exception ex)
332            {
333                ex.printStackTrace();
334            }
335        }
336    }
337    private static final String cardfileData =
338            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
339            + "<!--Please do not modify titles. Note that these titles are not"
340            + "sent into the actual screen. If titles are to be modified,"
341            + "they need to be changed in CardfileHandler.java  -->"
342            + ""
343            + ""
344            + ""
345            + "<CARDFILE> "
346            + " <TAB title = \"Coastal Division Units\">"
347            + "         <CARDFILE_OBJ name = \"Name\" >"
348            + "                 <ADDRESS>Address</ADDRESS>"
349            + "                 <CITY>City</CITY>"
350            + "                 <STATE>State</STATE>"
351            + "                 <ZIP>Zip</ZIP>"
352            + "                 <PHONE1>Phone1</PHONE1>"
353            + "                 <PHONE2>Phone2</PHONE2>"
354            + "                 <FAX>Fax</FAX>"
355            + "         </CARDFILE_OBJ>"
356            + "         "
357            + "         <CARDFILE_OBJ  name = \"Name2\" >"
358            + "                 <ADDRESS>Address2</ADDRESS>"
359            + "                 <CITY>City2</CITY>"
360            + "                 <STATE>State2</STATE>"
361            + "                 <ZIP>Zip2</ZIP>"
362            + "                 <PHONE1>Phone12</PHONE1>"
363            + "                 <PHONE2>Phone22</PHONE2>"
364            + "                 <FAX>Fax2</FAX>"
365            + "         </CARDFILE_OBJ>"
366            + "         "
367            + " </TAB>"
368            + " "
369            + " <TAB title = \"Police/Sheriff/Coroner\">"
370            + " "
371            + "         <CARDFILE_OBJ  name = \"Name\" >"
372            + "                 <ADDRESS>Address</ADDRESS>"
373            + "                 <CITY>City</CITY>"
374            + "                 <STATE>State</STATE>"
375            + "                 <ZIP>Zip</ZIP>"
376            + "                 <PHONE1>Phone1</PHONE1>"
377            + "                 <PHONE2>Phone2</PHONE2>"
378            + "                 <FAX>Fax</FAX>"
379            + "         </CARDFILE_OBJ>"
380            + "         "
381            + " </TAB>"
382            + " "
383            + " <TAB title = \"Courts\">"
384            + "         "
385            + "         "
386            + "         "
387            + " </TAB>"
388            + " "
389            + " <TAB title = \"Public Transportation\">"
390            + "         "
391            + "         "
392            + "         "
393            + " </TAB>"
394            + " "
395            + " <TAB title = \"GG Other\">"
396            + "         "
397            + "         "
398            + "         "
399            + " </TAB>"
400            + " "
401            + " <TAB title = \"MY Misc\">"
402            + "         "
403            + "         "
404            + "         "
405            + " </TAB>"
406            + " "
407            + " <TAB title = \"SL Misc\">"
408            + "         "
409            + "         "
410            + "         "
411            + " </TAB>"
412            + " "
413            + " <TAB title = \"VT Misc\">"
414            + "         "
415            + "         "
416            + "         "
417            + " </TAB>"
418            + " "
419            + " <TAB title = \"CHP Offices\">"
420            + "         "
421            + "         "
422            + "         "
423            + " </TAB>"
424            + " "
425            + " <TAB title = \"State Agencies/Facilities\">"
426            + "         "
427            + "         "
428            + "         "
429            + " </TAB>"
430            + " "
431            + " <TAB title = \"Government Officials\">"
432            + "         "
433            + "         "
434            + "         "
435            + " </TAB>"
436            + " "
437            + " <TAB title = \"Federal Agencies\">"
438            + "         "
439            + "         "
440            + "         "
441            + " </TAB>"
442            + " "
443            + " <TAB title = \"Fire/EMS\">"
444            + "         "
445            + "         "
446            + "         "
447            + " </TAB>"
448            + " "
449            + " <TAB title = \"Jails\">"
450            + "         "
451            + "         "
452            + "         "
453            + " </TAB>"
454            + " "
455            + " <TAB title = \"Hospitals/Med Centers\">"
456            + "         "
457            + "         "
458            + "         "
459            + " </TAB>"
460            + " "
461            + " <TAB title = \"Tow Companies\">"
462            + "         "
463            + "         "
464            + "         "
465            + " </TAB>"
466            + " "
467            + " <TAB title = \"CalTrans\">"
468            + "         "
469            + "         "
470            + "         "
471            + " </TAB>"
472            + " "
473            + " <TAB title = \"County Roads\">"
474            + "         "
475            + "         "
476            + "         "
477            + " </TAB>"
478            + " "
479            + " <TAB title = \"Utilities\">"
480            + "         "
481            + "         "
482            + "         "
483            + " </TAB>"
484            + " "
485            + " <TAB title = \"Animal Control\">"
486            + "         "
487            + "         "
488            + "         "
489            + " </TAB>"
490            + " "
491            + " <TAB title = \"Airports\">"
492            + "         "
493            + "         "
494            + "         "
495            + " </TAB>"
496            + " "
497            + " <TAB title = \"Credit Cards\">"
498            + "         "
499            + "         "
500            + "         "
501            + " </TAB>"
502            + " "
503            + " <TAB title = \"GG Crisis Shelters\">"
504            + "         "
505            + "         "
506            + "         "
507            + " </TAB>"
508            + " "
509            + " <TAB title = \"Ranges\">"
510            + "         "
511            + "         "
512            + "         "
513            + " </TAB>"
514            + " "
515            + " <TAB title = \"Hotlines\">"
516            + "         "
517            + "         "
518            + "         "
519            + " </TAB>"
520            + " "
521            + " <TAB title = \"Hwy Patrols OOS\">"
522            + "         "
523            + "         "
524            + "         "
525            + " </TAB>"
526            + " "
527            + " <TAB title = \"Parks/Recreation\">"
528            + "         "
529            + "         "
530            + "         "
531            + " </TAB>"
532            + " "
533            + " <TAB title = \"Shelters\">"
534            + "         "
535            + "         "
536            + "         "
537            + " </TAB>"
538            + " "
539            + " <TAB title = \"SL County Services\">"
540            + "         "
541            + "         "
542            + "         "
543            + " </TAB>"
544            + " "
545            + " <TAB title = \"SL Resources\">"
546            + "         "
547            + "         "
548            + "         "
549            + " </TAB>"
550            + " "
551            + " <TAB title = \"Truck/Tire Repair\">"
552            + "         "
553            + "         "
554            + "         "
555            + " </TAB>"
556            + " "
557            + " <TAB title = \"MCC Employees\">"
558            + "         "
559            + "         "
560            + "         "
561            + " </TAB>"
562            + " "
563            + " <TAB title = \"Gate Access Codes\">"
564            + "         "
565            + "         "
566            + "         "
567            + " </TAB>"
568            + " "
569            + " <TAB title = \"VT Call Signs\">"
570            + "         "
571            + "         "
572            + "         "
573            + " </TAB>"
574            + " "
575            + " <TAB title = \"SLCC Employees\">"
576            + "         "
577            + "         "
578            + "         "
579            + " </TAB>"
580            + ""
581            + "</CARDFILE>";
582    private static final String oneincidentXML =
583            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
584            + ""
585            + "<TMC_SCRIPT title=\"One Incident Simulation\">"
586            + ""
587            + " <SCRIPT_EVENT>"
588            + "         <TIME_INDEX>00:00:00</TIME_INDEX>"
589            + "         <INCIDENT LogNum=\"100\">Media Log</INCIDENT>           "
590            + "         "
591            + "         <CAD_DATA>"
592            + "                 <HEADER_INFO>"
593            + "                         <Type>Media</Type>"
594            + "                         <Beat>"
595            + "                         </Beat>"
596            + "                         <TruncLoc>"
597            + "                         </TruncLoc>"
598            + "                         <FullLoc>"
599            + "                         </FullLoc>"
600            + "                 </HEADER_INFO>                  "
601            + "                 "
602            + "                 <CAD_INCIDENT_EVENT>    "
603            + "                 </CAD_INCIDENT_EVENT>           "
604            + "                 "
605            + "         </CAD_DATA>                             "
606            + "         "
607            + " </SCRIPT_EVENT> "
608            + "</TMC_SCRIPT>";
609}
Note: See TracBrowser for help on using the repository browser.