Exemplo n.º 1
0
 public IActionResult Post(RoverModel model)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (model.RoverId <= 0)
     {
         return(BadRequest(Messages.InvalidId));
     }
     try
     {
         var rover = RoverFactory.Create(model.RoverId, model.RoverName, 0, 0, Direction.N);
         return(Ok(new { RoverId = rover.Id, RoverName = rover.Name }));
     }
     catch (System.Exception ex)
     {
         if (ex.Message == "EXISTS")
         {
             return(Conflict(new ApiResponse
             {
                 Status = 409,
                 Message = Messages.RoverAlreadyExist
             }));
         }
         else
         {
             return(StatusCode(500));
         }
     }
 }
        public void CreateRoverShouldReturnExpectedType(RoverFactory sut)
        {
            var actual = sut.CreateRover();

            actual.Should().BeOfType <Rover>();
            actual.Should().BeAssignableTo <IRover>();
        }
        public void ParseInputTest()
        {
            string input          = $"5 5{Environment.NewLine}1 2 N{Environment.NewLine}LMLMLMLMM{Environment.NewLine}3 3 E{Environment.NewLine}MMRMMRMRRM{Environment.NewLine}";
            string output         = string.Empty;
            string expectedOutput = $"1 3 N{Environment.NewLine}5 1 E{Environment.NewLine}";

            //poor man's dependency injection
            IInstructionParser parser  = new InstructionParser();
            IRoverFactory      factory = new RoverFactory();
            IRoverService      service = new RoverService(factory, parser);

            var response = service.ParseInput(new Domain.Parser.Messages.ExecuteRequest()
            {
                Input = input
            });

            //if no exceptions encountered
            if (response.Exception == null)
            {
                //get the output
                output = response.Output;
            }
            else
            {
                output = response.Exception.Message;
            }

            Console.WriteLine(output);
            Assert.AreEqual(expectedOutput, output);
        }
Exemplo n.º 4
0
        public void AddRoverToList(RoverDto roverDto)
        {
            var plateu = PlateuFactory.CreatePlateu(roverDto.Plateu.CoordinateX, roverDto.Plateu.CoordinateY);
            var rover  = RoverFactory.CreateRover(roverDto.Id, roverDto.CoordinateX, roverDto.CoordinateY, roverDto.Direction, roverDto.Command, plateu);

            rovers.Add(rover);
        }
 public IActionResult Post(RoverModel model)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (model.RoverId <= 0)
     {
         return(BadRequest("Id should be a positive integer value."));
     }
     try
     {
         var rover = RoverFactory.Create(model.RoverId, model.RoverName, 0, 0, Direction.N);
         return(Ok(new { RoverId = rover.Id, RoverName = rover.Name }));
     }
     catch (System.Exception ex)
     {
         if (ex.Message == "EXISTS")
         {
             return(Conflict(new ApiResponse
             {
                 Status = 409,
                 Message = "Rover already exists, Cannot be  created"
             }));
         }
         else
         {
             return(StatusCode(500));
         }
     }
 }
Exemplo n.º 6
0
        public void CreateRover_ReturnsIRover_WhenInputInCorrectFormat(string coordinates)
        {
            IRover rover = RoverFactory.CreateRover(coordinates, surface.Object);

            Assert.NotNull(rover);
            Assert.True(rover.X > 0 && rover.Y > 0);
            Assert.True(directions.Contains(rover.Direction));
        }
Exemplo n.º 7
0
        private static void Main()
        {
            var roverFactory   = new RoverFactory();
            var userIo         = new ConsoleUserIO();
            var roverManager   = new RoverManager(roverFactory);
            var missionControl = new MissionControl(roverManager, userIo);

            missionControl.StartMission();
        }
        public IActionResult Put(int roverId, RoverModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var rover = RoverFactory.Update(roverId, model.RoverName);

            return(Ok(new { RoverId = rover.Id, RoverName = rover.Name }));
        }
        public async Task StartRoverOperation()
        {
            var commandsToExecute = await roverCommandRetriever.GetAll();

            EnsureNotEmpty(commandsToExecute);

            // Assumption: we are going to operate rovers sequentially
            // i.e. the first rover will execute all its commands, then the next and so on...
            foreach (var roverCommand in commandsToExecute)
            {
                if (roverCommand.ManouvreCommands.Any())
                {
                    var rover = RoverFactory.CreateWith(roverCommand);

                    foreach (var manouvreCommand in roverCommand.ManouvreCommands)
                    {
                        // This is not necessarily the prettiest because if NASA asks me to extend
                        // the capability of the Rover, then I will have to change rover, use case,
                        // parser and add an extra RoverCommands member. This surface area could be
                        // reduced by moving the responsbility of instantiating the rover and
                        // mapping input to rover functions via delegates, into the parser itself
                        // since each parser will need to determine that for itself anyway. The use
                        // case will then simply loop over all the commands and execute them one by
                        // one without having to know which is which (i.e. no switch...case).
                        // Ofcourse, this only works as long as the commands have a homogenous
                        // signature and return types. For now though, this will do since we are not
                        // extending rover behaviour!
                        CurrentRoverPosition currentRoverPosition = default;

                        switch (manouvreCommand)
                        {
                        case RoverCommands.TurnLeft:
                            currentRoverPosition = rover.TurnLeft();
                            break;

                        case RoverCommands.TurnRight:
                            currentRoverPosition = rover.TurnRight();
                            break;

                        case RoverCommands.Move:
                            currentRoverPosition = rover.Move();
                            break;

                        default:
                            throw new InvalidOperationException(
                                      $"Rover command {manouvreCommand} is not supported!");
                        }

                        await roverPositionTransmitter.Transmit(
                            currentRoverPosition);
                    }
                }
            }
        }
Exemplo n.º 10
0
        public void Test_Move_Rover(int plateuXCoordinate, int plateuYCoordinate, int roverId, int coordinateX, int coordinateY, char direction, string command, int expectedX, int expectedY, Type expectedDirection, bool isOutOfThePlateu)
        {
            var plateu = PlateuFactory.CreatePlateu(plateuXCoordinate, plateuYCoordinate);
            var rover  = RoverFactory.CreateRover(roverId, coordinateX, coordinateY, direction, command, plateu);

            rover.Move();

            Assert.Equal(expectedX, rover.CoordinateX);
            Assert.Equal(expectedY, rover.CoordinateY);
            Assert.Equal(expectedDirection, rover.Direction.GetType());
            Assert.Equal(isOutOfThePlateu, rover.IsOutOfThePlateu);
        }
Exemplo n.º 11
0
        public void Move_ReturnCorrectMovement_WhenMovementIsInSurface()
        {
            IRover rover = RoverFactory.CreateRover("1 2 N", surface.Object);

            rover.Move("M");
            rover.Move("R");
            rover.Move("M");

            Assert.Equal(2, rover.X);
            Assert.Equal(3, rover.Y);
            Assert.Equal("E", rover.Direction);
        }
Exemplo n.º 12
0
        public void Test_Run_Should_Move_To_51E_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(3, 3, 'E', "Rover", new Plateau(5, 5)), "MMRMMRMRRM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("5 1 E", result[0]);
        }
Exemplo n.º 13
0
        public void Test_Run_Should_Move_To_13N_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(1, 2, 'N', "Rover", new Plateau(5, 5)), "LMLMLMLMM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("1 3 N", result[0]);
        }
Exemplo n.º 14
0
        public void Test_Run_Should_Move_All_Rovers_Sequentially_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(1, 2, 'N', "Rover-1", new Plateau(5, 5)), "LMLMLMLMM");
            roverRunner.Add(RoverFactory.Create(3, 3, 'E', "Rover-2", new Plateau(5, 5)), "MMRMMRMRRM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("1 3 N", result[0]);
            Assert.Equal("5 1 E", result[1]);
        }
Exemplo n.º 15
0
        public IActionResult GetPosition(int roverId)
        {
            var rover = RoverFactory.GetRover(roverId);

            if (rover == null)
            {
                return(NotFound());
            }
            return(Ok(new RoverMoveResponse
            {
                RoverId = rover.Id,
                RoverName = rover.Name,
                CurrentX = rover.Position.X,
                CurrentY = rover.Position.Y,
                CurrentDirection = rover.Position.Direction.ToString()
            }));
        }
Exemplo n.º 16
0
        public IActionResult Move(int roverId, RoverMoveModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var rover = RoverFactory.GetRover(roverId);

            if (rover == null)
            {
                throw new RoverException("Rover doesn't exist.");
            }
            var movements = new List <Movement>();

            foreach (var inst in model.MovementInstruction.ToUpper())
            {
                switch (inst)
                {
                case 'L':
                    movements.Add(Movement.Left);
                    break;

                case 'R':
                    movements.Add(Movement.Right);
                    break;

                case 'M':
                    movements.Add(Movement.Move);
                    break;
                }
            }
            rover.Move(movements);
            return(Ok(new RoverMoveResponse
            {
                RoverId = rover.Id,
                RoverName = rover.Name,
                CurrentX = rover.Position.X,
                CurrentY = rover.Position.Y,
                CurrentDirection = rover.Position.Direction.ToString()
            }));
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            //Test Input:
            //5 5
            //1 2 N
            //LMLMLMLMM
            //3 3 E
            //MMRMMRMRRM
            //Expected Output:
            //1 3 N
            //5 1 E

            string   surf    = Console.ReadLine(); //5 5
            ISurface surface = SurfaceFactory.CreateSurface(surf);

            string rov   = Console.ReadLine(); //1 2 N
            IRover rover = RoverFactory.CreateRover(rov, surface);

            string        moves     = Console.ReadLine(); //LMLMLMLMM
            List <string> movements = MovementFactory.CreateMovement(moves);

            foreach (var movement in movements)
            {
                rover.Move(movement);
            }

            string rov2   = Console.ReadLine(); //3 3 E
            IRover rover2 = RoverFactory.CreateRover(rov2, surface);

            string        moves2     = Console.ReadLine(); //MMRMMRMRRM
            List <string> movements2 = MovementFactory.CreateMovement(moves2);

            foreach (var movement2 in movements2)
            {
                rover2.Move(movement2);
            }

            Console.WriteLine($"{rover.X} {rover.Y} {rover.Direction}");
            Console.WriteLine($"{rover2.X} {rover2.Y} {rover2.Direction}");
            Console.ReadLine();
        }
Exemplo n.º 18
0
 public void CreateRover_ThrowsLengthException_WhenInputStringNotHasOnlyTwoSpace(string coordinates)
 {
     Assert.Throws <ArgumentException>(() => RoverFactory.CreateRover(coordinates, surface.Object));
 }
Exemplo n.º 19
0
 public void CreateRover_ThrowsIntegerTypeException_WhenFirstTwoCharacterHasChar(string coordinates)
 {
     Assert.Throws <ArgumentException>(() => RoverFactory.CreateRover(coordinates, surface.Object));
 }
Exemplo n.º 20
0
 public void CreateRover_ThrowsDirectionException_WhenThirdCharIsNotaDirection(string coordinates)
 {
     Assert.Throws <ArgumentException>(() => RoverFactory.CreateRover(coordinates, surface.Object));
 }
Exemplo n.º 21
0
        public void Move_ThrowsMovementCodeInvalidException_WhenMovementCodeIsInvalid(string code)
        {
            IRover rover = RoverFactory.CreateRover("1 2 N", surface.Object);

            Assert.Throws <ArgumentException>(() => rover.Move(code));
        }
Exemplo n.º 22
0
        public void Move_ThrowsRoverIsOurOfRangeException_WhenYCoordinateIsBiggerThanSufaceY()
        {
            IRover rover = RoverFactory.CreateRover("2 5 N", surface.Object);

            Assert.Throws <ArgumentException>(() => rover.Move("M"));
        }
Exemplo n.º 23
0
 public string LandRover(char direction, int x, int y)
 {
     ActiveRover = RoverFactory.Create(direction, x, y);
     return(ActiveRover.Position);
 }