mil.dod.th.ose.core/test/mil/dod/th/ose/core/impl/mp/TestTemplateProgramManagerImpl.java
@Test
    public void testLoadFromFile() throws IOException, IllegalArgumentException, PersistenceFailedException, 
        UnmarshalException, URISyntaxException
    {
        //activate the manager
        m_SUT.activate(context);
        
        //create a template
        MissionProgramTemplate localTemplate = new MissionProgramTemplate().withName("SpiderOne").
            withSource("ASpiderBite");
        URL url1 = templateUrl(localTemplate);
        
        //load the template
        m_SUT.loadFromFile(url1, true);

        //verify
        ArgumentCaptor<byte[]> dataByte = ArgumentCaptor.forClass(byte[].class);
        ArgumentCaptor<UUID> uuidCaptor = ArgumentCaptor.forClass(UUID.class);
        verify(m_PersistentDataStore, times(1)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
            anyString(), Mockito.any(Serializable.class));
        PersistentData data1 = mock(PersistentData.class);
        byte[] tmpBytes = XmlUtils.toXML(localTemplate, false);
        when((byte[])data1.getEntity()).thenReturn(tmpBytes);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data1);
        
        // verify the template was loaded from the file, and overwrote the previously loaded one
        assertThat(m_SUT.getTemplates().size(), is(1));
        
        //change the template
        MissionProgramTemplate localTemplate2 = new MissionProgramTemplate().withName("SpiderTwo").
            withSource("ILikeSpecialChars && @ <return>");
        URL url2 = templateUrl(localTemplate2);
        
        //load the template
        m_SUT.loadFromFile(url2, true);
        
        //mocking and verifying
        verify(m_PersistentDataStore, times(2)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), Mockito.any(Serializable.class));
        PersistentData data2 = mock(PersistentData.class);
        final byte[] tmpBytes2 = XmlUtils.toXML(localTemplate2, false);
        when((byte[])data2.getEntity()).thenReturn(tmpBytes2);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data2);
        
        // verify the template was loaded from the file and that the template was overwritten
        Set<MissionProgramTemplate> templates = m_SUT.getTemplates();
        assertThat(templates.size(), is(2));
        
        //Lists are not place specific, check that the template added is within the templates in the list
        boolean foundFlag = false;
        for (MissionProgramTemplate template : templates)
        {
            if (template.getName().equals(localTemplate.getName()))
            {
                foundFlag = true;
                assertThat(template.getSource(), is(localTemplate.getSource()));             
            }
        }
        
        //check that found flag is true
        assertThat(foundFlag, is(true));
        
        //try to add a file that is not meant to overwrite the template with the same name
        MissionProgramTemplate localTemplate3 = new MissionProgramTemplate().withName("SpiderOne").
                withSource("I will get in");
        URL url3 = templateUrl(localTemplate3);
        
        //load the template, with overwrite as false
        m_SUT.loadFromFile(url3, false);

        //mocking and verifying
        verify(m_PersistentDataStore, times(3)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), dataByte.capture());
        PersistentData data3 = mock(PersistentData.class);
        final byte[] tmpBytes3 = XmlUtils.toXML(localTemplate3, false);
        when((byte[])data3.getEntity()).thenReturn(tmpBytes3);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data3);

        //recap
        templates = m_SUT.getTemplates();
        assertThat(templates.size(), is(2));
        //verify
        for (MissionProgramTemplate template : templates)
        {
            if (template.getName().contains("SpiderOne"))
            {
                assertThat(template.getSource(), is("I will get in"));
            }
        }
    }
/**
     * Verify loading from file and overwriting behavior.
     * TODO: TH-805, support syncing of templates, need to be able to preserve unique names,
     * but also allow for templates to be update if wanted.
     */
Verify loading from file and overwriting behavior.
TODO: TH-805, support syncing of templates, need to be able to preserve unique names,
but also allow for templates to be update if wanted.
Verify loading from file and overwriting behavior . TODO : TH - 805 support syncing of templates need to be able to preserve unique names but also allow for templates to be update if wanted .
@Test
    public void testLoadFromFile() throws IOException, IllegalArgumentException, PersistenceFailedException, 
        UnmarshalException, URISyntaxException
    {
        m_SUT.activate(context);
        MissionProgramTemplate localTemplate = new MissionProgramTemplate().withName("SpiderOne").
            withSource("ASpiderBite");
        URL url1 = templateUrl(localTemplate);
        m_SUT.loadFromFile(url1, true);
        ArgumentCaptor<byte[]> dataByte = ArgumentCaptor.forClass(byte[].class);
        ArgumentCaptor<UUID> uuidCaptor = ArgumentCaptor.forClass(UUID.class);
        verify(m_PersistentDataStore, times(1)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
            anyString(), Mockito.any(Serializable.class));
        PersistentData data1 = mock(PersistentData.class);
        byte[] tmpBytes = XmlUtils.toXML(localTemplate, false);
        when((byte[])data1.getEntity()).thenReturn(tmpBytes);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data1);
        assertThat(m_SUT.getTemplates().size(), is(1));
        MissionProgramTemplate localTemplate2 = new MissionProgramTemplate().withName("SpiderTwo").
            withSource("ILikeSpecialChars && @ <return>");
        URL url2 = templateUrl(localTemplate2);
        m_SUT.loadFromFile(url2, true);
        verify(m_PersistentDataStore, times(2)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), Mockito.any(Serializable.class));
        PersistentData data2 = mock(PersistentData.class);
        final byte[] tmpBytes2 = XmlUtils.toXML(localTemplate2, false);
        when((byte[])data2.getEntity()).thenReturn(tmpBytes2);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data2);
        Set<MissionProgramTemplate> templates = m_SUT.getTemplates();
        assertThat(templates.size(), is(2));
        boolean foundFlag = false;
        for (MissionProgramTemplate template : templates)
        {
            if (template.getName().equals(localTemplate.getName()))
            {
                foundFlag = true;
                assertThat(template.getSource(), is(localTemplate.getSource()));             
            }
        }
        assertThat(foundFlag, is(true));
        MissionProgramTemplate localTemplate3 = new MissionProgramTemplate().withName("SpiderOne").
                withSource("I will get in");
        URL url3 = templateUrl(localTemplate3);
        , with overwrite as false
        m_SUT.loadFromFile(url3, false);
        verify(m_PersistentDataStore, times(3)).persist(eq(TemplateProgramManagerImpl.class), uuidCaptor.capture(), 
                anyString(), dataByte.capture());
        PersistentData data3 = mock(PersistentData.class);
        final byte[] tmpBytes3 = XmlUtils.toXML(localTemplate3, false);
        when((byte[])data3.getEntity()).thenReturn(tmpBytes3);
        when(m_PersistentDataStore.find(uuidCaptor.getValue())).thenReturn(data3);
        templates = m_SUT.getTemplates();
        assertThat(templates.size(), is(2));
        for (MissionProgramTemplate template : templates)
        {
            if (template.getName().contains("SpiderOne"))
            {
                assertThat(template.getSource(), is("I will get in"));
            }
        }
    }
@ Test public void testLoadFromFile ( ) throws IOException , IllegalArgumentException , PersistenceFailedException , UnmarshalException , URISyntaxException { m_SUT . activate ( context ) ; MissionProgramTemplate localTemplate = new MissionProgramTemplate ( ) . withName ( "SpiderOne" ) . withSource ( "ASpiderBite" ) ; URL url1 = templateUrl ( localTemplate ) ; m_SUT . loadFromFile ( url1 , true ) ; ArgumentCaptor < byte [ ] > dataByte = ArgumentCaptor . forClass ( byte [ ] . class ) ; ArgumentCaptor < UUID > uuidCaptor = ArgumentCaptor . forClass ( UUID . class ) ; verify ( m_PersistentDataStore , times ( 1 ) ) . persist ( eq ( TemplateProgramManagerImpl . class ) , uuidCaptor . capture ( ) , anyString ( ) , Mockito . any ( Serializable . class ) ) ; PersistentData data1 = mock ( PersistentData . class ) ; byte [ ] tmpBytes = XmlUtils . toXML ( localTemplate , false ) ; when ( ( byte [ ] ) data1 . getEntity ( ) ) . thenReturn ( tmpBytes ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getValue ( ) ) ) . thenReturn ( data1 ) ; assertThat ( m_SUT . getTemplates ( ) . size ( ) , is ( 1 ) ) ; MissionProgramTemplate localTemplate2 = new MissionProgramTemplate ( ) . withName ( "SpiderTwo" ) . withSource ( "ILikeSpecialChars && @ <return>" ) ; URL url2 = templateUrl ( localTemplate2 ) ; m_SUT . loadFromFile ( url2 , true ) ; verify ( m_PersistentDataStore , times ( 2 ) ) . persist ( eq ( TemplateProgramManagerImpl . class ) , uuidCaptor . capture ( ) , anyString ( ) , Mockito . any ( Serializable . class ) ) ; PersistentData data2 = mock ( PersistentData . class ) ; final byte [ ] tmpBytes2 = XmlUtils . toXML ( localTemplate2 , false ) ; when ( ( byte [ ] ) data2 . getEntity ( ) ) . thenReturn ( tmpBytes2 ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getValue ( ) ) ) . thenReturn ( data2 ) ; Set < MissionProgramTemplate > templates = m_SUT . getTemplates ( ) ; assertThat ( templates . size ( ) , is ( 2 ) ) ; boolean foundFlag = false ; for ( MissionProgramTemplate template : templates ) { if ( template . getName ( ) . equals ( localTemplate . getName ( ) ) ) { foundFlag = true ; assertThat ( template . getSource ( ) , is ( localTemplate . getSource ( ) ) ) ; } } assertThat ( foundFlag , is ( true ) ) ; MissionProgramTemplate localTemplate3 = new MissionProgramTemplate ( ) . withName ( "SpiderOne" ) . withSource ( "I will get in" ) ; URL url3 = templateUrl ( localTemplate3 ) ; m_SUT . loadFromFile ( url3 , false ) ; verify ( m_PersistentDataStore , times ( 3 ) ) . persist ( eq ( TemplateProgramManagerImpl . class ) , uuidCaptor . capture ( ) , anyString ( ) , dataByte . capture ( ) ) ; PersistentData data3 = mock ( PersistentData . class ) ; final byte [ ] tmpBytes3 = XmlUtils . toXML ( localTemplate3 , false ) ; when ( ( byte [ ] ) data3 . getEntity ( ) ) . thenReturn ( tmpBytes3 ) ; when ( m_PersistentDataStore . find ( uuidCaptor . getValue ( ) ) ) . thenReturn ( data3 ) ; templates = m_SUT . getTemplates ( ) ; assertThat ( templates . size ( ) , is ( 2 ) ) ; for ( MissionProgramTemplate template : templates ) { if ( template . getName ( ) . contains ( "SpiderOne" ) ) { assertThat ( template . getSource ( ) , is ( "I will get in" ) ) ; } } }
Verify loading from file and overwriting behavior.
Verify loading from file and overwriting behavior .
//activate the manager //create a template //load the template //verify // verify the template was loaded from the file, and overwrote the previously loaded one //change the template //load the template //mocking and verifying // verify the template was loaded from the file and that the template was overwritten //Lists are not place specific, check that the template added is within the templates in the list //check that found flag is true //try to add a file that is not meant to overwrite the template with the same name //load the template, with overwrite as false //mocking and verifying //recap //verify