public void RunCreatesNewSummaryAndAddsItToCacheWhenNotFoundInCache()
        {
            var mockUpdateableQueue = new Mock <IUpdateableAzureQueue>();
            var mockTenantSurveyProcessingInfoCache = new Mock <IDictionary <string, TenantSurveyProcessingInfo> >();
            var mockSurveyAnswerStore = new Mock <ISurveyAnswerStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, new Mock <ISurveyAnswersSummaryStore>().Object);
            var message = new SurveyAnswerStoredMessage
            {
                Tenant            = "tenant",
                SurveySlugName    = "slug-name",
                AppendedToAnswers = false
            };

            message.SetUpdateableQueueReference(mockUpdateableQueue.Object);

            mockTenantSurveyProcessingInfoCache.Setup(c => c["tenant-slug-name"])
            .Returns(default(TenantSurveyProcessingInfo));

            command.Run(message);

            mockTenantSurveyProcessingInfoCache.VerifySet(
                c => c["tenant-slug-name"] = It.Is <TenantSurveyProcessingInfo>(s => s.AnswersSummary.Tenant == "tenant" && s.AnswersSummary.SlugName == "slug-name"),
                Times.Once());
            mockUpdateableQueue.Verify(q => q.UpdateMessage(message), Times.Once());
        }
        public void RunAddsTheSurveyAnswerToTheSummary()
        {
            var mockTenantSurveyProcessingInfo      = new Mock <TenantSurveyProcessingInfo>("tenant", "slug-name");
            var mockTenantSurveyProcessingInfoObj   = mockTenantSurveyProcessingInfo.Object;
            var mockTenantSurveyProcessingInfoCache = new Mock <IDictionary <string, TenantSurveyProcessingInfo> >();
            var mockSurveyAnswerStore = new Mock <ISurveyAnswerStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, new Mock <ISurveyAnswersSummaryStore>().Object);

            mockTenantSurveyProcessingInfoCache.Setup(c => c.ContainsKey("tenant-slug-name")).Returns(true);
            mockTenantSurveyProcessingInfoCache.Setup(c => c["tenant-slug-name"]).Returns(mockTenantSurveyProcessingInfoObj);
            var surveyAnswer = new SurveyAnswer()
            {
                Tenant = "tenant", SlugName = "slug-name"
            };

            mockSurveyAnswerStore.Setup(r => r.GetSurveyAnswer(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Returns(surveyAnswer);
            var message = new SurveyAnswerStoredMessage
            {
                Tenant            = "tenant",
                SurveySlugName    = "slug-name",
                AppendedToAnswers = true
            };

            command.Run(message);

            Assert.AreEqual(1, mockTenantSurveyProcessingInfoObj.AnswersSummary.TotalAnswers);
        }
        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 void RunGetsTheSurveyAnswersSummaryFromTheCache()
        {
            var mockTenantSurveyProcessingInfoCache = new Mock <IDictionary <string, TenantSurveyProcessingInfo> >();
            var mockSurveyAnswerStore = new Mock <ISurveyAnswerStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, new Mock <ISurveyAnswersSummaryStore>().Object);

            mockTenantSurveyProcessingInfoCache.Setup(c => c.ContainsKey("tenant-slug-name")).Returns(true);
            mockTenantSurveyProcessingInfoCache.Setup(c => c["tenant-slug-name"]).Returns(new TenantSurveyProcessingInfo("tenant", "slug-name"));
            var message = new SurveyAnswerStoredMessage
            {
                Tenant            = "tenant",
                SurveySlugName    = "slug-name",
                AppendedToAnswers = false
            };

            command.Run(message);

            mockTenantSurveyProcessingInfoCache.Verify(c => c["tenant-slug-name"], Times.Once());
        }
        public void RunGetsTheSurveyAnswerFromTheStore()
        {
            var mockTenantSurveyProcessingInfoCache = new Mock <IDictionary <string, TenantSurveyProcessingInfo> >();
            var mockSurveyAnswerStore = new Mock <ISurveyAnswerStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, new Mock <ISurveyAnswersSummaryStore>().Object);

            mockTenantSurveyProcessingInfoCache.Setup(c => c["tenant-slug-name"])
            .Returns(new TenantSurveyProcessingInfo("tenant", "slug-name"));
            var message = new SurveyAnswerStoredMessage
            {
                Tenant             = "tenant",
                SurveySlugName     = "slug-name",
                SurveyAnswerBlobId = "id",
                AppendedToAnswers  = true
            };

            command.Run(message);

            mockSurveyAnswerStore.Verify(r => r.GetSurveyAnswer("tenant", "slug-name", "id"));
        }
        public void RunAddTheAnswerIdToTheListInTheStore()
        {
            var mockUpdateableQueue = new Mock <IUpdateableAzureQueue>();
            var mockTenantSurveyProcessingInfoCache = new Mock <IDictionary <string, TenantSurveyProcessingInfo> >();
            var mockSurveyAnswerStore = new Mock <ISurveyAnswerStore>();
            var command = new UpdatingSurveyResultsSummaryCommand(mockTenantSurveyProcessingInfoCache.Object, mockSurveyAnswerStore.Object, new Mock <ISurveyAnswersSummaryStore>().Object);
            var message = new SurveyAnswerStoredMessage
            {
                Tenant             = "tenant",
                SurveySlugName     = "slug-name",
                SurveyAnswerBlobId = "id",
                AppendedToAnswers  = false
            };

            message.SetUpdateableQueueReference(mockUpdateableQueue.Object);

            command.Run(message);

            mockSurveyAnswerStore.Verify(r => r.AppendSurveyAnswerIdToAnswersList("tenant", "slug-name", "id"));
            mockUpdateableQueue.Verify(q => q.UpdateMessage(message), Times.Once());
        }