mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestBundleManagementMessageService.java
@Test
    public void testInstallBundle() throws BundleException, IOException
    {
        //mock bundle
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);

        //mock behavior
        when(m_Context.installBundle(anyString(), Mockito.any(InputStream.class))).thenReturn(bundleA);

        //request
        InstallRequestData request = InstallRequestData.newBuilder().
            setBundleLocation("org.those.x.Bundle").
            setBundleFile(ByteString.copyFrom(new byte[] { 0, 1, 3, 5, 9, (byte)0xff, 0x3f, 0x56, (byte)0xdd, 18})).
            setStartAtInstall(true).
            build();
        TerraHarvestPayload payload = createPayload(request, BundleMessageType.InstallRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(request, BundleMessageType.InstallRequest);

        //mock the channel the message came from
        RemoteChannel channel = mock(RemoteChannel.class);

        // handle the message
        m_SUT.handleMessage(message, payload, channel);
        
        //verify
        ArgumentCaptor<InstallResponseData> responseCaptor = ArgumentCaptor.forClass(
            InstallResponseData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.InstallResponse), 
            responseCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);

        // verify event is posted for install bundle request
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        Event postedEvent = eventCaptor.getValue();
        assertThat(postedEvent.getTopic(), is(RemoteConstants.TOPIC_MESSAGE_RECEIVED));
        assertThat((TerraHarvestMessage)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE), is(message));
        assertThat((Integer)postedEvent.getProperty(RemoteConstants.EVENT_PROP_SOURCE_ID), is(0));
        assertThat((Integer)postedEvent.getProperty(RemoteConstants.EVENT_PROP_DEST_ID), is(1));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE), is(Namespace.Bundle.
            toString()));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE_TYPE), 
            is(BundleMessageType.InstallRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((BundleNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
            is(getNamespaceMessage(message)));
        
        ArgumentCaptor<InputStream> stream = ArgumentCaptor.forClass(InputStream.class);
        verify(m_Context).installBundle(eq("org.those.x.Bundle"), stream.capture());
        verify(bundleA).start();
        
        byte[] actualBytes = new byte[10];
        stream.getValue().read(actualBytes);
        assertThat(actualBytes, is(new byte[] { 0, 1, 3, 5, 9, (byte)0xff, 0x3f, 0x56, (byte)0xdd, 18}));
        
        //verify bundle id returned
        InstallResponseData responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleId(), is (1L));
        
        when(m_Context.installBundle(anyString(), Mockito.any(InputStream.class))).
            thenThrow(new BundleException("test"));
        
        // handle the message again... expecting exception
        m_SUT.handleMessage(message, payload, channel);
        
        //verify
        ArgumentCaptor<BundleNamespaceErrorData> responseCaptorError = ArgumentCaptor.forClass(
            BundleNamespaceErrorData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.BundleNamespaceError),
            responseCaptorError.capture());
        //reused channel
        verify(m_ResponseWrapper, times(2)).queue(channel);
        BundleNamespaceErrorData error = responseCaptorError.getValue();
        assertThat(error.getError(), is(BundleErrorCode.OSGiBundleException));
    }
/**
     * Test install bundle request.
     * 
     * Verify that the correct responses are sent.
     */
Test install bundle request.

Verify that the correct responses are sent.
Test install bundle request . Verify that the correct responses are sent .
@Test
    public void testInstallBundle() throws BundleException, IOException
    {
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);
        when(m_Context.installBundle(anyString(), Mockito.any(InputStream.class))).thenReturn(bundleA);
        InstallRequestData request = InstallRequestData.newBuilder().
            setBundleLocation("org.those.x.Bundle").
            setBundleFile(ByteString.copyFrom(new byte[] { 0, 1, 3, 5, 9, (byte)0xff, 0x3f, 0x56, (byte)0xdd, 18})).
            setStartAtInstall(true).
            build();
        TerraHarvestPayload payload = createPayload(request, BundleMessageType.InstallRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(request, BundleMessageType.InstallRequest);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        ArgumentCaptor<InstallResponseData> responseCaptor = ArgumentCaptor.forClass(
            InstallResponseData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.InstallResponse), 
            responseCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        Event postedEvent = eventCaptor.getValue();
        assertThat(postedEvent.getTopic(), is(RemoteConstants.TOPIC_MESSAGE_RECEIVED));
        assertThat((TerraHarvestMessage)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE), is(message));
        assertThat((Integer)postedEvent.getProperty(RemoteConstants.EVENT_PROP_SOURCE_ID), is(0));
        assertThat((Integer)postedEvent.getProperty(RemoteConstants.EVENT_PROP_DEST_ID), is(1));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE), is(Namespace.Bundle.
            toString()));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE_TYPE), 
            is(BundleMessageType.InstallRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((BundleNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
            is(getNamespaceMessage(message)));
        ArgumentCaptor<InputStream> stream = ArgumentCaptor.forClass(InputStream.class);
        verify(m_Context).installBundle(eq("org.those.x.Bundle"), stream.capture());
        verify(bundleA).start();
        byte[] actualBytes = new byte[10];
        stream.getValue().read(actualBytes);
        assertThat(actualBytes, is(new byte[] { 0, 1, 3, 5, 9, (byte)0xff, 0x3f, 0x56, (byte)0xdd, 18}));
         bundle id returned
        InstallResponseData responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleId(), is (1L));
        when(m_Context.installBundle(anyString(), Mockito.any(InputStream.class))).
            thenThrow(new BundleException("test"));
         again... expecting exception
        m_SUT.handleMessage(message, payload, channel);
        ArgumentCaptor<BundleNamespaceErrorData> responseCaptorError = ArgumentCaptor.forClass(
            BundleNamespaceErrorData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.BundleNamespaceError),
            responseCaptorError.capture());
        verify(m_ResponseWrapper, times(2)).queue(channel);
        BundleNamespaceErrorData error = responseCaptorError.getValue();
        assertThat(error.getError(), is(BundleErrorCode.OSGiBundleException));
    }
@ Test public void testInstallBundle ( ) throws BundleException , IOException { Bundle bundleA = mock ( Bundle . class ) ; when ( bundleA . getBundleId ( ) ) . thenReturn ( 1L ) ; when ( m_Context . installBundle ( anyString ( ) , Mockito . any ( InputStream . class ) ) ) . thenReturn ( bundleA ) ; InstallRequestData request = InstallRequestData . newBuilder ( ) . setBundleLocation ( "org.those.x.Bundle" ) . setBundleFile ( ByteString . copyFrom ( new byte [ ] { 0 , 1 , 3 , 5 , 9 , ( byte ) 0xff , 0x3f , 0x56 , ( byte ) 0xdd , 18 } ) ) . setStartAtInstall ( true ) . build ( ) ; TerraHarvestPayload payload = createPayload ( request , BundleMessageType . InstallRequest ) ; TerraHarvestMessage message = createTerraHarvestMessage ( request , BundleMessageType . InstallRequest ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; ArgumentCaptor < InstallResponseData > responseCaptor = ArgumentCaptor . forClass ( InstallResponseData . class ) ; verify ( m_MessageFactory ) . createBundleResponseMessage ( eq ( message ) , eq ( BundleMessageType . InstallResponse ) , responseCaptor . capture ( ) ) ; verify ( m_ResponseWrapper ) . queue ( channel ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin ) . postEvent ( eventCaptor . capture ( ) ) ; Event postedEvent = eventCaptor . getValue ( ) ; assertThat ( postedEvent . getTopic ( ) , is ( RemoteConstants . TOPIC_MESSAGE_RECEIVED ) ) ; assertThat ( ( TerraHarvestMessage ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_MESSAGE ) , is ( message ) ) ; assertThat ( ( Integer ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_SOURCE_ID ) , is ( 0 ) ) ; assertThat ( ( Integer ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_DEST_ID ) , is ( 1 ) ) ; assertThat ( ( String ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_NAMESPACE ) , is ( Namespace . Bundle . toString ( ) ) ) ; assertThat ( ( String ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_MESSAGE_TYPE ) , is ( BundleMessageType . InstallRequest . toString ( ) ) ) ; assertThat ( ( RemoteChannel ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; assertThat ( ( BundleNamespace ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_NAMESPACE_MESSAGE ) , is ( getNamespaceMessage ( message ) ) ) ; ArgumentCaptor < InputStream > stream = ArgumentCaptor . forClass ( InputStream . class ) ; verify ( m_Context ) . installBundle ( eq ( "org.those.x.Bundle" ) , stream . capture ( ) ) ; verify ( bundleA ) . start ( ) ; byte [ ] actualBytes = new byte [ 10 ] ; stream . getValue ( ) . read ( actualBytes ) ; assertThat ( actualBytes , is ( new byte [ ] { 0 , 1 , 3 , 5 , 9 , ( byte ) 0xff , 0x3f , 0x56 , ( byte ) 0xdd , 18 } ) ) ; InstallResponseData responseData = responseCaptor . getValue ( ) ; assertThat ( responseData . getBundleId ( ) , is ( 1L ) ) ; when ( m_Context . installBundle ( anyString ( ) , Mockito . any ( InputStream . class ) ) ) . thenThrow ( new BundleException ( "test" ) ) ; m_SUT . handleMessage ( message , payload , channel ) ; ArgumentCaptor < BundleNamespaceErrorData > responseCaptorError = ArgumentCaptor . forClass ( BundleNamespaceErrorData . class ) ; verify ( m_MessageFactory ) . createBundleResponseMessage ( eq ( message ) , eq ( BundleMessageType . BundleNamespaceError ) , responseCaptorError . capture ( ) ) ; verify ( m_ResponseWrapper , times ( 2 ) ) . queue ( channel ) ; BundleNamespaceErrorData error = responseCaptorError . getValue ( ) ; assertThat ( error . getError ( ) , is ( BundleErrorCode . OSGiBundleException ) ) ; }
Test install bundle request.
Test install bundle request .
//mock bundle //mock behavior //request //mock the channel the message came from // handle the message //verify // verify event is posted for install bundle request //verify bundle id returned // handle the message again... expecting exception //verify //reused channel