package net.contargo.iris.connection.dto;

import net.contargo.iris.connection.MainRunConnection;
import net.contargo.iris.connection.service.MainRunConnectionService;
import net.contargo.iris.route.RouteType;
import net.contargo.iris.seaport.Seaport;
import net.contargo.iris.terminal.Terminal;

import org.junit.Before;
import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import org.mockito.runners.MockitoJUnitRunner;

import java.math.BigDecimal;
import java.math.BigInteger;

import java.util.Collections;
import java.util.List;

import static net.contargo.iris.route.RouteType.BARGE;

import static org.hamcrest.MatcherAssert.assertThat;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

import static org.mockito.Matchers.any;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static java.util.Arrays.asList;


/**
 * @author  Oliver Messner - messner@synyx.de
 */
@RunWith(MockitoJUnitRunner.class)
public class MainRunConnectionDtoServiceImplUnitTest {

    private MainRunConnectionDtoServiceImpl sut;

    @Mock
    private MainRunConnectionService mainRunConnectionServiceMock;
    private BigInteger terminalUID;
    private Terminal terminal;
    private BigInteger seaportOneUID;
    private Seaport seaportOne;
    private BigInteger seaportTwoUID;
    private Seaport seaportTwo;

    @Before
    public void before() {

        sut = new MainRunConnectionDtoServiceImpl(mainRunConnectionServiceMock);

        terminalUID = new BigInteger("2");
        terminal = new Terminal();
        terminal.setUniqueId(terminalUID);

        seaportOneUID = new BigInteger("3");
        seaportOne = new Seaport();
        seaportOne.setUniqueId(seaportOneUID);

        seaportTwoUID = new BigInteger("4");
        seaportTwo = new Seaport();
        seaportTwo.setUniqueId(seaportTwoUID);
    }


    @Test
    public void getConnectionsForTerminal() {

        MainRunConnection c1 = newConnection(seaportOne, terminal, BARGE);
        MainRunConnection c2 = newConnection(seaportTwo, terminal, BARGE);

        when(mainRunConnectionServiceMock.getConnectionsForTerminal(terminalUID)).thenReturn(asList(c1, c2));

        SimpleMainRunConnectionDto dto1 = new SimpleMainRunConnectionDto(seaportOneUID.toString(),
                terminalUID.toString(), BARGE);
        SimpleMainRunConnectionDto dto2 = new SimpleMainRunConnectionDto(seaportTwoUID.toString(),
                terminalUID.toString(), BARGE);

        List<SimpleMainRunConnectionDto> expectedConnections = asList(dto1, dto2);

        assertConnections(sut.getConnectionsForTerminal(terminalUID), expectedConnections);
    }


    @Test
    public void get() {

        when(mainRunConnectionServiceMock.getById(42L)).thenReturn(newConnection(seaportOne, terminal, BARGE));

        MainRunConnectionDto dto = sut.get(42L);

        assertThat(dto.getRouteType(), is(BARGE));
    }


    @Test
    public void save() {

        MainRunConnectionDto dto = new MainRunConnectionDto(42L, BigInteger.ONE.toString(), BigInteger.TEN.toString(),
                BigDecimal.ONE, BigDecimal.TEN, BigDecimal.ZERO, BARGE, true,
                Collections.<AbstractSubConnectionDto>emptyList());

        MainRunConnection connection = newConnection(seaportOne, terminal, BARGE);
        connection.setId(42L);

        when(mainRunConnectionServiceMock.save(any(MainRunConnection.class))).thenReturn(connection);

        MainRunConnectionDto savedDto = sut.save(dto);

        assertThat(savedDto.getId(), is(42L));

        ArgumentCaptor<MainRunConnection> captor = ArgumentCaptor.forClass(MainRunConnection.class);
        verify(mainRunConnectionServiceMock).save(captor.capture());

        MainRunConnection value = captor.getValue();
        assertThat(value.getId(), is(42L));
        assertThat(value.getRouteType(), is(BARGE));
    }


    private MainRunConnection newConnection(Seaport seaport, Terminal terminal, RouteType type) {

        MainRunConnection connection = new MainRunConnection();
        connection.setSeaport(seaport);
        connection.setTerminal(terminal);
        connection.setRouteType(type);

        return connection;
    }


    private void assertConnections(List<SimpleMainRunConnectionDto> actual, List<SimpleMainRunConnectionDto> expected) {

        assertThat(actual, hasSize(expected.size()));

        for (int i = 0; i < actual.size(); i++) {
            assertThat(actual.get(i).getTerminalUid(), is(expected.get(i).getTerminalUid()));
            assertThat(actual.get(i).getSeaportUid(), is(expected.get(i).getSeaportUid()));
            assertThat(actual.get(i).getRouteType(), is(expected.get(i).getRouteType()));
        }
    }
}

@@ -486,37 +486,8 @@
 r;%0A%0A
-import java.math.BigDecimal;%0A
 impo
@@ -909,32 +909,214 @@
 ava.
-util.Arrays.asList;%0A%0A%0A/*
+math.BigDecimal.ONE;%0Aimport static java.math.BigDecimal.TEN;%0Aimport static java.math.BigDecimal.ZERO;%0A%0Aimport static java.util.Arrays.asList;%0A%0A%0A/**%0A * Unit test of %7B@link MainRunConnectionDtoServiceImpl%7D.%0A 
 *%0A *
@@ -1381,16 +1381,17 @@
 ceMock;%0A
+%0A
     priv
@@ -2717,92 +2717,8 @@
 );%0A%0A
-        List%3CSimpleMainRunConnectionDto%3E expectedConnections = asList(dto1, dto2);%0A%0A
     
@@ -2787,27 +2787,26 @@
 D), 
-expectedConnections
+asList(dto1, dto2)
 );%0A 
@@ -3244,51 +3244,18 @@
     
-BigDecimal.ONE, BigDecimal.TEN, BigDecimal.
+ONE, TEN, 
 ZERO
@@ -3268,32 +3268,16 @@
 E, true,
-%0A               
  Collect

add static imports