/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2007 Phex Development Group
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  --- SVN Information ---
 *  $Id: TestMsgQueryResponse.java 3859 2007-07-01 20:15:19Z gregork $
 */
package phex.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import junit.framework.TestCase;
import phex.common.URN;
import phex.common.address.DefaultDestAddress;
import phex.common.address.DestAddress;
import phex.common.bandwidth.BandwidthController;
import phex.io.buffer.ByteBuffer;
import phex.msg.GUID;
import phex.msg.MessageProcessor;
import phex.msg.MsgHeader;
import phex.msg.QueryResponseMsg;
import phex.msg.QueryResponseRecord;
import phex.net.connection.Connection;
import phex.net.repres.SocketFacade;
import phex.prefs.core.LibraryPrefs;
import phex.prefs.core.NetworkPrefs;
import phex.query.QHDConstants;
import phex.upload.UploadManager;
import phex.utils.IOUtil;


public class TestMsgQueryResponse extends TestCase
{

    public TestMsgQueryResponse(String s)
    {
        super(s);
    }

    protected void setUp()
    {
    }

    protected void tearDown()
    {
    }

    public void testCreateAndParse()
        throws Exception
    {
        MsgHeader header = new MsgHeader( new GUID(), MsgHeader.QUERY_HIT_PAYLOAD,
            (byte)0x7, (byte)0x0, 0 );
        QueryResponseRecord rec = new QueryResponseRecord( 1, new URN(
            "urn:sha1:LO4DP3SD3I3CZZP6PIKG3VCQHG4KTQD2" ), 1, "file", -1, null );
        QueryResponseRecord[] recArr =
        {
            rec
        };

        QueryResponseMsg respIn = new QueryResponseMsg( header, new GUID(),
            new DefaultDestAddress( "111.111.111.111", 1111 ), 0, recArr );
        
        ByteBuffer headerBuf = respIn.createHeaderBuffer();
        ByteBuffer messageBuf = respIn.createMessageBuffer();
        int size = headerBuf.remaining() + messageBuf.remaining();
        ByteBuffer combi = ByteBuffer.allocate( size );
        combi.put( headerBuf ).put( messageBuf );
        byte[] output = combi.array();
        
        //System.out.println( new String(output) + "\n" + HexConverter.toHexString( output ) );

        DummySocketFacade socketFac = new DummySocketFacade( output );
        Connection connection = new Connection( socketFac, 
            BandwidthController.acquireBandwidthController( "JUnitText", Long.MAX_VALUE ) );

        QueryResponseMsg respOut = (QueryResponseMsg)MessageProcessor.parseMessage( connection );

        MsgHeader outHeader = respOut.getHeader();
        
        assertEquals( header.getDataLength(), outHeader.getDataLength() );
        assertEquals( header.getHopsTaken(), outHeader.getHopsTaken() );
        assertEquals( header.getMsgID().toHexString(), outHeader.getMsgID().toHexString() );
        assertEquals( header.getPayload(), outHeader.getPayload() );
        assertEquals( header.getTTL(), outHeader.getTTL() );
        assertEquals( respIn.getDestAddress(), respOut.getDestAddress() );
        assertEquals( respIn.getRecordCount(), respOut.getRecordCount() );
        assertEquals( respIn.getRemoteClientID(), respOut.getRemoteClientID() );
        assertEquals( respIn.getRemoteHostSpeed(), respOut.getRemoteHostSpeed() );
        assertEquals( respIn.getUploadSpeedFlag(), respOut.getUploadSpeedFlag() );
        assertEquals( respIn.getPushNeededFlag(), respOut.getPushNeededFlag() );
        assertEquals( LibraryPrefs.AllowBrowsing.get().booleanValue(), respOut.isBrowseHostSupported() );
        assertEquals( QHDConstants.QHD_UNKNOWN_FLAG, respOut.getHasUploadedFlag() );
        assertEquals( "PHEX", respOut.getVendorCode() );
        assertEquals( true, respOut.isChatSupported() );
        assertEquals( NetworkPrefs.AllowChatConnection.get().booleanValue(), respOut.isChatSupported() );

        assertEquals( UploadManager.getInstance().isHostBusy() ?
            QHDConstants.QHD_TRUE_FLAG : QHDConstants.QHD_FALSE_FLAG,
            respOut.getServerBusyFlag() );
    }
    
    private class DummySocketFacade implements SocketFacade
    {
        private ByteArrayInputStream inStream;
        private ByteArrayOutputStream outStream;
        
        public DummySocketFacade( byte[] inStreamContent )
        {
            inStream = new ByteArrayInputStream( inStreamContent );
            outStream = new ByteArrayOutputStream( );
        }

        public void close() throws IOException
        {
            IOUtil.closeQuietly( inStream );
            IOUtil.closeQuietly( outStream );
        }

        public InputStream getInputStream() throws IOException
        {
            return inStream;
        }

        public OutputStream getOutputStream() throws IOException
        {
            return outStream;
        }

        public DestAddress getRemoteAddress()
        {
            return null;
        }

        public void setSoTimeout(int socketRWTimeout) throws SocketException
        {
        }        
    }
}