mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestSocketMessageListener.java
@Test
    public void testRunWithMessageMaxSourceId() throws IOException
    {
        // setup the message to be received
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(Integer.MAX_VALUE, 1, Namespace.Base, 102, 
                        baseMessage);
        
        final TerraHarvestMessage message2 = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(100, 1, Namespace.Base, 100, 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);
        
        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 event is sent to notify remotechannellookupimpl to sync channel, should happen twice, once to set
        // the remote channel to max integer ID and once to set actual ID.
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        //give time for thread to start and for mock interactions to register
        verify(m_EventAdmin, timeout(1000).times(2)).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(Integer.MAX_VALUE));
        
        //Assert that the second message caused a new or changed channel id event.
        Event event2 = postedEvents.get(1);
        assertThat(event2.getTopic(), is(RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID));
        assertThat((AbstractSocketChannel)event2.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((Integer)event2.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(100));
    }
/**
     * Verify that the remote ID is updated when equal to the max integer value.
     */
Verify that the remote ID is updated when equal to the max integer value.
Verify that the remote ID is updated when equal to the max integer value .
@Test
    public void testRunWithMessageMaxSourceId() throws IOException
    {
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        final TerraHarvestMessage message = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(Integer.MAX_VALUE, 1, Namespace.Base, 102, 
                        baseMessage);
        final TerraHarvestMessage message2 = 
                TerraHarvestMessageHelper.createTerraHarvestMessage(100, 1, Namespace.Base, 100, baseMessage);
        Map<String, Object> props = new HashMap<String, Object>();
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        message2.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);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, timeout(1000).times(2)).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(Integer.MAX_VALUE));
        Event event2 = postedEvents.get(1);
        assertThat(event2.getTopic(), is(RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID));
        assertThat((AbstractSocketChannel)event2.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), is(channel));
        assertThat((Integer)event2.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(100));
    }
@ Test public void testRunWithMessageMaxSourceId ( ) throws IOException { BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . build ( ) ; final TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( Integer . MAX_VALUE , 1 , Namespace . Base , 102 , baseMessage ) ; final TerraHarvestMessage message2 = TerraHarvestMessageHelper . createTerraHarvestMessage ( 100 , 1 , Namespace . Base , 100 , baseMessage ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; message . writeDelimitedTo ( output ) ; message2 . 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 ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin , timeout ( 1000 ) . times ( 2 ) ) . 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 ( Integer . MAX_VALUE ) ) ; Event event2 = postedEvents . get ( 1 ) ; assertThat ( event2 . getTopic ( ) , is ( RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID ) ) ; assertThat ( ( AbstractSocketChannel ) event2 . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( channel ) ) ; assertThat ( ( Integer ) event2 . getProperty ( RemoteConstants . EVENT_PROP_SYS_ID ) , is ( 100 ) ) ; }
Verify that the remote ID is updated when equal to the max integer value.
Verify that the remote ID is updated when equal to the max integer value .
// 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 twice, once to set // the remote channel to max integer ID and once to set actual ID. //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. //Assert that the second message caused a new or changed channel id event.