public async Task GetInProgressAsync_WithMatchInProgressAsHomeTeam_ReturnsMatchId() { // Arrange var matchId = Guid.NewGuid(); var userId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match { Id = matchId, HomeTeam = new TeamDetails() { UserId = userId }, AwayTeam = new TeamDetails() { UserId = Guid.NewGuid() }, KickOff = DateTime.Now.AddMinutes(-10) }; var stubMatchList = new List <ApplicationCore.Models.Match.Match>() { stubMatch }; var dbContextStub = new InMemoryDataContext <ApplicationCore.Models.Match.Match>(stubMatchList); // Act var repository = new MatchRepository(dbContextStub); var result = await repository.GetInProgressAsync(userId); //Assert Assert.Equal(stubMatch.Id, result); }
public async Task GetAllSync_WithMatchNotInvolvingUser_ReturnsEmptyList() { // Arrange var stubMatch = new ApplicationCore.Models.Match.Match { Id = Guid.NewGuid(), HomeTeam = new TeamDetails() { UserId = Guid.NewGuid() }, AwayTeam = new TeamDetails() { UserId = Guid.NewGuid() } }; var stubMatchList = new List <ApplicationCore.Models.Match.Match>() { stubMatch }; var dbContextStub = new InMemoryDataContext <ApplicationCore.Models.Match.Match>(stubMatchList); // Act var repository = new MatchRepository(dbContextStub); var result = await repository.GetAllAsync(0, 1, Guid.NewGuid()); //Assert Assert.False(result.Any()); }
public async Task Get_ReturnsMatch() { var mockMatchmakingService = new Mock <IMatchmakingService>(); var mockMatchRepository = new Mock <IMatchRepository>(); var matchId = Guid.NewGuid(); var match = new ApplicationCore.Models.Match.Match { Id = matchId }; mockMatchRepository.Setup(x => x.GetAsync(matchId)).Returns(Task.FromResult(match)); var stubMatchEngine = new Mock <IMatchEngine>(); var mockBus = new Mock <IBus>(); _matchService = new MatchService(mockMatchmakingService.Object, stubMatchEngine.Object, mockMatchRepository.Object, mockBus.Object); //Act var returnedMatch = await _matchService.GetAsync(matchId); //Assert Assert.Equal(returnedMatch, match); }
public async Task GetAllSync_WithOneMatchPlayed_ReturnsOneMatch() { // Arrange var matchId = Guid.NewGuid(); var userId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match { Id = matchId, HomeTeam = new TeamDetails() { UserId = userId, }, AwayTeam = new TeamDetails() { UserId = Guid.NewGuid() } }; var stubMatchList = new List <ApplicationCore.Models.Match.Match>() { stubMatch }; var dbContextStub = new InMemoryDataContext <ApplicationCore.Models.Match.Match>(stubMatchList); // Act var repository = new MatchRepository(dbContextStub); var result = await repository.GetAllAsync(0, 1, userId); //Assert Assert.Equal(stubMatch, result.First()); }
public async Task GetUnclaimedAsync_WhenUserHasAnUnclaimedMatch_ReturnsUnclaimedMatch() { var matchId = Guid.NewGuid(); var userId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match { Id = matchId, HomeTeam = new TeamDetails() { UserId = userId, HasClaimedRewards = false }, AwayTeam = new TeamDetails() { UserId = Guid.NewGuid() }, KickOff = DateTime.Now.AddMinutes(-100) }; var stubMatchList = new List <ApplicationCore.Models.Match.Match>() { stubMatch }; var dbContextStub = new InMemoryDataContext <ApplicationCore.Models.Match.Match>(stubMatchList); // Act var repository = new MatchRepository(dbContextStub); var result = await repository.GetUnclaimedAsync(userId); //Assert Assert.Equal(stubMatch, result.First()); }
public void Fitness_Returns_Correct_Value() { var cardId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match(); stubMatch.Minutes = new MatchMinute[] { new MatchMinute() { Modifiers = new List <Modifier>() { new Modifier() { CardId = cardId, DrainValue = 1, Type = ModifierType.Fitness } } } }; var service = new CalculatedCardStatService(); //Act var fitness = service.Fitness(stubMatch, cardId); //Assert Assert.Equal(99, fitness); }
public async Task ClaimingMatchReward_AsHomeTeam_ShouldMarkMatchAsClaimedByHomeTeam() { var mockMatchmakingService = new Mock <IMatchmakingService>(); var mockMatchRepository = new Mock <IMatchRepository>(); var userId = Guid.NewGuid(); var unclaimedMatch = new ApplicationCore.Models.Match.Match { HomeTeam = new TeamDetails { UserId = userId, Squad = new Squad { Id = Guid.NewGuid() } }, AwayTeam = new TeamDetails { UserId = Guid.NewGuid(), Squad = new Squad { Id = Guid.NewGuid() } } }; ApplicationCore.Models.Match.Match updatedMatch = null; MatchCompletedEvent publishedEvent = null; mockMatchRepository.Setup(x => x.GetUnclaimedAsync(userId)).Returns( Task.FromResult((IEnumerable <ApplicationCore.Models.Match.Match>) new List <ApplicationCore.Models.Match.Match> { unclaimedMatch })); mockMatchRepository.Setup(x => x.UpdateAsync(It.IsAny <ApplicationCore.Models.Match.Match>())) .Callback <ApplicationCore.Models.Match.Match>(r => updatedMatch = r); var stubMatchEngine = new Mock <IMatchEngine>(); var mockBus = new Mock <IBus>(); mockBus.Setup(x => x.PublishAsync(It.IsAny <MatchCompletedEvent>())) .Callback <MatchCompletedEvent>(r => publishedEvent = r); _matchService = new MatchService(mockMatchmakingService.Object, stubMatchEngine.Object, mockMatchRepository.Object, mockBus.Object); //Act await _matchService.ClaimAsync(userId); //Assert Assert.True(updatedMatch.HomeTeam.HasClaimedRewards); Assert.NotNull(publishedEvent); }
private void Setup() { _matchId = Guid.NewGuid(); var userId = Guid.NewGuid(); var matchStub = new ApplicationCore.Models.Match.Match { HomeTeam = new TeamDetails { UserId = userId, Squad = new Squad { Id = Guid.NewGuid() } }, AwayTeam = new TeamDetails { UserId = Guid.NewGuid(), Squad = new Squad { Id = Guid.NewGuid() } } }; var mockMatchService = new Mock <IMatchService>(); mockMatchService.Setup(x => x.GetAsync(_matchId)).Returns(Task.FromResult(matchStub)); var mockAutomapper = new Mock <IMapper>(); mockAutomapper.Setup(x => x.Map <LineupModel>(It.IsAny <Lineup>())).Returns(new LineupModel()); _matchControllerStub = new MatchController(mockMatchService.Object, mockAutomapper.Object); var claims = new List <Claim> { new Claim(ClaimTypes.Name, "username"), new Claim(ClaimTypes.NameIdentifier, userId.ToString()) }; var identity = new ClaimsIdentity(claims, "TestAuthType"); var claimsPrincipal = new ClaimsPrincipal(identity); var contextStub = new DefaultHttpContext(); contextStub.User = claimsPrincipal; var controllerContextStub = new ControllerContext(); controllerContextStub.HttpContext = contextStub; _matchControllerStub.ControllerContext = controllerContextStub; }
public async Task KickOff_ReturnsMatch() { var mockMatchmakingService = new Mock <IMatchmakingService>(); var sessionId = Guid.NewGuid(); var session = new MatchmakingSession { Id = sessionId, HostPlayerId = Guid.NewGuid(), JoinedPlayerId = Guid.NewGuid() }; mockMatchmakingService.Setup(x => x.GetSession(sessionId)).Returns(session); var stubMatchEngine = new Mock <IMatchEngine>(); stubMatchEngine.Setup(x => x.Simulate(It.IsAny <ApplicationCore.Models.Match.Match>())) .Returns(new ApplicationCore.Models.Match.Match()); ApplicationCore.Models.Match.Match simulatedMatch = null; var mockBus = new Mock <IBus>(); var squadResponse = new GetSquadResponse { Lineup = new Dictionary <string, Card>() }; mockBus.Setup(x => x.RequestAsync <GetSquadRequest, GetSquadResponse>(It.IsAny <GetSquadRequest>())) .Returns(Task.FromResult(squadResponse)); var mockMatchRepository = new Mock <IMatchRepository>(); mockMatchRepository.Setup(x => x.CreateAsync(It.IsAny <ApplicationCore.Models.Match.Match>())) .Callback <ApplicationCore.Models.Match.Match>(r => simulatedMatch = r); _matchService = new MatchService(mockMatchmakingService.Object, stubMatchEngine.Object, mockMatchRepository.Object, mockBus.Object); //Act await _matchService.KickOff(sessionId); //Assert Assert.NotNull(simulatedMatch); }
public void FoulAction_OnAYellowCard_WithASecondYellow_ShouldSpawnARedCardEvent() { //Arrange var randomnessProvider = new TestRandomnessProvider(3); var foul = new Foul(randomnessProvider); var cardId = Guid.NewGuid(); var card = new Card { Id = cardId }; var match = new ApplicationCore.Models.Match.Match(); match.Minutes[5].Events.Add(new YellowCard(cardId, new Guid())); //Act var @event = foul.SpawnEvent(card, new Guid(), match); //Assert Assert.Equal(typeof(RedCard), @event.GetType()); }
public async Task UpdateAsync_CallsUpdateAsyncOnce() { // Arrange var matchId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match() { Id = matchId }; var dbContextMock = new Mock <IDataContext <ApplicationCore.Models.Match.Match> >(); dbContextMock.Setup(x => x.CreateAsync(stubMatch)); // Act var repository = new MatchRepository(dbContextMock.Object); var result = await repository.UpdateAsync(stubMatch); //Assert dbContextMock.Verify(x => x.UpdateAsync(stubMatch), Times.Once); }
public async Task GetAsync_MatchIdGuidPassed_ReturnsMatchWithId() { // Arrange var matchId = Guid.NewGuid(); var stubMatch = new ApplicationCore.Models.Match.Match { Id = matchId }; var stubMatchList = new List <ApplicationCore.Models.Match.Match>() { stubMatch }; var dbContextStub = new InMemoryDataContext <ApplicationCore.Models.Match.Match>(stubMatchList); // Act var repository = new MatchRepository(dbContextStub); var result = await repository.GetAsync(matchId); //Assert Assert.Equal(stubMatch, result); }
public void Drain_Adds_Modifier() { var cardId = Guid.NewGuid(); var stubCard = new Card() { Id = cardId }; var stubSquad = new Squad() { Lineup = new Dictionary <string, IEnumerable <Card> >() { { "ST", new[] { stubCard } } } }; var stubMatch = new ApplicationCore.Models.Match.Match() { HomeTeam = new TeamDetails() { Squad = stubSquad }, AwayTeam = new TeamDetails() { Squad = new Squad() } }; var minute = 50; var randomnessProvider = new ActionTests.TestRandomnessProvider(100); var service = new FitnessDrainService(randomnessProvider); //Act service.Drain(stubMatch, minute); //Assert Assert.NotNull(stubMatch.Minutes[minute].Modifiers.First()); Assert.Equal(ModifierType.Fitness, stubMatch.Minutes[minute].Modifiers.First().Type); Assert.Equal(cardId, stubMatch.Minutes[minute].Modifiers.First().CardId); Assert.Equal(1, stubMatch.Minutes[minute].Modifiers.First().DrainValue); }
protected MatchTestBase() { //TODO Mock these var randomnessProvider = new ThreadSafeRandomnessProvider(); var calculatedCardStatService = new CalculatedCardStatService(); var ratingService = new RatingService(calculatedCardStatService); var fitnessDrainService = new FitnessDrainService(randomnessProvider); var possessionService = new PossessionService(ratingService); var actions = new IAction[] { new Foul(randomnessProvider), new Shot(randomnessProvider, ratingService) }; var actionService = new ActionService(actions, randomnessProvider); StubMatchEngine = new MatchEngine(actionService, possessionService, fitnessDrainService); StubHomePlayer = new Card { Id = Guid.NewGuid(), Name = "GK", Rating = 80 }; StubHomeSub = new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 40 }; StubAwayPlayer = new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 40 }; StubAwaySub = new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 40 }; var stubHomeTeamLineup = new Dictionary <string, IEnumerable <Card> > { { "GK", new List <Card> { StubHomePlayer } }, { "DEF", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "LB", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "CB", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "CB", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "RB", Rating = 80 } } }, { "MID", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "LM", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "CM", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "CM", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "RM", Rating = 80 } } }, { "ATT", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 80 }, new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 80 } } } }; var stubAwayTeamLineup = new Dictionary <string, IEnumerable <Card> > { { "GK", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "GK", Rating = 40 } } }, { "DEF", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "LB", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "CB", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "CB", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "RB", Rating = 40 } } }, { "MID", new List <Card> { new Card { Id = Guid.NewGuid(), Name = "LM", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "CM", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "CM", Rating = 40 }, new Card { Id = Guid.NewGuid(), Name = "RM", Rating = 40 } } }, { "ATT", new List <Card> { StubAwayPlayer, new Card { Id = Guid.NewGuid(), Name = "ST", Rating = 40 } } } }; StubHomeSquad = new Squad { Id = Guid.NewGuid(), Lineup = stubHomeTeamLineup, Subs = new[] { StubHomeSub }, Name = "Good FC" }; StubAwaySquad = new Squad { Id = Guid.NewGuid(), Lineup = stubAwayTeamLineup, Subs = new[] { StubAwaySub }, Name = "Shitty FC" }; StubHomeUserId = Guid.NewGuid(); var stubAwayUserId = Guid.NewGuid(); StubHomeTeamDetails = new TeamDetails { UserId = StubHomeUserId, Squad = StubHomeSquad }; StubMatch = new ApplicationCore.Models.Match.Match { Id = Guid.NewGuid(), KickOff = DateTime.Now, HomeTeam = StubHomeTeamDetails, AwayTeam = new TeamDetails { UserId = stubAwayUserId, Squad = StubAwaySquad } }; }
protected void SimulateStubMatch() { StubMatch = StubMatchEngine.Simulate(StubMatch); }
public async Task <ApplicationCore.Models.Match.Match> UpdateAsync(ApplicationCore.Models.Match.Match match) { await _dataContext.UpdateAsync(match); return(match); }