package com.braintree.commands.impl;

import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.WebhookNotificationGateway;
import com.braintreegateway.WebhookNotification;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.braintree.data.BrainTreeWebhookNotificationRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;


@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class DefaultBrainTreeWebhookNotificationCommandTest {

    private static final String SIGNATURE = "signature";
    private static final String PAYLOAD = "payload";

    @Mock
    private BraintreeGateway mockGateway;

    @Spy
    @InjectMocks
    private DefaultBrainTreeWebhookNotificationCommand webhookNotificationCommand;

    @Test
    public void shouldReturnWebhookNotification() {
        BrainTreeWebhookNotificationRequest notificationRequest = createWebhookNotificationRequest();
        WebhookNotificationGateway mockWebhookNotificationGateway = mock(WebhookNotificationGateway.class);
        WebhookNotification mockWebhookNotification = mock(WebhookNotification.class);

        doReturn(mockGateway).when(webhookNotificationCommand).getBraintreeGateway();
        when(mockGateway.webhookNotification()).thenReturn(mockWebhookNotificationGateway);
        when(mockWebhookNotificationGateway.parse(anyString(), anyString())).thenReturn(mockWebhookNotification);

        WebhookNotification result = webhookNotificationCommand.perform(notificationRequest);

        assertNotNull(result);
        verify(webhookNotificationCommand).getBraintreeGateway();
        verify(mockGateway).webhookNotification();
        verify(mockWebhookNotificationGateway).parse(eq(SIGNATURE), eq(PAYLOAD));
    }

    private BrainTreeWebhookNotificationRequest createWebhookNotificationRequest() {
        BrainTreeWebhookNotificationRequest notificationRequest = new BrainTreeWebhookNotificationRequest();
        notificationRequest.setBtPayload(PAYLOAD);
        notificationRequest.setBtSignature(SIGNATURE);
        return notificationRequest;
    }
}
