mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestBundleManagementMessageService.java
@Test
    public void testStopBundleBad() throws BundleException, IOException
    {
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);
        
        // mock behavior
        when(m_Context.getBundle(1L)).thenReturn(bundleA);
        
        //request
        StopRequestData request = StopRequestData.newBuilder().setBundleId(5L).build();
        TerraHarvestPayload payload = createPayload(request, BundleMessageType.StopRequest);

        TerraHarvestMessage message = createTerraHarvestMessage(request, BundleMessageType.StopRequest);

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

        // handle the message
        m_SUT.handleMessage(message, payload, channel);
        
        //verify, bundle should of been null
        verify(m_MessageFactory).createBaseErrorMessage(eq(message), eq(ErrorCode.INVALID_VALUE), Mockito.anyString());
        verify(m_ResponseWrapper).queue(channel);
        
        //try again mocking exception
        when(m_Context.getBundle(1L)).thenReturn(bundleA);
        request = StopRequestData.newBuilder().setBundleId(1L).build();
        payload = createPayload(request, BundleMessageType.StopRequest);

        message = createTerraHarvestMessage(request, BundleMessageType.StopRequest);
        
        //mock exception
        doThrow(new BundleException("Whammy!")).when(bundleA).stop();
        
        // handle the message
        m_SUT.handleMessage(message, payload, channel);
        
        //verify
        ArgumentCaptor<BundleNamespaceErrorData> responseCaptor = ArgumentCaptor.forClass(
            BundleNamespaceErrorData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.BundleNamespaceError),
            responseCaptor.capture());
        //reused channel
        verify(m_ResponseWrapper, times(2)).queue(channel);
        verify(bundleA).stop();
        BundleNamespaceErrorData error = responseCaptor.getValue();
        assertThat(error.getError(), is(BundleErrorCode.OSGiBundleException));
    }
/**
     * Test stop bundle request with exception and null bundle.
     * 
     * Verify that the correct responses are sent.
     */
Test stop bundle request with exception and null bundle.

Verify that the correct responses are sent.
Test stop bundle request with exception and null bundle . Verify that the correct responses are sent .
@Test
    public void testStopBundleBad() throws BundleException, IOException
    {
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);
        when(m_Context.getBundle(1L)).thenReturn(bundleA);
        StopRequestData request = StopRequestData.newBuilder().setBundleId(5L).build();
        TerraHarvestPayload payload = createPayload(request, BundleMessageType.StopRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(request, BundleMessageType.StopRequest);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        verify(m_MessageFactory).createBaseErrorMessage(eq(message), eq(ErrorCode.INVALID_VALUE), Mockito.anyString());
        verify(m_ResponseWrapper).queue(channel);
        when(m_Context.getBundle(1L)).thenReturn(bundleA);
        request = StopRequestData.newBuilder().setBundleId(1L).build();
        payload = createPayload(request, BundleMessageType.StopRequest);
        message = createTerraHarvestMessage(request, BundleMessageType.StopRequest);
        doThrow(new BundleException("Whammy!")).when(bundleA).stop();
        m_SUT.handleMessage(message, payload, channel);
        ArgumentCaptor<BundleNamespaceErrorData> responseCaptor = ArgumentCaptor.forClass(
            BundleNamespaceErrorData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.BundleNamespaceError),
            responseCaptor.capture());
        verify(m_ResponseWrapper, times(2)).queue(channel);
        verify(bundleA).stop();
        BundleNamespaceErrorData error = responseCaptor.getValue();
        assertThat(error.getError(), is(BundleErrorCode.OSGiBundleException));
    }
@ Test public void testStopBundleBad ( ) throws BundleException , IOException { Bundle bundleA = mock ( Bundle . class ) ; when ( bundleA . getBundleId ( ) ) . thenReturn ( 1L ) ; when ( m_Context . getBundle ( 1L ) ) . thenReturn ( bundleA ) ; StopRequestData request = StopRequestData . newBuilder ( ) . setBundleId ( 5L ) . build ( ) ; TerraHarvestPayload payload = createPayload ( request , BundleMessageType . StopRequest ) ; TerraHarvestMessage message = createTerraHarvestMessage ( request , BundleMessageType . StopRequest ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( m_MessageFactory ) . createBaseErrorMessage ( eq ( message ) , eq ( ErrorCode . INVALID_VALUE ) , Mockito . anyString ( ) ) ; verify ( m_ResponseWrapper ) . queue ( channel ) ; when ( m_Context . getBundle ( 1L ) ) . thenReturn ( bundleA ) ; request = StopRequestData . newBuilder ( ) . setBundleId ( 1L ) . build ( ) ; payload = createPayload ( request , BundleMessageType . StopRequest ) ; message = createTerraHarvestMessage ( request , BundleMessageType . StopRequest ) ; doThrow ( new BundleException ( "Whammy!" ) ) . when ( bundleA ) . stop ( ) ; m_SUT . handleMessage ( message , payload , channel ) ; ArgumentCaptor < BundleNamespaceErrorData > responseCaptor = ArgumentCaptor . forClass ( BundleNamespaceErrorData . class ) ; verify ( m_MessageFactory ) . createBundleResponseMessage ( eq ( message ) , eq ( BundleMessageType . BundleNamespaceError ) , responseCaptor . capture ( ) ) ; verify ( m_ResponseWrapper , times ( 2 ) ) . queue ( channel ) ; verify ( bundleA ) . stop ( ) ; BundleNamespaceErrorData error = responseCaptor . getValue ( ) ; assertThat ( error . getError ( ) , is ( BundleErrorCode . OSGiBundleException ) ) ; }
Test stop bundle request with exception and null bundle.
Test stop bundle request with exception and null bundle .
// mock behavior //request //mock the channel the message came from // handle the message //verify, bundle should of been null //try again mocking exception //mock exception // handle the message //verify //reused channel