mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestRemoteChannelLookupImpl.java
@SuppressWarnings({ "rawtypes", "unchecked" })
    @Test
    public void testSyncClientSocketChannel() throws IllegalArgumentException, PersistenceFailedException, 
        InvalidProtocolBufferException
    {
        // mock the socket props used for syncing, have socket channel match with it
        stubSocketChannel(m_ClientSocketChannel, "host", 100, false);
        stubAsExistingSocketChannel(m_ClientSocketChannel);
        
        int systemId = 300;
        
        SocketChannel channel = m_SUT.syncClientSocketChannel("host", 100, systemId);
        
        // should be the socket channel that gets created by the factory
        assertThat((SocketChannel)m_SUT.getChannel(systemId), is(channel));
        ArgumentCaptor<Dictionary> dictionaryCaptor = ArgumentCaptor.forClass(Dictionary.class);
        verify(m_ClientSocketChannelFactory).newInstance(dictionaryCaptor.capture());
        assertThat(channel, is(m_ClientSocketChannel));
        // verify transport channel is created with proper properties
        assertThat((String)dictionaryCaptor.getValue().get(ClientSocketChannel.HOST_PROP_KEY), is("host"));
        assertThat((Integer)dictionaryCaptor.getValue().get(ClientSocketChannel.PORT_PROP_KEY), is(100));
        
        //verify that the channel added topic is created
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        
        Event event = eventCaptor.getValue();
        assertThat(event.getTopic(), is(RemoteChannelLookup.TOPIC_CHANNEL_UPDATED));
        assertThat((Integer)event.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(300));
        assertThat(event.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), notNullValue());
        
        // syncing again should not create a new channel
        m_SUT.syncClientSocketChannel("host", 100, systemId);
        
        verify(m_EventAdmin, times(2)).postEvent(eventCaptor.capture());
        
        channel = (SocketChannel)m_SUT.getChannel(systemId);
        verify(m_ClientSocketChannelFactory, times(1)).newInstance(Mockito.any(Dictionary.class)); // still holding at 1
        assertThat(channel, is(m_ClientSocketChannel));
        
        // sync to a different system id, shouldn't be available at the old one anymore
        int otherSystemId = 400;
        m_SUT.syncClientSocketChannel("host", 100, otherSystemId);
        
        try
        {
            m_SUT.getChannel(systemId);
            fail("Expecting exception since channel is no longer associated with this id");
        }
        catch (IllegalArgumentException e)
        {
            
        }
        
        verify(m_EventAdmin, times(3)).postEvent(eventCaptor.capture());
        
        channel = (SocketChannel)m_SUT.getChannel(otherSystemId);
        verify(m_ClientSocketChannelFactory, times(1)).newInstance(Mockito.any(Dictionary.class)); // still holding at 1
        assertThat(channel, is(m_ClientSocketChannel));
        
        ArgumentCaptor<TerraHarvestMessage> messageCaptor = ArgumentCaptor.forClass(TerraHarvestMessage.class);
        verify(channel, times(1)).queueMessage(messageCaptor.capture());
        verify(m_ThSystem, times(1)).getId();
        verify(m_RemoteSettings, times(1)).getEncryptionMode();
        
        for (TerraHarvestMessage message : messageCaptor.getAllValues())
        {
            assertThat(message.getSourceId(), is(m_SystemId));
            assertThat(message.getDestId(), anyOf(equalTo(systemId), equalTo(otherSystemId)));
            TerraHarvestPayload payload = TerraHarvestPayload.parseFrom(message.getTerraHarvestPayload());
            assertThat(payload.getNamespace(), is(Namespace.EncryptionInfo));
            EncryptionInfoNamespace namespace = EncryptionInfoNamespace.parseFrom(payload.getNamespaceMessage());
            assertThat(namespace.getType(), is(EncryptionInfoMessageType.GetEncryptionTypeResponse));
            GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData.parseFrom(namespace.getData());
            assertThat(data.getType(), is(EncryptType.AES_ECDH_ECDSA));
        }
    }
/**
     * Verify syncing a client socket will allow it to be retrieved later.
     * 
     * Verify channel is not created if already in lookup
     * 
     * Verify syncing channel to a new id is handled correctly (should move channel)
     * 
     * Verify new channel is persisted in the list of system channels.
     */
Verify syncing a client socket will allow it to be retrieved later.

Verify channel is not created if already in lookup

Verify syncing channel to a new id is handled correctly (should move channel)

Verify new channel is persisted in the list of system channels.
Verify syncing a client socket will allow it to be retrieved later . Verify channel is not created if already in lookup Verify syncing channel to a new id is handled correctly ( should move channel ) Verify new channel is persisted in the list of system channels .
@SuppressWarnings({ "rawtypes", "unchecked" })
    @Test
    public void testSyncClientSocketChannel() throws IllegalArgumentException, PersistenceFailedException, 
        InvalidProtocolBufferException
    {
        stubSocketChannel(m_ClientSocketChannel, "host", 100, false);
        stubAsExistingSocketChannel(m_ClientSocketChannel);
        int systemId = 300;
        SocketChannel channel = m_SUT.syncClientSocketChannel("host", 100, systemId);
        assertThat((SocketChannel)m_SUT.getChannel(systemId), is(channel));
        ArgumentCaptor<Dictionary> dictionaryCaptor = ArgumentCaptor.forClass(Dictionary.class);
        verify(m_ClientSocketChannelFactory).newInstance(dictionaryCaptor.capture());
        assertThat(channel, is(m_ClientSocketChannel));
        assertThat((String)dictionaryCaptor.getValue().get(ClientSocketChannel.HOST_PROP_KEY), is("host"));
        assertThat((Integer)dictionaryCaptor.getValue().get(ClientSocketChannel.PORT_PROP_KEY), is(100));
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        Event event = eventCaptor.getValue();
        assertThat(event.getTopic(), is(RemoteChannelLookup.TOPIC_CHANNEL_UPDATED));
        assertThat((Integer)event.getProperty(RemoteConstants.EVENT_PROP_SYS_ID), is(300));
        assertThat(event.getProperty(RemoteConstants.EVENT_PROP_CHANNEL), notNullValue());
        m_SUT.syncClientSocketChannel("host", 100, systemId);
        verify(m_EventAdmin, times(2)).postEvent(eventCaptor.capture());
        channel = (SocketChannel)m_SUT.getChannel(systemId);
        verify(m_ClientSocketChannelFactory, times(1)).newInstance(Mockito.any(Dictionary.class)); 
        assertThat(channel, is(m_ClientSocketChannel));
        int otherSystemId = 400;
        m_SUT.syncClientSocketChannel("host", 100, otherSystemId);
        try
        {
            m_SUT.getChannel(systemId);
            fail("Expecting exception since channel is no longer associated with this id");
        }
        catch (IllegalArgumentException e)
        {
        }
        verify(m_EventAdmin, times(3)).postEvent(eventCaptor.capture());
        channel = (SocketChannel)m_SUT.getChannel(otherSystemId);
        verify(m_ClientSocketChannelFactory, times(1)).newInstance(Mockito.any(Dictionary.class)); 
        assertThat(channel, is(m_ClientSocketChannel));
        ArgumentCaptor<TerraHarvestMessage> messageCaptor = ArgumentCaptor.forClass(TerraHarvestMessage.class);
        verify(channel, times(1)).queueMessage(messageCaptor.capture());
        verify(m_ThSystem, times(1)).getId();
        verify(m_RemoteSettings, times(1)).getEncryptionMode();
        for (TerraHarvestMessage message : messageCaptor.getAllValues())
        {
            assertThat(message.getSourceId(), is(m_SystemId));
            assertThat(message.getDestId(), anyOf(equalTo(systemId), equalTo(otherSystemId)));
            TerraHarvestPayload payload = TerraHarvestPayload.parseFrom(message.getTerraHarvestPayload());
            assertThat(payload.getNamespace(), is(Namespace.EncryptionInfo));
            EncryptionInfoNamespace namespace = EncryptionInfoNamespace.parseFrom(payload.getNamespaceMessage());
            assertThat(namespace.getType(), is(EncryptionInfoMessageType.GetEncryptionTypeResponse));
            GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData.parseFrom(namespace.getData());
            assertThat(data.getType(), is(EncryptType.AES_ECDH_ECDSA));
        }
    }
@ SuppressWarnings ( { "rawtypes" , "unchecked" } ) @ Test public void testSyncClientSocketChannel ( ) throws IllegalArgumentException , PersistenceFailedException , InvalidProtocolBufferException { stubSocketChannel ( m_ClientSocketChannel , "host" , 100 , false ) ; stubAsExistingSocketChannel ( m_ClientSocketChannel ) ; int systemId = 300 ; SocketChannel channel = m_SUT . syncClientSocketChannel ( "host" , 100 , systemId ) ; assertThat ( ( SocketChannel ) m_SUT . getChannel ( systemId ) , is ( channel ) ) ; ArgumentCaptor < Dictionary > dictionaryCaptor = ArgumentCaptor . forClass ( Dictionary . class ) ; verify ( m_ClientSocketChannelFactory ) . newInstance ( dictionaryCaptor . capture ( ) ) ; assertThat ( channel , is ( m_ClientSocketChannel ) ) ; assertThat ( ( String ) dictionaryCaptor . getValue ( ) . get ( ClientSocketChannel . HOST_PROP_KEY ) , is ( "host" ) ) ; assertThat ( ( Integer ) dictionaryCaptor . getValue ( ) . get ( ClientSocketChannel . PORT_PROP_KEY ) , is ( 100 ) ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin ) . postEvent ( eventCaptor . capture ( ) ) ; Event event = eventCaptor . getValue ( ) ; assertThat ( event . getTopic ( ) , is ( RemoteChannelLookup . TOPIC_CHANNEL_UPDATED ) ) ; assertThat ( ( Integer ) event . getProperty ( RemoteConstants . EVENT_PROP_SYS_ID ) , is ( 300 ) ) ; assertThat ( event . getProperty ( RemoteConstants . EVENT_PROP_CHANNEL ) , notNullValue ( ) ) ; m_SUT . syncClientSocketChannel ( "host" , 100 , systemId ) ; verify ( m_EventAdmin , times ( 2 ) ) . postEvent ( eventCaptor . capture ( ) ) ; channel = ( SocketChannel ) m_SUT . getChannel ( systemId ) ; verify ( m_ClientSocketChannelFactory , times ( 1 ) ) . newInstance ( Mockito . any ( Dictionary . class ) ) ; assertThat ( channel , is ( m_ClientSocketChannel ) ) ; int otherSystemId = 400 ; m_SUT . syncClientSocketChannel ( "host" , 100 , otherSystemId ) ; try { m_SUT . getChannel ( systemId ) ; fail ( "Expecting exception since channel is no longer associated with this id" ) ; } catch ( IllegalArgumentException e ) { } verify ( m_EventAdmin , times ( 3 ) ) . postEvent ( eventCaptor . capture ( ) ) ; channel = ( SocketChannel ) m_SUT . getChannel ( otherSystemId ) ; verify ( m_ClientSocketChannelFactory , times ( 1 ) ) . newInstance ( Mockito . any ( Dictionary . class ) ) ; assertThat ( channel , is ( m_ClientSocketChannel ) ) ; ArgumentCaptor < TerraHarvestMessage > messageCaptor = ArgumentCaptor . forClass ( TerraHarvestMessage . class ) ; verify ( channel , times ( 1 ) ) . queueMessage ( messageCaptor . capture ( ) ) ; verify ( m_ThSystem , times ( 1 ) ) . getId ( ) ; verify ( m_RemoteSettings , times ( 1 ) ) . getEncryptionMode ( ) ; for ( TerraHarvestMessage message : messageCaptor . getAllValues ( ) ) { assertThat ( message . getSourceId ( ) , is ( m_SystemId ) ) ; assertThat ( message . getDestId ( ) , anyOf ( equalTo ( systemId ) , equalTo ( otherSystemId ) ) ) ; TerraHarvestPayload payload = TerraHarvestPayload . parseFrom ( message . getTerraHarvestPayload ( ) ) ; assertThat ( payload . getNamespace ( ) , is ( Namespace . EncryptionInfo ) ) ; EncryptionInfoNamespace namespace = EncryptionInfoNamespace . parseFrom ( payload . getNamespaceMessage ( ) ) ; assertThat ( namespace . getType ( ) , is ( EncryptionInfoMessageType . GetEncryptionTypeResponse ) ) ; GetEncryptionTypeResponseData data = GetEncryptionTypeResponseData . parseFrom ( namespace . getData ( ) ) ; assertThat ( data . getType ( ) , is ( EncryptType . AES_ECDH_ECDSA ) ) ; } }
Verify syncing a client socket will allow it to be retrieved later.
Verify syncing a client socket will allow it to be retrieved later .
// mock the socket props used for syncing, have socket channel match with it // should be the socket channel that gets created by the factory // verify transport channel is created with proper properties //verify that the channel added topic is created // syncing again should not create a new channel // still holding at 1 // sync to a different system id, shouldn't be available at the old one anymore // still holding at 1