diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/CommonTests.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/CommonTests.java deleted file mode 100644 index c3f4f14..0000000 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/CommonTests.java +++ /dev/null @@ -1,64 +0,0 @@ -package cat.hack3.codingtests.marsrover.test; - -import cat.hack3.codingtests.marsrover.api.RotableRiderRover; -import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; -import cat.hack3.codingtests.marsrover.api.cartography.Direction; -import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap; -import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; -import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory; - -import java.util.List; -import java.util.ServiceLoader; -import java.util.stream.IntStream; - -import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH; -import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*; - -public class CommonTests { - - public static final Direction STARTING_DIRECTION = SOUTH; - - protected RotableRiderRover rover; - protected RoverCommand moveForwardCommand; - protected RoverCommand moveBackwardsCommand; - protected RoverCommand turnLeftCommand; - protected RoverCommand turnRightCommand; - - protected void setup() { - int mapWidth = 10; - int mapHeight = 10; - int latitudeStartingPoint = 2; - int longitudeStartingPoint = 3; - var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); - - var marsMap = getImplProviderOf(PlanetMap.Provider.class) - .provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles()); - - rover = getImplProviderOf(RotableRiderRover.Provider.class) - .provideWith(marsMap, CommonTests.STARTING_DIRECTION); - - var commandFactory = getImplProviderOf(RoverCommandFactory.Provider.class) - .provideWith(rover); - - moveForwardCommand = commandFactory.create(MOVE_FORWARD); - moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS); - turnLeftCommand = commandFactory.create(TURN_LEFT); - turnRightCommand = commandFactory.create(TURN_RIGHT); - } - - private T getImplProviderOf(Class interfaceProvider) { - return ServiceLoader - .load(interfaceProvider) - .findFirst() - .orElseThrow(); - } - - protected List getObstacles() { - return List.of(); - } - - protected void repeatAction(int times, Runnable actionToRepeat) { - IntStream.rangeClosed(1, times) - .forEach(i -> actionToRepeat.run()); - } -} diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java index a909bcf..1a40539 100644 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java @@ -2,17 +2,18 @@ package cat.hack3.codingtests.marsrover.test; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Direction; +import cat.hack3.codingtests.marsrover.test.context.GivenRotableAndMoveCommandsOnDeployedRoverWithMap; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static cat.hack3.codingtests.marsrover.api.cartography.Direction.*; import static org.testng.Assert.assertEquals; -public class MarsRoverTest extends CommonTests { +public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWithMap { @BeforeMethod public void setUp() { - setup(); + super.setUp(); } @Test @@ -109,7 +110,7 @@ public class MarsRoverTest extends CommonTests { } @Test - public void crazyTour() { + public void acceptanceTest() { moveForwardCommand.execute(); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3)); assertEquals(rover.getCurrentDirection(), SOUTH); diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java index 76be79d..0333217 100644 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java @@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.test; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Direction; +import cat.hack3.codingtests.marsrover.test.context.GivenRotableAndMoveCommandsOnDeployedRoverWithMap; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -10,11 +11,19 @@ import java.util.List; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotEquals; -public class MarsRoverWithObstaclesTest extends CommonTests { +public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDeployedRoverWithMap { + + @Override + protected List getObstacles() { + return List.of( + Coordinates.of(3, 3), + Coordinates.of(5, 5) + ); + } @BeforeMethod public void setUp() { - super.setup(); + super.setUp(); } @Test @@ -44,12 +53,4 @@ public class MarsRoverWithObstaclesTest extends CommonTests { assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5)); } - - @Override - protected List getObstacles() { - return List.of( - Coordinates.of(3, 3), - Coordinates.of(5, 5) - ); - } } diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsMapSquaredWithInitialCoordinates.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsMapSquaredWithInitialCoordinates.java new file mode 100644 index 0000000..e33bc5a --- /dev/null +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsMapSquaredWithInitialCoordinates.java @@ -0,0 +1,29 @@ +package cat.hack3.codingtests.marsrover.test.context; + +import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; +import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap; + +import java.util.List; + +public abstract class GivenMarsMapSquaredWithInitialCoordinates implements TestingCommons { + + public static final Class PLANET_MAP_PROVIDER = PlanetMap.Provider.class; + public static final List NONE_OBSTACLE = List.of(); + + protected PlanetMap marsMap; + + public void setUp() { + int mapWidth = 10; + int mapHeight = 10; + int latitudeStartingPoint = 2; + int longitudeStartingPoint = 3; + var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); + + marsMap = getImplProviderOf(PLANET_MAP_PROVIDER) + .provideWith(mapHeight, mapWidth, startingCoordinates, getObstacles()); + } + + protected List getObstacles() { + return NONE_OBSTACLE; + } +} diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsRoverDeployedOnMarsMap.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsRoverDeployedOnMarsMap.java new file mode 100644 index 0000000..70c3a05 --- /dev/null +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenMarsRoverDeployedOnMarsMap.java @@ -0,0 +1,18 @@ +package cat.hack3.codingtests.marsrover.test.context; + +import cat.hack3.codingtests.marsrover.api.RotableRiderRover; +import cat.hack3.codingtests.marsrover.api.cartography.Direction; + +import static cat.hack3.codingtests.marsrover.api.cartography.Direction.SOUTH; + +public abstract class GivenMarsRoverDeployedOnMarsMap extends GivenMarsMapSquaredWithInitialCoordinates{ + static final Direction STARTING_DIRECTION = SOUTH; + + protected RotableRiderRover rover; + + public void setUp() { + super.setUp(); + rover = getImplProviderOf(RotableRiderRover.Provider.class) + .provideWith(marsMap, STARTING_DIRECTION); + } +} diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenRotableAndMoveCommandsOnDeployedRoverWithMap.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenRotableAndMoveCommandsOnDeployedRoverWithMap.java new file mode 100644 index 0000000..66ce38a --- /dev/null +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/GivenRotableAndMoveCommandsOnDeployedRoverWithMap.java @@ -0,0 +1,26 @@ +package cat.hack3.codingtests.marsrover.test.context; + +import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; +import cat.hack3.codingtests.marsrover.api.commands.RoverCommandFactory; + +import static cat.hack3.codingtests.marsrover.api.commands.RoverCommand.Type.*; + +public class GivenRotableAndMoveCommandsOnDeployedRoverWithMap extends GivenMarsRoverDeployedOnMarsMap { + + public static final Class COMMAND_FACTORY_PROVIDER = RoverCommandFactory.Provider.class; + protected RoverCommand moveForwardCommand; + protected RoverCommand moveBackwardsCommand; + protected RoverCommand turnLeftCommand; + protected RoverCommand turnRightCommand; + + public void setUp() { + super.setUp(); + var commandFactory = getImplProviderOf(COMMAND_FACTORY_PROVIDER) + .provideWith(rover); + + moveForwardCommand = commandFactory.create(MOVE_FORWARD); + moveBackwardsCommand = commandFactory.create(MOVE_BACKWARDS); + turnLeftCommand = commandFactory.create(TURN_LEFT); + turnRightCommand = commandFactory.create(TURN_RIGHT); + } +} diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java new file mode 100644 index 0000000..cac3913 --- /dev/null +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java @@ -0,0 +1,19 @@ +package cat.hack3.codingtests.marsrover.test.context; + +import java.util.ServiceLoader; +import java.util.stream.IntStream; + +public interface TestingCommons { + + default T getImplProviderOf(Class interfaceProvider) { + return ServiceLoader + .load(interfaceProvider) + .findFirst() + .orElseThrow(); + } + + default void repeatAction(int times, Runnable actionToRepeat) { + IntStream.rangeClosed(1, times) + .forEach(i -> actionToRepeat.run()); + } +}