public void HandleStatusResponseUpdatesCaptureStateToTrue()
        {
            // given
            var responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithCapture(true).Build();
            var response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var session = Substitute.For <ISessionInternals>();

            var target = CreateSendingContext().Build();

            target.DisableCaptureAndClear();
            target.AddSession(session);
            Assert.That(target.IsCaptureOn, Is.False);

            // when
            target.HandleStatusResponse(response);

            // then
            Assert.That(session.ReceivedCalls(), Is.Empty);
            Assert.That(target.IsCaptureOn, Is.True);
        }
        public void HandleStatusResponseRemovesFinishedSessionsIfResponseIsCaptureOff()
        {
            // given
            var responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithCapture(false).Build();
            var response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var sessionState = Substitute.For <ISessionState>();

            sessionState.IsFinished.Returns(true);
            var session = Substitute.For <ISessionInternals>();

            session.State.Returns(sessionState);

            var target = CreateSendingContext().Build();

            target.AddSession(session);

            // when
            target.HandleStatusResponse(response);

            // then
            Assert.That(target.SessionCount, Is.EqualTo(0));
            session.Received(1).ClearCapturedData();
        }
        public void HandleStatusResponseUpdatesSendInterval()
        {
            // given
            const int sendInterval       = 999;
            var       responseAttributes = ResponseAttributes.WithUndefinedDefaults()
                                           .WithSendIntervalInMilliseconds(sendInterval).Build();
            var response = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var session = Substitute.For <ISessionInternals>();

            var target = CreateSendingContext().Build();

            target.AddSession(session);
            Assert.That(target.SendInterval, Is.Not.EqualTo(sendInterval));

            // then
            target.HandleStatusResponse(response);
            var obtained = target.SendInterval;

            // then
            Assert.That(session.ReceivedCalls(), Is.Empty);
            Assert.That(obtained, Is.EqualTo(sendInterval));
        }
        public void HandleStatusResponseUpdatesHttpClientConfig()
        {
            mockHttpClientConfig.BaseUrl.Returns("https://localhost:9999/1");
            mockHttpClientConfig.ApplicationId.Returns("some cryptic appId");
            mockHttpClientConfig.ServerId.Returns(42);
            mockHttpClientConfig.SslTrustManager.Returns(Substitute.For <ISSLTrustManager>());

            const int serverId           = 73;
            var       responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithServerId(serverId).Build();
            var       response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var target = Substitute.ForPartsOf <BeaconSendingContext>(
                mockLogger,
                mockHttpClientConfig,
                mockHttpClientProvider,
                mockTimingProvider,
                mockThreadSuspender
                );

            Assert.That(mockHttpClientConfig.ReceivedCalls(), Is.Empty);

            // when
            target.HandleStatusResponse(response);

            // then
            target.Received(1).CreateHttpClientConfigurationWith(serverId);
            _ = mockHttpClientConfig.Received(2).ServerId;
            _ = mockHttpClientConfig.Received(1).BaseUrl;
            _ = mockHttpClientConfig.Received(1).ApplicationId;
            _ = mockHttpClientConfig.Received(1).SslTrustManager;

            // and when
            IHttpClientConfiguration configCapture = null;

            mockHttpClientProvider.CreateClient(Arg.Do <IHttpClientConfiguration>(c => configCapture = c));

            target.GetHttpClient();

            // then
            Assert.That(configCapture, Is.Not.Null);
            mockHttpClientProvider.Received(1).CreateClient(configCapture);

            Assert.That(configCapture, Is.Not.SameAs(mockHttpClientConfig));
            Assert.That(configCapture.ServerId, Is.EqualTo(serverId));
            Assert.That(configCapture.BaseUrl, Is.EqualTo(mockHttpClientConfig.BaseUrl));
            Assert.That(configCapture.ApplicationId, Is.EqualTo(mockHttpClientConfig.ApplicationId));
            Assert.That(configCapture.SslTrustManager, Is.SameAs(mockHttpClientConfig.SslTrustManager));
        }
        public void ApplicationIdMatchesIfApplicationIdWasNotReceived()
        {
            // given
            mockHttpClientConfig.ApplicationId.Returns("application id");
            var attributes = ResponseAttributes.WithUndefinedDefaults().Build();

            var target = CreateSendingContext().Build();

            // when
            var obtained = target.IsApplicationIdMismatch(attributes);

            // then
            Assert.That(obtained, Is.False);
        }
        public void ConfigurationTimestampReturnsValueFromResponseAttributes()
        {
            // given
            const long timestamp  = 1234;
            var        attributes = ResponseAttributes.WithUndefinedDefaults().WithTimestampInMilliseconds(timestamp).Build();
            var        response   =
                StatusResponse.CreateSuccessResponse(mockLogger, attributes, 200,
                                                     new Dictionary <string, List <string> >());
            var target = CreateSendingContext().Build();

            // when
            target.UpdateFrom(response);

            // then
            Assert.That(target.ConfigurationTimestamp, Is.EqualTo(timestamp));
        }
        public void ApplicationIdMismatchesIfStoredAndReceivedApplicationIdsAreNotEqual()
        {
            // given
            const string applicationId = "application id";

            mockHttpClientConfig.ApplicationId.Returns("application ID");
            var attributes = ResponseAttributes.WithUndefinedDefaults()
                             .WithApplicationId(applicationId)
                             .Build();

            var target = CreateSendingContext().Build();

            // when
            var obtained = target.IsApplicationIdMismatch(attributes);

            // then
            Assert.That(obtained, Is.True);
        }
Beispiel #8
0
        internal BeaconSendingContext(
            ILogger logger,
            IHttpClientConfiguration httpClientConfiguration,
            IHttpClientProvider httpClientProvider,
            ITimingProvider timingProvider,
            IInterruptibleThreadSuspender threadSuspender,
            AbstractBeaconSendingState initialState
            )
        {
            this.logger = logger;
            this.httpClientConfiguration = httpClientConfiguration;
            serverConfiguration          = ServerConfiguration.Default;
            HttpClientProvider           = httpClientProvider;
            this.timingProvider          = timingProvider;
            this.threadSuspender         = threadSuspender;
            lastResponseAttributes       = ResponseAttributes.WithUndefinedDefaults().Build();

            CurrentState = initialState;
        }
        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 UpdateFromDisablesCapturingIfReceivedApplicationIdMismatches()
        {
            // given
            mockHttpClientConfig.ApplicationId.Returns("some application id");
            var attributes = ResponseAttributes.WithUndefinedDefaults()
                             .WithApplicationId("different application id")
                             .Build();
            var response = Substitute.For <IStatusResponse>();

            response.ResponseAttributes.Returns(attributes);
            response.IsErroneousResponse.Returns(false);

            var target           = CreateSendingContext().Build();
            var initialCaptureOn = target.IsCaptureOn;

            // when
            target.UpdateFrom(response);

            // then
            Assert.That(initialCaptureOn, Is.True);
            Assert.That(target.IsCaptureOn, Is.False);
        }
        public void UpdateFromMergesResponseAttributesFromStatusResponse()
        {
            // given
            const int serverId   = 9999;
            var       attributes = ResponseAttributes.WithUndefinedDefaults().WithServerId(serverId).Build();
            var       response   = Substitute.For <IStatusResponse>();

            response.ResponseAttributes.Returns(attributes);
            response.IsErroneousResponse.Returns(false);

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

            // when
            var obtained = target.UpdateFrom(response);

            // then
            Assert.That(obtained, Is.EqualTo(target.LastResponseAttributes));
            Assert.That(obtained, Is.Not.EqualTo(initialAttributes));
            Assert.That(obtained, Is.Not.EqualTo(attributes));
            Assert.That(obtained.ServerId, Is.EqualTo(serverId));
        }
        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));
        }
 public AlexaResponse()
 {
     Version  = "1.0";
     Session  = new SessionAttributes();
     Response = new ResponseAttributes();
 }