Ejemplo n.º 1
0
        public RoverResponse Post([FromBody] RoverInstructions value)
        {
            // REGular EXpression for checking instructions are limited to R, M, and L characters.
            Regex instructionsRegex = new Regex(@"^[RML]+$");

            // Check JSON values are within constraints
            if (value.RoverId.Length > 10 || value.MovementInstruction.Length > 100)
            {
                return(new RoverResponse {
                    Message = "Rover ID and/or Movement Instructions are over the allowed limit. Rover ID should be less than 10 characters. Instructions should be less than 100 characters.", CurrentPosition = "()"
                });
            }
            else if (!instructionsRegex.IsMatch(value.MovementInstruction))
            {
                return(new RoverResponse {
                    Message = "Movement Instructions are incorrect. Instructions can only have values of 'R', 'L', and/or 'M'.", CurrentPosition = "()"
                });
            }

            // Retreive rover from Session Storage. If rover does not exist, then create a new one.
            Rover rover = SessionRoverExtension.GetRover <Rover>(HttpContext.Session, value.RoverId) == null ? new Rover(value.RoverId, "N", 0, 0) : SessionRoverExtension.GetRover <Rover>(HttpContext.Session, value.RoverId);

            // Execute all instructions sent for rover.
            rover = RoverOperationsService.ExecuteInstructions(value.MovementInstruction, rover);

            // Save rover to Session Storage, and return result.
            SessionRoverExtension.SetRover(HttpContext.Session, value.RoverId, rover);
            return(new RoverResponse {
                Message = $"Rover {rover.RoverId} has moved. Orientation is: {rover.RoverOrientation}.", CurrentPosition = $"({rover.RoverX},{rover.RoverY})"
            });
        }
Ejemplo n.º 2
0
        public void ConvertValueToInstruction_ThrowsError_WhenInvalidValueGiven()
        {
            Exception testException = null;

            try
            {
                var throwawayValue = RoverInstructions.ConvertValueToInstruction('S');
            }
            catch (Exception testE)
            {
                testException = testE;
            }

            Assert.NotNull(testException);
        }
Ejemplo n.º 3
0
        public ActionResult Post([FromBody] RoverInstructions roverInstructions)
        {
            if (roverInstructions.Instructions == null || roverInstructions.Instructions == string.Empty)
            {
                return(BadRequest("Error: Instructions are empty"));
            }

            var stringBuilder = new StringBuilder();

            _navigationService.AssignPlateau(roverInstructions.PlateauHeight, roverInstructions.PlateauWidth);
            stringBuilder.AppendLine(string.Format("for Plateau ( {0} , {1} )", roverInstructions.PlateauHeight,
                                                   roverInstructions.PlateauWidth));
            stringBuilder.AppendLine("-->" + _navigationService.ReceiveInstructions(roverInstructions.Instructions));
            return(Ok((stringBuilder.ToString())));
        }
Ejemplo n.º 4
0
        public void ConvertValueToInstruction_ConvertsToInstruction_WhenValidValueGiven(char valueToConvert, RoverInstruction expectedInstruction)
        {
            var conversionResult = RoverInstructions.ConvertValueToInstruction(valueToConvert);

            Assert.True(conversionResult == expectedInstruction);
        }