Example #1
0
        public void VisitorCacheTest()
        {
            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>();

            var variationHistory = new Dictionary <string, string>
            {
                ["varGrID"] = "varID"
            };

            foreach (var item in campaigns)
            {
                variationHistory[item.VariationGroupId] = item.Variation.Id;

                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 data = new VisitorCacheDTOV1
            {
                Version = 1,
                Data    = new VisitorCacheData
                {
                    VisitorId          = visitorDelegate.VisitorId,
                    AnonymousId        = visitorDelegate.AnonymousId,
                    Consent            = visitorDelegate.HasConsented,
                    Context            = visitorDelegate.Context,
                    Campaigns          = VisitorCacheCampaigns,
                    AssignmentsHistory = variationHistory
                }
            };



            visitorDelegate.VisitorCache = new VisitorCache
            {
                Version = 1,
                Data    = new VisitorCacheDTOV1
                {
                    Version = 1,
                    Data    = new VisitorCacheData
                    {
                        VisitorId          = visitorDelegate.VisitorId,
                        AnonymousId        = visitorDelegate.AnonymousId,
                        Consent            = visitorDelegate.HasConsented,
                        Context            = visitorDelegate.Context,
                        AssignmentsHistory = new Dictionary <string, string>
                        {
                            ["varGrID"] = "varID"
                        }
                    }
                }
            };

            var dataJson = JObject.FromObject(data);

            var visitorCache = new Mock <IVisitorCacheImplementation>();
            var visitorId    = visitorDelegate.VisitorId;

            visitorDelegate.Config.VisitorCacheImplementation = visitorCache.Object;

            visitorDelegate.Campaigns = campaigns;

            var defaultStrategy = new DefaultStrategy(visitorDelegate);

            defaultStrategy.CacheVisitorAsync();

            visitorCache.Verify(x => x.CacheVisitor(visitorId, It.Is <JObject>(y => y.ToString() == dataJson.ToString())), Times.Once);

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

            visitorCache.Setup(x => x.CacheVisitor(It.IsAny <string>(), It.IsAny <JObject>())).Throws(error);

            defaultStrategy.CacheVisitorAsync();

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