public void NewSessionRequestsAreMadeForAllNotConfiguredSessions()
        {
            // given
            const int multiplicity = 5;
            var       target       = new BeaconSendingCaptureOnState();

            var successResponse = StatusResponse.CreateSuccessResponse(mockLogger,
                                                                       ResponseAttributes.WithJsonDefaults().WithMultiplicity(multiplicity).Build(), 200,
                                                                       new Dictionary <string, List <string> >());

            var mockClient = Substitute.For <IHttpClient>();

            mockContext.GetHttpClient().Returns(mockClient);
            mockContext.GetAllNotConfiguredSessions()
            .Returns(new List <ISessionInternals> {
                mockSession5New, mockSession6New
            });
            mockContext.UpdateFrom(Arg.Any <StatusResponse>()).Returns(successResponse.ResponseAttributes);
            mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>())
            .Returns(successResponse,
                     StatusResponse.CreateErrorResponse(mockLogger, StatusResponse.HttpBadRequest));
            mockSession5New.CanSendNewSessionRequest.Returns(true);
            mockSession6New.CanSendNewSessionRequest.Returns(true);

            IServerConfiguration serverConfigCapture = null;

            mockSession5New.UpdateServerConfiguration(Arg.Do <IServerConfiguration>(c => serverConfigCapture = c));

            // when
            target.Execute(mockContext);

            // verify for both new sessions a new session request has been made
            mockClient.Received(2).SendNewSessionRequest(mockContext);

            // verify first new session has been updated
            Assert.That(serverConfigCapture, Is.Not.Null);
            mockSession5New.Received(1).UpdateServerConfiguration(serverConfigCapture);
            Assert.That(serverConfigCapture.Multiplicity, Is.EqualTo(multiplicity));

            // verify second new session decreased number of retries
            mockSession6New.Received(1).DecreaseNumRemainingSessionRequests();
        }
        public void CaptureIsDisabledIfNoFurtherNewSessionRequestsAreAllowed()
        {
            // given
            var target = new BeaconSendingCaptureOnState();

            var successResponse = StatusResponse.CreateSuccessResponse(
                mockLogger,
                ResponseAttributes.WithJsonDefaults().WithMultiplicity(5).Build(),
                200,
                new Dictionary <string, List <string> >()
                );

            var mockClient = Substitute.For <IHttpClient>();

            mockContext.GetHttpClient().Returns(mockClient);
            mockContext.GetAllNotConfiguredSessions()
            .Returns(new List <ISessionInternals> {
                mockSession5New, mockSession6New
            });
            mockClient.SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>())
            .Returns(
                successResponse,
                StatusResponse.CreateErrorResponse(mockLogger, StatusResponse.HttpBadRequest)
                );
            mockSession5New.CanSendNewSessionRequest.Returns(false);
            mockSession6New.CanSendNewSessionRequest.Returns(false);

            // when
            target.Execute(mockContext);

            // verify for no session a new session request has been made
            mockClient.Received(0).SendNewSessionRequest(Arg.Any <IAdditionalQueryParameters>());

            // verify both sessions disabled capture
            mockSession5New.Received(1).DisableCapture();
            mockSession6New.Received(1).DisableCapture();
        }
        public void HandleStatusResponseMergesLastStatusResponse()
        {
            // given
            const int beaconSize         = 1234;
            var       responseAttributes = ResponseAttributes.WithJsonDefaults().WithMaxBeaconSizeInBytes(beaconSize).Build();
            var       response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var target            = CreateSendingContext().Build();
            var initialAttributes = target.LastResponseAttributes;

            // when
            target.HandleStatusResponse(response);
            var obtained = target.LastResponseAttributes;

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(initialAttributes, Is.Not.EqualTo(obtained));
            Assert.That(obtained.MaxBeaconSizeInBytes, Is.EqualTo(beaconSize));
        }