package org.epcc.ps.client.shell.command;

import org.epcc.ps.client.shell.AbstractTest;
import org.epcc.ps.client.shell.exception.ConvertException;
import org.epcc.ps.client.shell.exception.SimulationSourceNotFoundException;
import org.epcc.ps.client.shell.service.ConvertService;
import org.epcc.ps.core.entity.creature.Creature;
import org.epcc.ps.core.entity.creature.CreatureFactory;
import org.epcc.ps.core.entity.creature.Species;
import org.epcc.ps.core.entity.environment.Grid;
import org.epcc.ps.core.entity.environment.GridFactory;
import org.epcc.ps.core.entity.environment.Landscape;
import org.epcc.ps.core.entity.environment.Terrain;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * @author shaohan.yin
 * Created on 29/10/2017
 */
public class SimulationCommandTest extends AbstractTest {
    private static final String FILE_FLAG = "-f";
    private static final String NON_EXISTS = "non-exists";

    private ConvertService convertService;
    private SimulationCommand simulationCommand;

    @Before
    public void setUp() throws ConvertException {
        convertService = Mockito.mock(ConvertService.class);
        simulationCommand = new DefaultSimulationCommand(convertService);


        Map<Species, Creature> creatures = new HashMap<>();
        creatures.put(Species.HARE, CreatureFactory.create(Species.HARE));
        creatures.put(Species.PUMA, CreatureFactory.create(Species.PUMA));
        Landscape landscape = new Landscape(1, 1,
                new Grid[][]{{GridFactory.create(Terrain.LAND, creatures)}});
        when(convertService.convertLandscapeFromFile(anyString())).thenReturn(landscape);
    }

    @Test
    public void testSimulationCommand() throws Exception {
        simulationCommand.simulate(new String[]{FILE_FLAG, NON_EXISTS});

        verify(convertService, times(1)).convertLandscapeFromFile(anyString());

        verify(convertService, times(26)).convertLandscapeWithSpeciesToPPM(any(), any(), any());
    }

    @Test
    public void testSimulationCommandWithWrongFlag() {
        try {
            simulationCommand.simulate(null);
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof SimulationSourceNotFoundException);
            logger.error("Expected error:",e);
        }
    }
}

@@ -146,85 +146,8 @@
 on;%0A
-import org.epcc.ps.client.shell.exception.SimulationSourceNotFoundException;%0A
 impo
@@ -198,16 +198,16 @@
 ervice;%0A
+
 import o
@@ -569,33 +569,8 @@
 in;%0A
-import org.junit.Assert;%0A
 impo
@@ -831,95 +831,9 @@
 ito.
-times;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when
+*
 ;%0A%0A/
@@ -2179,26 +2179,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -2221,203 +2221,8 @@
 l);%0A
-            Assert.fail();%0A        %7D catch (Exception e) %7B%0A            Assert.assertTrue(e instanceof SimulationSourceNotFoundException);%0A            logger.error(%22Expected error:%22,e);%0A        %7D%0A
     

fix ci failure