add order and order test classes
This commit is contained in:
64
src/test/java/ats/orders/MarketOrderRequestTest.java
Normal file
64
src/test/java/ats/orders/MarketOrderRequestTest.java
Normal file
@ -0,0 +1,64 @@
|
||||
package ats.orders;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* MarketOrderRequestTest tests JSON format serialization of
|
||||
* MarketOrderRequest objects.
|
||||
*/
|
||||
public class MarketOrderRequestTest extends OrderSerializationTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Test the simplest form, including default values.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONSmall()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
MarketOrderRequest obj = new MarketOrderRequest("EUR_USD", 34);
|
||||
|
||||
String expected = "{\"type\":\"MARKET\",\"instrument\":\"EUR_USD\",\"units\":34,\"timeInForce\":\"FOK\",\"positionFill\":\"DEFAULT\"}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a fully filled out object.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONLarge()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
TakeProfitDetails tpd = new TakeProfitDetails();
|
||||
tpd.price = "66.2";
|
||||
tpd.timeInForce = TimeInForce.GTD;
|
||||
tpd.gtdTime = new DateTime("2010-10-10T14:15:16.000Z");
|
||||
|
||||
StopLossDetails sld = new StopLossDetails();
|
||||
sld.distance = new BigDecimal("22.7");
|
||||
sld.timeInForce = TimeInForce.IOC;
|
||||
sld.gtdTime = new DateTime("2012-12-12T14:15:16.000Z");
|
||||
sld.guaranteed = true;
|
||||
|
||||
TrailingStopLossDetails tsld = new TrailingStopLossDetails();
|
||||
tsld.distance = new BigDecimal("33.6");
|
||||
tsld.timeInForce = TimeInForce.IOC;
|
||||
tsld.gtdTime = new DateTime("2013-3-13T14:15:16.000Z");
|
||||
|
||||
MarketOrderRequest obj =
|
||||
new MarketOrderRequest("EUR_USD", 34, TimeInForce.IOC,
|
||||
"99.44", OrderPositionFill.REDUCE_ONLY,
|
||||
null, tpd, sld, tsld, null);
|
||||
|
||||
String expected = "{\"stopLossOnFill\":{\"distance\":22.7,\"timeInForce\":\"IOC\",\"gtdTime\":\"2012-12-12T14:15:16.000Z\",\"guaranteed\":true},\"takeProfitOnFill\":{\"price\":\"66.2\",\"timeInForce\":\"GTD\",\"gtdTime\":\"2010-10-10T14:15:16.000Z\"},\"instrument\":\"EUR_USD\",\"units\":34,\"priceBound\":\"99.44\",\"type\":\"MARKET\",\"timeInForce\":\"IOC\",\"positionFill\":\"REDUCE_ONLY\",\"trailingStopLossOnFill\":{\"distance\":33.6,\"timeInForce\":\"IOC\",\"gtdTime\":\"2013-03-13T14:15:16.000Z\"}}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
}
|
||||
42
src/test/java/ats/orders/OrderSerializationTestBase.java
Normal file
42
src/test/java/ats/orders/OrderSerializationTestBase.java
Normal file
@ -0,0 +1,42 @@
|
||||
package ats.orders;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* OrderSerializationTestBase is a base class for testing JSON format
|
||||
* serialization of order types for sending to the OANDA api.
|
||||
*/
|
||||
public class OrderSerializationTestBase {
|
||||
private static final Logger log = LoggerFactory.getLogger(OrderSerializationTestBase.class);
|
||||
|
||||
/**
|
||||
* Test an object against its expected json representation.
|
||||
*/
|
||||
protected void doTest(Object obj, String expected)
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
doTest(obj, expected, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an object against its expected json representation.
|
||||
*
|
||||
* @param verbose Log a little extra if true.
|
||||
*/
|
||||
protected void doTest(Object obj, String expected, boolean verbose)
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
ObjectMapper mapper = OrderRequest.getJSONMapper();
|
||||
String actual = mapper.writeValueAsString(obj);
|
||||
|
||||
if (verbose) log.warn(actual);
|
||||
|
||||
JSONAssert.assertEquals(expected, actual, true);
|
||||
}
|
||||
}
|
||||
53
src/test/java/ats/orders/StopLossDetailsTest.java
Normal file
53
src/test/java/ats/orders/StopLossDetailsTest.java
Normal file
@ -0,0 +1,53 @@
|
||||
package ats.orders;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* StopLossDetailsTest tests JSON format serialization of
|
||||
* StopLossDetails objects.
|
||||
*/
|
||||
public class StopLossDetailsTest extends OrderSerializationTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Test the simplest form, including default values.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONSmall()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
StopLossDetails obj = new StopLossDetails();
|
||||
obj.price = "12.34";
|
||||
|
||||
String expected = "{\"price\":\"12.34\",\"timeInForce\":\"GTC\",\"guaranteed\":false}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a fully filled out object.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONLarge()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
StopLossDetails obj = new StopLossDetails();
|
||||
obj.distance = new BigDecimal("12.34");
|
||||
obj.timeInForce = TimeInForce.GFD;
|
||||
obj.gtdTime = new DateTime("2000-09-17T18:02:52.957Z");
|
||||
|
||||
obj.clientExtensions = new ClientExtensions();
|
||||
obj.clientExtensions.id = "my id";
|
||||
obj.clientExtensions.tag = "my tag";
|
||||
obj.clientExtensions.comment = "my comment";
|
||||
|
||||
String expected = "{\"distance\":12.34,\"timeInForce\":\"GFD\",\"gtdTime\":\"2000-09-17T18:02:52.957Z\",\"clientExtensions\":{\"id\":\"my id\",\"tag\":\"my tag\",\"comment\":\"my comment\"},\"guaranteed\":false}";
|
||||
doTest(obj, expected);
|
||||
}
|
||||
}
|
||||
46
src/test/java/ats/orders/TakeProfitDetailsTest.java
Normal file
46
src/test/java/ats/orders/TakeProfitDetailsTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package ats.orders;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* TakeProfitDetailsTest tests JSON format serialization of
|
||||
* TakeProfitDetails objects.
|
||||
*/
|
||||
public class TakeProfitDetailsTest extends OrderSerializationTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Test the simplest form, including default values.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONSmall() throws JsonProcessingException, JSONException {
|
||||
TakeProfitDetails obj = new TakeProfitDetails();
|
||||
obj.price = "12.34";
|
||||
|
||||
String expected = "{\"price\":\"12.34\",\"timeInForce\":\"GTC\"}";
|
||||
doTest(obj, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a fully filled out object.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONLarge() throws JsonProcessingException, JSONException {
|
||||
TakeProfitDetails obj = new TakeProfitDetails();
|
||||
obj.price = "12.34";
|
||||
obj.timeInForce = TimeInForce.GFD;
|
||||
obj.gtdTime = new DateTime("2000-09-17T18:02:52.957Z");
|
||||
|
||||
obj.clientExtensions = new ClientExtensions();
|
||||
obj.clientExtensions.id = "my id";
|
||||
obj.clientExtensions.tag = "my tag";
|
||||
obj.clientExtensions.comment = "my comment";
|
||||
|
||||
String expected = "{\"price\":\"12.34\",\"timeInForce\":\"GFD\",\"gtdTime\":\"2000-09-17T18:02:52.957Z\",\"clientExtensions\":{\"id\":\"my id\",\"tag\":\"my tag\",\"comment\":\"my comment\"}}";
|
||||
doTest(obj, expected);
|
||||
}
|
||||
}
|
||||
54
src/test/java/ats/orders/TakeProfitOrderRequestTest.java
Normal file
54
src/test/java/ats/orders/TakeProfitOrderRequestTest.java
Normal file
@ -0,0 +1,54 @@
|
||||
package ats.orders;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.oanda.v20.order.OrderTriggerCondition;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* TakeProfitOrderRequestTest tests JSON format serialization of
|
||||
* TakeProfitOrderRequest objects.
|
||||
*/
|
||||
public class TakeProfitOrderRequestTest extends OrderSerializationTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Test the simplest form, including default values.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONSmall()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
TakeProfitOrderRequest obj = new TakeProfitOrderRequest("12", "23", "45.67");
|
||||
|
||||
String expected = "{\"type\":\"TAKE_PROFIT\",\"tradeID\":\"12\",\"clientTradeID\":\"23\",\"price\":\"45.67\",\"timeInForce\":\"GTC\",\"triggerCondition\":\"DEFAULT\"}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a fully filled out object.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONLarge()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
ClientExtensions clientExtensions = new ClientExtensions();
|
||||
clientExtensions.id = "my id";
|
||||
clientExtensions.tag = "my tag";
|
||||
clientExtensions.comment = "my comment";
|
||||
|
||||
TakeProfitOrderRequest obj = new TakeProfitOrderRequest("12", "23", "45.67",
|
||||
TimeInForce.IOC,
|
||||
new DateTime("2000-09-17T18:02:52.957Z"),
|
||||
OrderTriggerCondition.MID,
|
||||
clientExtensions);
|
||||
|
||||
|
||||
String expected = "{\"type\":\"TAKE_PROFIT\",\"tradeID\":\"12\",\"clientTradeID\":\"23\",\"price\":\"45.67\",\"timeInForce\":\"IOC\",\"gtdTime\":\"2000-09-17T18:02:52.957Z\",\"triggerCondition\":\"MID\",\"clientExtensions\":{\"id\":\"my id\",\"tag\":\"my tag\",\"comment\":\"my comment\"}}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
}
|
||||
53
src/test/java/ats/orders/TrailingStopLossDetailsTest.java
Normal file
53
src/test/java/ats/orders/TrailingStopLossDetailsTest.java
Normal file
@ -0,0 +1,53 @@
|
||||
package ats.orders;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* TrailingStopLossDetailsTest tests JSON format serialization of
|
||||
* TrailingStopLossDetails objects.
|
||||
*/
|
||||
public class TrailingStopLossDetailsTest extends OrderSerializationTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Test the simplest form, including default values.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONSmall()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
TrailingStopLossDetails obj = new TrailingStopLossDetails();
|
||||
obj.distance = new BigDecimal("12.34");
|
||||
|
||||
String expected = "{\"distance\":12.34,\"timeInForce\":\"GTC\"}";
|
||||
|
||||
doTest(obj, expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a fully filled out object.
|
||||
*/
|
||||
@Test
|
||||
public void testToJSONLarge()
|
||||
throws JsonProcessingException, JSONException
|
||||
{
|
||||
TrailingStopLossDetails obj = new TrailingStopLossDetails();
|
||||
obj.distance = new BigDecimal("12.34");
|
||||
obj.timeInForce = TimeInForce.GFD;
|
||||
obj.gtdTime = new DateTime("2000-09-17T18:02:52.957Z");
|
||||
|
||||
obj.clientExtensions = new ClientExtensions();
|
||||
obj.clientExtensions.id = "my id";
|
||||
obj.clientExtensions.tag = "my tag";
|
||||
obj.clientExtensions.comment = "my comment";
|
||||
|
||||
String expected = "{\"distance\":12.34,\"timeInForce\":\"GFD\",\"gtdTime\":\"2000-09-17T18:02:52.957Z\",\"clientExtensions\":{\"id\":\"my id\",\"tag\":\"my tag\",\"comment\":\"my comment\"}}";
|
||||
doTest(obj, expected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user