mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/transport/TestTransportMessageListener.java
@Test
    public void testHandleEvent() throws IOException
    {  
        Thread thread = new Thread(m_SUT);
        thread.start();
        
        int sourceId = 100;
        // setup the message to be received
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        
        TerraHarvestMessage message = TerraHarvestMessageHelper.createTerraHarvestMessage(Integer.MAX_VALUE, 1, 
                Namespace.Base, 100, baseMessage);
        
        //mimics message that would be sent through TransportChannel
        Map<String, Object> properties = new HashMap<String, Object>();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        
        //mimics type of event that would be caught by listener
        TransportPacket packet = mock(TransportPacket.class);
        when(packet.getPayload()).thenReturn(ByteBuffer.wrap(output.toByteArray()));
        properties.put(TransportLayer.EVENT_PROP_PACKET, packet);
        Event event = new Event(TransportLayer.TOPIC_PACKET_RECEIVED, properties);
        
        EventAdminSyncer syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID);
        
        m_SUT.handleEvent(event);
        
        // event is posted on a separate thread, wait for it
        syncer.waitFor(1, TimeUnit.SECONDS);
        
        // the listener should read in the message and parse as the same message
        verify(m_MessageRouter).handleMessage(message, m_Channel);
        
        // wait for event
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        List<Event> postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 1);
        
        assertThat((TransportChannel)postedEvents.get(0).getProperty(RemoteConstants.EVENT_PROP_CHANNEL), 
                is(m_Channel));
        assertThat((Integer)postedEvents.get(0).getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(Integer.MAX_VALUE));
        
        // send out different message (with different source)
        TerraHarvestMessage message2 = TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 
                100, baseMessage);
        
        ByteArrayOutputStream output2 = new ByteArrayOutputStream();
        message2.writeDelimitedTo(output2);
        when(packet.getPayload()).thenReturn(ByteBuffer.wrap(output2.toByteArray()));
        properties.put(TransportLayer.EVENT_PROP_PACKET, packet);
        Event event2 = new Event(TransportLayer.TOPIC_PACKET_RECEIVED, properties);
        
        // wait for the event again
        syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID);
        
        m_SUT.handleEvent(event2);
        
        // wait again for separate thread
        syncer.waitFor(1, TimeUnit.SECONDS);
        
        eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, times(2)).postEvent(eventCaptor.capture());
        postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 2); // should now be a 2nd event
        assertThat((Integer)postedEvents.get(1).getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(sourceId));
        
        //verify logging enabled
        //multiple messages are sent so logging will occur more than once
        verify(m_Logging, atLeast(1)).debug(anyString(), anyVararg());
        
        // wait for the event again, but not expecting a new event posted (0 times)
        syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 0);
        
        // send same event again, should not see another changed id event
        m_SUT.handleEvent(event);
        
        // wait again for separate thread, event should not be found
        syncer.waitFor(1, TimeUnit.SECONDS);
        
        postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 2);  // should still be 2
    }
/**
     * Method verifies correct behavior of events in handleEvent method.
     * 
     * Verify new/changed id event is only sent when id changes
     */
Method verifies correct behavior of events in handleEvent method.

Verify new/changed id event is only sent when id changes
Method verifies correct behavior of events in handleEvent method . Verify new / changed id event is only sent when id changes
@Test
    public void testHandleEvent() throws IOException
    {  
        Thread thread = new Thread(m_SUT);
        thread.start();
        int sourceId = 100;
        BaseNamespace baseMessage = BaseNamespace.newBuilder().
                setType(BaseMessageType.RequestControllerInfo).
                build();
        TerraHarvestMessage message = TerraHarvestMessageHelper.createTerraHarvestMessage(Integer.MAX_VALUE, 1, 
                Namespace.Base, 100, baseMessage);
        Map<String, Object> properties = new HashMap<String, Object>();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeDelimitedTo(output);
        TransportPacket packet = mock(TransportPacket.class);
        when(packet.getPayload()).thenReturn(ByteBuffer.wrap(output.toByteArray()));
        properties.put(TransportLayer.EVENT_PROP_PACKET, packet);
        Event event = new Event(TransportLayer.TOPIC_PACKET_RECEIVED, properties);
        EventAdminSyncer syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID);
        m_SUT.handleEvent(event);
        syncer.waitFor(1, TimeUnit.SECONDS);
        verify(m_MessageRouter).handleMessage(message, m_Channel);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        List<Event> postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 1);
        assertThat((TransportChannel)postedEvents.get(0).getProperty(RemoteConstants.EVENT_PROP_CHANNEL), 
                is(m_Channel));
        assertThat((Integer)postedEvents.get(0).getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(Integer.MAX_VALUE));
        TerraHarvestMessage message2 = TerraHarvestMessageHelper.createTerraHarvestMessage(sourceId, 1, Namespace.Base, 
                100, baseMessage);
        ByteArrayOutputStream output2 = new ByteArrayOutputStream();
        message2.writeDelimitedTo(output2);
        when(packet.getPayload()).thenReturn(ByteBuffer.wrap(output2.toByteArray()));
        properties.put(TransportLayer.EVENT_PROP_PACKET, packet);
        Event event2 = new Event(TransportLayer.TOPIC_PACKET_RECEIVED, properties);
        syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID);
        m_SUT.handleEvent(event2);
        syncer.waitFor(1, TimeUnit.SECONDS);
        eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin, times(2)).postEvent(eventCaptor.capture());
        postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 2); 
        assertThat((Integer)postedEvents.get(1).getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(sourceId));
        verify(m_Logging, atLeast(1)).debug(anyString(), anyVararg());
        , but not expecting a new event posted (0 times)
        syncer = new EventAdminSyncer(m_EventAdmin, RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 0);
        m_SUT.handleEvent(event);
        , event should not be found
        syncer.waitFor(1, TimeUnit.SECONDS);
        postedEvents = EventAdminVerifier.assertEventByTopicOnly(eventCaptor, 
                RemoteConstants.TOPIC_NEW_OR_CHANGED_CHANNEL_ID, 2);  
    }
@ Test public void testHandleEvent ( ) throws IOException { Thread thread = new Thread ( m_SUT ) ; thread . start ( ) ; int sourceId = 100 ; BaseNamespace baseMessage = BaseNamespace . newBuilder ( ) . setType ( BaseMessageType . RequestControllerInfo ) . build ( ) ; TerraHarvestMessage message = TerraHarvestMessageHelper . createTerraHarvestMessage ( Integer . MAX_VALUE , 1 , Namespace . Base , 100 , baseMessage ) ; Map < String , Object > properties = new HashMap < String , Object > ( ) ; ByteArrayOutputStream output = new ByteArrayOutputStream ( ) ; message . writeDelimitedTo ( output ) ; TransportPacket packet = mock ( TransportPacket . class ) ; when ( packet . getPayload ( ) ) . thenReturn ( ByteBuffer . wrap ( output . toByteArray ( ) ) ) ; properties . put ( TransportLayer . EVENT_PROP_PACKET , packet ) ; Event event = new Event ( TransportLayer . TOPIC_PACKET_RECEIVED , properties ) ; EventAdminSyncer syncer = new EventAdminSyncer ( m_EventAdmin , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID ) ; m_SUT . handleEvent ( event ) ; syncer . waitFor ( 1 , TimeUnit . SECONDS ) ; verify ( m_MessageRouter ) . handleMessage ( message , m_Channel ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin ) . postEvent ( eventCaptor . capture ( ) ) ; List < Event > postedEvents = EventAdminVerifier . assertEventByTopicOnly ( eventCaptor , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID , 1 ) ; assertThat ( ( TransportChannel ) postedEvents . get ( 0 ) . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , is ( m_Channel ) ) ; assertThat ( ( Integer ) postedEvents . get ( 0 ) . getProperty ( RemoteConstants . EVENT_PROP_SYS_ID ) , is ( Integer . MAX_VALUE ) ) ; TerraHarvestMessage message2 = TerraHarvestMessageHelper . createTerraHarvestMessage ( sourceId , 1 , Namespace . Base , 100 , baseMessage ) ; ByteArrayOutputStream output2 = new ByteArrayOutputStream ( ) ; message2 . writeDelimitedTo ( output2 ) ; when ( packet . getPayload ( ) ) . thenReturn ( ByteBuffer . wrap ( output2 . toByteArray ( ) ) ) ; properties . put ( TransportLayer . EVENT_PROP_PACKET , packet ) ; Event event2 = new Event ( TransportLayer . TOPIC_PACKET_RECEIVED , properties ) ; syncer = new EventAdminSyncer ( m_EventAdmin , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID ) ; m_SUT . handleEvent ( event2 ) ; syncer . waitFor ( 1 , TimeUnit . SECONDS ) ; eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin , times ( 2 ) ) . postEvent ( eventCaptor . capture ( ) ) ; postedEvents = EventAdminVerifier . assertEventByTopicOnly ( eventCaptor , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID , 2 ) ; assertThat ( ( Integer ) postedEvents . get ( 1 ) . getProperty ( RemoteConstants . EVENT_PROP_SYS_ID ) , is ( sourceId ) ) ; verify ( m_Logging , atLeast ( 1 ) ) . debug ( anyString ( ) , anyVararg ( ) ) ; syncer = new EventAdminSyncer ( m_EventAdmin , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID , 0 ) ; m_SUT . handleEvent ( event ) ; syncer . waitFor ( 1 , TimeUnit . SECONDS ) ; postedEvents = EventAdminVerifier . assertEventByTopicOnly ( eventCaptor , RemoteConstants . TOPIC_NEW_OR_CHANGED_CHANNEL_ID , 2 ) ; }
Method verifies correct behavior of events in handleEvent method.
Method verifies correct behavior of events in handleEvent method .
// setup the message to be received //mimics message that would be sent through TransportChannel //mimics type of event that would be caught by listener // event is posted on a separate thread, wait for it // the listener should read in the message and parse as the same message // wait for event // send out different message (with different source) // wait for the event again // wait again for separate thread // should now be a 2nd event //verify logging enabled //multiple messages are sent so logging will occur more than once // wait for the event again, but not expecting a new event posted (0 times) // send same event again, should not see another changed id event // wait again for separate thread, event should not be found // should still be 2