Ejemplo n.º 1
0
        public async Task GetProjectLobbyStateAsync_IfNotFoundAndProjectExists_ReturnsState()
        {
            var project = Project.Example();

            SetupApisForRecalculate(HttpStatusCode.OK, false, project);

            var state = ProjectLobbyState.Example();

            state.ProjectId = project.Id;

            var participants = new List <Participant>();
            var participant  = Participant.Example();

            ParticipantExtensions.SetProjectId(participant, project.Id);
            participant.GuestSessionId = null;
            participant.IsGuest        = false;
            participants.Add(participant);

            _sessionServiceMock.Setup(m => m.GetParticipantsByGroupNameAsync(It.IsAny <string>(), It.IsAny <bool>(), false))
            .ReturnsAsync(participants);

            _cacheMock
            .Setup(m => m.ItemSetAsync(It.IsAny <string>(), It.IsAny <ProjectLobbyState>(), It.IsAny <TimeSpan>(), It.IsAny <CacheCommandOptions>()))
            .Returns(Task.FromResult(state));

            _cacheMock
            .SetupSequence(m => m.ItemGetAsync(It.IsAny <List <string> >(), typeof(ProjectLobbyState)))
            .ReturnsAsync(default(List <ProjectLobbyState>))
            .ReturnsAsync(new List <ProjectLobbyState>()
            {
                state
            });

            var result = await _target.GetProjectLobbyStateAsync(project.Id);

            Assert.IsAssignableFrom <ProjectLobbyState>(result);
            Assert.Equal(state.ProjectId, result.ProjectId);
            Assert.Equal(state.LobbyState, result.LobbyState);
        }
Ejemplo n.º 2
0
        public async Task RecalculateProjectLobbyStateAsync_ReturnsExpectedLobbyState(int fullMemberParticipantCount, int guestSessionCount, LobbyState lobbyState)
        {
            var project   = Project.Example();
            var projectId = project.Id;

            var participants = new List <Participant>();

            for (int i = 1; i <= fullMemberParticipantCount; i++)
            {
                var participant = Participant.Example();
                ParticipantExtensions.SetProjectId(participant, project.Id);
                participant.GuestSessionId = null;
                participant.IsGuest        = false;
                participants.Add(participant);
            }

            _sessionServiceMock.Setup(m => m.GetParticipantsByGroupNameAsync(It.IsAny <string>(), It.IsAny <bool>(), false))
            .ReturnsAsync(participants);

            var guestSessions = new List <GuestSession>();

            project.GuestAccessCode = Guid.NewGuid().ToString();
            for (int i = 1; i <= guestSessionCount; i++)
            {
                var guestSession = GuestSession.Example();
                guestSession.ProjectId         = projectId;
                guestSession.ProjectAccessCode = project.GuestAccessCode;
                guestSession.GuestSessionState = GuestState.InProject;
                guestSession.CreatedDateTime   = DateTime.UtcNow;
                guestSession.UserId            = Guid.NewGuid();
                guestSessions.Add(guestSession);

                // Should never have more than one InProject sessions for same user and project,
                // but need to test LINQ query with group by, where, and order by clauses,
                // for correct calculation of current guest quantity.
                var guestSession2 = CloneGuestSession(guestSession);
                guestSession2.CreatedDateTime = DateTime.UtcNow.AddHours(-1.0);
                guestSessions.Add(guestSession2);

                var guestSession3 = CloneGuestSession(guestSession);
                guestSession3.CreatedDateTime = DateTime.UtcNow.AddHours(-2.0);
                guestSessions.Add(guestSession3);
            }

            _guestSessionRepositoryMock
            .Setup(m => m.GetItemsAsync(It.IsAny <Expression <Func <GuestSession, bool> > >(), It.IsAny <BatchOptions>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(guestSessions);

            _projectApi
            .Setup(m => m.GetProjectByIdAsync(It.IsAny <Guid>(), null))
            .ReturnsAsync(MicroserviceResponse.Create(HttpStatusCode.OK, project));

            _cacheMock
            .SetupSequence(m => m.ItemGetAsync(It.IsAny <List <string> >(), typeof(ProjectLobbyState)))
            .ReturnsAsync(new List <ProjectLobbyState>()
            {
                default(ProjectLobbyState)
            });

            _cacheMock
            .Setup(m => m.ItemSetAsync(It.IsAny <string>(), It.IsAny <ProjectLobbyState>(), It.IsAny <TimeSpan>(), It.IsAny <CacheCommandOptions>()))
            .Returns(Task.FromResult(ProjectLobbyState.Example()));

            var result = await _target.RecalculateProjectLobbyStateAsync(project.Id);

            Assert.IsAssignableFrom <ProjectLobbyState>(result);
            Assert.Equal(lobbyState, result.LobbyState);
        }