Example #1
0
 public static void AssertMovementIsValid(MovementResource movement)
 {
     Assert.That(movement, Is.Not.Null, $"{nameof(movement)}");
     Assert.That(movement.Name, Is.Not.Null, $"{nameof(movement.Name)}");
     Assert.That(movement.Owner, Is.Not.Null, $"{nameof(movement.Owner)}");
     Assert.That(movement.Value, Is.Not.Null, $"{nameof(movement.Value)}");
     Assert.That(movement.InstanceId, Is.Not.Null, $"{nameof(movement.InstanceId)}");
     Assert.That(movement.Links.Any(l => l.Rel.Equals(CharacterLinkName)), $"Unable to find '{CharacterLinkName}' link.");
 }
        public async Task <IActionResult> Post(MovementResource movement)
        {
            if (!movement.isResourceValid())
            {
                return(BadRequest());
            }

            return(Ok(await _movementHandler.HandleAsync(movement)));
        }
        public async void Post_ShouldReturn_BadRequest_For_NoCommands()
        {
            var query = new MovementResource {
                Commands = new List <CommandResource>(), Start = new PositionResource()
            };

            var result = await controller.Post(query);

            Assert.IsType <BadRequestResult>(result);
        }
        public async void Post_ShouldReturn_BadRequest_For_Empty_CommandsData()
        {
            var query = new MovementResource {
                Start = new PositionResource()
            };

            var result = await controller.Post(query);

            Assert.IsType <BadRequestResult>(result);
        }
Example #5
0
        public void GetCleanPoints_ShouldReturn_CorrectData(MovementResource resource, int expectedResult)
        {
            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MappingConfiguration());
            });
            var mapper = mockMapper.CreateMapper();
            var helper = new RobotMovementHelper(mapper);

            var result = helper.GetCleanPoints(resource);

            Assert.Equal(expectedResult, result);
        }
Example #6
0
        public int GetCleanPoints(MovementResource movement)
        {
            var uniquePoints = new List <PositionResource>();
            var commands     = mapper.Map <MovementResource, IEnumerable <ICommand> >(movement);
            var lastPosition = new PositionResource();

            foreach (var _command in commands)
            {
                var newPosition = _command.GetNewPoint(lastPosition.X, lastPosition.Y);

                lastPosition = newPosition ?? lastPosition;

                AddPoint(uniquePoints, newPosition);
            }
            return(uniquePoints.Count);
        }
        public async Task <int> HandleAsync(MovementResource movement)
        {
            var entity = mapper.Map <MovementResource, Movement>(movement);

            entity.Result = helper.GetCleanPoints(movement);

            var difference         = DateTime.Now.Ticks - entity.Timestamp.Ticks;
            var differenceTimeSpan = new TimeSpan(difference);

            entity.Duration = Convert.ToDecimal(differenceTimeSpan.TotalSeconds);

            unitOfWork.MovementRepository.Add(entity);
            await unitOfWork.SaveAsync();

            return(entity.Result);
        }
        public async void Post_ShouldReturn_BadRequest_For_Invalid_Steps(int steps)
        {
            var commands = new List <CommandResource>
            {
                new CommandResource()
                {
                    Direction = "east", Steps = steps
                }
            };
            var query = new MovementResource {
                Commands = commands, Start = new PositionResource()
            };

            var result = await controller.Post(query);

            Assert.IsType <BadRequestResult>(result);
        }
        public async void Post_ShoulReturn_OkResponse_For_CorrectData()
        {
            var commands = new List <CommandResource>
            {
                new CommandResource()
                {
                    Direction = "east", Steps = 1
                }
            };
            var query = new MovementResource {
                Commands = commands, Start = new PositionResource()
            };

            var result = await controller.Post(query);

            Assert.IsType <OkObjectResult>(result);
        }
        public IEnumerable <ICommand> Convert(MovementResource source, IEnumerable <ICommand> destination, ResolutionContext context)
        {
            var result = new List <ICommand>();

            foreach (var s in source.Commands)
            {
                for (int i = 0; i < s.Steps; i++)
                {
                    switch (s.Direction)
                    {
                    case "east":
                        result.Add(new EastCommand
                        {
                            Limit = source.Start.X
                        });
                        break;

                    case "west":
                        result.Add(new WestCommand
                        {
                            Limit = source.Start.X
                        });
                        break;

                    case "north":
                        result.Add(new NorthCommand
                        {
                            Limit = source.Start.Y
                        });
                        break;

                    case "south":
                        result.Add(new SouthCommand
                        {
                            Limit = source.Start.Y
                        });
                        break;
                    }
                }
            }

            return(result);
        }
        public async void HandleAsync_ShouldCall_SaveChanges()
        {
            var resource = new MovementResource
            {
                Start = new PositionResource {
                    X = 10, Y = 20
                },
                Commands = new List <CommandResource>
                {
                    new CommandResource {
                        Direction = "east", Steps = 2
                    }
                }
            };

            await handler.HandleAsync(resource);

            unitOfWorkMock.Verify(r => r.SaveAsync(), Times.Once);
        }
        public async void HandleAsync_ShouldCall_Repository()
        {
            var resource = new MovementResource
            {
                Start = new PositionResource {
                    X = 10, Y = 20
                },
                Commands = new List <CommandResource>
                {
                    new CommandResource {
                        Direction = "east", Steps = 2
                    }
                }
            };

            await handler.HandleAsync(resource);

            repositoryMock.Verify(r => r.Add(It.IsAny <Movement>()), Times.Once);
        }
        public async void HandleAsync_ShouldCall_RobotMovementHelper()
        {
            var resource = new MovementResource
            {
                Start = new PositionResource {
                    X = 10, Y = 20
                },
                Commands = new List <CommandResource>
                {
                    new CommandResource {
                        Direction = "east", Steps = 2
                    }
                }
            };

            await handler.HandleAsync(resource);

            helperMock.Verify(h => h.GetCleanPoints(resource), Times.Once);
        }
        public async void Post_ShouldCall_RobotMovementHandler()
        {
            var handler = new Mock <IRobotMovementHandler>();

            handler.Setup(f => f.HandleAsync(It.IsAny <MovementResource>())).ReturnsAsync(It.IsAny <int>());
            var commands = new List <CommandResource>
            {
                new CommandResource()
                {
                    Direction = "east", Steps = 1
                }
            };
            var query = new MovementResource {
                Commands = commands, Start = new PositionResource()
            };
            var controller = new RobotController(handler.Object);

            await controller.Post(query);

            handler.Verify(f => f.HandleAsync(query), Times.Once);
        }