Exemplo n.º 1
0
        public Player CreatePlayer(string firstName, string lastName, string nationality, int rating, int position)
        {
            var price       = rating * 1000;
            var playerExist = this.RetrievePlayer(firstName, lastName);

            Validations.ValidateAlreadyExists(playerExist, "Player already exists");

            if (playerExist != null && playerExist.IsDeleted)
            {
                throw new EntityDoesntExistsException("Player retired");
            }
            var player = new Player()
            {
                FirstName   = firstName,
                LastName    = lastName,
                Nationality = nationality,
                Rating      = rating,
                PositionId  = position,
                Price       = price
            };

            context.Players.Add(player);
            context.SaveChanges();
            return(player);
        }
Exemplo n.º 2
0
        public Team BuyPlayer(string name, string city, string firstName, string lastName)
        {
            var team = this.RetrieveTeam(name, city);

            Validations.ValidateDoesntExists(team, "Team doesn't exists");

            var player = playerService.RetrievePlayer(firstName, lastName);

            Validations.ValidateDoesntExists(player, "Player doesn't exists");

            if (player.IsDeleted)
            {
                throw new EntityDoesntExistsException("Player already retired");
            }
            if (player.TeamId != null)
            {
                throw new EntityAlreadyExistsException("Player already has a team");
            }
            if (player.Price > team.Budget)
            {
                throw new ArgumentOutOfRangeException($"Team doesn't have enogh money to buy {player.FirstName} {player.LastName}");
            }

            player.TeamId = team.Id;
            team.Budget  -= player.Price;
            context.SaveChanges();
            return(team);
        }
Exemplo n.º 3
0
        public League CreateLeague(string name)
        {
            var leagueExist = this.RetrieveLeague(name);

            Validations.ValidateAlreadyExists(leagueExist, "League already exists");

            var league = new League()
            {
                Name = name,
            };

            context.Leagues.Add(league);
            context.SaveChanges();
            return(league);
        }
        public void ReturnsLeagues()
        {
            //Arrange

            var service     = new Mock <TeamService>();
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();
            var options     = TestUtils.GetOptions(nameof(ReturnsLeagues));

            using (var arrangeContext = new FMDbContext(options))
            {
                var league = new League()
                {
                    Name = "Test",
                    Id   = 1
                };
                arrangeContext.Leagues.Add(league);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut     = new LeagueService(assertContext, teamService.Object, handler.Object);
                var leagues = sut.ShowAllLeagues().ToList();
                Assert.AreEqual(leagues.Count(), 1);
                Assert.AreEqual(leagues[0].Name, "Test");
            }
        }
Exemplo n.º 5
0
        public void CreateMatches_IfLeagueHasMatches_ReturnCollectionOfMatches()
        {
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();
            var match       = new Mock <Match>();
            var options     = TestUtils.GetOptions(nameof(CreateMatches_IfLeagueHasMatches_ReturnCollectionOfMatches));
            var league      = new League()
            {
                Name = "l"
            };
            var matchesMock = new List <Match>()
            {
                match.Object
            };

            handler.Setup(h => h.CreateMatches(league)).Returns(matchesMock);
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Leagues.Add(league);
                arrangeContext.SaveChanges();
            }
            using (var assertContext = new FMDbContext(options))
            {
                var sut     = new LeagueService(assertContext, teamService.Object, handler.Object);
                var matches = sut.CreateMatches("l");
                Assert.AreEqual(0, matches.Count());
            }
        }
        public void Throws_PlayerAlreadyRetired()
        {
            //Arrange
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var firstName     = "Hristo";
            var lastName      = "Konov";
            var playerService = new Mock <IPlayerService>();
            var player        = new Mock <Player>();

            player.SetupAllProperties();
            player.Object.FirstName = firstName;
            player.Object.LastName  = lastName;
            player.Object.IsDeleted = true;
            var options = TestUtils.GetOptions(nameof(Throws_PlayerAlreadyRetired));

            playerService.Setup(p => p.RetrievePlayer(firstName, lastName)).Returns(player.Object);//new List<Match>(){match.Object}
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(new Team()
                {
                    Name = teamName, City = teamCity
                });

                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut = new TeamService(assertContext, playerService.Object);
                var ex  = Assert.ThrowsException <EntityDoesntExistsException>(() => sut.BuyPlayer(teamName, teamCity, firstName, lastName));
                Assert.AreEqual(ex.Message, "Player already retired");
            }
        }
Exemplo n.º 7
0
        public void ReturnTeam()
        {
            //Arrange
            var playerService = new Mock <IPlayerService>();
            var options       = TestUtils.GetOptions(nameof(ReturnTeam));
            var teamName      = "Cunami";
            var teamCity      = "Sofia";

            //Act,Assert
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(new Team()
                {
                    Name = teamName, City = teamCity
                });
                arrangeContext.SaveChanges();
            }
            using (var assertContext = new FMDbContext(options))
            {
                var sut  = new TeamService(assertContext, playerService.Object);
                var team = sut.ShowTeam(teamName, teamCity);
                Assert.AreEqual(team.Name, teamName);
                Assert.AreEqual(team.City, teamCity);
            }
        }
Exemplo n.º 8
0
        private void FillDbWithTestData()
        {
            context.Database.ExecuteSqlCommand("DELETE FROM people");
            context.Database.ExecuteSqlCommand("DELETE FROM shoppinglistItems");
            context.Database.ExecuteSqlCommand("DELETE FROM families");
            context.Database.ExecuteSqlCommand("DELETE FROM items");
            context.Database.ExecuteSqlCommand("DELETE FROM stores");

            var store = new Store()
            {
                Id = Guid.NewGuid(), Name = "Delhaize"
            };
            var item = new Item()
            {
                Id = Guid.NewGuid(), Name = "Mango", Description = "Tropical Fruit"
            };
            var family = new Family()
            {
                Name = "Van den Driessche", Id = Guid.NewGuid()
            };
            var sli = new ShoppingListItem()
            {
                Id = Guid.NewGuid(), Amount = 3, BuyAtStoreId = store.Id, ItemId = item.Id, FamilyId = family.Id
            };
            var user = new Person()
            {
                Id = Guid.NewGuid(), Name = "Jens", LastName = "Van den Driessche", Email = "*****@*****.**", Family = family
            };

            context.Set <Person>().Add(user);
            context.Set <Item>().Add(item);
            context.Set <Store>().Add(store);
            context.Set <ShoppingListItem>().Add(sli);
            context.SaveChanges();
        }
Exemplo n.º 9
0
        public void ReturnsManager()
        {
            //Arrange
            var teamService = new Mock <ITeamService>();
            var options     = TestUtils.GetOptions(nameof(ReturnsManager));
            var firstName   = "Hristo";
            var lastName    = "Konov";
            var nationality = "Bulgarian";


            using (var arrangeContext = new FMDbContext(options))
            {
                var manager = new Manager()
                {
                    FirstName   = firstName,
                    LastName    = lastName,
                    Nationality = nationality
                };
                arrangeContext.Managers.Add(manager);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut     = new ManagerService(assertContext, teamService.Object);
                var manager = sut.RetrieveManager(firstName, lastName);

                Assert.AreEqual(manager.FirstName, firstName);
                Assert.AreEqual(manager.LastName, lastName);
                Assert.AreEqual(manager.Nationality, nationality);
            }
        }
Exemplo n.º 10
0
        public Manager CreateManager(string firstName, string lastName, string nationality)
        {
            var managerexist = RetrieveManager(firstName, lastName);

            Validations.ValidateAlreadyExists(managerexist, "Manager already exists");

            var manager = new Manager
            {
                FirstName   = firstName,
                LastName    = lastName,
                Nationality = nationality
            };

            context.Managers.Add(manager);
            context.SaveChanges();
            return(manager);
        }
Exemplo n.º 11
0
        public Stadium CreateStadium(string name, string city, string country, int capacity)
        {
            var stadiumExists = this.RetrieveStadium(name, city);

            Validations.ValidateAlreadyExists(stadiumExists, "Stadium already exists");

            var stadium = new Stadium()
            {
                Name     = name,
                City     = city,
                Country  = country,
                Capacity = capacity
            };

            context.Stadiums.Add(stadium);
            context.SaveChanges();
            return(stadium);
        }
Exemplo n.º 12
0
        public void ReturnsTeam()
        {
            //Arrange
            var playerService = new Mock <IPlayerService>();
            var options       = TestUtils.GetOptions(nameof(ReturnsTeam));
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var country       = "Bulgaria";
            var owner         = "Hristo Konov";
            var manager       = new Manager()
            {
                FirstName   = "Hristo",
                LastName    = "Konov",
                Nationality = "Bulgarian",
                TeamId      = 1
            };

            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(new Team()
                {
                    Name    = teamName,
                    City    = teamCity,
                    Country = country,
                    Owner   = owner,
                    Id      = 1
                });
                arrangeContext.Managers.Add(manager);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var arrangeContext = new FMDbContext(options))
            {
                var sut  = new TeamService(arrangeContext, playerService.Object);
                var team = sut.RetrieveTeam(teamName, teamCity);

                Assert.AreEqual(team.Name, teamName);
                Assert.AreEqual(team.City, teamCity);
                Assert.AreEqual(team.Country, country);
                Assert.AreEqual(team.Owner, owner);
                Assert.AreEqual(1, arrangeContext.Teams.Count());
                Assert.AreEqual(team.Manager.FirstName, manager.FirstName);
                Assert.AreEqual(team.Manager.LastName, manager.LastName);
            }
        }
Exemplo n.º 13
0
        public void TeamBudgetChanged()
        {
            //Arrange
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var firstName     = "Hristo";
            var lastName      = "Konov";
            var playerService = new Mock <IPlayerService>();

            var player = new Mock <Player>();

            player.SetupAllProperties();
            player.Object.FirstName = firstName;
            player.Object.LastName  = lastName;
            player.Object.IsDeleted = false;
            player.Object.TeamId    = null;
            player.Object.Price     = 20;
            var options = TestUtils.GetOptions(nameof(TeamBudgetChanged));

            playerService.Setup(p => p.RetrievePlayer(firstName, lastName)).Returns(player.Object);
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(new Team()
                {
                    Name = teamName, City = teamCity, Budget = 50, Id = 1
                });

                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var arrangeContext = new FMDbContext(options))
            {
                var sut  = new TeamService(arrangeContext, playerService.Object);
                var team = sut.BuyPlayer(teamName, teamCity, firstName, lastName);

                Assert.AreEqual(30, team.Budget);
                Assert.IsInstanceOfType(team, typeof(Team));
                Assert.AreEqual(teamName, team.Name);
                Assert.AreEqual(teamCity, team.City);
                Assert.AreEqual(1, arrangeContext.Teams.Count());
            }
        }
Exemplo n.º 14
0
        public void RetrieveLeague_WhenExecuted_FirstOrDefaultReturnNull()
        {
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();

            using (var arrangeContext = new FMDbContext(TestUtils.GetOptions(nameof(RetrieveLeague_WhenExecuted_FirstOrDefaultReturnNull))))
            {
                arrangeContext.Leagues.Add(new League()
                {
                    Name = "League"
                });
                arrangeContext.SaveChanges();
            }

            using (var assertContext = new FMDbContext(TestUtils.GetOptions(nameof(RetrieveLeague_WhenExecuted_FirstOrDefaultReturnNull))))
            {
                var sut = new LeagueService(assertContext, teamService.Object, handler.Object);
                var res = sut.RetrieveLeague("L");
                Assert.IsNull(res);
            }
        }
Exemplo n.º 15
0
        public void ShowMatches_IfLeagueDoesntExists_ThrowEntityDoesntExistsException()
        {
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();

            var options = TestUtils.GetOptions(nameof(ShowMatches_IfLeagueDoesntExists_ThrowEntityDoesntExistsException));

            using (var context = new FMDbContext(options))
            {
                var league = new League();
                league.Name = "Primera";
                context.Leagues.Add(league);
                context.SaveChanges();
            }

            using (var context = new FMDbContext(options))
            {
                var sut = new LeagueService(context, teamService.Object, handler.Object);

                Assert.ThrowsException <EntityDoesntExistsException>(() => sut.ShowAllMatches("Premier"));
            }
        }
        public void ReturnsTeamRating()
        {
            //Arrange
            var playerService = new Mock <IPlayerService>();
            var options       = TestUtils.GetOptions(nameof(ReturnsTeamRating));
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var country       = "Bulgaria";
            var owner         = "Hristo Konov";
            var player        = new Mock <Player>();

            player.SetupAllProperties();
            player.Object.Rating = 100;
            var team = new Team()
            {
                Name    = teamName,
                City    = teamCity,
                Country = country,
                Owner   = owner
            };

            playerService.Setup(p => p.RetrieveTeamPlayers(team))
            .Returns(new List <Player>()
            {
                player.Object
            });
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(team);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut    = new TeamService(assertContext, playerService.Object);
                var rating = sut.RetrieveTeamRatings(team);
                Assert.AreEqual(rating, 100);
            }
        }
Exemplo n.º 17
0
        public void ReturnsTeams()
        {
            //Arrange
            var playerService = new Mock <IPlayerService>();
            var options       = TestUtils.GetOptions(nameof(ReturnsTeams));
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var country       = "Bulgaria";
            var owner         = "Hristo Konov";
            var league        = new League()
            {
                Name = "Test",
                Id   = 1
            };
            var team = new Team()
            {
                Name     = teamName,
                City     = teamCity,
                Country  = country,
                Owner    = owner,
                LeagueId = 1
            };

            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Leagues.Add(league);
                arrangeContext.Teams.Add(team);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut   = new TeamService(assertContext, playerService.Object);
                var teams = sut.RetrieveLeagueTeams(league);
                Assert.AreEqual(1, teams.Count);
                Assert.AreEqual(teamName, teams[0].Name);
                Assert.AreEqual(teamCity, teams[0].City);
            }
        }
Exemplo n.º 18
0
        public void ReturnsManager()
        {
            //Arrange
            var teamService = new Mock <ITeamService>();
            var options     = TestUtils.GetOptions(nameof(ReturnsManager));
            var firstName   = "Hristo";
            var lastName    = "Konov";
            var nationality = "Bulgarian";
            var team        = new Mock <Team>();
            var manager     = new Manager()
            {
                FirstName   = firstName,
                LastName    = lastName,
                Nationality = nationality,
                TeamId      = 1
            };

            team.Object.Name    = "Cunami";
            team.Object.City    = "Sofia";
            team.Object.Id      = 1;
            team.Object.Manager = manager;
            teamService.Setup(t => t.RetrieveTeam("Cunami", "Sofia")).Returns(team.Object);
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Managers.Add(manager);
                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut         = new ManagerService(assertContext, teamService.Object);
                var managerTest = sut.FireManager(firstName, lastName, "Cunami", "Sofia");


                Assert.IsNull(managerTest.TeamId);
                Assert.IsNull(team.Object.Manager);
            }
        }
Exemplo n.º 19
0
        public void RetrieveLeague_WhenExecuted_ReturnLeague()
        {
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();
            var options     = TestUtils.GetOptions(nameof(RetrieveLeague_WhenExecuted_ReturnLeague));

            using (var arrangeContext = new FMDbContext(options))
            {
                var league = new League()
                {
                    Name = "League"
                };
                arrangeContext.Leagues.Add(league);
                arrangeContext.SaveChanges();
            }

            using (var assertContext = new FMDbContext(options))
            {
                var sut = new LeagueService(assertContext, teamService.Object, handler.Object);
                var ex  = sut.RetrieveLeague("League");
                Assert.IsInstanceOfType(ex, typeof(League));
            }
        }
Exemplo n.º 20
0
        public void Throws_TeamDoesntHaveMoneyToBuyPlayer()
        {
            //Arrange
            var teamName      = "Cunami";
            var teamCity      = "Sofia";
            var firstName     = "Hristo";
            var lastName      = "Konov";
            var playerService = new Mock <IPlayerService>();

            var player = new Mock <Player>();

            player.SetupAllProperties();
            player.Object.FirstName = firstName;
            player.Object.LastName  = lastName;
            player.Object.IsDeleted = false;
            player.Object.TeamId    = null;
            player.Object.Price     = 20;
            var options = TestUtils.GetOptions(nameof(Throws_TeamDoesntHaveMoneyToBuyPlayer));

            playerService.Setup(p => p.RetrievePlayer(firstName, lastName)).Returns(player.Object);
            using (var arrangeContext = new FMDbContext(options))
            {
                arrangeContext.Teams.Add(new Team()
                {
                    Name = teamName, City = teamCity, Budget = 0
                });

                arrangeContext.SaveChanges();
            }
            //Act,Assert
            using (var assertContext = new FMDbContext(options))
            {
                var sut = new TeamService(assertContext, playerService.Object);
                var ex  = Assert.ThrowsException <ArgumentOutOfRangeException>(() => sut.BuyPlayer(teamName, teamCity, firstName, lastName));
                Assert.AreEqual(ex.ParamName, $"Team doesn't have enogh money to buy {firstName} {lastName}");
            }
        }
        public void CreateLeague_IfLeagueIsReturned_ReturnLeague()
        {
            var teamService = new Mock <ITeamService>();
            var handler     = new Mock <IMatchHandler>();

            using (var arrangeContext = new FMDbContext(TestUtils.GetOptions(nameof(CreateLeague_IfLeagueIsReturned_ReturnLeague))))
            {
                arrangeContext.Leagues.Add(new League()
                {
                    Name = "League"
                });
                arrangeContext.SaveChanges();
            }

            using (var assertContext = new FMDbContext(TestUtils.GetOptions(nameof(CreateLeague_IfLeagueIsReturned_ReturnLeague))))
            {
                var sut    = new LeagueService(assertContext, teamService.Object, handler.Object);
                var ex     = sut.CreateLeague("League1");
                var league = assertContext.Leagues.FirstOrDefault(l => l.Name == "League1");

                //Assert.IsInstanceOfType(ex, typeof(League));
                Assert.IsNotNull(league);
            }
        }
Exemplo n.º 22
0
        public IEnumerable <Match> CreateMatches(League league)
        {
            Random rnd     = new Random();
            var    matches = new List <Match>();
            var    teams   = teamService.RetrieveLeagueTeams(league).ToArray();

            Validations.ValidateCollectionZero(teams, "There are no teams in the league");
            if (teams.Length % 2 != 0)
            {
                throw new ArgumentOutOfRangeException("There are odd teams in the league");
            }
            Team[,] matrix = new Team[teams.Length / 2, 2];
            var i = 0;

            for (int j = 0; j < 2; j++)
            {
                for (int t = 0; t < teams.Length / 2; t++)
                {
                    matrix[t, j] = teams[i];
                    i++;
                }
            }
            int      index    = 0;
            var      statiums = context.Stadiums.ToArray();
            DateTime date1    = new DateTime(2019, 3, 1);

            for (int z = 0; z < teams.Length - 1; z++)
            {
                for (int j = 0; j < teams.Length / 2; j++)
                {
                    var match = new Match
                    {
                        HomeTeam = matrix[j, 0],
                        AwayTeam = matrix[j, 1],
                        Stadium  = statiums[index],
                        League   = league,
                        Date     = date1
                    };
                    index++;
                    date1 = date1.AddDays(1);
                    matches.Add(match);
                    context.Matches.Add(match);
                }
                index = 0;
                RotateMatrix(matrix.GetLength(0), matrix.GetLength(1), matrix);
            }
            context.SaveChanges();
            var newMatches = new List <Match>();

            foreach (var match in matches)
            {
                var newMatch = new Match
                {
                    HomeTeam = match.AwayTeam,
                    AwayTeam = match.HomeTeam,
                    Stadium  = match.Stadium,
                    League   = league,
                    Date     = match.Date.AddDays(teams.Length + 1)
                };
                newMatches.Add(newMatch);
                context.Matches.Add(newMatch);
                context.SaveChanges();
            }
            matches.AddRange(newMatches);
            return(matches);
        }