mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestRemoteChannelLookupImpl.java
@Test
    public void testPersistenceSyncUpdate() throws PersistenceFailedException, InvalidProtocolBufferException, 
        IllegalArgumentException, ValidationFailedException
    {
        int systemId = 300;
        
        // mock out persistent data store with nothing in it yet
        Collection<PersistentData> channelDataList = new ArrayList<PersistentData>();
        when(m_PersistentDataStore.query(RemoteChannelLookupImpl.class, new Integer(systemId).toString()))
            .thenReturn(channelDataList);
        
        // mock channel that will be created
        stubSocketChannel(m_ClientSocketChannel, "host", 100, false);
        
        // sync channel to see if it is persisted
        SocketChannel channel = m_SUT.syncClientSocketChannel("host", 100, systemId);
        
        // mock so that the existing channel shows up
        stubAsExistingSocketChannel(m_ClientSocketChannel);
        
        // mock existing channel data
        PersistentData channelData = mock(PersistentData.class);
        channelDataList.add(channelData);
        
        // sync to a different system id, but this time call syncChannel
        m_SUT.syncChannel(channel, systemId + 1);
        
        // expected channels will be the same but with a different system id
        SystemChannels expectedChannels = SystemChannels.newBuilder().setSysId(systemId + 1)
                .addSocketChannel(
                        SocketChannelType.newBuilder().setHost("host").setPort(100).setSslEnabled(false).build())
                .build();
        
        // verify object is persisted with different system id
        ArgumentCaptor<byte[]> byteCapture = ArgumentCaptor.forClass(byte[].class);
        verify(m_PersistentDataStore).persist(eq(RemoteChannelLookupImpl.class), Mockito.any(UUID.class), 
                eq(new Integer(systemId + 1).toString()), byteCapture.capture());   
        SystemChannels actualChannels = SystemChannels.parseFrom(byteCapture.getValue());
        assertThat(actualChannels, is(expectedChannels));
        
        // expected channels are empty for the old system id
        expectedChannels = SystemChannels.newBuilder().setSysId(systemId).build();
        
        // verify new channel data is merged with existing data
        verify(channelData).setEntity(byteCapture.capture());
        actualChannels = SystemChannels.parseFrom(byteCapture.getValue());
        assertThat(actualChannels, is(expectedChannels));
        
        verify(m_PersistentDataStore).merge(channelData);
    }
/**
     * Verify syncChannel works the same way as syncing a specific channel if synced to new system id
     *  - old list is updated with channel removed
     *  - new list is persisted
     */
Verify syncChannel works the same way as syncing a specific channel if synced to new system id
- old list is updated with channel removed
- new list is persisted
Verify syncChannel works the same way as syncing a specific channel if synced to new system id - old list is updated with channel removed - new list is persisted
@Test
    public void testPersistenceSyncUpdate() throws PersistenceFailedException, InvalidProtocolBufferException, 
        IllegalArgumentException, ValidationFailedException
    {
        int systemId = 300;
        Collection<PersistentData> channelDataList = new ArrayList<PersistentData>();
        when(m_PersistentDataStore.query(RemoteChannelLookupImpl.class, new Integer(systemId).toString()))
            .thenReturn(channelDataList);
        stubSocketChannel(m_ClientSocketChannel, "host", 100, false);
        SocketChannel channel = m_SUT.syncClientSocketChannel("host", 100, systemId);
        stubAsExistingSocketChannel(m_ClientSocketChannel);
        PersistentData channelData = mock(PersistentData.class);
        channelDataList.add(channelData);
        m_SUT.syncChannel(channel, systemId + 1);
        SystemChannels expectedChannels = SystemChannels.newBuilder().setSysId(systemId + 1)
                .addSocketChannel(
                        SocketChannelType.newBuilder().setHost("host").setPort(100).setSslEnabled(false).build())
                .build();
        ArgumentCaptor<byte[]> byteCapture = ArgumentCaptor.forClass(byte[].class);
        verify(m_PersistentDataStore).persist(eq(RemoteChannelLookupImpl.class), Mockito.any(UUID.class), 
                eq(new Integer(systemId + 1).toString()), byteCapture.capture());   
        SystemChannels actualChannels = SystemChannels.parseFrom(byteCapture.getValue());
        assertThat(actualChannels, is(expectedChannels));
        expectedChannels = SystemChannels.newBuilder().setSysId(systemId).build();
        verify(channelData).setEntity(byteCapture.capture());
        actualChannels = SystemChannels.parseFrom(byteCapture.getValue());
        assertThat(actualChannels, is(expectedChannels));
        verify(m_PersistentDataStore).merge(channelData);
    }
@ Test public void testPersistenceSyncUpdate ( ) throws PersistenceFailedException , InvalidProtocolBufferException , IllegalArgumentException , ValidationFailedException { int systemId = 300 ; Collection < PersistentData > channelDataList = new ArrayList < PersistentData > ( ) ; when ( m_PersistentDataStore . query ( RemoteChannelLookupImpl . class , new Integer ( systemId ) . toString ( ) ) ) . thenReturn ( channelDataList ) ; stubSocketChannel ( m_ClientSocketChannel , "host" , 100 , false ) ; SocketChannel channel = m_SUT . syncClientSocketChannel ( "host" , 100 , systemId ) ; stubAsExistingSocketChannel ( m_ClientSocketChannel ) ; PersistentData channelData = mock ( PersistentData . class ) ; channelDataList . add ( channelData ) ; m_SUT . syncChannel ( channel , systemId + 1 ) ; SystemChannels expectedChannels = SystemChannels . newBuilder ( ) . setSysId ( systemId + 1 ) . addSocketChannel ( SocketChannelType . newBuilder ( ) . setHost ( "host" ) . setPort ( 100 ) . setSslEnabled ( false ) . build ( ) ) . build ( ) ; ArgumentCaptor < byte [ ] > byteCapture = ArgumentCaptor . forClass ( byte [ ] . class ) ; verify ( m_PersistentDataStore ) . persist ( eq ( RemoteChannelLookupImpl . class ) , Mockito . any ( UUID . class ) , eq ( new Integer ( systemId + 1 ) . toString ( ) ) , byteCapture . capture ( ) ) ; SystemChannels actualChannels = SystemChannels . parseFrom ( byteCapture . getValue ( ) ) ; assertThat ( actualChannels , is ( expectedChannels ) ) ; expectedChannels = SystemChannels . newBuilder ( ) . setSysId ( systemId ) . build ( ) ; verify ( channelData ) . setEntity ( byteCapture . capture ( ) ) ; actualChannels = SystemChannels . parseFrom ( byteCapture . getValue ( ) ) ; assertThat ( actualChannels , is ( expectedChannels ) ) ; verify ( m_PersistentDataStore ) . merge ( channelData ) ; }
Verify syncChannel works the same way as syncing a specific channel if synced to new system id
  - old list is updated with channel removed
  - new list is persisted
Verify syncChannel works the same way as syncing a specific channel if synced to new system id - old list is updated with channel removed - new list is persisted
// mock out persistent data store with nothing in it yet // mock channel that will be created // sync channel to see if it is persisted // mock so that the existing channel shows up // mock existing channel data // sync to a different system id, but this time call syncChannel // expected channels will be the same but with a different system id // verify object is persisted with different system id // expected channels are empty for the old system id // verify new channel data is merged with existing data