mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestMessageRouterImpl.java
@Test
    public void testHandleMessage() throws Exception
    {
        // construct a single base namespace message to verify sent to base message service
        BaseNamespace baseNamespaceMessage = BaseNamespace.newBuilder()
                .setType(BaseMessageType.ControllerInfo)
                .build();
        TerraHarvestPayload payload = TerraHarvestPayload.newBuilder().
            setNamespace(Namespace.Base).
            setNamespaceMessage(baseNamespaceMessage.toByteString()).
            build();
        
        TerraHarvestMessage baseMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, m_SystemId, Namespace.Base, 100, 
                        baseNamespaceMessage);
        
        // mock the channel the message came from
        RemoteChannel channel = mock(RemoteChannel.class);
        
        //decryption call
        when(m_EcryptService.decryptRemoteMessage(Mockito.any(TerraHarvestMessage.class))).thenReturn(payload);
        // replay without a message service registered, should not bomb out
        m_SUT.handleMessage(baseMessage, channel);
        
        // now register a service that will handle the message
        MessageService baseMessageService = mock(MessageService.class);
        when(baseMessageService.getNamespace()).thenReturn(Namespace.Base);
        m_SUT.bindMessageService(baseMessageService);
        
        // replay
        m_SUT.handleMessage(baseMessage, channel);
        
        // verify proper message service is sent the same message
        verify(baseMessageService).handleMessage(baseMessage, payload, channel);
        
        // now register a service that will handle a different namespace
        MessageService assetMessageService = mock(MessageService.class);
        when(assetMessageService.getNamespace()).thenReturn(Namespace.Asset);
        m_SUT.bindMessageService(assetMessageService);
        
        // replay
        m_SUT.handleMessage(baseMessage, channel);
        
        // verify proper message service is sent the same message, should be 2nd time
        verify(baseMessageService, times(2)).handleMessage(baseMessage, payload, channel);
        
        // set destination ID of message to max value
        TerraHarvestMessage maxIdMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, Integer.MAX_VALUE, Namespace.Base, 100, 
                        baseNamespaceMessage);
        
        // replay with message that has max destination ID, message should still be handled
        m_SUT.handleMessage(maxIdMessage, channel);
        
        // verify proper message service is sent the message with the max destination ID
        verify(baseMessageService).handleMessage(maxIdMessage, payload, channel);
        
        // create a new message of a different type
        TerraHarvestMessage assetMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, m_SystemId, Namespace.Asset, 100, 
                        baseNamespaceMessage);
        TerraHarvestPayload payload1 = TerraHarvestPayload.newBuilder().
                setNamespace(Namespace.Asset).
                setNamespaceMessage(baseNamespaceMessage.toByteString()).
                build();
        
        //decryption call
        when(m_EcryptService.decryptRemoteMessage(Mockito.any(TerraHarvestMessage.class))).thenReturn(payload1);
        
        // replay
        m_SUT.handleMessage(assetMessage, channel);
        
        // verify proper message service is sent the same message, base should still be at 3, asset at 1
        verify(baseMessageService, times(2)).handleMessage(baseMessage, payload, channel);
        verify(baseMessageService, times(1)).handleMessage(maxIdMessage, payload, channel);
        verify(assetMessageService, times(1)).handleMessage(assetMessage, payload1, channel);
    }
/**
     * Test the message handler.  Verifies messages are routed to proper MessageService.
     * 
     * Verify message are only routed once bound.
     */
Test the message handler.  Verifies messages are routed to proper MessageService.

Verify message are only routed once bound.
Test the message handler . Verifies messages are routed to proper MessageService . Verify message are only routed once bound .
@Test
    public void testHandleMessage() throws Exception
    {
        BaseNamespace baseNamespaceMessage = BaseNamespace.newBuilder()
                .setType(BaseMessageType.ControllerInfo)
                .build();
        TerraHarvestPayload payload = TerraHarvestPayload.newBuilder().
            setNamespace(Namespace.Base).
            setNamespaceMessage(baseNamespaceMessage.toByteString()).
            build();
        TerraHarvestMessage baseMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, m_SystemId, Namespace.Base, 100, 
                        baseNamespaceMessage);
        RemoteChannel channel = mock(RemoteChannel.class);
        when(m_EcryptService.decryptRemoteMessage(Mockito.any(TerraHarvestMessage.class))).thenReturn(payload);
        m_SUT.handleMessage(baseMessage, channel);
        MessageService baseMessageService = mock(MessageService.class);
        when(baseMessageService.getNamespace()).thenReturn(Namespace.Base);
        m_SUT.bindMessageService(baseMessageService);
        m_SUT.handleMessage(baseMessage, channel);
        verify(baseMessageService).handleMessage(baseMessage, payload, channel);
        MessageService assetMessageService = mock(MessageService.class);
        when(assetMessageService.getNamespace()).thenReturn(Namespace.Asset);
        m_SUT.bindMessageService(assetMessageService);
        m_SUT.handleMessage(baseMessage, channel);
        , should be 2nd time
        verify(baseMessageService, times(2)).handleMessage(baseMessage, payload, channel);
        TerraHarvestMessage maxIdMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, Integer.MAX_VALUE, Namespace.Base, 100, 
                        baseNamespaceMessage);
         with message that has max destination ID, message should still be handled
        m_SUT.handleMessage(maxIdMessage, channel);
        verify(baseMessageService).handleMessage(maxIdMessage, payload, channel);
        TerraHarvestMessage assetMessage = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(0, m_SystemId, Namespace.Asset, 100, 
                        baseNamespaceMessage);
        TerraHarvestPayload payload1 = TerraHarvestPayload.newBuilder().
                setNamespace(Namespace.Asset).
                setNamespaceMessage(baseNamespaceMessage.toByteString()).
                build();
        when(m_EcryptService.decryptRemoteMessage(Mockito.any(TerraHarvestMessage.class))).thenReturn(payload1);
        m_SUT.handleMessage(assetMessage, channel);
        , base should still be at 3, asset at 1
        verify(baseMessageService, times(2)).handleMessage(baseMessage, payload, channel);
        verify(baseMessageService, times(1)).handleMessage(maxIdMessage, payload, channel);
        verify(assetMessageService, times(1)).handleMessage(assetMessage, payload1, channel);
    }
@ Test public void testHandleMessage ( ) throws Exception { BaseNamespace baseNamespaceMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . ControllerInfo ) . build ( ) ; TerraHarvestPayload payload = TerraHarvestPayload . newBuilder ( ) . setNamespace ( Namespace . Base ) . setNamespaceMessage ( baseNamespaceMessage . toByteString ( ) ) . build ( ) ; TerraHarvestMessage baseMessage = TerraHarvestMessageHelper . createTerraHarvestMessage ( 0 , m_SystemId , Namespace . Base , 100 , baseNamespaceMessage ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; when ( m_EcryptService . decryptRemoteMessage ( Mockito . any ( TerraHarvestMessage . class ) ) ) . thenReturn ( payload ) ; m_SUT . handleMessage ( baseMessage , channel ) ; MessageService baseMessageService = mock ( MessageService . class ) ; when ( baseMessageService . getNamespace ( ) ) . thenReturn ( Namespace . Base ) ; m_SUT . bindMessageService ( baseMessageService ) ; m_SUT . handleMessage ( baseMessage , channel ) ; verify ( baseMessageService ) . handleMessage ( baseMessage , payload , channel ) ; MessageService assetMessageService = mock ( MessageService . class ) ; when ( assetMessageService . getNamespace ( ) ) . thenReturn ( Namespace . Asset ) ; m_SUT . bindMessageService ( assetMessageService ) ; m_SUT . handleMessage ( baseMessage , channel ) ; verify ( baseMessageService , times ( 2 ) ) . handleMessage ( baseMessage , payload , channel ) ; TerraHarvestMessage maxIdMessage = TerraHarvestMessageHelper . createTerraHarvestMessage ( 0 , Integer . MAX_VALUE , Namespace . Base , 100 , baseNamespaceMessage ) ; m_SUT . handleMessage ( maxIdMessage , channel ) ; verify ( baseMessageService ) . handleMessage ( maxIdMessage , payload , channel ) ; TerraHarvestMessage assetMessage = TerraHarvestMessageHelper . createTerraHarvestMessage ( 0 , m_SystemId , Namespace . Asset , 100 , baseNamespaceMessage ) ; TerraHarvestPayload payload1 = TerraHarvestPayload . newBuilder ( ) . setNamespace ( Namespace . Asset ) . setNamespaceMessage ( baseNamespaceMessage . toByteString ( ) ) . build ( ) ; when ( m_EcryptService . decryptRemoteMessage ( Mockito . any ( TerraHarvestMessage . class ) ) ) . thenReturn ( payload1 ) ; m_SUT . handleMessage ( assetMessage , channel ) ; verify ( baseMessageService , times ( 2 ) ) . handleMessage ( baseMessage , payload , channel ) ; verify ( baseMessageService , times ( 1 ) ) . handleMessage ( maxIdMessage , payload , channel ) ; verify ( assetMessageService , times ( 1 ) ) . handleMessage ( assetMessage , payload1 , channel ) ; }
Test the message handler.
Test the message handler .
// construct a single base namespace message to verify sent to base message service // mock the channel the message came from //decryption call // replay without a message service registered, should not bomb out // now register a service that will handle the message // replay // verify proper message service is sent the same message // now register a service that will handle a different namespace // replay // verify proper message service is sent the same message, should be 2nd time // set destination ID of message to max value // replay with message that has max destination ID, message should still be handled // verify proper message service is sent the message with the max destination ID // create a new message of a different type //decryption call // replay // verify proper message service is sent the same message, base should still be at 3, asset at 1