Beispiel #1
0
        public void PropertieTest()
        {
            var config = new Flagship.Config.DecisionApiConfig();

            visitorDelegateMock.SetupGet(x => x.Config).Returns(config);
            Assert.AreEqual(config, Visitor.Config);

            var anonymousId = "anonymousId";

            visitorDelegateMock.SetupGet(x => x.AnonymousId).Returns(anonymousId);
            Assert.AreEqual(anonymousId, Visitor.AnonymousId);

            var context = new Dictionary <string, object>();

            visitorDelegateMock.SetupGet(x => x.Context).Returns(context);
            Assert.AreEqual(context, Visitor.Context);

            var flags = new Collection <Flagship.Model.FlagDTO>();

            visitorDelegateMock.SetupGet(x => x.Flags).Returns(flags);
            Assert.AreEqual(flags, Visitor.Flags);

            visitorDelegateMock.SetupGet(x => x.HasConsented).Returns(true);
            Assert.AreEqual(true, Visitor.HasConsented);

            Visitor.VisitorId = "newvisitorId";

            Assert.AreEqual(visitorDelegateMock.Object.VisitorId, Visitor.VisitorId);
        }
Beispiel #2
0
        public async Task GetCampaignsTest()
        {
            var config = new Flagship.Config.DecisionApiConfig()
            {
                EnvId = "envID"
            };
            HttpResponseMessage httpResponse = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent(GetCampaigns(), Encoding.UTF8, "application/json")
            };

            var url = $"{Constants.BASE_API_URL}{config.EnvId}/campaigns?exposeAllKeys=true&{Constants.SEND_CONTEXT_EVENT}=false";

            Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>();

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri.ToString() == url),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse);

            var httpClient          = new HttpClient(mockHandler.Object);
            var trackingManagerMock = new Mock <Flagship.Api.ITrackingManager>();
            var decisionManagerMock = new Mock <Flagship.Decision.IDecisionManager>();
            var configManager       = new Flagship.Config.ConfigManager(config, decisionManagerMock.Object, trackingManagerMock.Object);

            var context = new Dictionary <string, object>();

            var visitorDelegate = new Flagship.FsVisitor.VisitorDelegate("visitorId", false, context, false, configManager);

            var decisionManager = new Flagship.Decision.ApiManager(config, httpClient);

            decisionManager.StatusChange += DecisionManager_StatusChange1;

            Collection <Flagship.Model.Campaign> campaigns = (Collection <Model.Campaign>) await decisionManager.GetCampaigns(visitorDelegate).ConfigureAwait(false);

            Collection <Flagship.Model.FlagDTO> flags = (Collection <Model.FlagDTO>) await decisionManager.GetFlags(campaigns).ConfigureAwait(false);

            Assert.AreEqual(campaigns.Count, 3);
            Assert.AreEqual(campaigns[0].Id, "c3ev1afkprbg5u3burag");
            Assert.AreEqual(campaigns[2].Id, "c20j8bk3fk9hdphqtd1g");

            Assert.AreEqual(flags.Count, 6);
            Assert.AreEqual(flags[0].Key, "array");
            Assert.AreEqual(flags[5].Key, "my_text");

            httpClient.Dispose();
            httpResponse.Dispose();
        }
Beispiel #3
0
        public VisitorDelegateTests()
        {
            var config = new Flagship.Config.DecisionApiConfig();

            configManager = new Mock <Flagship.Config.IConfigManager>();
            configManager.SetupGet(x => x.Config).Returns(config);

            visitorDelegateMock = new Mock <VisitorDelegate>(new object[] { visitorId, false, context, false, configManager.Object });
            visitorDelegateMock.Setup(x => x.GetStrategy()).CallBase();
            defaultStrategy = new Mock <VisitorStrategyAbstract>(visitorDelegateMock.Object);

            visitorDelegateMock.Setup <VisitorStrategyAbstract>(x => x.GetStrategy()).Returns(defaultStrategy.Object);
            visitorDelegateMock.CallBase = true;
        }
Beispiel #4
0
        public async Task GetCampaignsTestFailTest()
        {
            var fsLogManagerMock = new Mock <IFsLogManager>();


            var config = new Flagship.Config.DecisionApiConfig()
            {
                EnvId      = "envID",
                LogManager = fsLogManagerMock.Object,
            };

            var responseContent = "Error";

            HttpResponseMessage httpResponse = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.BadRequest,
                Content    = new StringContent(responseContent, Encoding.UTF8, "application/json")
            };

            var url = $"{Constants.BASE_API_URL}{config.EnvId}/campaigns?exposeAllKeys=true&{Constants.SEND_CONTEXT_EVENT}=false";

            Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>();

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri.ToString() == url),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse);

            var httpClient          = new HttpClient(mockHandler.Object);
            var trackingManagerMock = new Mock <Flagship.Api.ITrackingManager>();
            var decisionManagerMock = new Mock <Flagship.Decision.IDecisionManager>();
            var configManager       = new Flagship.Config.ConfigManager(config, decisionManagerMock.Object, trackingManagerMock.Object);

            var context = new Dictionary <string, object>();

            var visitorDelegate = new Flagship.FsVisitor.VisitorDelegate("visitorId", false, context, false, configManager);

            var decisionManager = new Flagship.Decision.ApiManager(config, httpClient);

            var campaigns = await decisionManager.GetCampaigns(visitorDelegate).ConfigureAwait(false);

            Assert.AreEqual(campaigns.Count, 0);

            fsLogManagerMock.Verify(x => x.Error("Bad Request", "GetCampaigns"), Times.Once());

            httpClient.Dispose();
            httpResponse.Dispose();
        }
Beispiel #5
0
        public async Task GetCampaignsPanicModeTest()
        {
            var config = new Flagship.Config.DecisionApiConfig()
            {
                EnvId = "envID"
            };
            HttpResponseMessage httpResponse = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("{'visitorId':'anonymeId','campaigns':[],'panic':true}", Encoding.UTF8, "application/json")
            };

            var url = $"{Constants.BASE_API_URL}{config.EnvId}/campaigns?exposeAllKeys=true&{Constants.SEND_CONTEXT_EVENT}=false";

            Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>();

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post && req.RequestUri.ToString() == url),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse);

            var httpClient          = new HttpClient(mockHandler.Object);
            var trackingManagerMock = new Mock <Flagship.Api.ITrackingManager>();
            var decisionManagerMock = new Mock <Flagship.Decision.IDecisionManager>();
            var configManager       = new Flagship.Config.ConfigManager(config, decisionManagerMock.Object, trackingManagerMock.Object);

            var context = new Dictionary <string, object>();

            var visitorDelegate = new Flagship.FsVisitor.VisitorDelegate("visitorId", false, context, false, configManager);

            var decisionManager = new Flagship.Decision.ApiManager(config, httpClient);

            decisionManager.StatusChange += DecisionManager_StatusChange;

            var campaigns = await decisionManager.GetCampaigns(visitorDelegate).ConfigureAwait(false);

            Assert.AreEqual(campaigns.Count, 0);

            Assert.IsTrue(decisionManager.IsPanic);

            httpClient.Dispose();
            httpResponse.Dispose();
        }
Beispiel #6
0
        public NoConsentStrategyTests()
        {
            fsLogManagerMock = new Mock <IFsLogManager>();
            config           = new Flagship.Config.DecisionApiConfig()
            {
                EnvId      = "envID",
                LogManager = fsLogManagerMock.Object,
            };
            trackingManagerMock = new Mock <Flagship.Api.ITrackingManager>();
            decisionManagerMock = new Mock <Flagship.Decision.DecisionManager>(new object[] { null, null });
            var configManager = new Flagship.Config.ConfigManager(config, decisionManagerMock.Object, trackingManagerMock.Object);

            var context = new Dictionary <string, object>()
            {
                ["key0"] = 1,
            };

            visitorDelegate = new Flagship.FsVisitor.VisitorDelegate("visitorId", false, context, false, configManager);
        }
Beispiel #7
0
        public async Task SendHit()
        {
            HttpResponseMessage httpResponse = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("", Encoding.UTF8, "application/json")
            };

            Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>();

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse);


            var fsLogManagerMock = new Mock <IFsLogManager>();

            var config = new Flagship.Config.DecisionApiConfig
            {
                LogManager = fsLogManagerMock.Object,
            };

            var httpClient      = new HttpClient(mockHandler.Object);
            var trackingManager = new Flagship.Api.TrackingManager(config, httpClient);


            var hit = new Hit.Screen("Screen")
            {
                Config = config,
            };

            await trackingManager.SendHit(hit).ConfigureAwait(false);

            await trackingManager.SendHit(hit).ConfigureAwait(false);

            mockHandler.Protected().Verify("SendAsync", Times.Exactly(2), new object[] { ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() });

            httpResponse.Dispose();
            httpClient.Dispose();
        }
Beispiel #8
0
        public async Task SendActive()
        {
            HttpResponseMessage httpResponse = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("", Encoding.UTF8, "application/json")
            };

            Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>();

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse);


            var fsLogManagerMock = new Mock <IFsLogManager>();

            var config = new Flagship.Config.DecisionApiConfig
            {
                ApiKey     = "apiKey",
                EnvId      = "envId",
                LogManager = fsLogManagerMock.Object,
            };

            var httpClient      = new HttpClient(mockHandler.Object);
            var trackingManager = new Flagship.Api.TrackingManager(config, httpClient);

            var decisionManagerMock = new Mock <Flagship.Decision.IDecisionManager>();
            var configManager       = new Flagship.Config.ConfigManager(config, decisionManagerMock.Object, trackingManager);

            var context = new Dictionary <string, object>();

            var visitorDelegate = new Flagship.FsVisitor.VisitorDelegate("visitorId", false, context, true, configManager);

            var flag = new Flagship.Model.FlagDTO()
            {
                VariationGroupId = "varGroupID",
                VariationId      = "varID",
            };

            var postData = new Dictionary <string, object>
            {
                [Constants.VISITOR_ID_API_ITEM]         = visitorDelegate.VisitorId,
                [Constants.VARIATION_ID_API_ITEM]       = flag.VariationId,
                [Constants.VARIATION_GROUP_ID_API_ITEM] = flag.VariationGroupId,
                [Constants.CUSTOMER_ENV_ID_API_ITEM]    = config.EnvId,
                [Constants.ANONYMOUS_ID] = null
            };



            Func <HttpRequestMessage, bool> action = (HttpRequestMessage x) => {
                var postDataString = JsonConvert.SerializeObject(postData);
                var headers        = new HttpRequestMessage().Headers;
                headers.Add(Constants.HEADER_X_API_KEY, config.ApiKey);
                headers.Add(Constants.HEADER_X_SDK_CLIENT, Constants.SDK_LANGUAGE);
                headers.Add(Constants.HEADER_X_SDK_VERSION, Constants.SDK_VERSION);
                headers.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.HEADER_APPLICATION_JSON));

                var result = x.Content.ReadAsStringAsync().Result;
                return(result == postDataString && headers.ToString() == x.Headers.ToString() && x.Method == HttpMethod.Post);
            };

            var errorSendAsyn = new Exception("Error Send");

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(x => action(x)),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse).Verifiable();

            await trackingManager.SendActive(visitorDelegate, flag).ConfigureAwait(false);

            // XC test

            mockHandler.Protected().Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(x => action(x)),
                ItExpr.IsAny <CancellationToken>()
                ).ReturnsAsync(httpResponse).Verifiable();


            var newVisitorId = "newVisitorId";

            visitorDelegate.Authenticate(newVisitorId);

            postData[Constants.VISITOR_ID_API_ITEM] = visitorDelegate.VisitorId;
            postData[Constants.ANONYMOUS_ID]        = visitorDelegate.AnonymousId;


            await trackingManager.SendActive(visitorDelegate, flag).ConfigureAwait(false);

            mockHandler.Verify();

            httpResponse.Dispose();
            httpClient.Dispose();
        }