public void CalculateRoversPosition_LowerCaseValues_Success()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 5,
                PlateauY = 5,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 2,
                        InitialDirection = "s",
                        Commands         = "mmlmmrrm"
                    },
                }
            };

            // Act
            var result = marsRoverManager.CalculateRoversPosition(model);

            // Assert
            Assert.AreEqual(1, result.RoverPositions.Count);

            Assert.IsTrue(result.RoverPositions[0].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[0].StatusMessage));
            Assert.AreEqual(3, result.RoverPositions[0].PositionX);
            Assert.AreEqual(0, result.RoverPositions[0].PositionY);
            Assert.AreEqual("W", result.RoverPositions[0].Direction);
        }
        public void Validate_AllIncorrectValuesModel_Fail()
        {
            // Arrange
            var validator = new MarsRoversValidator();
            var model     = new MarsRoversModel()
            {
                PlateauX = -2,
                PlateauY = -5,
                Rovers   = new List <RoverProgramModel>()
            };

            // Act
            var validationResult = validator.ValidateModelIfNotNull(model);

            // Assert
            Assert.IsFalse(validationResult.IsValid);
            Assert.AreEqual(2, validationResult.Errors.Count);

            Assert.AreEqual("1", validationResult.Errors[0].ErrorCode);
            Assert.AreEqual("Field value has to be greater than or equal to 0", validationResult.Errors[0].ErrorMessage);
            Assert.AreEqual("PlateauY", validationResult.Errors[0].PropertyName);

            Assert.AreEqual("1", validationResult.Errors[1].ErrorCode);
            Assert.AreEqual("Field value has to be greater than or equal to 0", validationResult.Errors[1].ErrorMessage);
            Assert.AreEqual("PlateauX", validationResult.Errors[1].PropertyName);
        }
        public void CalculateRoversPosition_IncorrectCommand_ExceptionThrown()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 5,
                PlateauY = 5,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 4,
                        InitialDirection = "W",
                        Commands         = "KRF"
                    },
                }
            };

            // Act => Assert
            var ex = Assert.ThrowsException <MarsRoversValidationException>(() => marsRoverManager.CalculateRoversPosition(model));

            Assert.AreEqual("Invalid rover command K", ex.Message);
            Assert.AreEqual(MarsRoversExceptionCodes.Validation, ex.Code);
            Assert.AreEqual("CommandParser", ex.Target);
        }
        public void CalculateRoversPosition_CollisionLastAndCollisionFirstAndValid_OneSuccess()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 5,
                PlateauY = 5,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 2,
                        InitialDirection = "N",
                        Commands         = "MMLMRM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 0,
                        InitialY         = 3,
                        InitialDirection = "S",
                        Commands         = "LMMLMRM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 4,
                        InitialDirection = "E",
                        Commands         = "MMMLMLM"
                    },
                }
            };

            // Act
            var result = marsRoverManager.CalculateRoversPosition(model);

            // Assert
            Assert.AreEqual(3, result.RoverPositions.Count);

            Assert.IsFalse(result.RoverPositions[0].Success);
            Assert.AreEqual("Rover can't move further because the position 2, 4 occupied by other rover!", result.RoverPositions[0].StatusMessage);
            Assert.AreEqual(2, result.RoverPositions[0].PositionX);
            Assert.AreEqual(3, result.RoverPositions[0].PositionY);
            Assert.AreEqual("N", result.RoverPositions[0].Direction);

            Assert.IsFalse(result.RoverPositions[1].Success);
            Assert.AreEqual("Rover can't move further because the position 2, 3 occupied by other rover!", result.RoverPositions[1].StatusMessage);
            Assert.AreEqual(1, result.RoverPositions[1].PositionX);
            Assert.AreEqual(3, result.RoverPositions[1].PositionY);
            Assert.AreEqual("E", result.RoverPositions[1].Direction);

            Assert.IsTrue(result.RoverPositions[2].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[2].StatusMessage));
            Assert.AreEqual(4, result.RoverPositions[2].PositionX);
            Assert.AreEqual(5, result.RoverPositions[2].PositionY);
            Assert.AreEqual("W", result.RoverPositions[2].Direction);
        }
Example #5
0
        public IActionResult CalculateRoversPosition([FromBody] MarsRoversModel model)
        {
            var validationResult = _marsRoversValidator.ValidateModelIfNotNull(model);

            if (!validationResult.IsValid)
            {
                return(ValidationErrorResult.Create(validationResult, nameof(MarsRoversModel)));
            }

            var resource = _marsRoversManager.CalculateRoversPosition(model);

            return(Created("", resource));
        }
        public void CalculateRoversPosition_TwoValidRoversData_Success()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 5,
                PlateauY = 5,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 1,
                        InitialY         = 2,
                        InitialDirection = "N",
                        Commands         = "LMLMLMLMM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 3,
                        InitialY         = 3,
                        InitialDirection = "E",
                        Commands         = "MMRMMRMRRM"
                    },
                }
            };

            // Act
            var result = marsRoverManager.CalculateRoversPosition(model);

            // Assert
            Assert.AreEqual(2, result.RoverPositions.Count);

            Assert.IsTrue(result.RoverPositions[0].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[0].StatusMessage));
            Assert.AreEqual(1, result.RoverPositions[0].PositionX);
            Assert.AreEqual(3, result.RoverPositions[0].PositionY);
            Assert.AreEqual("N", result.RoverPositions[0].Direction);

            Assert.IsTrue(result.RoverPositions[1].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[1].StatusMessage));
            Assert.AreEqual(5, result.RoverPositions[1].PositionX);
            Assert.AreEqual(1, result.RoverPositions[1].PositionY);
            Assert.AreEqual("E", result.RoverPositions[1].Direction);
        }
        public void CalculateRoversPosition_MoveTooFarAndCollision_Unsuccessfull()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 10,
                PlateauY = 10,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 5,
                        InitialY         = 5,
                        InitialDirection = "N",
                        Commands         = "MMMMMM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 10,
                        InitialY         = 10,
                        InitialDirection = "W",
                        Commands         = "MMMMMMLMM"
                    },
                }
            };

            // Act
            var result = marsRoverManager.CalculateRoversPosition(model);

            // Assert
            Assert.AreEqual(2, result.RoverPositions.Count);

            Assert.IsFalse(result.RoverPositions[0].Success);
            Assert.AreEqual("Rover can't move further because the position 5, 11 is out of plateau!", result.RoverPositions[0].StatusMessage);
            Assert.AreEqual(5, result.RoverPositions[0].PositionX);
            Assert.AreEqual(10, result.RoverPositions[0].PositionY);
            Assert.AreEqual("N", result.RoverPositions[0].Direction);

            Assert.IsFalse(result.RoverPositions[1].Success);
            Assert.AreEqual("Rover can't move further because the position 5, 10 occupied by other rover!", result.RoverPositions[1].StatusMessage);
            Assert.AreEqual(6, result.RoverPositions[1].PositionX);
            Assert.AreEqual(10, result.RoverPositions[1].PositionY);
            Assert.AreEqual("W", result.RoverPositions[1].Direction);
        }
        public void Validate_AllCorrectValuesModel_Success()
        {
            // Arrange
            var validator = new MarsRoversValidator();
            var model     = new MarsRoversModel()
            {
                PlateauX = 3,
                PlateauY = 6,
                Rovers   = new List <RoverProgramModel>()
            };

            // Act
            var validationResult = validator.ValidateModelIfNotNull(model);

            // Assert
            Assert.IsTrue(validationResult.IsValid);
        }
Example #9
0
        public MarsRoversResource CalculateRoversPosition(MarsRoversModel model)
        {
            var result = new MarsRoversResource();
            var rovers = new Dictionary <int, Rover>();

            var directions = EnumDescriptionHelper.GetEnumDescriptions <Direction>().ToList();
            var commands   = EnumDescriptionHelper.GetEnumDescriptions <RoverCommand>().ToList();

            var index = 0;

            try
            {
                // InitializeRovers
                foreach (var roverModel in model.Rovers)
                {
                    index++;
                    rovers.Add(index, InitializeRover(index, roverModel, directions, commands));
                }
                ;

                // Validate starting positions
                foreach (var rover in rovers)
                {
                    PositionValidator.ValidatePosition(rover.Value, rover.Value.CurrentPosition, rovers, model.PlateauX, model.PlateauY, true);
                }

                // ProcessRovers
                var deployedRovers = rovers.Where(x => x.Value.Deployed).ToDictionary(x => x.Key, x => x.Value);;
                foreach (var rover in deployedRovers)
                {
                    ProcessRoverCommands(rover.Value, deployedRovers, model.PlateauX, model.PlateauY);
                }

                // Map results
                result = MapRoversToResource(rovers);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw;
            }

            return(result);
        }
        public void CalculateRoversPosition_MissPlateuAndSamePlaceAndTwoValidRovers_TwoSuccesseses()
        {
            // Arrange
            var marsRoverManager = new MarsRoversManager(_logger.Object);

            MarsRoversModel model = new MarsRoversModel()
            {
                PlateauX = 3,
                PlateauY = 3,
                Rovers   = new List <RoverProgramModel>()
                {
                    new RoverProgramModel()
                    {
                        InitialX         = 4,
                        InitialY         = 2,
                        InitialDirection = "N",
                        Commands         = "LMLMLMLMM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 2,
                        InitialDirection = "S",
                        Commands         = ""
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 2,
                        InitialY         = 2,
                        InitialDirection = "E",
                        Commands         = "MMRMMRMRRM"
                    },
                    new RoverProgramModel()
                    {
                        InitialX         = 1,
                        InitialY         = 1,
                        InitialDirection = "W",
                        Commands         = "MRMMRMMM"
                    },
                }
            };

            // Act
            var result = marsRoverManager.CalculateRoversPosition(model);

            // Assert
            Assert.AreEqual(4, result.RoverPositions.Count);

            Assert.IsFalse(result.RoverPositions[0].Success);
            Assert.AreEqual("Rover can't be deployed because the position 4, 2 is out of plateau!", result.RoverPositions[0].StatusMessage);
            Assert.AreEqual(4, result.RoverPositions[0].PositionX);
            Assert.AreEqual(2, result.RoverPositions[0].PositionY);
            Assert.AreEqual("N", result.RoverPositions[0].Direction);

            Assert.IsTrue(result.RoverPositions[1].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[1].StatusMessage));
            Assert.AreEqual(2, result.RoverPositions[1].PositionX);
            Assert.AreEqual(2, result.RoverPositions[1].PositionY);
            Assert.AreEqual("S", result.RoverPositions[1].Direction);

            Assert.IsFalse(result.RoverPositions[2].Success);
            Assert.AreEqual("Rover can't be deployed because the position 2, 2 occupied by other rover!", result.RoverPositions[2].StatusMessage);
            Assert.AreEqual(2, result.RoverPositions[2].PositionX);
            Assert.AreEqual(2, result.RoverPositions[2].PositionY);
            Assert.AreEqual("E", result.RoverPositions[2].Direction);

            Assert.IsTrue(result.RoverPositions[3].Success);
            Assert.IsTrue(string.IsNullOrEmpty(result.RoverPositions[3].StatusMessage));
            Assert.AreEqual(3, result.RoverPositions[3].PositionX);
            Assert.AreEqual(3, result.RoverPositions[3].PositionY);
            Assert.AreEqual("E", result.RoverPositions[3].Direction);
        }