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

Revision 658, 22.2 KB checked in by jdalbey, 4 years ago (diff)

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