mil.dod.th.ose.core/test/mil/dod/th/ose/core/impl/mp/TestMissionProgramManagerImpl.java
@Test
    public void testExecuteProgram() 
        throws ScriptException, IllegalArgumentException, MissionProgramException, PersistenceFailedException
    {
        // mock
        MissionScript missionMock = mock(MissionScript.class);
        when(m_ScriptEngine.eval(eq("test"), Mockito.any(ScriptContext.class))).thenReturn(new Object());
        when(((Invocable) m_ScriptEngine).getInterface(any(), eq(MissionScript.class))).thenReturn(missionMock);
       
        AssetDirectoryService assetDirectoryService = mock(AssetDirectoryService.class);
        m_SUT.setAssetDirectoryService(assetDirectoryService);
        
        setScriptEngineAndActivate();
        
        //program to add
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("testArg", 282);
        MissionProgramTemplate program = new MissionProgramTemplate().withName("blah").withSource("test");
        MissionProgramSchedule schedule = new MissionProgramSchedule().withImmediately(true).
            withIndefiniteInterval(false).withActive(true);
        MissionProgramParameters params = new MissionProgramParameters().withProgramName("Bagel").
            withParameters(MapTranslator.translateMap(map)).withTemplateName("blah").withSchedule(schedule);
        //mocking
        Set<String> templateNames = new HashSet<String>();
        templateNames.add("blah");
        when(m_TemplateManager.getMissionTemplateNames()).thenReturn(templateNames);
        doReturn(program).when(m_TemplateManager).getTemplate("blah");
        
        // replay
        m_SUT.loadParameters(params);
        ArgumentCaptor<ScriptContext> context = ArgumentCaptor.forClass(ScriptContext.class);
        verify(m_ScriptEngine, times(1)).eval(eq("test"), context.capture());
        verify(m_ScriptEngine, times(1)).put(ScriptEngine.FILENAME, "Bagel");
        assertThat((AssetDirectoryService)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.ASSET_DIRECTORY_SERVICE),
                is(assetDirectoryService));
        assertThat((BundleContext)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.BUNDLE_CONTEXT), is(m_Context));
        assertThat((PersistentDataStore)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.PERSISTENT_DATA_STORE),
                is(m_PersistentDataStore));

        ArgumentCaptor<ProgramImpl> prog = ArgumentCaptor.forClass(ProgramImpl.class);
        verify(m_Scheduler).executeProgram(prog.capture());
        assertThat((Integer)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE).get("testArg"), is(282));

        //mock exception
        when(m_ScriptEngine.eval(eq("bad-code"), Mockito.any(ScriptContext.class))).thenThrow(new ScriptException(""));

        //program to add
        MissionProgramTemplate program2 = new MissionProgramTemplate().withName("program2").withSource("bad-code");
        schedule = new MissionProgramSchedule().withImmediately(true).withActive(true).
            withIndefiniteInterval(false);
        params = new MissionProgramParameters().withSchedule(schedule).withProgramName("Fret").
            withParameters(MapTranslator.translateMap(map)).withTemplateName("program2");
        templateNames.add("program2");
        doReturn(program2).when(m_TemplateManager).getTemplate("program2");

        Program badProgram = m_SUT.loadParameters(params);
        verify(m_ScriptEngine).put(ScriptEngine.FILENAME, "Fret");

        //verify that at initialization that the program failed
        verify(m_ScriptEngine, times(1)).eval(eq("bad-code"), context.capture());
        assertThat(badProgram.getProgramStatus(), is(ProgramStatus.INITIALIZATION_ERROR));
    }
/**
     * Test the execution of a mission script.
     * 
     * Verify that the mission is initialized before execution.
     * 
     * Verify that if initialization fails that the program does not execute.
     */
Test the execution of a mission script.

Verify that the mission is initialized before execution.

Verify that if initialization fails that the program does not execute.
Test the execution of a mission script . Verify that the mission is initialized before execution . Verify that if initialization fails that the program does not execute .
@Test
    public void testExecuteProgram() 
        throws ScriptException, IllegalArgumentException, MissionProgramException, PersistenceFailedException
    {
        MissionScript missionMock = mock(MissionScript.class);
        when(m_ScriptEngine.eval(eq("test"), Mockito.any(ScriptContext.class))).thenReturn(new Object());
        when(((Invocable) m_ScriptEngine).getInterface(any(), eq(MissionScript.class))).thenReturn(missionMock);
        AssetDirectoryService assetDirectoryService = mock(AssetDirectoryService.class);
        m_SUT.setAssetDirectoryService(assetDirectoryService);
        setScriptEngineAndActivate();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("testArg", 282);
        MissionProgramTemplate program = new MissionProgramTemplate().withName("blah").withSource("test");
        MissionProgramSchedule schedule = new MissionProgramSchedule().withImmediately(true).
            withIndefiniteInterval(false).withActive(true);
        MissionProgramParameters params = new MissionProgramParameters().withProgramName("Bagel").
            withParameters(MapTranslator.translateMap(map)).withTemplateName("blah").withSchedule(schedule);
        Set<String> templateNames = new HashSet<String>();
        templateNames.add("blah");
        when(m_TemplateManager.getMissionTemplateNames()).thenReturn(templateNames);
        doReturn(program).when(m_TemplateManager).getTemplate("blah");
        m_SUT.loadParameters(params);
        ArgumentCaptor<ScriptContext> context = ArgumentCaptor.forClass(ScriptContext.class);
        verify(m_ScriptEngine, times(1)).eval(eq("test"), context.capture());
        verify(m_ScriptEngine, times(1)).put(ScriptEngine.FILENAME, "Bagel");
        assertThat((AssetDirectoryService)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.ASSET_DIRECTORY_SERVICE),
                is(assetDirectoryService));
        assertThat((BundleContext)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.BUNDLE_CONTEXT), is(m_Context));
        assertThat((PersistentDataStore)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE)
                .get(MissionProgramManager.PERSISTENT_DATA_STORE),
                is(m_PersistentDataStore));
        ArgumentCaptor<ProgramImpl> prog = ArgumentCaptor.forClass(ProgramImpl.class);
        verify(m_Scheduler).executeProgram(prog.capture());
        assertThat((Integer)context.getValue().getBindings(ScriptContext.ENGINE_SCOPE).get("testArg"), is(282));
        when(m_ScriptEngine.eval(eq("bad-code"), Mockito.any(ScriptContext.class))).thenThrow(new ScriptException(""));
        MissionProgramTemplate program2 = new MissionProgramTemplate().withName("program2").withSource("bad-code");
        schedule = new MissionProgramSchedule().withImmediately(true).withActive(true).
            withIndefiniteInterval(false);
        params = new MissionProgramParameters().withSchedule(schedule).withProgramName("Fret").
            withParameters(MapTranslator.translateMap(map)).withTemplateName("program2");
        templateNames.add("program2");
        doReturn(program2).when(m_TemplateManager).getTemplate("program2");
        Program badProgram = m_SUT.loadParameters(params);
        verify(m_ScriptEngine).put(ScriptEngine.FILENAME, "Fret");
        verify(m_ScriptEngine, times(1)).eval(eq("bad-code"), context.capture());
        assertThat(badProgram.getProgramStatus(), is(ProgramStatus.INITIALIZATION_ERROR));
    }
@ Test public void testExecuteProgram ( ) throws ScriptException , IllegalArgumentException , MissionProgramException , PersistenceFailedException { MissionScript missionMock = mock ( MissionScript . class ) ; when ( m_ScriptEngine . eval ( eq ( "test" ) , Mockito . any ( ScriptContext . class ) ) ) . thenReturn ( new Object ( ) ) ; when ( ( ( Invocable ) m_ScriptEngine ) . getInterface ( any ( ) , eq ( MissionScript . class ) ) ) . thenReturn ( missionMock ) ; AssetDirectoryService assetDirectoryService = mock ( AssetDirectoryService . class ) ; m_SUT . setAssetDirectoryService ( assetDirectoryService ) ; setScriptEngineAndActivate ( ) ; Map < String , Object > map = new HashMap < String , Object > ( ) ; map . put ( "testArg" , 282 ) ; MissionProgramTemplate program = new MissionProgramTemplate ( ) . withName ( "blah" ) . withSource ( "test" ) ; MissionProgramSchedule schedule = new MissionProgramSchedule ( ) . withImmediately ( true ) . withIndefiniteInterval ( false ) . withActive ( true ) ; MissionProgramParameters params = new MissionProgramParameters ( ) . withProgramName ( "Bagel" ) . withParameters ( MapTranslator . translateMap ( map ) ) . withTemplateName ( "blah" ) . withSchedule ( schedule ) ; Set < String > templateNames = new HashSet < String > ( ) ; templateNames . add ( "blah" ) ; when ( m_TemplateManager . getMissionTemplateNames ( ) ) . thenReturn ( templateNames ) ; doReturn ( program ) . when ( m_TemplateManager ) . getTemplate ( "blah" ) ; m_SUT . loadParameters ( params ) ; ArgumentCaptor < ScriptContext > context = ArgumentCaptor . forClass ( ScriptContext . class ) ; verify ( m_ScriptEngine , times ( 1 ) ) . eval ( eq ( "test" ) , context . capture ( ) ) ; verify ( m_ScriptEngine , times ( 1 ) ) . put ( ScriptEngine . FILENAME , "Bagel" ) ; assertThat ( ( AssetDirectoryService ) context . getValue ( ) . getBindings ( ScriptContext . ENGINE_SCOPE ) . get ( MissionProgramManager . ASSET_DIRECTORY_SERVICE ) , is ( assetDirectoryService ) ) ; assertThat ( ( BundleContext ) context . getValue ( ) . getBindings ( ScriptContext . ENGINE_SCOPE ) . get ( MissionProgramManager . BUNDLE_CONTEXT ) , is ( m_Context ) ) ; assertThat ( ( PersistentDataStore ) context . getValue ( ) . getBindings ( ScriptContext . ENGINE_SCOPE ) . get ( MissionProgramManager . PERSISTENT_DATA_STORE ) , is ( m_PersistentDataStore ) ) ; ArgumentCaptor < ProgramImpl > prog = ArgumentCaptor . forClass ( ProgramImpl . class ) ; verify ( m_Scheduler ) . executeProgram ( prog . capture ( ) ) ; assertThat ( ( Integer ) context . getValue ( ) . getBindings ( ScriptContext . ENGINE_SCOPE ) . get ( "testArg" ) , is ( 282 ) ) ; when ( m_ScriptEngine . eval ( eq ( "bad-code" ) , Mockito . any ( ScriptContext . class ) ) ) . thenThrow ( new ScriptException ( "" ) ) ; MissionProgramTemplate program2 = new MissionProgramTemplate ( ) . withName ( "program2" ) . withSource ( "bad-code" ) ; schedule = new MissionProgramSchedule ( ) . withImmediately ( true ) . withActive ( true ) . withIndefiniteInterval ( false ) ; params = new MissionProgramParameters ( ) . withSchedule ( schedule ) . withProgramName ( "Fret" ) . withParameters ( MapTranslator . translateMap ( map ) ) . withTemplateName ( "program2" ) ; templateNames . add ( "program2" ) ; doReturn ( program2 ) . when ( m_TemplateManager ) . getTemplate ( "program2" ) ; Program badProgram = m_SUT . loadParameters ( params ) ; verify ( m_ScriptEngine ) . put ( ScriptEngine . FILENAME , "Fret" ) ; verify ( m_ScriptEngine , times ( 1 ) ) . eval ( eq ( "bad-code" ) , context . capture ( ) ) ; assertThat ( badProgram . getProgramStatus ( ) , is ( ProgramStatus . INITIALIZATION_ERROR ) ) ; }
Test the execution of a mission script.
Test the execution of a mission script .
// mock //program to add //mocking // replay //mock exception //program to add //verify that at initialization that the program failed