private async Task RunAsync(SurveyAnswerStoredMessage message)
        {
            if (!message.AppendedToAnswers)
            {
                await this.surveyAnswerStore.AppendSurveyAnswerIdToAnswersListAsync(
                    message.TenantId,
                    message.SurveySlugName,
                    message.SurveyAnswerBlobId);

                message.AppendedToAnswers = true;
                await message.UpdateQueueMessageAsync().ConfigureAwait(false);
            }

            var surveyAnswer = await this.surveyAnswerStore.GetSurveyAnswerAsync(
                message.TenantId,
                message.SurveySlugName,
                message.SurveyAnswerBlobId);

            var keyInCache = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", message.TenantId, message.SurveySlugName);
            TenantSurveyProcessingInfo surveyInfo;

            if (!this.tenantSurveyProcessingInfoCache.ContainsKey(keyInCache))
            {
                surveyInfo = new TenantSurveyProcessingInfo(message.TenantId, message.SurveySlugName);
                this.tenantSurveyProcessingInfoCache[keyInCache] = surveyInfo;
            }
            else
            {
                surveyInfo = this.tenantSurveyProcessingInfoCache[keyInCache];
            }

            surveyInfo.AnswersSummary.AddNewAnswer(surveyAnswer);
            surveyInfo.AnswersMessages.Add(message);
        }
        public void PostRunAddsTheAnswerIdToTheListInTheStore()
        {
            var mockTenantSurveyProcessingInfoCache = new Mock<IDictionary<string, TenantSurveyProcessingInfo>>();
            var mockUpdateableQueue = new Mock<IUpdateableAzureQueue>();
            var mockSurveyAnswerStore = new Mock<ISurveyAnswerStore>();
            var mockSurveyAnswersSummaryStore = new Mock<ISurveyAnswersSummaryStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, mockSurveyAnswersSummaryStore.Object);
            var message = new SurveyAnswerStoredMessage
            {
                Tenant = "tenant",
                SurveySlugName = "slug-name",
                SurveyAnswerBlobId = "id",
                AppendedToAnswers = false
            };
            message.SetUpdateableQueueReference(mockUpdateableQueue.Object);
            var tenantSurveyProcessingInfo = new TenantSurveyProcessingInfo("tenant", "slug-name");
            tenantSurveyProcessingInfo.AnswersMessages.Add(message);
            mockTenantSurveyProcessingInfoCache.Setup(c => c.Values).Returns(new[] { tenantSurveyProcessingInfo });

            command.PostRun();

            mockSurveyAnswerStore.Verify(
                r => r.AppendSurveyAnswerIdsToAnswersList("tenant", "slug-name", It.IsAny<IEnumerable<string>>()),
                Times.Once());
            mockUpdateableQueue.Verify(q => q.UpdateMessage(message), Times.Once());
        }
        public bool Run(SurveyAnswerStoredMessage message)
        {
            if (!message.AppendedToAnswers)
            {
                this.surveyAnswerStore.AppendSurveyAnswerIdToAnswersList(
                    message.Tenant,
                    message.SurveySlugName,
                    message.SurveyAnswerBlobId);
                message.AppendedToAnswers = true;
                message.UpdateQueueMessage();
            }

            var surveyAnswer = this.surveyAnswerStore.GetSurveyAnswer(
                message.Tenant,
                message.SurveySlugName,
                message.SurveyAnswerBlobId);

            var keyInCache = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", message.Tenant, message.SurveySlugName);
            TenantSurveyProcessingInfo surveyInfo;

            if (!this.tenantSurveyProcessingInfoCache.ContainsKey(keyInCache))
            {
                surveyInfo = new TenantSurveyProcessingInfo(message.Tenant, message.SurveySlugName);
                this.tenantSurveyProcessingInfoCache[keyInCache] = surveyInfo;
            }
            else
            {
                surveyInfo = this.tenantSurveyProcessingInfoCache[keyInCache];
            }

            surveyInfo.AnswersSummary.AddNewAnswer(surveyAnswer);
            surveyInfo.AnswersMessages.Add(message);

            return(false);   // won't remove the message from the queue
        }
        public void PostRunMergesTheSummaryToTheStore()
        {
            var mockTenantSurveyProcessingInfoCache = new Mock<IDictionary<string, TenantSurveyProcessingInfo>>();
            var mockSurveyAnswerStore = new Mock<ISurveyAnswerStore>();
            var mockSurveyAnswersSummaryStore = new Mock<ISurveyAnswersSummaryStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, mockSurveyAnswersSummaryStore.Object);
            var tenantSurveyProcessingInfo = new TenantSurveyProcessingInfo("tenant", "slug-name");
            mockTenantSurveyProcessingInfoCache.Setup(c => c.Values).Returns(new[] { tenantSurveyProcessingInfo });

            command.PostRun();

            mockSurveyAnswersSummaryStore.Verify(
                r => r.MergeSurveyAnswersSummary(It.Is<SurveyAnswersSummary>(s => s == tenantSurveyProcessingInfo.AnswersSummary)),
                Times.Once());
        }