示例#1
0
        public async Task GetProjectLobbyStateAsync_RetrievesLobbyState()
        {
            var state = ProjectLobbyState.Example();

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

            var result = await _target.GetProjectLobbyStateAsync(Guid.NewGuid());

            Assert.IsAssignableFrom <ProjectLobbyState>(result);
            Assert.Equal(state, result);
        }
        /// <inheritdoc />
        public ProjectLobbyStateModule(IMetadataRegistry metadataRegistry,
                                       IPolicyEvaluator policyEvaluator,
                                       ILoggerFactory loggerFactory, IProjectLobbyStateController projectLobbyStateController) :
            base(GuestServiceBootstrapper.ServiceNameShort, metadataRegistry, policyEvaluator, loggerFactory)
        {
            _projectLobbyStateController = projectLobbyStateController;

            CreateRoute("GetProjectLobbyState", HttpMethod.Get, $"{Routing.ProjectsRoute}/{{projectId:guid}}/{Routing.ProjectLobbyStatePath}", GetProjectLobbyStateAsync)
            .Description("Retrieves lobby state for a project.")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError)
            .ResponseFormat(JsonConvert.SerializeObject(new ProjectLobbyState()));

            CreateRoute("RecalculateProjectLobbyState", HttpMethod.Put, $"{Routing.ProjectsRoute}/{{projectId:guid}}/{Routing.ProjectLobbyStatePath}", RecalculateProjectLobbyStateAsync)
            .Description("Recalculates the the lobby state of a project.")
            .StatusCodes(HttpStatusCode.OK, HttpStatusCode.BadRequest, HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden, HttpStatusCode.NotFound, HttpStatusCode.InternalServerError)
            .ResponseFormat(ProjectLobbyState.Example());
        }
示例#3
0
        public async Task RecalculateProjectLobbyStateAsync_IfNotFoundAndProjectExists_CreatesAndReturnsLobbyState()
        {
            SetupApisForRecalculate();

            var stateRef = new Reference <ProjectLobbyState> {
                Value = ProjectLobbyState.Example()
            };

            _cacheMock
            .SetupSequence(m => m.TryItemGetAsync(It.IsAny <string>(), typeof(ProjectLobbyState), stateRef))
            .ReturnsAsync(false)
            .ReturnsAsync(true);

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

            var result = await _target.RecalculateProjectLobbyStateAsync(Guid.NewGuid());

            Assert.IsAssignableFrom <ProjectLobbyState>(result);
        }
示例#4
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);
        }
示例#5
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);
        }