mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestBundleManagementMessageService.java
@Test
    public void testGetBundles() throws IOException
    {
        //mock behavior
        when(m_Context.getBundles()).thenReturn(new Bundle[] {});

        //test that there are no bundles and the an empty list is returned
        TerraHarvestPayload payload = createPayload(null, BundleMessageType.GetBundlesRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(null, BundleMessageType.GetBundlesRequest);

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

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

        // verify event is posted for the get 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.GetBundlesRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((BundleNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
            is(getNamespaceMessage(message)));
        
        //verify empty list
        GetBundlesResponseData responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleIdCount(), is(0));
        
        //send again with bundles
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);
        Bundle bundleB = mock(Bundle.class);
        when(bundleB.getBundleId()).thenReturn(2L);
        Bundle bundleC = mock(Bundle.class);
        when(bundleC.getBundleId()).thenReturn(3L);
        when(m_Context.getBundles()).thenReturn(new Bundle[] { bundleA, bundleC, bundleB});
        
        // handle the message
        m_SUT.handleMessage(message, payload, channel);
        verify(m_MessageFactory, times(2)).createBundleResponseMessage(eq(message), 
                eq(BundleMessageType.GetBundlesResponse), responseCaptor.capture());
        //reused channel
        verify(m_ResponseWrapper, times(2)).queue(channel);
        
        //verify non-empty list
        responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleIdCount(), greaterThan(0));
        //verify all ids were returned
        assertThat(responseData.getBundleIdList(), hasItem(1L));
        assertThat(responseData.getBundleIdList(), hasItem(2L));
        assertThat(responseData.getBundleIdList(), hasItem(3L));
    }
/**
     * Test get bundles request.
     * 
     * Verify that the appropriate response is sent if there are no bundles, or if there are multiples.
     */
Test get bundles request.

Verify that the appropriate response is sent if there are no bundles, or if there are multiples.
Test get bundles request . Verify that the appropriate response is sent if there are no bundles or if there are multiples .
@Test
    public void testGetBundles() throws IOException
    {
        when(m_Context.getBundles()).thenReturn(new Bundle[] {});
        TerraHarvestPayload payload = createPayload(null, BundleMessageType.GetBundlesRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(null, BundleMessageType.GetBundlesRequest);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        ArgumentCaptor<GetBundlesResponseData> responseCaptor = ArgumentCaptor.forClass(
            GetBundlesResponseData.class);
        verify(m_MessageFactory).createBundleResponseMessage(eq(message), eq(BundleMessageType.GetBundlesResponse), 
            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.GetBundlesRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((BundleNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
            is(getNamespaceMessage(message)));
         empty list
        GetBundlesResponseData responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleIdCount(), is(0));
        Bundle bundleA = mock(Bundle.class);
        when(bundleA.getBundleId()).thenReturn(1L);
        Bundle bundleB = mock(Bundle.class);
        when(bundleB.getBundleId()).thenReturn(2L);
        Bundle bundleC = mock(Bundle.class);
        when(bundleC.getBundleId()).thenReturn(3L);
        when(m_Context.getBundles()).thenReturn(new Bundle[] { bundleA, bundleC, bundleB});
        m_SUT.handleMessage(message, payload, channel);
        verify(m_MessageFactory, times(2)).createBundleResponseMessage(eq(message), 
                eq(BundleMessageType.GetBundlesResponse), responseCaptor.capture());
        verify(m_ResponseWrapper, times(2)).queue(channel);
         non-empty list
        responseData = responseCaptor.getValue();
        assertThat(responseData.getBundleIdCount(), greaterThan(0));
         all ids were returned
        assertThat(responseData.getBundleIdList(), hasItem(1L));
        assertThat(responseData.getBundleIdList(), hasItem(2L));
        assertThat(responseData.getBundleIdList(), hasItem(3L));
    }
@ Test public void testGetBundles ( ) throws IOException { when ( m_Context . getBundles ( ) ) . thenReturn ( new Bundle [ ] { } ) ; TerraHarvestPayload payload = createPayload ( null , BundleMessageType . GetBundlesRequest ) ; TerraHarvestMessage message = createTerraHarvestMessage ( null , BundleMessageType . GetBundlesRequest ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; ArgumentCaptor < GetBundlesResponseData > responseCaptor = ArgumentCaptor . forClass ( GetBundlesResponseData . class ) ; verify ( m_MessageFactory ) . createBundleResponseMessage ( eq ( message ) , eq ( BundleMessageType . GetBundlesResponse ) , 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 . GetBundlesRequest . toString ( ) ) ) ; assertThat ( ( RemoteChannel ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; assertThat ( ( BundleNamespace ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_NAMESPACE_MESSAGE ) , is ( getNamespaceMessage ( message ) ) ) ; GetBundlesResponseData responseData = responseCaptor . getValue ( ) ; assertThat ( responseData . getBundleIdCount ( ) , is ( 0 ) ) ; Bundle bundleA = mock ( Bundle . class ) ; when ( bundleA . getBundleId ( ) ) . thenReturn ( 1L ) ; Bundle bundleB = mock ( Bundle . class ) ; when ( bundleB . getBundleId ( ) ) . thenReturn ( 2L ) ; Bundle bundleC = mock ( Bundle . class ) ; when ( bundleC . getBundleId ( ) ) . thenReturn ( 3L ) ; when ( m_Context . getBundles ( ) ) . thenReturn ( new Bundle [ ] { bundleA , bundleC , bundleB } ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( m_MessageFactory , times ( 2 ) ) . createBundleResponseMessage ( eq ( message ) , eq ( BundleMessageType . GetBundlesResponse ) , responseCaptor . capture ( ) ) ; verify ( m_ResponseWrapper , times ( 2 ) ) . queue ( channel ) ; responseData = responseCaptor . getValue ( ) ; assertThat ( responseData . getBundleIdCount ( ) , greaterThan ( 0 ) ) ; assertThat ( responseData . getBundleIdList ( ) , hasItem ( 1L ) ) ; assertThat ( responseData . getBundleIdList ( ) , hasItem ( 2L ) ) ; assertThat ( responseData . getBundleIdList ( ) , hasItem ( 3L ) ) ; }
Test get bundles request.
Test get bundles request .
//mock behavior //test that there are no bundles and the an empty list is returned //mock the channel the message came from // handle the message //verify // verify event is posted for the get bundle request //verify empty list //send again with bundles // handle the message //reused channel //verify non-empty list //verify all ids were returned