/// <summary>
        /// Allocate Campaign based on userStorageMap, trafficAllocation by computing userHash for userId and provided CampaignTKey.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="userStorageMap"></param>
        /// <param name="campaignKey"></param>
        /// <param name="userId"></param>
        /// <param name="apiName">Api name which called this implementation, Activate/GetVariation/Track. This is for logging purpose.</param>
        /// <returns></returns>
        public BucketedCampaign Allocate(AccountSettings settings, UserStorageMap userStorageMap, string campaignKey, string userId, string apiName = null)
        {
            BucketedCampaign allocatedCampaign = null;
            BucketedCampaign requestedCampaign = settings.Campaigns.Find((campaign) => campaign.Key.Equals(campaignKey));

            if (requestedCampaign != null)
            {
                allocatedCampaign = AllocateCampaign(userId, campaignKey, userStorageMap, requestedCampaign);

                if (allocatedCampaign != null)
                {
                    if (allocatedCampaign.Status.Equals(Constants.Campaign.STATUS_RUNNING, System.StringComparison.InvariantCultureIgnoreCase))
                    {
                        LogInfoMessage.UserEligibilityForCampaign(file, userId, true);
                        return(allocatedCampaign);
                    }
                }
            }

            LogErrorMessage.CampaignNotRunning(file, campaignKey, apiName);

            LogInfoMessage.UserEligibilityForCampaign(file, userId, false);
            LogDebugMessage.UserNotPartOfCampaign(file, userId, campaignKey, nameof(Allocate));
            return(null);
        }
        public void SaveAndLoadInfoMessage()
        {
            var info       = new LogInfoMessage("A test info message");
            var data       = BinaryDataExtensions.SaveToMemoryStream(info);
            var loadedInfo = data.CreateFromMemoryStream() as LogInfoMessage;

            Assert.AreEqual(info.Text, loadedInfo.Text);
            Assert.AreEqual(info.TimeStamp, loadedInfo.TimeStamp);
        }
        /// <summary>
        /// Compute userHash and check for traffic allocation for given campaign.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="requestedCampaign"></param>
        /// <returns></returns>
        private BucketedCampaign AllocateByTrafficAllocation(string userId, BucketedCampaign requestedCampaign)
        {
            var selectedCampaign = requestedCampaign;
            var userHash         = this._userHasher.ComputeBucketValue(userId, Constants.Campaign.MAX_TRAFFIC_PERCENT, 1);

            if (requestedCampaign.PercentTraffic < userHash)
            {
                selectedCampaign = null;
                LogInfoMessage.AudienceConditionNotMet(file, userId);
            }
            return(selectedCampaign);
        }
        /// <summary>
        /// Allocate variation by checking UserProfileService, Campaign Traffic Allocation and compute UserHash to check variation allocation by bucketing.
        /// </summary>
        /// <param name="campaignTestKey"></param>
        /// <param name="userId"></param>
        /// <returns>
        /// If Variation is allocated, returns UserAssignedInfo with valid details, else return Empty UserAssignedInfo.
        /// </returns>
        private UserAllocationInfo AllocateVariation(string campaignTestKey, string userId, string apiName = null)
        {
            UserProfileMap   userProfileMap   = this._userProfileService.GetUserMap(campaignTestKey, userId);
            BucketedCampaign selectedCampaign = this._campaignAllocator.Allocate(this._settings, userProfileMap, campaignTestKey, userId, apiName);

            if (selectedCampaign != null)
            {
                Variation variation = this._variationAllocator.Allocate(userProfileMap, selectedCampaign, userId);
                if (variation != null)
                {
                    LogInfoMessage.VariationAllocated(file, userId, campaignTestKey, variation.Name);
                    LogDebugMessage.GotVariationForUser(file, userId, campaignTestKey, variation.Name, nameof(AllocateVariation));

                    this._userProfileService.SaveUserMap(userId, selectedCampaign.Key, variation.Name);
                    return(new UserAllocationInfo(variation, selectedCampaign));
                }
            }

            LogInfoMessage.NoVariationAllocated(file, userId, campaignTestKey);
            return(new UserAllocationInfo());
        }