/****************************************************************************
** Copyright (c) 2001-2014
**
** 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 "TestHelper.h"
#include <SocketAcceptor.h>
#include <Utility.h>
#include <fix42/Logon.h>
#include <sstream>

#include "catch_amalgamated.hpp"

using namespace FIX;

TEST_CASE("SocketAcceptorTests") {
  TestApplication application;
  MemoryStoreFactory factory;
  socket_handle socket;

  SessionSettings settings;
  std::string input = "[DEFAULT]\n"
                      "ConnectionType=acceptor\n"
                      "SocketAcceptPort=0\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;

  SocketAcceptor object(application, factory, settings);
  object.poll();
  socket = createSocket(object.sessionToPort().find(SessionID("FIX.4.2", "ISLD", "TW"))->second, "127.0.0.1");
  object.poll();

  SECTION("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::now());
    logon.set(HeartBtInt(30));

    CHECK(socket_send(socket, logon.toString().c_str(), (int)strlen(logon.toString().c_str())));
    object.poll();
    CHECK(socket_send(socket, firstPart.c_str(), (int)strlen(firstPart.c_str())));
    object.poll();
    CHECK(socket_send(socket, secondPart.c_str(), (int)strlen(secondPart.c_str())));
    object.poll();
  }

  object.stop(true);
  destroySocket(socket);
}
