public void BotLogic_CreateBot_Should_Not_Create_A_Bot_In_Rapid_Succession() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var botRepository = new Mock <IRepository <Bot> >(); var scriptRepository = new Mock <IRepository <BotScript> >(); var playerRepository = new Mock <IRepository <Player> >(); var botMapper = new BotMapper(); var botToCreateMapper = new BotToCreateMapper(); var arenaLogic = new Mock <IArenaLogic>(); var configurationHelper = new Mock <IConfigurationHelper>(); IBotLogic botLogic = new BotLogic( randomHelper.Object, botRepository.Object, scriptRepository.Object, playerRepository.Object, botMapper, botToCreateMapper, arenaLogic.Object, configurationHelper.Object); var arenaDto = new ArenaDto(3, 3); var player = new Player { Id = Guid.NewGuid(), LastDeployment = DateTime.UtcNow }; var botToCreateDto = new BotToCreateDto(Guid.NewGuid(), "BotName", 100, 200, "BotScript"); // Mock arenaLogic.Setup(x => x.GetArena()).ReturnsAsync(arenaDto); playerRepository.Setup(x => x.Single(Any.Predicate <Player>())).ReturnsAsync(player); configurationHelper.Setup(x => x.BotDeploymentLimit).Returns(1); // Act Func <Task> act = async() => await botLogic.CreateBot(botToCreateDto); // Assert act.Should().ThrowAsync <LogicException>().WithMessage("You are not allowed to create multiple robots in rapid succession!"); }
public async Task BotLogic_CreateBot_Should_Create_A_Bot() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var botRepository = new Mock <IRepository <Bot> >(); var scriptRepository = new Mock <IRepository <BotScript> >(); var playerRepository = new Mock <IRepository <Player> >(); var botMapper = new BotMapper(); var botToCreateMapper = new BotToCreateMapper(); var arenaLogic = new Mock <IArenaLogic>(); var configurationHelper = new Mock <IConfigurationHelper>(); IBotLogic botLogic = new BotLogic( randomHelper.Object, botRepository.Object, scriptRepository.Object, playerRepository.Object, botMapper, botToCreateMapper, arenaLogic.Object, configurationHelper.Object); var arenaDto = new ArenaDto(4, 3); var player = new Player { Id = Guid.NewGuid(), LastDeployment = DateTime.MinValue }; var botToCreateDto = new BotToCreateDto(Guid.NewGuid(), "BotName", 100, 200, "BotScript"); var botScripts = new List <BotScript> { new BotScript { Id = Guid.Empty, Script = botToCreateDto.Script } }; // Mock randomHelper.Setup(x => x.Get <PossibleOrientations>()).Returns(PossibleOrientations.South); botRepository.Setup(x => x.Find(Any.Predicate <Bot>(), i => i.Player)).ReturnsAsync(new List <Bot>()); randomHelper.Setup(x => x.Get(It.IsAny <int>())).Returns(5); arenaLogic.Setup(x => x.GetArena()).ReturnsAsync(arenaDto); botRepository.Setup(x => x.Create(It.IsAny <Bot>())).Returns <Bot>(Task.FromResult); playerRepository.Setup(x => x.Single(Any.Predicate <Player>())).ReturnsAsync(player); scriptRepository.Setup(x => x.Single(Any.Predicate <BotScript>())) .ReturnsAsync((Expression <Func <BotScript, bool> > predicate) => botScripts.SingleOrDefault(predicate.Compile())); scriptRepository.Setup(x => x.Update(It.IsAny <BotScript>())).Returns <BotScript>(Task.FromResult); // Act var result = await botLogic.CreateBot(botToCreateDto); // Assert result.Should().NotBeNull(); result.Name.Should().Be(botToCreateDto.Name); result.Orientation.Should().Be(PossibleOrientations.South); result.X.Should().Be(1); result.Y.Should().Be(2); result.FromX.Should().Be(1); result.FromY.Should().Be(2); result.MaximumHealth.Should().Be(botToCreateDto.MaximumHealth); result.CurrentHealth.Should().Be(botToCreateDto.MaximumHealth); result.MaximumStamina.Should().Be(botToCreateDto.MaximumStamina); result.CurrentStamina.Should().Be(botToCreateDto.MaximumStamina); result.Memory.Should().NotBeNull(); result.TimeOfDeath.Should().Be(DateTime.MaxValue); }
public async Task BotLogic_CreateBot_Should_Create_A_Bot_And_Not_Overlap_With_Existing_Bots() { // Arrange var randomHelper = new Mock <IRandomHelper>(); var botRepository = new Mock <IRepository <Bot> >(); var scriptRepository = new Mock <IRepository <BotScript> >(); var playerRepository = new Mock <IRepository <Player> >(); var botMapper = new BotMapper(); var botToCreateMapper = new BotToCreateMapper(); var arenaLogic = new Mock <IArenaLogic>(); var configurationHelper = new Mock <IConfigurationHelper>(); IBotLogic botLogic = new BotLogic( randomHelper.Object, botRepository.Object, scriptRepository.Object, playerRepository.Object, botMapper, botToCreateMapper, arenaLogic.Object, configurationHelper.Object); var arenaDto = new ArenaDto { Width = 3, Height = 3 }; var player = new Player { Id = Guid.NewGuid(), LastDeployment = DateTime.MinValue }; var botToCreateDto = new BotToCreateDto { Name = "BotName", MaximumHealth = 100, MaximumStamina = 200, Script = "BotScript" }; var otherBots = new List <Bot> { new Bot { X = 0, Y = 0 }, new Bot { X = 1, Y = 0 }, new Bot { X = 2, Y = 0 }, new Bot { X = 0, Y = 1 }, new Bot { X = 2, Y = 1 }, new Bot { X = 0, Y = 2 }, new Bot { X = 1, Y = 2 }, new Bot { X = 2, Y = 2 }, }; var botScripts = new List <BotScript> { new BotScript { Id = Guid.Empty, Script = botToCreateDto.Script } }; // Mock randomHelper.Setup(x => x.Get <PossibleOrientations>()).Returns(PossibleOrientations.South); botRepository.Setup(x => x.Find(Any.Predicate <Bot>(), i => i.Player)).ReturnsAsync(otherBots); randomHelper.Setup(x => x.Get(It.IsAny <int>())).Returns(0); arenaLogic.Setup(x => x.GetArena()).ReturnsAsync(arenaDto); botRepository.Setup(x => x.Create(It.IsAny <Bot>())).Returns <Bot>(Task.FromResult); playerRepository.Setup(x => x.Single(Any.Predicate <Player>())).ReturnsAsync(player); scriptRepository.Setup(x => x.Single(Any.Predicate <BotScript>())) .ReturnsAsync((Expression <Func <BotScript, bool> > predicate) => botScripts.SingleOrDefault(predicate.Compile())); scriptRepository.Setup(x => x.Update(It.IsAny <BotScript>())).Returns <BotScript>(Task.FromResult); // Act var result = await botLogic.CreateBot(botToCreateDto); // Assert result.Should().NotBeNull(); result.X.Should().Be(1); result.Y.Should().Be(1); }