package org.knowm.xchange.dto.trade;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import org.junit.Test;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.utils.ObjectMapperHelper;

public class StopOrderTest {
  @Test
  public void testBuilder() {
    final Order.OrderType type = Order.OrderType.BID;
    final BigDecimal originalAmount = new BigDecimal("99.401");
    final BigDecimal averagePrice = new BigDecimal("255.00");
    final BigDecimal cumulativeAmount = new BigDecimal("0.00");
    final CurrencyPair currencyPair = CurrencyPair.LTC_BTC;
    final BigDecimal limitPrice = new BigDecimal("251.64");
    final BigDecimal stopPrice = new BigDecimal("266.21");
    final BigDecimal fee = new BigDecimal("22.2");
    final Date timestamp = new Date();
    final String id = "id";
    final Order.OrderStatus status = Order.OrderStatus.FILLED;

    final StopOrder copy =
        new StopOrder.Builder(type, currencyPair)
            .originalAmount(originalAmount)
            .averagePrice(averagePrice)
            .cumulativeAmount(cumulativeAmount)
            .limitPrice(limitPrice)
            .stopPrice(stopPrice)
            .orderStatus(status)
            .timestamp(timestamp)
            .id(id)
            .flag(TestFlags.TEST1)
            .fee(fee)
            .build();

    assertThat(copy.getType()).isEqualTo(type);
    assertThat(copy.getOriginalAmount()).isEqualTo(originalAmount);
    assertThat(copy.getAveragePrice()).isEqualTo(averagePrice);
    assertThat(copy.getCumulativeAmount()).isEqualTo(cumulativeAmount);
    assertThat(copy.getCurrencyPair()).isEqualTo(currencyPair);
    assertThat(copy.getStopPrice()).isEqualTo(stopPrice);
    assertThat(copy.getLimitPrice()).isEqualTo(limitPrice);
    assertThat(copy.getTimestamp()).isEqualTo(timestamp);
    assertThat(copy.getId()).isEqualTo(id);
    assertThat(copy.getOrderFlags()).hasSize(1);
    assertThat(copy.getOrderFlags()).containsExactly(TestFlags.TEST1);
    assertThat(copy.hasFlag(TestFlags.TEST1));
    assertThat(copy.getStatus()).isEqualTo(status);
    assertThat(copy.getFee()).isEqualTo(fee);
  }

  @Test
  public void testBuilderFrom() throws IOException {
    final Order.OrderType type = Order.OrderType.ASK;
    final BigDecimal originalAmount = new BigDecimal("100.501");
    final CurrencyPair currencyPair = CurrencyPair.BTC_USD;
    final BigDecimal limitPrice = new BigDecimal("250.34");
    final BigDecimal averagePrice = new BigDecimal("255.00");
    final BigDecimal cumulativeAmount = new BigDecimal("0.00");
    final BigDecimal stopPrice = new BigDecimal("266.21");
    final BigDecimal fee = new BigDecimal("22.2");
    final Date timestamp = new Date();
    final String id = "id";
    final Order.OrderStatus status = Order.OrderStatus.FILLED;

    final StopOrder original =
        new StopOrder(
            type,
            originalAmount,
            currencyPair,
            id,
            timestamp,
            stopPrice,
            limitPrice,
            averagePrice,
            cumulativeAmount,
            fee,
            status);
    original.addOrderFlag(TestFlags.TEST1);
    original.addOrderFlag(TestFlags.TEST3);
    final StopOrder copy = StopOrder.Builder.from(original).build();

    assertThat(copy).isEqualToComparingFieldByField(original);

    StopOrder jsonCopy = ObjectMapperHelper.viaJSON(copy);
    assertThat(jsonCopy).isEqualToIgnoringGivenFields(copy, "cumulativeAmount");
    assertTrue(jsonCopy.getCumulativeAmount().compareTo(copy.getCumulativeAmount()) == 0);
  }

  private enum TestFlags implements Order.IOrderFlags {
    TEST1,
    TEST2,
    TEST3
  }
}

@@ -213,16 +213,17 @@
 l.Date;%0A
+%0A
 import o
@@ -3490,16 +3490,1091 @@
 iginal);
+%0A  %7D%0A%0A  @Test%0A  public void testSerializeDeserialize() throws IOException %7B%0A    final Order.OrderType type = Order.OrderType.ASK;%0A    final BigDecimal originalAmount = new BigDecimal(%22100.501%22);%0A    final CurrencyPair currencyPair = CurrencyPair.BTC_USD;%0A    final BigDecimal limitPrice = new BigDecimal(%22250.34%22);%0A    final BigDecimal averagePrice = new BigDecimal(%22255.00%22);%0A    final BigDecimal cumulativeAmount = new BigDecimal(%220.00%22);%0A    final BigDecimal stopPrice = new BigDecimal(%22266.21%22);%0A    final BigDecimal fee = new BigDecimal(%2222.2%22);%0A    final Date timestamp = new Date();%0A    final String id = %22id%22;%0A    final Order.OrderStatus status = Order.OrderStatus.FILLED;%0A%0A    final StopOrder original =%0A        new StopOrder(%0A            type,%0A            originalAmount,%0A            currencyPair,%0A            id,%0A            timestamp,%0A            stopPrice,%0A            limitPrice,%0A            averagePrice,%0A            cumulativeAmount,%0A            fee,%0A            status);%0A    original.addOrderFlag(TestFlags.TEST1);%0A    original.addOrderFlag(TestFlags.TEST3);
 %0A%0A    St
@@ -4619,20 +4619,24 @@
 viaJSON(
-copy
+original
 );%0A    a
@@ -4684,20 +4684,24 @@
 nFields(
-copy
+original
 , %22cumul
@@ -4771,20 +4771,24 @@
 mpareTo(
-copy
+original
 .getCumu

Move serialization/deserialization to its own test