mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/TestServerSocketChannel.java
@Test
    public void testTrySendMessageLoggingDisabled() throws IOException
    {
        //disable logging
        when(m_RemoteSettings.isLogRemoteMessagesEnabled()).thenReturn(false);
    
        // mock the socket that will be used to send the message
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        when(socket.getOutputStream()).thenReturn(outStream);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);
        
        // activate method expects a connected socket to be passed in
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        
        TerraHarvestMessage message = createBaseMessage();
        
        // replay
        boolean result = m_SUT.trySendMessage(message);
        assertThat(result, is(true));
        
        // verify mock socket's output stream was asked to send message in binary form with delimiter at beginning
        byte[] binaryMessage = message.toByteArray();
        // first byte sent will be the varint (https://developers.google.com/protocol-buffers/docs/encoding#varints)
        // representing the length of the message following
        byte[] fullBinaryMessage = new byte[binaryMessage.length + 1]; // leave room for delimiter
        fullBinaryMessage[0] = (byte)binaryMessage.length;
        System.arraycopy(binaryMessage, 0, fullBinaryMessage, 1, binaryMessage.length);
        
        assertThat(outStream.toByteArray(), is(fullBinaryMessage));
        
        // verify logging is disabled
        verify(m_Logging, times(0)).debug(anyString());
    }
/**
     * Verify the message is sent using the given socket in binary form but no message is logged
     */
Verify the message is sent using the given socket in binary form but no message is logged
Verify the message is sent using the given socket in binary form but no message is logged
@Test
    public void testTrySendMessageLoggingDisabled() throws IOException
    {
        when(m_RemoteSettings.isLogRemoteMessagesEnabled()).thenReturn(false);
        Socket socket = mock(Socket.class);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        when(socket.getOutputStream()).thenReturn(outStream);
        SocketAddress socketAddress = mock(SocketAddress.class);
        when(socketAddress.toString()).thenReturn(REMOTE_SOCKET_ADDR);
        when(socket.getRemoteSocketAddress()).thenReturn(socketAddress);
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(ServerSocketChannel.SOCKET_PROP_KEY, socket);
        m_SUT.activate(props);
        TerraHarvestMessage message = createBaseMessage();
        boolean result = m_SUT.trySendMessage(message);
        assertThat(result, is(true));
        byte[] binaryMessage = message.toByteArray();
        byte[] fullBinaryMessage = new byte[binaryMessage.length + 1]; 
        fullBinaryMessage[0] = (byte)binaryMessage.length;
        System.arraycopy(binaryMessage, 0, fullBinaryMessage, 1, binaryMessage.length);
        assertThat(outStream.toByteArray(), is(fullBinaryMessage));
        verify(m_Logging, times(0)).debug(anyString());
    }
@ Test public void testTrySendMessageLoggingDisabled ( ) throws IOException { when ( m_RemoteSettings . isLogRemoteMessagesEnabled ( ) ) . thenReturn ( false ) ; Socket socket = mock ( Socket . class ) ; ByteArrayOutputStream outStream = new ByteArrayOutputStream ( ) ; when ( socket . getOutputStream ( ) ) . thenReturn ( outStream ) ; SocketAddress socketAddress = mock ( SocketAddress . class ) ; when ( socketAddress . toString ( ) ) . thenReturn ( REMOTE_SOCKET_ADDR ) ; when ( socket . getRemoteSocketAddress ( ) ) . thenReturn ( socketAddress ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( ServerSocketChannel . SOCKET_PROP_KEY , socket ) ; m_SUT . activate ( props ) ; TerraHarvestMessage message = createBaseMessage ( ) ; boolean result = m_SUT . trySendMessage ( message ) ; assertThat ( result , is ( true ) ) ; byte [ ] binaryMessage = message . toByteArray ( ) ; byte [ ] fullBinaryMessage = new byte [ binaryMessage . length + 1 ] ; fullBinaryMessage [ 0 ] = ( byte ) binaryMessage . length ; System . arraycopy ( binaryMessage , 0 , fullBinaryMessage , 1 , binaryMessage . length ) ; assertThat ( outStream . toByteArray ( ) , is ( fullBinaryMessage ) ) ; verify ( m_Logging , times ( 0 ) ) . debug ( anyString ( ) ) ; }
Verify the message is sent using the given socket in binary form but no message is logged
Verify the message is sent using the given socket in binary form but no message is logged
//disable logging // mock the socket that will be used to send the message // activate method expects a connected socket to be passed in // replay // verify mock socket's output stream was asked to send message in binary form with delimiter at beginning // first byte sent will be the varint (https://developers.google.com/protocol-buffers/docs/encoding#varints) // representing the length of the message following // leave room for delimiter // verify logging is disabled