using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NT8.Core.OMS;
namespace NT8.Core.Tests.Mocks
{
///
/// Mock implementation of INT8OrderAdapter for testing purposes
///
public class MockNT8OrderAdapter : INT8OrderAdapter
{
private readonly List> _callbacks;
private readonly object _lock;
private bool _disposed = false;
private bool _isConnected = false;
private bool _shouldSucceed = true;
private bool _shouldFail = false;
private int _submitOrderCallCount = 0;
private int _modifyOrderCallCount = 0;
private int _cancelOrderCallCount = 0;
///
/// Gets or sets whether the next operation should succeed
///
public bool ShouldSucceed
{
get { return _shouldSucceed; }
set { _shouldSucceed = value; }
}
///
/// Gets or sets whether the next operation should fail
///
public bool ShouldFail
{
get { return _shouldFail; }
set { _shouldFail = value; }
}
///
/// Gets the count of submitted orders
///
public int SubmitOrderCallCount
{
get { return _submitOrderCallCount; }
private set { _submitOrderCallCount = value; }
}
///
/// Gets the count of modified orders
///
public int ModifyOrderCallCount
{
get { return _modifyOrderCallCount; }
private set { _modifyOrderCallCount = value; }
}
///
/// Gets the count of cancelled orders
///
public int CancelOrderCallCount
{
get { return _cancelOrderCallCount; }
private set { _cancelOrderCallCount = value; }
}
///
/// Constructor for MockNT8OrderAdapter
///
public MockNT8OrderAdapter()
{
_callbacks = new List>();
_lock = new object();
}
///
/// Submit order to NinjaTrader 8 (mock implementation)
///
public async Task SubmitOrderAsync(OrderRequest request)
{
SubmitOrderCallCount++;
if (ShouldFail)
{
return false;
}
// Simulate successful submission
return ShouldSucceed;
}
///
/// Modify existing order in NinjaTrader 8 (mock implementation)
///
public async Task ModifyOrderAsync(OrderModification modification)
{
ModifyOrderCallCount++;
if (ShouldFail)
{
return false;
}
// Simulate successful modification
return ShouldSucceed;
}
///
/// Cancel order in NinjaTrader 8 (mock implementation)
///
public async Task CancelOrderAsync(OrderCancellation cancellation)
{
CancelOrderCallCount++;
if (ShouldFail)
{
return false;
}
// Simulate successful cancellation
return ShouldSucceed;
}
///
/// Register callback for order status updates (mock implementation)
///
public void RegisterOrderCallback(Action callback)
{
if (callback == null)
throw new ArgumentNullException("callback");
lock (_lock)
{
_callbacks.Add(callback);
}
}
///
/// Unregister callback for order status updates (mock implementation)
///
public void UnregisterOrderCallback(Action callback)
{
if (callback == null)
throw new ArgumentNullException("callback");
lock (_lock)
{
_callbacks.Remove(callback);
}
}
///
/// Connect to NinjaTrader 8 (mock implementation)
///
public async Task ConnectAsync()
{
if (ShouldFail)
{
return false;
}
_isConnected = true;
return ShouldSucceed;
}
///
/// Disconnect from NinjaTrader 8 (mock implementation)
///
public async Task DisconnectAsync()
{
_isConnected = false;
return true;
}
///
/// Fire an order status update to all registered callbacks
///
/// The order status to fire
public void FireOrderUpdate(OrderStatus status)
{
lock (_lock)
{
foreach (var callback in _callbacks)
{
try
{
callback(status);
}
catch
{
// Ignore exceptions in callbacks for this mock
}
}
}
}
///
/// Gets whether the adapter is currently connected
///
public bool IsConnected
{
get
{
return _isConnected;
}
}
///
/// Dispose resources
///
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
}
}
}
}