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

Revision 558, 21.4 KB checked in by jdalbey, 6 years ago (diff)

Change hardcoded "scripts" as name of folder to a constant in SimulationManager?. See #228.

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