mil.dod.th.ose.core/test/mil/dod/th/ose/core/impl/mp/TestTemplateProgramManagerImpl.java
@Test
    public void testLoadFromStream() throws IOException, UnmarshalException, IllegalArgumentException, 
        PersistenceFailedException, URISyntaxException
    {
        //mock the stored templates
        mockTemplateProgramManagerTemplates();
        mockTemplatesFromDir();
        //activate the manager
        m_SUT.activate(context);
        
        //make a list of uuids, and byte data assigned to each template from the template directory
        ArgumentCaptor<byte[]> dataByte = ArgumentCaptor.forClass(byte[].class);
        ArgumentCaptor<UUID> uuidCaptor = ArgumentCaptor.forClass(UUID.class);
        verify(m_PersistentDataStore, times(2)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), dataByte.capture());
        assertThat(dataByte.getAllValues().size(), is(2));
            
        
        //mock behavior of look-up, the order and data for each isn't particularly important at this point
        PersistentData data = mock(PersistentData.class);
        PersistentData data1 = mock(PersistentData.class);
        when((byte[])data.getEntity()).thenReturn(dataByte.getAllValues().get(0));
        when(m_PersistentDataStore.find(uuidCaptor.getAllValues().get(0))).thenReturn(data);
        when((byte[])data1.getEntity()).thenReturn(dataByte.getAllValues().get(1));
        when(m_PersistentDataStore.find(uuidCaptor.getAllValues().get(1))).thenReturn(data1);
        
        //get all templates known, should be 7, as five templates are mocked, 2 are from the template directory
        assertThat(m_SUT.getTemplates().size(), is(7));
        
        //create a template and save it to the file
        MissionProgramTemplate localTemplate = new MissionProgramTemplate().withName("TestStreamMission").
            withSource("ScriptSourceCode");
        final byte[] tmpBytes = XmlUtils.toXML(localTemplate, false);
        ByteArrayInputStream bais = new ByteArrayInputStream(tmpBytes);
        
        //mock behavior
        when(m_UnmarshalService.getXmlObject(MissionProgramTemplate.class, bais)).thenReturn(localTemplate);
        
        //load the template
        m_SUT.loadFromStream(bais, true);

        //mock behavior
        verify(m_PersistentDataStore, times(3)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
            anyString(), Mockito.any(Serializable.class));
        PersistentData data3 = mock(PersistentData.class);
        when((byte[])data3.getEntity()).thenReturn(tmpBytes);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data3);
        
        // verify the template was loaded from the file
        assertThat(m_SUT.getTemplates().size(), is(8));
    }
/**
     * Test the LoadFromStream method.
     * Verify that the template is loaded appropriately. 
     */
Test the LoadFromStream method.
Verify that the template is loaded appropriately.
Test the LoadFromStream method . Verify that the template is loaded appropriately .
@Test
    public void testLoadFromStream() throws IOException, UnmarshalException, IllegalArgumentException, 
        PersistenceFailedException, URISyntaxException
    {
        mockTemplateProgramManagerTemplates();
        mockTemplatesFromDir();
        m_SUT.activate(context);
        ArgumentCaptor<byte[]> dataByte = ArgumentCaptor.forClass(byte[].class);
        ArgumentCaptor<UUID> uuidCaptor = ArgumentCaptor.forClass(UUID.class);
        verify(m_PersistentDataStore, times(2)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), dataByte.capture());
        assertThat(dataByte.getAllValues().size(), is(2));
        PersistentData data = mock(PersistentData.class);
        PersistentData data1 = mock(PersistentData.class);
        when((byte[])data.getEntity()).thenReturn(dataByte.getAllValues().get(0));
        when(m_PersistentDataStore.find(uuidCaptor.getAllValues().get(0))).thenReturn(data);
        when((byte[])data1.getEntity()).thenReturn(dataByte.getAllValues().get(1));
        when(m_PersistentDataStore.find(uuidCaptor.getAllValues().get(1))).thenReturn(data1);
        assertThat(m_SUT.getTemplates().size(), is(7));
        MissionProgramTemplate localTemplate = new MissionProgramTemplate().withName("TestStreamMission").
            withSource("ScriptSourceCode");
        final byte[] tmpBytes = XmlUtils.toXML(localTemplate, false);
        ByteArrayInputStream bais = new ByteArrayInputStream(tmpBytes);
        when(m_UnmarshalService.getXmlObject(MissionProgramTemplate.class, bais)).thenReturn(localTemplate);
        m_SUT.loadFromStream(bais, true);
        verify(m_PersistentDataStore, times(3)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
            anyString(), Mockito.any(Serializable.class));
        PersistentData data3 = mock(PersistentData.class);
        when((byte[])data3.getEntity()).thenReturn(tmpBytes);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data3);
        assertThat(m_SUT.getTemplates().size(), is(8));
    }
@ Test public void testLoadFromStream ( ) throws IOException , UnmarshalException , IllegalArgumentException , PersistenceFailedException , URISyntaxException { mockTemplateProgramManagerTemplates ( ) ; mockTemplatesFromDir ( ) ; m_SUT . activate ( context ) ; ArgumentCaptor < byte [ ] > dataByte = ArgumentCaptor . forClass ( byte [ ] . class ) ; ArgumentCaptor < UUID > uuidCaptor = ArgumentCaptor . forClass ( UUID . class ) ; verify ( m_PersistentDataStore , times ( 2 ) ) . persist ( eq ( TemplateProgramManagerImpl . class ) , uuidCaptor . capture ( ) , anyString ( ) , dataByte . capture ( ) ) ; assertThat ( dataByte . getAllValues ( ) . size ( ) , is ( 2 ) ) ; PersistentData data = mock ( PersistentData . class ) ; PersistentData data1 = mock ( PersistentData . class ) ; when ( ( byte [ ] ) data . getEntity ( ) ) . thenReturn ( dataByte . getAllValues ( ) . get ( 0 ) ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getAllValues ( ) . get ( 0 ) ) ) . thenReturn ( data ) ; when ( ( byte [ ] ) data1 . getEntity ( ) ) . thenReturn ( dataByte . getAllValues ( ) . get ( 1 ) ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getAllValues ( ) . get ( 1 ) ) ) . thenReturn ( data1 ) ; assertThat ( m_SUT . getTemplates ( ) . size ( ) , is ( 7 ) ) ; MissionProgramTemplate localTemplate = new MissionProgramTemplate ( ) . withName ( "TestStreamMission" ) . withSource ( "ScriptSourceCode" ) ; final byte [ ] tmpBytes = XmlUtils . toXML ( localTemplate , false ) ; ByteArrayInputStream bais = new ByteArrayInputStream ( tmpBytes ) ; when ( m_UnmarshalService . getXmlObject ( MissionProgramTemplate . class , bais ) ) . thenReturn ( localTemplate ) ; m_SUT . loadFromStream ( bais , true ) ; verify ( m_PersistentDataStore , times ( 3 ) ) . persist ( eq ( TemplateProgramManagerImpl . class ) , uuidCaptor . capture ( ) , anyString ( ) , Mockito . any ( Serializable . class ) ) ; PersistentData data3 = mock ( PersistentData . class ) ; when ( ( byte [ ] ) data3 . getEntity ( ) ) . thenReturn ( tmpBytes ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getValue ( ) ) ) . thenReturn ( data3 ) ; assertThat ( m_SUT . getTemplates ( ) . size ( ) , is ( 8 ) ) ; }
Test the LoadFromStream method.
Test the LoadFromStream method .
//mock the stored templates //activate the manager //make a list of uuids, and byte data assigned to each template from the template directory //mock behavior of look-up, the order and data for each isn't particularly important at this point //get all templates known, should be 7, as five templates are mocked, 2 are from the template directory //create a template and save it to the file //mock behavior //load the template //mock behavior // verify the template was loaded from the file