mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestMetatypeMessageService.java
@Test
    public void testGetAttributeDefinitionInvalid() throws IOException
    {
      //mock 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);
        Bundle coreBundle = mock(Bundle.class);
        when(coreBundle.getBundleId()).thenReturn(0L);
        when(m_Context.getBundles()).thenReturn(new Bundle[] { bundleA, bundleC, bundleB, coreBundle});
        //mock meta type information for the bundles
        MetaTypeInformation infoA = mock(MetaTypeInformation.class);
        when(infoA.getPids()).thenReturn(new String[] {"configA", "configF"});
        when(infoA.getFactoryPids()).thenReturn(new String[] {"FactoryconfigA", "FactoryconfigF"});
        MetaTypeInformation infoB = mock(MetaTypeInformation.class);
        when(infoB.getPids()).thenReturn(new String[] {});
        when(infoB.getFactoryPids()).thenReturn(new String[] {"FactoryConfigG"});
        MetaTypeInformation infoC = mock(MetaTypeInformation.class);
        when(infoC.getPids()).thenReturn(new String[] {"configZ", "configD"});
        when(infoC.getFactoryPids()).thenReturn(new String[] {});
        //mock core meta information
        MetaTypeInformation infoCore = mock(MetaTypeInformation.class);
        when(infoCore.getPids()).thenReturn(new String[] {"core-pid", "configD"});
        when(infoCore.getFactoryPids()).thenReturn(new String[] {});
        when(m_MetaTypeService.getMetaTypeInformation(coreBundle)).thenReturn(infoCore);
        ObjectClassDefinition ocd = mock(ObjectClassDefinition.class);
        when(infoCore.getObjectClassDefinition("core-pid", null)).thenReturn(ocd);
        stubAttributeDefs(ocd);
        
        //testing not found bundle/pid
        ObjectClassDefinition ocdBad  = mock(ObjectClassDefinition.class);
        when(infoCore.getObjectClassDefinition("Swan", null)).thenReturn(ocdBad);
        GetAttributeDefinitionRequestData request = GetAttributeDefinitionRequestData.newBuilder().
            setBundleId(900).
            setPid("Swan").
            setKey("da key").build();
        TerraHarvestPayload payload = createPayload(request, MetaTypeMessageType.GetAttributeDefinitionRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(request, MetaTypeMessageType.
            GetAttributeDefinitionRequest);

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

        // handle the message
        m_SUT.handleMessage(message, payload, channel);

        //capture the message, verify error code
        verify(m_EventAdmin).postEvent(Mockito.any(Event.class));
        verify(m_MessageFactory).createBaseErrorMessage(message, ErrorCode.INVALID_VALUE, 
                "The bundle id 900, and pid Swan did not return any attribute definitions, key da key was ignored.");
        verify(m_ResponseWrapper).queue(channel);

        //new request with attribute returned, but the key not found
        request = GetAttributeDefinitionRequestData.newBuilder().
            setBundleId(0).
            setPid("core-pid").
            setKey("da key").build();
        payload = createPayload(request, MetaTypeMessageType.GetAttributeDefinitionRequest);
        message = createTerraHarvestMessage(request, MetaTypeMessageType.
            GetAttributeDefinitionRequest);

        //handle message
        m_SUT.handleMessage(message, payload, channel);

        //capture the message, verify error code
        verify(m_MessageFactory).createBaseErrorMessage(message, ErrorCode.INVALID_VALUE, 
                "Attribute definitions were found for the bundle id 0, and "
                        + "pid core-pid, but the attribute matching the key da key was not found.");
        //verify the queued to channel
        verify(m_ResponseWrapper, times(2)).queue(channel);

        // verify event is posted for get attribute definition request
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, times(2)).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.MetaType.
                toString()));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE_TYPE), 
                is(MetaTypeMessageType.GetAttributeDefinitionRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((MetaTypeNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
                is(getNamespaceMessage(message)));
    }
/**
     * Test that if the bundle id/pid does not return attributes and a key value is set that the 
     * GetAttributeDefinitionRequest is properly handled.
     * 
     * Verify error code INVALID_VALUE is sent when no attributes are returned from the MetaType service and a key is 
     * set.
     * 
     * Verify error code INVALID_VALUE is sent when attributes are returned, but key does not match an attribute id in  
     * the returned attributes from the MetaType service.
     * 
     * Verify event is posted with correct data message.
     */
Test that if the bundle id/pid does not return attributes and a key value is set that the
GetAttributeDefinitionRequest is properly handled.

Verify error code INVALID_VALUE is sent when no attributes are returned from the MetaType service and a key is
set.

Verify error code INVALID_VALUE is sent when attributes are returned, but key does not match an attribute id in
the returned attributes from the MetaType service.

Verify event is posted with correct data message.
Test that if the bundle id / pid does not return attributes and a key value is set that the GetAttributeDefinitionRequest is properly handled . Verify error code INVALID_VALUE is sent when no attributes are returned from the MetaType service and a key is set . Verify error code INVALID_VALUE is sent when attributes are returned but key does not match an attribute id in the returned attributes from the MetaType service . Verify event is posted with correct data message .
@Test
    public void testGetAttributeDefinitionInvalid() throws IOException
    {
        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);
        Bundle coreBundle = mock(Bundle.class);
        when(coreBundle.getBundleId()).thenReturn(0L);
        when(m_Context.getBundles()).thenReturn(new Bundle[] { bundleA, bundleC, bundleB, coreBundle});
        MetaTypeInformation infoA = mock(MetaTypeInformation.class);
        when(infoA.getPids()).thenReturn(new String[] {"configA", "configF"});
        when(infoA.getFactoryPids()).thenReturn(new String[] {"FactoryconfigA", "FactoryconfigF"});
        MetaTypeInformation infoB = mock(MetaTypeInformation.class);
        when(infoB.getPids()).thenReturn(new String[] {});
        when(infoB.getFactoryPids()).thenReturn(new String[] {"FactoryConfigG"});
        MetaTypeInformation infoC = mock(MetaTypeInformation.class);
        when(infoC.getPids()).thenReturn(new String[] {"configZ", "configD"});
        when(infoC.getFactoryPids()).thenReturn(new String[] {});
        MetaTypeInformation infoCore = mock(MetaTypeInformation.class);
        when(infoCore.getPids()).thenReturn(new String[] {"core-pid", "configD"});
        when(infoCore.getFactoryPids()).thenReturn(new String[] {});
        when(m_MetaTypeService.getMetaTypeInformation(coreBundle)).thenReturn(infoCore);
        ObjectClassDefinition ocd = mock(ObjectClassDefinition.class);
        when(infoCore.getObjectClassDefinition("core-pid", null)).thenReturn(ocd);
        stubAttributeDefs(ocd);
        ObjectClassDefinition ocdBad  = mock(ObjectClassDefinition.class);
        when(infoCore.getObjectClassDefinition("Swan", null)).thenReturn(ocdBad);
        GetAttributeDefinitionRequestData request = GetAttributeDefinitionRequestData.newBuilder().
            setBundleId(900).
            setPid("Swan").
            setKey("da key").build();
        TerraHarvestPayload payload = createPayload(request, MetaTypeMessageType.GetAttributeDefinitionRequest);
        TerraHarvestMessage message = createTerraHarvestMessage(request, MetaTypeMessageType.
            GetAttributeDefinitionRequest);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        verify(m_EventAdmin).postEvent(Mockito.any(Event.class));
        verify(m_MessageFactory).createBaseErrorMessage(message, ErrorCode.INVALID_VALUE, 
                "The bundle id 900, and pid Swan did not return any attribute definitions, key da key was ignored.");
        verify(m_ResponseWrapper).queue(channel);
        request = GetAttributeDefinitionRequestData.newBuilder().
            setBundleId(0).
            setPid("core-pid").
            setKey("da key").build();
        payload = createPayload(request, MetaTypeMessageType.GetAttributeDefinitionRequest);
        message = createTerraHarvestMessage(request, MetaTypeMessageType.
            GetAttributeDefinitionRequest);
        m_SUT.handleMessage(message, payload, channel);
        verify(m_MessageFactory).createBaseErrorMessage(message, ErrorCode.INVALID_VALUE, 
                "Attribute definitions were found for the bundle id 0, and "
                        + "pid core-pid, but the attribute matching the key da key was not found.");
        verify(m_ResponseWrapper, times(2)).queue(channel);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, times(2)).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.MetaType.
                toString()));
        assertThat((String)postedEvent.getProperty(RemoteConstants.EVENT_PROP_MESSAGE_TYPE), 
                is(MetaTypeMessageType.GetAttributeDefinitionRequest.toString()));
        assertThat((RemoteChannel)postedEvent.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((MetaTypeNamespace)postedEvent.getProperty(RemoteConstants.EVENT_PROP_NAMESPACE_MESSAGE), 
                is(getNamespaceMessage(message)));
    }
@ Test public void testGetAttributeDefinitionInvalid ( ) throws IOException { 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 ) ; Bundle coreBundle = mock ( Bundle . class ) ; when ( coreBundle . getBundleId ( ) ) . thenReturn ( 0L ) ; when ( m_Context . getBundles ( ) ) . thenReturn ( new Bundle [ ] { bundleA , bundleC , bundleB , coreBundle } ) ; MetaTypeInformation infoA = mock ( MetaTypeInformation . class ) ; when ( infoA . getPids ( ) ) . thenReturn ( new String [ ] { "configA" , "configF" } ) ; when ( infoA . getFactoryPids ( ) ) . thenReturn ( new String [ ] { "FactoryconfigA" , "FactoryconfigF" } ) ; MetaTypeInformation infoB = mock ( MetaTypeInformation . class ) ; when ( infoB . getPids ( ) ) . thenReturn ( new String [ ] { } ) ; when ( infoB . getFactoryPids ( ) ) . thenReturn ( new String [ ] { "FactoryConfigG" } ) ; MetaTypeInformation infoC = mock ( MetaTypeInformation . class ) ; when ( infoC . getPids ( ) ) . thenReturn ( new String [ ] { "configZ" , "configD" } ) ; when ( infoC . getFactoryPids ( ) ) . thenReturn ( new String [ ] { } ) ; MetaTypeInformation infoCore = mock ( MetaTypeInformation . class ) ; when ( infoCore . getPids ( ) ) . thenReturn ( new String [ ] { "core-pid" , "configD" } ) ; when ( infoCore . getFactoryPids ( ) ) . thenReturn ( new String [ ] { } ) ; when ( m_MetaTypeService . getMetaTypeInformation ( coreBundle ) ) . thenReturn ( infoCore ) ; ObjectClassDefinition ocd = mock ( ObjectClassDefinition . class ) ; when ( infoCore . getObjectClassDefinition ( "core-pid" , null ) ) . thenReturn ( ocd ) ; stubAttributeDefs ( ocd ) ; ObjectClassDefinition ocdBad = mock ( ObjectClassDefinition . class ) ; when ( infoCore . getObjectClassDefinition ( "Swan" , null ) ) . thenReturn ( ocdBad ) ; GetAttributeDefinitionRequestData request = GetAttributeDefinitionRequestData . newBuilder ( ) . setBundleId ( 900 ) . setPid ( "Swan" ) . setKey ( "da key" ) . build ( ) ; TerraHarvestPayload payload = createPayload ( request , MetaTypeMessageType . GetAttributeDefinitionRequest ) ; TerraHarvestMessage message = createTerraHarvestMessage ( request , MetaTypeMessageType . GetAttributeDefinitionRequest ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( m_EventAdmin ) . postEvent ( Mockito . any ( Event . class ) ) ; verify ( m_MessageFactory ) . createBaseErrorMessage ( message , ErrorCode . INVALID_VALUE , "The bundle id 900, and pid Swan did not return any attribute definitions, key da key was ignored." ) ; verify ( m_ResponseWrapper ) . queue ( channel ) ; request = GetAttributeDefinitionRequestData . newBuilder ( ) . setBundleId ( 0 ) . setPid ( "core-pid" ) . setKey ( "da key" ) . build ( ) ; payload = createPayload ( request , MetaTypeMessageType . GetAttributeDefinitionRequest ) ; message = createTerraHarvestMessage ( request , MetaTypeMessageType . GetAttributeDefinitionRequest ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( m_MessageFactory ) . createBaseErrorMessage ( message , ErrorCode . INVALID_VALUE , "Attribute definitions were found for the bundle id 0, and " + "pid core-pid, but the attribute matching the key da key was not found." ) ; verify ( m_ResponseWrapper , times ( 2 ) ) . queue ( channel ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin , times ( 2 ) ) . 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 . MetaType . toString ( ) ) ) ; assertThat ( ( String ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_MESSAGE_TYPE ) , is ( MetaTypeMessageType . GetAttributeDefinitionRequest . toString ( ) ) ) ; assertThat ( ( RemoteChannel ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; assertThat ( ( MetaTypeNamespace ) postedEvent . getProperty ( RemoteConstants . EVENT_PROP_NAMESPACE_MESSAGE ) , is ( getNamespaceMessage ( message ) ) ) ; }
Test that if the bundle id/pid does not return attributes and a key value is set that the
 GetAttributeDefinitionRequest is properly handled.
Test that if the bundle id / pid does not return attributes and a key value is set that the GetAttributeDefinitionRequest is properly handled .
//mock bundles //mock meta type information for the bundles //mock core meta information //testing not found bundle/pid //mock the channel the message came from // handle the message //capture the message, verify error code //new request with attribute returned, but the key not found //handle message //capture the message, verify error code //verify the queued to channel // verify event is posted for get attribute definition request