mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRun() throws InterruptedException, IOException
    {
        //disable logging
        when(m_RemoteSettings.isLogRemoteMessagesEnabled()).thenReturn(false);
    
        int sourceId = 100;
        
        // setup the message to be received
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 100, baseMessage);
        
        final TerraHarvestMessage message2 = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 2, Namespace.Base, 101, baseMessage);
        
        final TerraHarvestMessage message3 = 
              TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId + 100, 1, Namespace.Base, 102, baseMessage);
     
        // simulate received message from socket
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        message2.writeDelimitedTo(output);
        message3.writeDelimitedTo(output);
        
        InputStream inStream = spy(new ByteArrayInputStream(output.toByteArray()));
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        // mock the channel that will be sent in map
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        // the socket should read in each message and parse as the same message
        verify(m_MessageRouter, timeout(1000)).handleMessage(message, channel);
        verify(m_MessageRouter, timeout(1000)).handleMessage(message2, channel);
        verify(m_MessageRouter, timeout(1000)).handleMessage(message3, channel);
        
        // verify event is sent to notify remotechannellookupimpl to sync channel, should happen once
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        //give time for thread to start and for mock interactions to register
        verify(m_EventAdmin, timeout(1000)).sendEvent(eventCaptor.capture());
        
        //Need to iterate through events this way. End of input stream is executed before 
        //a check can be made for the NEW/CHANGED topic. This is because input stream is a 
        //set size and the message sent on the stream is the only data.
        List<Event> postedEvents = eventCaptor.getAllValues();
        
        //Assert that the first message caused a new or changed channel id event.
        Event event1 = postedEvents.get(0);
        assertThat(event1.getTopic(), is(RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID));
        assertThat((AbstractSocketChannel)event1.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((Integer)event1.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(sourceId));

        // verify event is posted to notify remotechannellookupimpl to remove channel
        verify(m_EventAdmin, timeout(1000)).postEvent(eventCaptor.capture());
        
        //Assert that a remove channel event is posted.
        Event event3 = eventCaptor.getValue();
        assertThat(event3.getTopic(), is(RemoteConstants.TOPIC_REMOVE_CHANNEL));
        assertThat((AbstractSocketChannel)event3.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        
        //SocketMessageListener exits out because input stream passed to message listener is a set size. 
        // therefore deactivate does not need to be called. (i.e. the message read in is the only message
        // on the stream).
        thread.join(100);
        
        // ensure input stream and socket gets closed
        verify(inStream).close();
        verify(socket).close();
        
        // verify bytes received is correct
        Long expectedBytesReceived = 
                (long)(message.getSerializedSize() + message2.getSerializedSize() + message3.getSerializedSize());
        verify(channel).setBytesReceived(expectedBytesReceived);
    
        // verify no logging
        verify(m_Logging, times(0)).debug(anyString());
    }
/**
     * Verify the thread will read messages as long as running and passed to router, also verify event for 
     * syncing of addresses is posted to the Event Admin.
     * 
     * Verify cleanup of socket and stream before thread stops as well as removing socket from lookup
     * 
     * Test that the channel is updated with the number of bytes received.
     * 
     * Test that logging does not occur if disabled.
     */
Verify the thread will read messages as long as running and passed to router, also verify event for
syncing of addresses is posted to the Event Admin.

Verify cleanup of socket and stream before thread stops as well as removing socket from lookup

Test that the channel is updated with the number of bytes received.

Test that logging does not occur if disabled.
Verify the thread will read messages as long as running and passed to router also verify event for syncing of addresses is posted to the Event Admin . Verify cleanup of socket and stream before thread stops as well as removing socket from lookup Test that the channel is updated with the number of bytes received . Test that logging does not occur if disabled .
@Test
    public void testRun() throws InterruptedException, IOException
    {
        when(m_RemoteSettings.isLogRemoteMessagesEnabled()).thenReturn(false);
        int sourceId = 100;
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 100, baseMessage);
        final TerraHarvestMessage message2 = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 2, Namespace.Base, 101, baseMessage);
        final TerraHarvestMessage message3 = 
              TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId + 100, 1, Namespace.Base, 102, baseMessage);
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        message2.writeDelimitedTo(output);
        message3.writeDelimitedTo(output);
        InputStream inStream = spy(new ByteArrayInputStream(output.toByteArray()));
        when(socket.getInputStream()).thenReturn(inStream);
        props.put(SocketMessageListener.SOCKET_PROP_KEY, socket);
        AbstractSocketChannel channel = mock(AbstractSocketChannel.class);
        props.put(SocketMessageListener.CHANNEL_PROP_KEY, channel);
        m_SUT.activate(props);
        Thread thread = new Thread(m_SUT);
        thread.start();
        verify(m_MessageRouter, timeout(1000)).handleMessage(message, channel);
        verify(m_MessageRouter, timeout(1000)).handleMessage(message2, channel);
        verify(m_MessageRouter, timeout(1000)).handleMessage(message3, channel);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, timeout(1000)).sendEvent(eventCaptor.capture());
        List<Event> postedEvents = eventCaptor.getAllValues();
        Event event1 = postedEvents.get(0);
        assertThat(event1.getTopic(), is(RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID));
        assertThat((AbstractSocketChannel)event1.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((Integer)event1.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(sourceId));
        verify(m_EventAdmin, timeout(1000)).postEvent(eventCaptor.capture());
        Event event3 = eventCaptor.getValue();
        assertThat(event3.getTopic(), is(RemoteConstants.TOPIC_REMOVE_CHANNEL));
        assertThat((AbstractSocketChannel)event3.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        thread.join(100);
        verify(inStream).close();
        verify(socket).close();
        Long expectedBytesReceived = 
                (long)(message.getSerializedSize() + message2.getSerializedSize() + message3.getSerializedSize());
        verify(channel).setBytesReceived(expectedBytesReceived);
        verify(m_Logging, times(0)).debug(anyString());
    }
@ Test public void testRun ( ) throws InterruptedException , IOException { when ( m_RemoteSettings . isLogRemoteMessagesEnabled ( ) ) . thenReturn ( false ) ; int sourceId = 100 ; BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . build ( ) ; final TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( sourceId , 1 , Namespace . Base , 100 , baseMessage ) ; final TerraHarvestMessage message2 = TerraHarvestMessageHelper . createTerraHarvestMessage ( sourceId , 2 , Namespace . Base , 101 , baseMessage ) ; final TerraHarvestMessage message3 = TerraHarvestMessageHelper . createTerraHarvestMessage ( sourceId + 100 , 1 , Namespace . Base , 102 , baseMessage ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; message . writeDelimitedTo ( output ) ; message2 . writeDelimitedTo ( output ) ; message3 . writeDelimitedTo ( output ) ; InputStream inStream = spy ( new ByteArrayInputStream ( output . toByteArray ( ) ) ) ; when ( socket . getInputStream ( ) ) . thenReturn ( inStream ) ; props . put ( SocketMessageListener . SOCKET_PROP_KEY , socket ) ; AbstractSocketChannel channel = mock ( AbstractSocketChannel . class ) ; props . put ( SocketMessageListener . CHANNEL_PROP_KEY , channel ) ; m_SUT . activate ( props ) ; Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; verify ( m_MessageRouter , timeout ( 1000 ) ) . handleMessage ( message , channel ) ; verify ( m_MessageRouter , timeout ( 1000 ) ) . handleMessage ( message2 , channel ) ; verify ( m_MessageRouter , timeout ( 1000 ) ) . handleMessage ( message3 , channel ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin , timeout ( 1000 ) ) . sendEvent ( eventCaptor . capture ( ) ) ; List < Event > postedEvents = eventCaptor . getAllValues ( ) ; Event event1 = postedEvents . get ( 0 ) ; assertThat ( event1 . getTopic ( ) , is ( RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID ) ) ; assertThat ( ( AbstractSocketChannel ) event1 . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; assertThat ( ( Integer ) event1 . getProperty ( RemoteConstants . EVENT_PROP_SYS_ID ) , is ( sourceId ) ) ; verify ( m_EventAdmin , timeout ( 1000 ) ) . postEvent ( eventCaptor . capture ( ) ) ; Event event3 = eventCaptor . getValue ( ) ; assertThat ( event3 . getTopic ( ) , is ( RemoteConstants . TOPIC_REMOVE_CHANNEL ) ) ; assertThat ( ( AbstractSocketChannel ) event3 . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; thread . join ( 100 ) ; verify ( inStream ) . close ( ) ; verify ( socket ) . close ( ) ; Long expectedBytesReceived = ( long ) ( message . getSerializedSize ( ) + message2 . getSerializedSize ( ) + message3 . getSerializedSize ( ) ) ; verify ( channel ) . setBytesReceived ( expectedBytesReceived ) ; verify ( m_Logging , times ( 0 ) ) . debug ( anyString ( ) ) ; }
Verify the thread will read messages as long as running and passed to router, also verify event for
 syncing of addresses is posted to the Event Admin.
Verify the thread will read messages as long as running and passed to router also verify event for syncing of addresses is posted to the Event Admin .
//disable logging // setup the message to be received // simulate received message from socket // mock the channel that will be sent in map // the socket should read in each message and parse as the same message // verify event is sent to notify remotechannellookupimpl to sync channel, should happen once //give time for thread to start and for mock interactions to register //Need to iterate through events this way. End of input stream is executed before  //a check can be made for the NEW/CHANGED topic. This is because input stream is a  //set size and the message sent on the stream is the only data. //Assert that the first message caused a new or changed channel id event. // verify event is posted to notify remotechannellookupimpl to remove channel //Assert that a remove channel event is posted. //SocketMessageListener exits out because input stream passed to message listener is a set size.  // therefore deactivate does not need to be called. (i.e. the message read in is the only message // on the stream). // ensure input stream and socket gets closed // verify bytes received is correct // verify no logging