Exemplo n.º 1
0
        public void BadPostInputReturnsBadRequest()
        {
            var inputs = new MapActionInputs();

            var navMock        = new Mock <INavigator>(MockBehavior.Strict);
            var translatorMock = new Mock <IMapTranslator>(MockBehavior.Strict);

            var thrownException = new Exception(
                message: "TestMessage");

            navMock.Setup(m => m.Simulate(inputs.Instructions)).
            Throws(thrownException);

            var controller = new MapController(
                navigator: navMock.Object,
                translator: translatorMock.Object);

            var controllerResult = controller.Post(inputs);

            // Make sure controller returns a bad request.
            var badRequestResult = Assert.IsType <BadRequestObjectResult>(controllerResult);

            // Make sure the exception message got attached to the result.
            Assert.Equal(thrownException.Message, badRequestResult.Value);
        }
Exemplo n.º 2
0
        public IActionResult Post([FromBody] MapActionInputs inputs)
        {
            // There are cleaner ways to handle exceptions than this, but
            // for one endpoint, this is alright. The parser will
            // throw exceptions for malformed inputs.

            MapResult simulationResult;

            // Simple catch-all error handling for navigation errors.
            // This will pick up badly formatted inputs and should
            // give back readable error messages.
            try
            {
                simulationResult = Map.Simulate(inputs.Instructions);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Convert the raw navigation output to an api-specific model.
            var outputTranslation = Translator.Translate(simulationResult);

            return(Ok(outputTranslation));
        }
Exemplo n.º 3
0
        public void TestPostWithDefaultTests(string instructions, int expectedBlocksToTravel)
        {
            var inputs = new MapActionInputs
            {
                Instructions = instructions
            };

            INavigator navigator = new Navigator(
                parser: new RelativeInstructionParser(
                    singleInstructionParser: new SingleNamedInstructionParser()),
                simulator: new InstructionSimulator());

            IMapTranslator translator = new MapTranslator();

            var controller = new MapController(
                navigator: navigator,
                translator: translator);

            var result = controller.Post(
                inputs: inputs);

            // Make sure result is an okay response.
            var okResult = Assert.IsType <OkObjectResult>(result);

            // Check type of output and capture its value.
            var objOutput = Assert.IsType <MapResultModel>(okResult.Value);

            Assert.Equal(expectedBlocksToTravel, objOutput.BlocksToTravel);
        }
Exemplo n.º 4
0
        public void SuccessfulPostReturnsOkay()
        {
            var inputs = new MapActionInputs();

            var navMock = new Mock <INavigator>(MockBehavior.Strict);

            var navMockResult = new MapResult(
                startPosition: new GridCoordinate(),
                endPosition: new GridCoordinate());

            navMock.Setup(m => m.Simulate(inputs.Instructions)).
            Returns(navMockResult);

            var translatorMock = new Mock <IMapTranslator>(MockBehavior.Strict);

            var translatorResult = new MapResultModel();

            translatorMock.Setup(t => t.Translate(navMockResult)).
            Returns(translatorResult);

            var controller = new MapController(
                navigator: navMock.Object,
                translator: translatorMock.Object);

            var postResult = controller.Post(
                inputs: inputs);

            // Make sure api returns an "okay."
            var okResult = Assert.IsType <OkObjectResult>(postResult);

            // Make sure navigator got called exactly once with correct input.
            navMock.Verify(n => n.Simulate(inputs.Instructions), Times.Once);

            // Make sure translator got called exactly once with correct input.
            translatorMock.Verify(t => t.Translate(navMockResult), Times.Once);

            // Make sure the translator result got attached to the final output..
            Assert.Equal(translatorResult, okResult.Value);
        }