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

Revision 449, 21.4 KB checked in by jdalbey, 7 years ago (diff)

Updated unit tests to use CADcomments config property.

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\n"
278            + "CADcommentsLog         = CADcomments.log";
279    static final String trafficMgrData = "Highways_Map_File = config/vds_data/postmile_coordinates.txt\n"
280            +"Events_File = config/vds_data/atmsBatchEvents.txt\n"
281            +"FEPSim_IP_addr = localhost\n"
282            +"Output_Destination = Console\n"
283            +"Highway_Status_File = /tmp/highway_status.json\n";
284    static final String paramicsData = "ParamicsCommHost = 127.0.0.1\n"
285            + "ParamicsCommPort       = 4450\n"
286            + "IncidentUpdateInterval = 30\n"
287            + "IncidentUpdateFile     = exchange.xml\n"
288            + "ParamicsStatusInterval = 15\n"
289            + "ParamicsStatusFile     = paramics_status.xml\n"
290            + "CameraStatusInterval   = 30\n"
291            + "CameraStatusFile       = camera_status.xml\n";
292    static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7";
293    static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD";
294    /*
295     * Creating instance of app must be done only once or you get registry
296     * bind problems, and code Written in Constructor is Executed
297     * before each Test Method
298     */
299
300    private void writeConfigData()
301    {
302        // Declare a stream to the output
303//        bos = new ByteArrayOutputStream();
304//        ps = new PrintStream(bos);
305        // Redirect the standard output
306//        System.setOut(ps);
307        File cf = new File ("config.txt");
308        cf.delete();
309        writedata("config.txt", configData);
310        File tf = new File ("tconfig.txt");
311        tf.delete();
312        writedata("tconfig.txt", trafficMgrData);
313        writedata("pconfig.txt", paramicsData);
314        writedata("empty.txt", "");
315        writeScriptfiles();
316    }
317
318    private void writeScriptfiles()
319    {
320        new File("scripts").mkdir();
321        writedata("scripts/Cardfile.xml", cardfileData);
322        writedata("scripts/one-incident.xml", oneincidentXML);
323    }
324
325    // Write the test data to a file
326    private void writedata(String filename, String data)
327    {
328        File cardFile = new File(filename);
329        // If a cardfile exists, leave it
330        if (!cardFile.exists())
331        {
332            PrintWriter writer = null;
333            try
334            {
335                writer = new PrintWriter(new FileWriter(filename));
336                writer.println(data);
337                writer.close();
338            } catch (Exception ex)
339            {
340                ex.printStackTrace();
341            }
342        }
343    }
344    private static final String cardfileData =
345            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
346            + "<!--Please do not modify titles. Note that these titles are not"
347            + "sent into the actual screen. If titles are to be modified,"
348            + "they need to be changed in CardfileHandler.java  -->"
349            + ""
350            + ""
351            + ""
352            + "<CARDFILE> "
353            + " <TAB title = \"Coastal Division Units\">"
354            + "         <CARDFILE_OBJ name = \"Name\" >"
355            + "                 <ADDRESS>Address</ADDRESS>"
356            + "                 <CITY>City</CITY>"
357            + "                 <STATE>State</STATE>"
358            + "                 <ZIP>Zip</ZIP>"
359            + "                 <PHONE1>Phone1</PHONE1>"
360            + "                 <PHONE2>Phone2</PHONE2>"
361            + "                 <FAX>Fax</FAX>"
362            + "         </CARDFILE_OBJ>"
363            + "         "
364            + "         <CARDFILE_OBJ  name = \"Name2\" >"
365            + "                 <ADDRESS>Address2</ADDRESS>"
366            + "                 <CITY>City2</CITY>"
367            + "                 <STATE>State2</STATE>"
368            + "                 <ZIP>Zip2</ZIP>"
369            + "                 <PHONE1>Phone12</PHONE1>"
370            + "                 <PHONE2>Phone22</PHONE2>"
371            + "                 <FAX>Fax2</FAX>"
372            + "         </CARDFILE_OBJ>"
373            + "         "
374            + " </TAB>"
375            + " "
376            + " <TAB title = \"Police/Sheriff/Coroner\">"
377            + " "
378            + "         <CARDFILE_OBJ  name = \"Name\" >"
379            + "                 <ADDRESS>Address</ADDRESS>"
380            + "                 <CITY>City</CITY>"
381            + "                 <STATE>State</STATE>"
382            + "                 <ZIP>Zip</ZIP>"
383            + "                 <PHONE1>Phone1</PHONE1>"
384            + "                 <PHONE2>Phone2</PHONE2>"
385            + "                 <FAX>Fax</FAX>"
386            + "         </CARDFILE_OBJ>"
387            + "         "
388            + " </TAB>"
389            + " "
390            + " <TAB title = \"Courts\">"
391            + "         "
392            + "         "
393            + "         "
394            + " </TAB>"
395            + " "
396            + " <TAB title = \"Public Transportation\">"
397            + "         "
398            + "         "
399            + "         "
400            + " </TAB>"
401            + " "
402            + " <TAB title = \"GG Other\">"
403            + "         "
404            + "         "
405            + "         "
406            + " </TAB>"
407            + " "
408            + " <TAB title = \"MY Misc\">"
409            + "         "
410            + "         "
411            + "         "
412            + " </TAB>"
413            + " "
414            + " <TAB title = \"SL Misc\">"
415            + "         "
416            + "         "
417            + "         "
418            + " </TAB>"
419            + " "
420            + " <TAB title = \"VT Misc\">"
421            + "         "
422            + "         "
423            + "         "
424            + " </TAB>"
425            + " "
426            + " <TAB title = \"CHP Offices\">"
427            + "         "
428            + "         "
429            + "         "
430            + " </TAB>"
431            + " "
432            + " <TAB title = \"State Agencies/Facilities\">"
433            + "         "
434            + "         "
435            + "         "
436            + " </TAB>"
437            + " "
438            + " <TAB title = \"Government Officials\">"
439            + "         "
440            + "         "
441            + "         "
442            + " </TAB>"
443            + " "
444            + " <TAB title = \"Federal Agencies\">"
445            + "         "
446            + "         "
447            + "         "
448            + " </TAB>"
449            + " "
450            + " <TAB title = \"Fire/EMS\">"
451            + "         "
452            + "         "
453            + "         "
454            + " </TAB>"
455            + " "
456            + " <TAB title = \"Jails\">"
457            + "         "
458            + "         "
459            + "         "
460            + " </TAB>"
461            + " "
462            + " <TAB title = \"Hospitals/Med Centers\">"
463            + "         "
464            + "         "
465            + "         "
466            + " </TAB>"
467            + " "
468            + " <TAB title = \"Tow Companies\">"
469            + "         "
470            + "         "
471            + "         "
472            + " </TAB>"
473            + " "
474            + " <TAB title = \"CalTrans\">"
475            + "         "
476            + "         "
477            + "         "
478            + " </TAB>"
479            + " "
480            + " <TAB title = \"County Roads\">"
481            + "         "
482            + "         "
483            + "         "
484            + " </TAB>"
485            + " "
486            + " <TAB title = \"Utilities\">"
487            + "         "
488            + "         "
489            + "         "
490            + " </TAB>"
491            + " "
492            + " <TAB title = \"Animal Control\">"
493            + "         "
494            + "         "
495            + "         "
496            + " </TAB>"
497            + " "
498            + " <TAB title = \"Airports\">"
499            + "         "
500            + "         "
501            + "         "
502            + " </TAB>"
503            + " "
504            + " <TAB title = \"Credit Cards\">"
505            + "         "
506            + "         "
507            + "         "
508            + " </TAB>"
509            + " "
510            + " <TAB title = \"GG Crisis Shelters\">"
511            + "         "
512            + "         "
513            + "         "
514            + " </TAB>"
515            + " "
516            + " <TAB title = \"Ranges\">"
517            + "         "
518            + "         "
519            + "         "
520            + " </TAB>"
521            + " "
522            + " <TAB title = \"Hotlines\">"
523            + "         "
524            + "         "
525            + "         "
526            + " </TAB>"
527            + " "
528            + " <TAB title = \"Hwy Patrols OOS\">"
529            + "         "
530            + "         "
531            + "         "
532            + " </TAB>"
533            + " "
534            + " <TAB title = \"Parks/Recreation\">"
535            + "         "
536            + "         "
537            + "         "
538            + " </TAB>"
539            + " "
540            + " <TAB title = \"Shelters\">"
541            + "         "
542            + "         "
543            + "         "
544            + " </TAB>"
545            + " "
546            + " <TAB title = \"SL County Services\">"
547            + "         "
548            + "         "
549            + "         "
550            + " </TAB>"
551            + " "
552            + " <TAB title = \"SL Resources\">"
553            + "         "
554            + "         "
555            + "         "
556            + " </TAB>"
557            + " "
558            + " <TAB title = \"Truck/Tire Repair\">"
559            + "         "
560            + "         "
561            + "         "
562            + " </TAB>"
563            + " "
564            + " <TAB title = \"MCC Employees\">"
565            + "         "
566            + "         "
567            + "         "
568            + " </TAB>"
569            + " "
570            + " <TAB title = \"Gate Access Codes\">"
571            + "         "
572            + "         "
573            + "         "
574            + " </TAB>"
575            + " "
576            + " <TAB title = \"VT Call Signs\">"
577            + "         "
578            + "         "
579            + "         "
580            + " </TAB>"
581            + " "
582            + " <TAB title = \"SLCC Employees\">"
583            + "         "
584            + "         "
585            + "         "
586            + " </TAB>"
587            + ""
588            + "</CARDFILE>";
589    private static final String oneincidentXML =
590            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
591            + ""
592            + "<TMC_SCRIPT title=\"One Incident Simulation\">"
593            + ""
594            + " <SCRIPT_EVENT>"
595            + "         <TIME_INDEX>00:00:00</TIME_INDEX>"
596            + "         <INCIDENT LogNum=\"100\">Media Log</INCIDENT>           "
597            + "         "
598            + "         <CAD_DATA>"
599            + "                 <HEADER_INFO>"
600            + "                         <Type>Media</Type>"
601            + "                         <Beat>"
602            + "                         </Beat>"
603            + "                         <TruncLoc>"
604            + "                         </TruncLoc>"
605            + "                         <FullLoc>"
606            + "                         </FullLoc>"
607            + "                 </HEADER_INFO>                  "
608            + "                 "
609            + "                 <CAD_INCIDENT_EVENT>    "
610            + "                 </CAD_INCIDENT_EVENT>           "
611            + "                 "
612            + "         </CAD_DATA>                             "
613            + "         "
614            + " </SCRIPT_EVENT> "
615            + "</TMC_SCRIPT>";
616}
Note: See TracBrowser for help on using the repository browser.