public void AddStudentToTeam_StudentAndTeamExistsStudentNotYetAssigned()
        {
            // arrange
            var studentRepo = studentRepoMock.Object;
            var teamRepo    = teamRepoMock.Object;

            var s1 = new Student()
            {
                Id = 1
            };
            var s2 = new Student()
            {
                Id = 2
            };
            var s3 = new Student()
            {
                Id = 3
            };
            var s4 = new Student()
            {
                Id = 4
            };

            // all students exists in studentRepo
            studentRepo.Add(s1);
            studentRepo.Add(s2);
            studentRepo.Add(s3);
            studentRepo.Add(s4);

            var t = new Team()
            {
                Id = 1, Students = new List <Student>()
                {
                    s1, s2, s3
                }
            };

            // team t exists in teamRepo
            teamRepo.Add(t);

            var studentsBeforeTest = new List <Student>(studentRepo.GetAll());
            var teamsBeforeTest    = new List <Team>(teamRepo.GetAll());

            var service = new StudentTeamService(studentRepo, teamRepo);

            // act
            service.AddStudentToTeam(t, s4);

            // assert

            // student is now in the team
            Assert.NotNull(teamRepo.GetById(t.Id).Students.Where <Student>(s => s == s4));

            // team object har been updated in the team repository
            teamRepoMock.Verify(repo => repo.Update(It.Is <Team>(team => team == t)), Times.Once);

            // verify that repositories contains same objects as before act.
            Assert.Equal(studentsBeforeTest, studentRepoMock.Object.GetAll());
            Assert.Equal(teamsBeforeTest, teamRepoMock.Object.GetAll());
        }
        public void AddStudentToTeam_TeamDoesNotExist_ExpectArgumentException()
        {
            // arrange
            var studentRepo = studentRepoMock.Object;
            var teamRepo    = teamRepoMock.Object;

            var student = new Student()
            {
                Id = 1
            };

            // student exists in student repository
            studentRepo.Add(student);

            // team not in team repository
            var team = new Team()
            {
                Id = 1, Students = new List <Student>()
            };

            var studentsBeforeTest = new List <Student>(studentRepo.GetAll());
            var teamsBeforeTest    = new List <Team>(teamRepo.GetAll());

            var service = new StudentTeamService(studentRepoMock.Object, teamRepoMock.Object);

            // act + assert
            var ex = Assert.Throws <ArgumentException>(() => service.AddStudentToTeam(team, student));

            Assert.Equal("Team not found", ex.Message);
            teamRepoMock.Verify(repo => repo.Update(It.Is <Team>(t => t == null)), Times.Never);

            // verify that repositories contains same objects as before act.
            Assert.Equal(studentsBeforeTest, studentRepoMock.Object.GetAll());
            Assert.Equal(teamsBeforeTest, teamRepoMock.Object.GetAll());
        }
        public void AddStudentToTeam_TeamIsNull_ExpectArgumentException()
        {
            // arrange
            var studentRepo = studentRepoMock.Object;
            var teamRepo    = teamRepoMock.Object;

            var student = new Student()
            {
                Id = 1
            };

            studentRepo.Add(student);

            var studentsBeforeTest = new List <Student>(studentRepo.GetAll());
            var teamsBeforeTest    = new List <Team>(teamRepo.GetAll());

            var service = new StudentTeamService(studentRepo, teamRepo);

            // act + assert
            var ex = Assert.Throws <ArgumentException>(() => service.AddStudentToTeam(null, student));

            Assert.Equal("Team is missing", ex.Message);
            teamRepoMock.Verify(repo => repo.Update(It.Is <Team>(t => t == null)), Times.Never);

            // verify that repositories contains same objects as before act.
            Assert.Equal(studentsBeforeTest, studentRepoMock.Object.GetAll());
            Assert.Equal(teamsBeforeTest, teamRepoMock.Object.GetAll());
        }
        public void AddStudentToTeam_StudentIsAlreadyAssigned_ExpectInvalidOperationException()
        {
            // arrange
            var studentRepo = studentRepoMock.Object;
            var teamRepo    = teamRepoMock.Object;

            var s1 = new Student()
            {
                Id = 1
            };
            var s2 = new Student()
            {
                Id = 2
            };

            // all students exists in studentRepo
            studentRepo.Add(s1);
            studentRepo.Add(s2);

            var t1 = new Team()
            {
                Id = 1, Students = new List <Student>()
                {
                    s1
                }
            };
            var t2 = new Team()
            {
                Id = 2, Students = new List <Student>()
                {
                    s2
                }
            };

            // all teams exists in teamRepo
            teamRepo.Add(t1);
            teamRepo.Add(t2);

            var studentsBeforeTest = new List <Student>(studentRepo.GetAll());
            var teamsBeforeTest    = new List <Team>(teamRepo.GetAll());

            var service = new StudentTeamService(studentRepo, teamRepo);

            // act + assert
            var ex = Assert.Throws <InvalidOperationException>(() => service.AddStudentToTeam(t1, s2));

            Assert.Equal("Student is already assigned to a team", ex.Message);
            teamRepoMock.Verify(repo => repo.Update(It.Is <Team>(t => t == t1)), Times.Never);

            // verify that repositories contains same objects as before act.
            Assert.Equal(studentsBeforeTest, studentRepoMock.Object.GetAll());
            Assert.Equal(teamsBeforeTest, teamRepoMock.Object.GetAll());
        }
        public void AddStudentToTeam_ToManyStudents_ExpectInvalidOperationException()
        {
            // arrange
            var studentRepo = studentRepoMock.Object;
            var teamRepo    = teamRepoMock.Object;

            var s1 = new Student()
            {
                Id = 1
            };
            var s2 = new Student()
            {
                Id = 2
            };
            var s3 = new Student()
            {
                Id = 3
            };
            var s4 = new Student()
            {
                Id = 4
            };
            var s5 = new Student()
            {
                Id = 5
            };

            // all students exists in studentRepo
            studentRepo.Add(s1);
            studentRepo.Add(s2);
            studentRepo.Add(s3);
            studentRepo.Add(s4);
            studentRepo.Add(s5);

            // team t is full
            var t = new Team()
            {
                Id = 1, Students = new List <Student>()
                {
                    s1, s2, s3, s4
                }
            };

            // team t exists in teamRepo
            teamRepo.Add(t);

            var studentsBeforeTest = new List <Student>(studentRepo.GetAll());
            var teamsBeforeTest    = new List <Team>(teamRepo.GetAll());

            var service = new StudentTeamService(studentRepo, teamRepo);

            // act + assert
            var ex = Assert.Throws <InvalidOperationException>(() => service.AddStudentToTeam(t, s5));

            Assert.Equal("Team is full", ex.Message);
            teamRepoMock.Verify(repo => repo.Update(It.Is <Team>(team => team == t)), Times.Never);

            // verify that repositories contains same objects as before act.
            Assert.Equal(studentsBeforeTest, studentRepoMock.Object.GetAll());
            Assert.Equal(teamsBeforeTest, teamRepoMock.Object.GetAll());
        }