/****************************************************************************
** Copyright (c) quickfixengine.org  All rights reserved.
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/

#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 )
#include "stdafx.h"
#else
#include "config.h"
#endif

#include <UnitTest++.h>
#include <SocketAcceptor.h>
#include <Utility.h>
#include <fix42/Logon.h>
#include <sstream>
#include "TestHelper.h"

using namespace FIX;

SUITE(SocketAcceptorTests)
{

struct receivePartialMessageFixture
{
  receivePartialMessageFixture()
  {
    SessionSettings settings;
    std::string input =
      "[DEFAULT]\n"
      "ConnectionType=acceptor\n"
      "SocketAcceptPort=5000\n"
      "SocketReuseAddress=Y\n"
      "SendBufferSize=1024\n"
      "ReceiveBufferSize=1024\n"
      "StartTime=00:00:00\n"
      "EndTime=00:00:00\n"
      "UseDataDictionary=N\n"
      "CheckLatency=N\n"
      "[SESSION]\n"
      "BeginString=FIX.4.2\n"
      "SenderCompID=ISLD\n"
      "TargetCompID=TW\n"
      "[SESSION]\n"
      "BeginString=FIX.4.1\n"
      "SenderCompID=ISLD\n"
      "TargetCompID=WT\n";
    std::stringstream stream( input );
    stream >> settings;

    object = new SocketAcceptor( application, factory, settings );
    object->poll();
    s = createSocket( 5000, "127.0.0.1" );
    object->poll();
  }

  ~receivePartialMessageFixture()
  {
    object->stop( true );
    delete object;
    destroySocket( s );
  }

  TestApplication application;
  MemoryStoreFactory factory;
  SocketAcceptor* object;
  int s;
};

TEST_FIXTURE(receivePartialMessageFixture, receivePartialMessage)
{
  std::string firstPart = "8=FIX.4.29=28235=834=2369=31450"
                          "52=20041209-15:35:32.68749=TW50=G56=ISLD"
                          "60=20041209-15:35:3259=055=GE54=148=BLA000060467"
                          "107=BLF5167=FUT44=9740.0041=040=239=038=10"
                          "37=20041209004077151=10150=020=09717=67960"
                          "17=0068712004120909353214=011=000679606=0432=20041209"
                          "1=1234567810=1458=F";

  std::string secondPart = "IX.4.29=34035=834=3369=31450"
                           "52=20041209-15:35:32.69249=TW50=G56=ISLD"
                           "60=20041209-15:35:3259=055=GE54=148=CME000060467"
                           "107=BLF5167=FUT44=9740.0041=040=239=238=10"
                           "37=20041209004077337=0C032=1031=9740.00151=0150=2"
                           "20=09717=6796017=00687220041209093532TN0002843"
                           "75=2004120914=011=00067960375=BLA030A16=0"
                           "432=200412091=1234567810=217";

  FIX42::Logon logon;
  logon.getHeader().set( SenderCompID("TW") );
  logon.getHeader().set( TargetCompID("ISLD") );
  logon.getHeader().set( MsgSeqNum(1) );
  logon.getHeader().set( SendingTime() );
  logon.set( HeartBtInt(30) );

  CHECK( socket_send( s, logon.toString().c_str(), strlen(logon.toString().c_str()) ) );
  object->poll();
  CHECK( socket_send( s, firstPart.c_str(), strlen(firstPart.c_str()) ) );
  object->poll();
  CHECK( socket_send( s, secondPart.c_str(), strlen(secondPart.c_str()) ) );
  object->poll();
}
}
