/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2004 Gregor Koukkoullis ( phex <at> kouk <dot> de )
 *
 *  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
 * 
 *  --- CVS Information ---
 *  $Id: TestMsgQueryResponse.java,v 1.4 2004/01/18 16:45:26 gregork Exp $
 */
package phex.test;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.util.Arrays;

import junit.framework.TestCase;
import phex.common.*;
import phex.host.HostAddress;
import phex.msg.*;
import phex.query.QueryConstants;
import phex.upload.UploadManager;
import phex.utils.*;
import phex.utils.GnutellaInputStream;


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 );
        MsgResRecord rec = new MsgResRecord( 1, new URN(
            "urn:sha1:LO4DP3SD3I3CZZP6PIKG3VCQHG4KTQD2" ), 1, "file" );
        MsgResRecord[] recArr =
        {
            rec
        };

        MsgQueryResponse respIn = new MsgQueryResponse( header, new GUID(),
            new HostAddress( "111.111.111.111:1111" ), 0, recArr );
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        GnutellaOutputStream gOutStream = new GnutellaOutputStream(outStream);
        respIn.writeMessage( gOutStream );

        byte[] output = outStream.toByteArray();
        //System.out.println( new String(output) + "\n" + HexConverter.toHexString( output ) );

        GnutellaInputStream stream = new GnutellaInputStream(
            new ByteArrayInputStream( output ) );

        MsgQueryResponse respOut = (MsgQueryResponse)MessageProcessor.parseMessage( stream );

        MsgHeader outHeader = respOut.getHeader();
        assertEquals( header.getDataLen(), outHeader.getDataLen() );
        assertEquals( header.getHopsTaken(), outHeader.getHopsTaken() );
        assertEquals( header.getMsgID().toHexString(), outHeader.getMsgID().toHexString() );
        assertEquals( header.getPayload(), outHeader.getPayload() );
        assertEquals( header.getSize(), outHeader.getSize() );
        assertEquals( header.getTTL(), outHeader.getTTL() );

        assertEquals( respIn.getHostAddress(), respOut.getHostAddress() );
        assertEquals( true, Arrays.equals( respIn.getHostIP(), respOut.getHostIP() ) );
        assertEquals( respIn.getRecordCount(), respOut.getRecordCount() );
        assertEquals( respIn.getRemoteClientID(), respOut.getRemoteClientID() );
        assertEquals( respIn.getRemoteHostSpeed(), respOut.getRemoteHostSpeed() );
        assertEquals( respIn.getSize(), respOut.getSize() );
        assertEquals( respIn.getUploadSpeedFlag(), respOut.getUploadSpeedFlag() );
        assertEquals( respIn.getPushNeededFlag(), respOut.getPushNeededFlag() );
        assertEquals( ServiceManager.sCfg.mShareBrowseDir, respOut.isBrowseHostSupported() );
        assertEquals( QueryConstants.QHD_UNKNOWN_FLAG, respOut.getHasUploadedFlag() );
        assertEquals( "PHEX", respOut.getVendorCode() );
        assertEquals( true, respOut.isChatSupported() );
        assertEquals( ServiceManager.sCfg.isChatEnabled, respOut.isChatSupported() );

        assertEquals( UploadManager.getInstance().isHostBusy() ?
            QueryConstants.QHD_TRUE_FLAG : QueryConstants.QHD_FALSE_FLAG,
            respOut.getServerBusyFlag() );
    }
}