mil.dod.th.ose.gui.webapp/test/mil/dod/th/ose/gui/webapp/controller/TestControllerHistoryMgr.java
@Test
    public void testChannelEventHelperUpdatedChannel() throws IllegalArgumentException, PersistenceFailedException, 
        ValidationFailedException, InterruptedException
    {
        final String hostname = "2.1.168.192";
        final int port = 1001;
        final int systemId = 201;
        final long initialTime = System.currentTimeMillis();
        
        final ControllerHistory initialController = new ControllerHistory();
        initialController.setControllerId(systemId);
        initialController.setControllerName("test");
        initialController.setHostName(hostname);
        initialController.setPort(port);
        initialController.setLastConnected(initialTime);
        
        //Sleep needed to make sure initial time varies from the updated time.
        Thread.sleep(500);
        
        final UUID uuid = UUID.randomUUID();
        final PersistentData data = new PersistentData(uuid, hostname, ControllerHistory.class.toString(), 
                initialController);
        
        final Collection<PersistentData> collection = new HashSet<>();
        collection.add(data);
        when(m_DataStore.query(eq(ControllerHistory.class), Mockito.anyString())).thenReturn(collection);
        
        final RemoteChannel remoteChannel = mock(RemoteChannel.class, 
                withSettings().extraInterfaces(SocketChannel.class));
        when(((SocketChannel)remoteChannel).getHost()).thenReturn(hostname);
        when(((SocketChannel)remoteChannel).getPort()).thenReturn(port);
        when(((SocketChannel)remoteChannel).isSslEnabled()).thenReturn(true);
        
        final Map<String, Object> props = new HashMap<String, Object>();
        props.put(RemoteConstants.EVENT_PROP_SYS_ID, systemId);
        props.put(RemoteConstants.EVENT_PROP_CHANNEL, remoteChannel);
        final Event remoteChannelAddedEvent = new Event(RemoteChannelLookup.TOPIC_CHANNEL_UPDATED, props);
        
        m_ChannelEventHelper.handleEvent(remoteChannelAddedEvent);
        
        final ArgumentCaptor<PersistentData> dataCaptor = ArgumentCaptor.forClass(PersistentData.class);
        verify(m_DataStore).merge(dataCaptor.capture());
        final PersistentData updatedData = dataCaptor.getValue();
        final ControllerHistory updatedController = (ControllerHistory)updatedData.getEntity();
        assertThat(updatedController.getControllerId(), is(systemId));
        assertThat(updatedController.getControllerName(), is("test"));
        assertThat(updatedController.getHostName(), is(hostname));
        assertThat(updatedController.getPort(), is(port));
        assertThat(updatedController.isSslEnabled(), is(true));
        assertThat(updatedController.getLastConnected(), greaterThan(initialTime));
    }
/**
     * Tests the channel event helper and verifies that the persistent data for the controller history is updated 
     * and merged.
     */
Tests the channel event helper and verifies that the persistent data for the controller history is updated
and merged.
Tests the channel event helper and verifies that the persistent data for the controller history is updated and merged .
@Test
    public void testChannelEventHelperUpdatedChannel() throws IllegalArgumentException, PersistenceFailedException, 
        ValidationFailedException, InterruptedException
    {
        final String hostname = "2.1.168.192";
        final int port = 1001;
        final int systemId = 201;
        final long initialTime = System.currentTimeMillis();
        final ControllerHistory initialController = new ControllerHistory();
        initialController.setControllerId(systemId);
        initialController.setControllerName("test");
        initialController.setHostName(hostname);
        initialController.setPort(port);
        initialController.setLastConnected(initialTime);
        Thread.sleep(500);
        final UUID uuid = UUID.randomUUID();
        final PersistentData data = new PersistentData(uuid, hostname, ControllerHistory.class.toString(), 
                initialController);
        final Collection<PersistentData> collection = new HashSet<>();
        collection.add(data);
        when(m_DataStore.query(eq(ControllerHistory.class), Mockito.anyString())).thenReturn(collection);
        final RemoteChannel remoteChannel = mock(RemoteChannel.class, 
                withSettings().extraInterfaces(SocketChannel.class));
        when(((SocketChannel)remoteChannel).getHost()).thenReturn(hostname);
        when(((SocketChannel)remoteChannel).getPort()).thenReturn(port);
        when(((SocketChannel)remoteChannel).isSslEnabled()).thenReturn(true);
        final Map<String, Object> props = new HashMap<String, Object>();
        props.put(RemoteConstants.EVENT_PROP_SYS_ID, systemId);
        props.put(RemoteConstants.EVENT_PROP_CHANNEL, remoteChannel);
        final Event remoteChannelAddedEvent = new Event(RemoteChannelLookup.TOPIC_CHANNEL_UPDATED, props);
        m_ChannelEventHelper.handleEvent(remoteChannelAddedEvent);
        final ArgumentCaptor<PersistentData> dataCaptor = ArgumentCaptor.forClass(PersistentData.class);
        verify(m_DataStore).merge(dataCaptor.capture());
        final PersistentData updatedData = dataCaptor.getValue();
        final ControllerHistory updatedController = (ControllerHistory)updatedData.getEntity();
        assertThat(updatedController.getControllerId(), is(systemId));
        assertThat(updatedController.getControllerName(), is("test"));
        assertThat(updatedController.getHostName(), is(hostname));
        assertThat(updatedController.getPort(), is(port));
        assertThat(updatedController.isSslEnabled(), is(true));
        assertThat(updatedController.getLastConnected(), greaterThan(initialTime));
    }
@ Test public void testChannelEventHelperUpdatedChannel ( ) throws IllegalArgumentException , PersistenceFailedException , ValidationFailedException , InterruptedException { final String hostname = "2.1.168.192" ; final int port = 1001 ; final int systemId = 201 ; final long initialTime = System . currentTimeMillis ( ) ; final ControllerHistory initialController = new ControllerHistory ( ) ; initialController . setControllerId ( systemId ) ; initialController . setControllerName ( "test" ) ; initialController . setHostName ( hostname ) ; initialController . setPort ( port ) ; initialController . setLastConnected ( initialTime ) ; Thread . sleep ( 500 ) ; final UUID uuid = UUID . randomUUID ( ) ; final PersistentData data = new PersistentData ( uuid , hostname , ControllerHistory . class . toString ( ) , initialController ) ; final Collection < PersistentData > collection = new HashSet < > ( ) ; collection . add ( data ) ; when ( m_DataStore . query ( eq ( ControllerHistory . class ) , Mockito . anyString ( ) ) ) . thenReturn ( collection ) ; final RemoteChannel remoteChannel = mock ( RemoteChannel . class , withSettings ( ) . extraInterfaces ( SocketChannel . class ) ) ; when ( ( ( SocketChannel ) remoteChannel ) . getHost ( ) ) . thenReturn ( hostname ) ; when ( ( ( SocketChannel ) remoteChannel ) . getPort ( ) ) . thenReturn ( port ) ; when ( ( ( SocketChannel ) remoteChannel ) . isSslEnabled ( ) ) . thenReturn ( true ) ; final Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( RemoteConstants . EVENT_PROP_SYS_ID , systemId ) ; props . put ( RemoteConstants . EVENT_PROP_CHANNEL , remoteChannel ) ; final Event remoteChannelAddedEvent = new Event ( RemoteChannelLookup . TOPIC_CHANNEL_UPDATED , props ) ; m_ChannelEventHelper . handleEvent ( remoteChannelAddedEvent ) ; final ArgumentCaptor < PersistentData > dataCaptor = ArgumentCaptor . forClass ( PersistentData . class ) ; verify ( m_DataStore ) . merge ( dataCaptor . capture ( ) ) ; final PersistentData updatedData = dataCaptor . getValue ( ) ; final ControllerHistory updatedController = ( ControllerHistory ) updatedData . getEntity ( ) ; assertThat ( updatedController . getControllerId ( ) , is ( systemId ) ) ; assertThat ( updatedController . getControllerName ( ) , is ( "test" ) ) ; assertThat ( updatedController . getHostName ( ) , is ( hostname ) ) ; assertThat ( updatedController . getPort ( ) , is ( port ) ) ; assertThat ( updatedController . isSslEnabled ( ) , is ( true ) ) ; assertThat ( updatedController . getLastConnected ( ) , greaterThan ( initialTime ) ) ; }
Tests the channel event helper and verifies that the persistent data for the controller history is updated
 and merged.
Tests the channel event helper and verifies that the persistent data for the controller history is updated and merged .
//Sleep needed to make sure initial time varies from the updated time.