package com.commercetools.pspadapter.payone.domain.ctp;

import static org.assertj.core.api.Assertions.assertThat;

import com.commercetools.pspadapter.payone.mapping.CustomFieldKeys;
import io.sphere.sdk.carts.Cart;
import io.sphere.sdk.orders.Order;
import io.sphere.sdk.payments.Payment;
import org.junit.Test;
import util.PaymentTestHelper;

/**
 * @author fhaertig
 * @since 17.03.16
 */
public class PaymentWithCartLikeTest {

    private final PaymentTestHelper testHelper = new PaymentTestHelper();

    @Test
    public void createFromOrderAndPaymentWithReference() throws Exception {
        Payment payment = testHelper.dummyPaymentOneAuthPending20EuroCC();
        Order order = testHelper.dummyOrderMapToPayoneRequest();

        PaymentWithCartLike testee = new PaymentWithCartLike(payment, order);

        assertThat(testee.getOrderNumber()).isEqualTo(payment.getCustom().getFieldAsString(CustomFieldKeys.REFERENCE_FIELD));
        assertThat(testee.getCartLike()).isEqualTo(order);
    }

    @Test
    public void createFromCartAndPaymentWithReference() throws Exception {
        Payment payment = testHelper.dummyPaymentOneAuthPending20EuroCC();
        Cart cart = testHelper.dummyCart();

        PaymentWithCartLike testee = new PaymentWithCartLike(payment, cart);

        assertThat(testee.getOrderNumber()).isEqualTo(payment.getCustom().getFieldAsString(CustomFieldKeys.REFERENCE_FIELD));
        assertThat(testee.getCartLike()).isEqualTo(cart);
    }

    @Test
    public void createFromOrderAndPaymentWithoutReference() throws Exception {
        Payment payment = testHelper.dummyPaymentNoCustomFields();
        Order order = testHelper.dummyOrderMapToPayoneRequest();

        PaymentWithCartLike testee = new PaymentWithCartLike(payment, order);

        assertThat(testee.getOrderNumber()).isEqualTo(null);
        assertThat(testee.getCartLike()).isEqualTo(order);
    }

    @Test
    public void createWithNewPaymentOrderNumberChanges() throws Exception {
        Payment payment = testHelper.dummyPaymentNoCustomFields();
        Order order = testHelper.dummyOrderMapToPayoneRequest();

        PaymentWithCartLike testee = new PaymentWithCartLike(payment, order);

        assertThat(testee.getOrderNumber()).isEqualTo(null);
        assertThat(testee.getCartLike()).isEqualTo(order);

        Payment newPayment = testHelper.dummyPaymentOneAuthPending20EuroCC();

        testee = testee.withPayment(newPayment);

        assertThat(testee.getOrderNumber()).isEqualTo(newPayment.getCustom().getFieldAsString(CustomFieldKeys.REFERENCE_FIELD));
        assertThat(testee.getCartLike()).isEqualTo(order);
    }
}
@@ -107,16 +107,83 @@
 ertThat;
+%0Aimport static org.assertj.core.api.ThrowableAssert.catchThrowable;
 %0A%0Aimport
@@ -351,16 +351,56 @@
 ayment;%0A
+import org.assertj.core.api.Assertions;%0A
 import o
@@ -1307,24 +1307,25 @@
 mmyCart();%0A%0A
+%0A
         Paym
@@ -1815,36 +1815,56 @@
     
-PaymentWithCartLike testee =
+final Throwable throwable = catchThrowable(() -%3E
  new
@@ -1891,32 +1891,33 @@
 (payment, order)
+)
 ;%0A%0A        asser
@@ -1903,32 +1903,43 @@
 der));%0A%0A        
+Assertions.
 assertThat(teste
@@ -1938,105 +1938,153 @@
 at(t
-estee.getOrderNumber()).isEqualTo(null);%0A        assertThat(testee.getCartLike()).isEqualTo(order
+hrowable)%0A                .isInstanceOf(IllegalStateException.class)%0A                .hasMessageContaining(%22is missing the required custom field%22
 );%0A 
@@ -2220,38 +2220,47 @@
 dummyPayment
-NoCustomFields
+OneAuthPending20EuroPNT
 ();%0A        
@@ -2454,12 +2454,77 @@
 lTo(
-null
+payment.getCustom().getFieldAsString(CustomFieldKeys.REFERENCE_FIELD)
 );%0A 

fix test for PaymentWithCartLike: check for IllegalStateException