public void Add(Group newGroup)
 {
     using (var uow = _unitOfWorkFactory.GetUnitOfWork())
     {
         uow.GroupRepository.Add(newGroup);
         uow.Save();
     }
 }
Exemple #2
0
 public GroupModel Map(Group group)
 {
     return new GroupModel()
     {
         Name = group.Name,
         CourseId = group.CourseId,
         OwnerId = group.OwnerId
     };
 }
Exemple #3
0
 public Group Map(GroupModel groupModel)
 {
     Group newGroup = new Group()
     {
         Name = groupModel.Name,
         CourseId = groupModel.CourseId,
         OwnerId = groupModel.OwnerId
     };
     return newGroup;
 }
 public GroupForListingModel MapToGroupModel(Group group)
 {
     return new GroupForListingModel()
     {
         Id = group.Id,
         Name = group.Name,
         CourseId = group.Course.Id,
         CourseName = group.Course.Name
     };
 }
 public GroupForListingModel Map(Group group)
 {
     return new GroupForListingModel()
     {
         Id = group.Id,
         Name = group.Name,
         CourseId = group.Course.Id,
         CourseName = group.Course.Name,
         OwnerId = group.OwnerId,
         OwnerName = group.Owner.Name
     };
 }
        public void AddTest()
        {
            // Arrange
            Group group = new Group();

            // To be sure that calls from another test methods don't calculate
            groupRepository.ResetCalls();
            unitOfWork.ResetCalls();

            // Act
            groupService.Add(group);

            // Assert
            groupRepository.Verify(x => x.Add(group), Times.Once());
            unitOfWork.Verify(x => x.Save(), Times.Once());
        }
        public void CopyProgressesForUsersInGroup_Copied()
        {
            //Arrange
            int _groupId = 1;
            List<WordSuite> _collection = new List<WordSuite>() 
            {
                new WordSuite(){Id = 1, PrototypeId = null},
                new WordSuite()
                {OwnerId = 3, PrototypeId = 1, 
                    PrototypeWordSuite=new WordSuite()
                    {
                         WordProgresses=new List<WordProgress>()
                    },
                    WordProgresses = new List<WordProgress>()
                    {
                        new WordProgress()
                    }
                }
            };
            Group _group = new Group()
            {
                Course = new Course()
                {
                    WordSuites = _collection
                }
            };
            var _users = new List<User>() 
            {
                new User()
                {
                    Id = 3
                }
            };
            var _progressesToAdd = new List<WordProgress>();
            var _groupRepository = new Mock<IRepository<WorldOfWords.Domain.Models.Group>>();
            _uow.Setup(u => u.GroupRepository).Returns(_groupRepository.Object);
            _groupRepository.Setup(g => g.GetById(_groupId)).Returns(_group);
            _uow.Setup(u => u.WordProgressRepository).Returns(_progressRepository.Object);
            _uow.Setup(u => u.Save());
            _progressRepository.Setup(p => p.Add(It.IsAny<IEnumerable<WordProgress>>()));

            var _service = new WordProgressService(_factory.Object);

            //Act
            _service.CopyProgressesForUsersInGroup(_users, _groupId);

            //Assert
            _factory.Verify(f => f.GetUnitOfWork(), Times.Once);
            _uow.Verify(u => u.GroupRepository, Times.Exactly(1));
            _uow.Verify(u => u.WordProgressRepository, Times.Exactly(1));
            _groupRepository.Verify(e => e.GetById(_groupId), Times.Exactly(1));
            _progressRepository.Verify(p => p.Add(It.IsAny<IEnumerable<WordProgress>>()), Times.Once);
            _uow.Verify(u => u.Save(), Times.Once);

        }
        public void GetUsersNotBelongingToGroupTestPositive()
        {
            // Arrange

            // The group, not belonging to it users must be returned by test method
            var group = new Group
            {
                Id = 111,
                Course = new Course { Id = 222 },
                OwnerId = 333,
                CourseId = 222
            };

            var role1 = new Role { Name = "Student" };
            var role2 = new Role { Name = "AnotherRole" };

            // On the exit must be this user with this enrollment 

            // Enrollment to return
            var enrollment = new Enrollment
            {
                GroupId = 100,
                Group = new Group { Course = new Course { Id = 100 } }
            };

            // User to return
            var user = new User
            {
                Id = 100,
                Roles = new List<Role> { role1 },
                Enrollments = new List<Enrollment> { enrollment }
            };

            enrollment.User = user;

            // User to test "u.Id != group.OwnerId" condition in Service
            var user1 = new User
            {
                Id = 333,
                Roles = new List<Role> { role1 },
                Enrollments = new List<Enrollment> { enrollment }
            };

            // User to test "u.Enrollments.Select(e => e.Group.Course).FirstOrDefault(c => c.Id == course.Id) == null"
            // condition in Service
            var enrollment2 = new Enrollment
            {
                GroupId = 100,
                Group = new Group { Course = new Course { Id = 222 } }
            };
            var user2 = new User
            {
                Id = 333,
                Roles = new List<Role> { role1 },
                Enrollments = new List<Enrollment> { enrollment2 }
            };

            // User to test "!usersCurrentlyInGroup.Any(u2 => u2.Id == u.Id)" condition
            var user3 = new User
            {
                Id = 333,
                Roles = new List<Role> { role1 },
                Enrollments = new List<Enrollment> { enrollment2 }
            };

            // User to test "u.Roles.Select(r => r.Name).Contains("Student")" condition
            var user4 = new User
            {
                Id = 333,
                Roles = new List<Role> { role2 },
                Enrollments = new List<Enrollment> { enrollment2 }
            };

            // For enrollmentRepository to return
            var enrollments = new List<Enrollment>(new Enrollment[] {
                enrollment,
                new Enrollment { GroupId = 111, User = user1}
            }).AsQueryable();

            var users = (new List<User> { user, user1, user2, user3, user4 }).AsQueryable();

            var groupRepository = new Mock<IRepository<Group>>();
            unitOfWork.Setup(x => x.GroupRepository).Returns(groupRepository.Object);
            groupRepository.Setup(x => x.GetById(group.Id)).Returns(group);
            enrollmentRepository.Setup(x => x.GetAll()).Returns(enrollments);

            var userRepository = new Mock<IUserRepository>();
            unitOfWork.Setup(x => x.UserRepository).Returns(userRepository.Object);
            userRepository.Setup(x => x.GetAll()).Returns(users);

            // Act
            var result = enrollmentService.GetUsersNotBelongingToGroup(group.Id);

            // Assert
            Assert.IsTrue(result.TrueForAll(u => u.Roles.Select(r => r.Name).Contains("Student")),
                "User should have the Student role");
            Assert.IsTrue(result.TrueForAll(u => !u.Enrollments.Any( e => e.GroupId == group.Id)),
                "User shouldn't be in that group");
            Assert.IsTrue(result.TrueForAll(u => !u.Enrollments.Any(e => e.Group.CourseId == group.CourseId)),
                "User shouldn't be in the course that contains given group");
            Assert.IsTrue(result.TrueForAll(u => u.Id != group.OwnerId));
        }
        public void Get_GetGroupById_ReturnsGroupModel_Positive()
        {
            var initial = new Group
            {
                Id = 1,
                OwnerId = 1,
                CourseId = 1,
                Name = "Simple English",
                Course = new Course
                {
                    Name = "English.A1"
                }
            };

            var expected = new GroupForListingModel
            {
                Id = 1,
                CourseId = 1,
                Name = "Simple English",
                CourseName = "English.A1"
            };

            GenerateData("1", new[] { "NoRoles" });
            Mock<IGroupForListingMapper> groupForListingMapper = new Mock<IGroupForListingMapper>();
            Mock<IGroupService> groupService = new Mock<IGroupService>();
            Mock<IEnrollmentService> enrollmentService = new Mock<IEnrollmentService>();
            Mock<IWordProgressService> wordProgressService = new Mock<IWordProgressService>();
            Mock<IWordSuiteService> wordsuiteService = new Mock<IWordSuiteService>();
            Mock<ICourseService> courseService = new Mock<ICourseService>();
            Mock<ICourseForGroupMapper> courseMapper = new Mock<ICourseForGroupMapper>();
            Mock<IGroupMapper> groupMapper = new Mock<IGroupMapper>();

            GroupController groupController = new GroupController(groupService.Object, groupForListingMapper.Object,
                enrollmentService.Object, wordProgressService.Object, wordsuiteService.Object, courseService.Object,
                courseMapper.Object, groupMapper.Object);

            int groupId = 1;
            int userId = 1;

            groupService.Setup(x => x.GetById(groupId, userId)).Returns(initial);
            groupForListingMapper.Setup(x => x.MapToGroupModel(initial)).Returns(expected);

            var actual = groupController.Get(groupId);

            Assert.AreEqual(expected, actual);
        }