예제 #1
0
        public void LookupVisitorTest()
        {
            var visitorCache = new Mock <IVisitorCacheImplementation>();
            var visitorId    = visitorDelegate.VisitorId;

            visitorDelegate.VisitorCache = null;
            visitorDelegate.Config.VisitorCacheImplementation = visitorCache.Object;

            var defaultStrategy = new DefaultStrategy(visitorDelegate);

            ICollection <Campaign> campaigns = new Collection <Campaign>()
            {
                new Campaign()
                {
                    Id        = "id",
                    Variation = new Variation
                    {
                        Id            = "varID",
                        Modifications = new Modifications
                        {
                            Type  = ModificationType.FLAG,
                            Value = new Dictionary <string, object>
                            {
                                ["key"] = "value"
                            }
                        },
                        Reference = false
                    },
                    Type             = "ab",
                    VariationGroupId = "varGroupId"
                }
            };

            var VisitorCacheCampaigns = new Collection <VisitorCacheCampaign>();

            foreach (var item in campaigns)
            {
                VisitorCacheCampaigns.Add(new VisitorCacheCampaign
                {
                    CampaignId       = item.Id,
                    VariationGroupId = item.VariationGroupId,
                    VariationId      = item.Variation.Id,
                    IsReference      = item.Variation.Reference,
                    Type             = item.Variation.Modifications.Type,
                    Activated        = false,
                    Flags            = item.Variation.Modifications.Value
                });
            }


            var failedData = new VisitorCacheDTOV1
            {
                Version = 1,
                Data    = new VisitorCacheData
                {
                    VisitorId   = "any",
                    AnonymousId = visitorDelegate.AnonymousId,
                    Consent     = visitorDelegate.HasConsented,
                    Context     = visitorDelegate.Context,
                    Campaigns   = VisitorCacheCampaigns
                }
            };

            visitorCache.Setup(x => x.LookupVisitor(visitorId)).Returns(Task.FromResult(JObject.FromObject(failedData)));

            defaultStrategy.LookupVisitor();

            fsLogManagerMock.Verify(x => x.Info(string.Format(VisitorStrategyAbstract.VISITOR_ID_MISMATCH_ERROR, "any", visitorId), "LookupVisitor"), Times.Once());

            Assert.IsNull(visitorDelegate.VisitorCache);

            var data = new VisitorCacheDTOV1
            {
                Version = 1,
                Data    = new VisitorCacheData
                {
                    VisitorId   = visitorDelegate.VisitorId,
                    AnonymousId = visitorDelegate.AnonymousId,
                    Consent     = visitorDelegate.HasConsented,
                    Context     = visitorDelegate.Context,
                    Campaigns   = VisitorCacheCampaigns
                }
            };

            var dataJson = JObject.FromObject(data);

            visitorCache.Setup(x => x.LookupVisitor(visitorId)).Returns(Task.FromResult(dataJson));

            defaultStrategy.LookupVisitor();

            Assert.IsNotNull(visitorDelegate.VisitorCache);

            Assert.AreEqual(JsonConvert.SerializeObject(visitorDelegate.VisitorCache.Data), JsonConvert.SerializeObject(data));

            var error = new Exception("LookupVisitor error");

            visitorCache.Setup(x => x.LookupVisitor(visitorId)).Throws(error);

            defaultStrategy.LookupVisitor();

            fsLogManagerMock.Verify(x => x.Error(error.Message, "LookupVisitor"), Times.Once());

            visitorCache.Setup(x => x.LookupVisitor(visitorId)).Returns(Task.FromResult(new JObject()));

            defaultStrategy.LookupVisitor();

            fsLogManagerMock.Verify(x => x.Error(VisitorStrategyAbstract.LOOKUP_VISITOR_JSON_OBJECT_ERROR, "LookupVisitor"), Times.Once());

            visitorCache.Setup(x => x.LookupVisitor(visitorId)).Returns(Task.FromResult <JObject>(null));

            defaultStrategy.LookupVisitor();
        }