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

Revision 422, 21.3 KB checked in by jdalbey, 7 years ago (diff)

Remove ATMS functionality. Reworked and simplified the Highway model to use only VDS data from PeMS. Updated all unit tests.

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