mil.dod.th.ose.gui.webapp/test/mil/dod/th/ose/gui/webapp/advanced/TestBundleImportMgrImpl.java
@SuppressWarnings("unchecked")
    @Test
    public void testHandleImportBundle() throws IOException, Exception
    {
        //Mock all classes needed for the import method.
        BundleConfigurationMgrImpl bundleMgr = mock(BundleConfigurationMgrImpl.class);
        JarInputStream jis = PowerMockito.mock(JarInputStream.class);
        InputStream is = mock(InputStream.class);
        Manifest manifest = mock(Manifest.class);
        Attributes attributes = mock(Attributes.class);
        
        //Mock jar file contents.
        byte[] byteArr = {1, 2, 3, 4};
        when(m_File.getContents()).thenReturn(byteArr);
        when(m_File.getInputstream()).thenReturn(is);
        
        //Mock methods for bundle configuration manager service.
        List<BundleModel> bundleList = new ArrayList<BundleModel>();
        BundleModel testBundle = mock(BundleModel.class);
        bundleList.add(testBundle);
        when(testBundle.getSymbolicName()).thenReturn("Some Name");
        when(testBundle.getBundleId()).thenReturn(Long.valueOf(5));
        m_SUT.setBundleConfigurationMgr(bundleMgr);
        when(bundleMgr.getBundles()).thenReturn(bundleList);
        
        
        //Mock the jar input stream constructor and all methods pertaining to he jar input stream.
        PowerMockito.whenNew(JarInputStream.class).withArguments(Mockito.any(InputStream.class)).thenReturn(jis);
        when(jis.getManifest()).thenReturn(manifest);
        when(manifest.getMainAttributes()).thenReturn(attributes);
        when(attributes.getValue("Bundle-SymbolicName")).thenReturn("Some Name");
        
        //Argument captor to capture the update message.
        ArgumentCaptor<BundleMessages.UpdateRequestData> updateCaptor = 
                ArgumentCaptor.forClass(BundleMessages.UpdateRequestData.class);
        
        //replay first time to verify updating bundle.
        m_SUT.handleImportBundle(1);
        
        //Verify that an update message was sent.
        verify(m_MessageFactory).createBundleMessage(eq(BundleMessageType.UpdateRequest), updateCaptor.capture());
        verify(m_MessageWrapper).queue(eq(1), (ResponseHandler) eq(null));
        
        //Verify the contents of the update message.
        UpdateRequestData updateMsg = updateCaptor.getValue();
        assertThat(updateMsg.getBundleId(), is(Long.valueOf(5)));
        assertThat(updateMsg.getBundleFile().toByteArray(), is(byteArr));
        
        //Remove the bundle from the bundle manager list so that an install request can be tested for.
        bundleList.remove(0);
        when(m_File.getFileName()).thenReturn("File Name");
        
        //replay second time to verify installing bundle.
        m_SUT.handleImportBundle(1);
        
        //Argument captor to capture the install message.
        ArgumentCaptor<BundleMessages.InstallRequestData> installCaptor = 
                ArgumentCaptor.forClass(BundleMessages.InstallRequestData.class);
        
        //Verify that an install request was sent.
        verify(m_MessageFactory).createBundleMessage(eq(BundleMessageType.InstallRequest), installCaptor.capture());
        verify(m_MessageWrapper, times(2)).queue(eq(1), (ResponseHandler) eq(null));
        
        //Verify the contents of the install message.
        InstallRequestData installMsg = installCaptor.getValue();
        assertThat(installMsg.getBundleLocation(), is("File Name"));
        assertThat(installMsg.getBundleFile().toByteArray(), is(byteArr));
        
        //Mock causing an IOException when the file being installed/updated is not a jar file.
        when(jis.getManifest()).thenReturn(null);
        m_SUT.handleImportBundle(1);
        
        //Mock IOException when jar input stream is closed.
        doThrow(IOException.class).when(jis).close();
        m_SUT.handleImportBundle(1);
        
        //Mock IOException when creating jar input stream.
        PowerMockito.whenNew(JarInputStream.class).withArguments(Mockito.any(InputStream.class)).
            thenThrow(IOException.class);
        m_SUT.handleImportBundle(1);
        
        //Verify that all three of the possible IOExceptions are caught and handled appropriately when thrown.
        verify(m_GrowlUtil, times(3)).createLocalFacesMessage(eq(FacesMessage.SEVERITY_ERROR), eq("Bundle Error!"), 
                Mockito.anyString(), Mockito.any(IOException.class));
        
        //Mock no file being selected to install/update.
        m_File = null;
        m_SUT.setFile(m_File);
        m_SUT.handleImportBundle(1);
        
        //Verify that a growl message is appropriately displayed when no bundle is selected to install/update.
        verify(m_GrowlUtil).createLocalFacesMessage(eq(FacesMessage.SEVERITY_WARN), eq("No file selected!"), 
                Mockito.anyString());
    }
/**
     * Test installing and updating a bundle.
     * Verify that the appropriate message with correct contents is sent for both installing and updating a bundle.
     */
Test installing and updating a bundle.
Verify that the appropriate message with correct contents is sent for both installing and updating a bundle.
Test installing and updating a bundle . Verify that the appropriate message with correct contents is sent for both installing and updating a bundle .
@SuppressWarnings("unchecked")
    @Test
    public void testHandleImportBundle() throws IOException, Exception
    {
        BundleConfigurationMgrImpl bundleMgr = mock(BundleConfigurationMgrImpl.class);
        JarInputStream jis = PowerMockito.mock(JarInputStream.class);
        InputStream is = mock(InputStream.class);
        Manifest manifest = mock(Manifest.class);
        Attributes attributes = mock(Attributes.class);
        byte[] byteArr = {1, 2, 3, 4};
        when(m_File.getContents()).thenReturn(byteArr);
        when(m_File.getInputstream()).thenReturn(is);
        List<BundleModel> bundleList = new ArrayList<BundleModel>();
        BundleModel testBundle = mock(BundleModel.class);
        bundleList.add(testBundle);
        when(testBundle.getSymbolicName()).thenReturn("Some Name");
        when(testBundle.getBundleId()).thenReturn(Long.valueOf(5));
        m_SUT.setBundleConfigurationMgr(bundleMgr);
        when(bundleMgr.getBundles()).thenReturn(bundleList);
        PowerMockito.whenNew(JarInputStream.class).withArguments(Mockito.any(InputStream.class)).thenReturn(jis);
        when(jis.getManifest()).thenReturn(manifest);
        when(manifest.getMainAttributes()).thenReturn(attributes);
        when(attributes.getValue("Bundle-SymbolicName")).thenReturn("Some Name");
        ArgumentCaptor<BundleMessages.UpdateRequestData> updateCaptor = 
                ArgumentCaptor.forClass(BundleMessages.UpdateRequestData.class);
        m_SUT.handleImportBundle(1);
        verify(m_MessageFactory).createBundleMessage(eq(BundleMessageType.UpdateRequest), updateCaptor.capture());
        verify(m_MessageWrapper).queue(eq(1), (ResponseHandler) eq(null));
        UpdateRequestData updateMsg = updateCaptor.getValue();
        assertThat(updateMsg.getBundleId(), is(Long.valueOf(5)));
        assertThat(updateMsg.getBundleFile().toByteArray(), is(byteArr));
        bundleList.remove(0);
        when(m_File.getFileName()).thenReturn("File Name");
        m_SUT.handleImportBundle(1);
        ArgumentCaptor<BundleMessages.InstallRequestData> installCaptor = 
                ArgumentCaptor.forClass(BundleMessages.InstallRequestData.class);
        verify(m_MessageFactory).createBundleMessage(eq(BundleMessageType.InstallRequest), installCaptor.capture());
        verify(m_MessageWrapper, times(2)).queue(eq(1), (ResponseHandler) eq(null));
        InstallRequestData installMsg = installCaptor.getValue();
        assertThat(installMsg.getBundleLocation(), is("File Name"));
        assertThat(installMsg.getBundleFile().toByteArray(), is(byteArr));
        when(jis.getManifest()).thenReturn(null);
        m_SUT.handleImportBundle(1);
        doThrow(IOException.class).when(jis).close();
        m_SUT.handleImportBundle(1);
        PowerMockito.whenNew(JarInputStream.class).withArguments(Mockito.any(InputStream.class)).
            thenThrow(IOException.class);
        m_SUT.handleImportBundle(1);
        verify(m_GrowlUtil, times(3)).createLocalFacesMessage(eq(FacesMessage.SEVERITY_ERROR), eq("Bundle Error!"), 
                Mockito.anyString(), Mockito.any(IOException.class));
        m_File = null;
        m_SUT.setFile(m_File);
        m_SUT.handleImportBundle(1);
        verify(m_GrowlUtil).createLocalFacesMessage(eq(FacesMessage.SEVERITY_WARN), eq("No file selected!"), 
                Mockito.anyString());
    }
@ SuppressWarnings ( "unchecked" ) @ Test public void testHandleImportBundle ( ) throws IOException , Exception { BundleConfigurationMgrImpl bundleMgr = mock ( BundleConfigurationMgrImpl . class ) ; JarInputStream jis = PowerMockito . mock ( JarInputStream . class ) ; InputStream is = mock ( InputStream . class ) ; Manifest manifest = mock ( Manifest . class ) ; Attributes attributes = mock ( Attributes . class ) ; byte [ ] byteArr = { 1 , 2 , 3 , 4 } ; when ( m_File . getContents ( ) ) . thenReturn ( byteArr ) ; when ( m_File . getInputstream ( ) ) . thenReturn ( is ) ; List < BundleModel > bundleList = new ArrayList < BundleModel > ( ) ; BundleModel testBundle = mock ( BundleModel . class ) ; bundleList . add ( testBundle ) ; when ( testBundle . getSymbolicName ( ) ) . thenReturn ( "Some Name" ) ; when ( testBundle . getBundleId ( ) ) . thenReturn ( Long . valueOf ( 5 ) ) ; m_SUT . setBundleConfigurationMgr ( bundleMgr ) ; when ( bundleMgr . getBundles ( ) ) . thenReturn ( bundleList ) ; PowerMockito . whenNew ( JarInputStream . class ) . withArguments ( Mockito . any ( InputStream . class ) ) . thenReturn ( jis ) ; when ( jis . getManifest ( ) ) . thenReturn ( manifest ) ; when ( manifest . getMainAttributes ( ) ) . thenReturn ( attributes ) ; when ( attributes . getValue ( "Bundle-SymbolicName" ) ) . thenReturn ( "Some Name" ) ; ArgumentCaptor < BundleMessages . UpdateRequestData > updateCaptor = ArgumentCaptor . forClass ( BundleMessages . UpdateRequestData . class ) ; m_SUT . handleImportBundle ( 1 ) ; verify ( m_MessageFactory ) . createBundleMessage ( eq ( BundleMessageType . UpdateRequest ) , updateCaptor . capture ( ) ) ; verify ( m_MessageWrapper ) . queue ( eq ( 1 ) , ( ResponseHandler ) eq ( null ) ) ; UpdateRequestData updateMsg = updateCaptor . getValue ( ) ; assertThat ( updateMsg . getBundleId ( ) , is ( Long . valueOf ( 5 ) ) ) ; assertThat ( updateMsg . getBundleFile ( ) . toByteArray ( ) , is ( byteArr ) ) ; bundleList . remove ( 0 ) ; when ( m_File . getFileName ( ) ) . thenReturn ( "File Name" ) ; m_SUT . handleImportBundle ( 1 ) ; ArgumentCaptor < BundleMessages . InstallRequestData > installCaptor = ArgumentCaptor . forClass ( BundleMessages . InstallRequestData . class ) ; verify ( m_MessageFactory ) . createBundleMessage ( eq ( BundleMessageType . InstallRequest ) , installCaptor . capture ( ) ) ; verify ( m_MessageWrapper , times ( 2 ) ) . queue ( eq ( 1 ) , ( ResponseHandler ) eq ( null ) ) ; InstallRequestData installMsg = installCaptor . getValue ( ) ; assertThat ( installMsg . getBundleLocation ( ) , is ( "File Name" ) ) ; assertThat ( installMsg . getBundleFile ( ) . toByteArray ( ) , is ( byteArr ) ) ; when ( jis . getManifest ( ) ) . thenReturn ( null ) ; m_SUT . handleImportBundle ( 1 ) ; doThrow ( IOException . class ) . when ( jis ) . close ( ) ; m_SUT . handleImportBundle ( 1 ) ; PowerMockito . whenNew ( JarInputStream . class ) . withArguments ( Mockito . any ( InputStream . class ) ) . thenThrow ( IOException . class ) ; m_SUT . handleImportBundle ( 1 ) ; verify ( m_GrowlUtil , times ( 3 ) ) . createLocalFacesMessage ( eq ( FacesMessage . SEVERITY_ERROR ) , eq ( "Bundle Error!" ) , Mockito . anyString ( ) , Mockito . any ( IOException . class ) ) ; m_File = null ; m_SUT . setFile ( m_File ) ; m_SUT . handleImportBundle ( 1 ) ; verify ( m_GrowlUtil ) . createLocalFacesMessage ( eq ( FacesMessage . SEVERITY_WARN ) , eq ( "No file selected!" ) , Mockito . anyString ( ) ) ; }
Test installing and updating a bundle.
Test installing and updating a bundle .
//Mock all classes needed for the import method. //Mock jar file contents. //Mock methods for bundle configuration manager service. //Mock the jar input stream constructor and all methods pertaining to he jar input stream. //Argument captor to capture the update message. //replay first time to verify updating bundle. //Verify that an update message was sent. //Verify the contents of the update message. //Remove the bundle from the bundle manager list so that an install request can be tested for. //replay second time to verify installing bundle. //Argument captor to capture the install message. //Verify that an install request was sent. //Verify the contents of the install message. //Mock causing an IOException when the file being installed/updated is not a jar file. //Mock IOException when jar input stream is closed. //Mock IOException when creating jar input stream. //Verify that all three of the possible IOExceptions are caught and handled appropriately when thrown. //Mock no file being selected to install/update. //Verify that a growl message is appropriately displayed when no bundle is selected to install/update.