public void CaptureMessageBody()
        {
            var trackingStream = new TrackingStream(FakeTextStream.Create(1024 * 1024));

            var messageMock = new Unit.Message.Mock <IBaseMessage> {
                DefaultValue = DefaultValue.Mock
            };

            messageMock.Object.BodyPart.Data = trackingStream;

            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(@"\\network\share");

            ClaimStore.Instance.SetupMessageBodyCapture(trackingStream, ActivityTrackingModes.Body, null);

            messageMock.Object.BodyPart.Data.Drain();

            // payload is claimed to disk and file extension is .trk
            var captureDescriptor = trackingStream.CaptureDescriptor;

            Assert.That(captureDescriptor.CaptureMode, Is.EqualTo(MessageBodyCaptureMode.Claimed));
            Assert.That(captureDescriptor.Data, Does.StartWith(DateTime.Today.ToString(@"yyyyMMdd\\")));
            Assert.That(File.Exists(Path.Combine(Path.GetTempPath(), captureDescriptor.Data.Replace("\\", "") + ".trk")), Is.True);
        }
        public void CaptureMessageBodyWithEmptyStreamWillHaveMessageUnclaimed()
        {
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(@"\\network\share");

            var trackingStreamMock = new Mock <TrackingStream>(new MemoryStream())
            {
                CallBase = true
            };

            ClaimStore.Instance.SetupMessageBodyCapture(trackingStreamMock.Object, ActivityTrackingModes.Body, null);

            trackingStreamMock.Verify(
                ts => ts.SetupCapture(It.Is <MessageBodyCaptureDescriptor>(cd => cd.CaptureMode == MessageBodyCaptureMode.Claimed)),
                Times.Never());
            trackingStreamMock.Verify(
                ts => ts.SetupCapture(It.Is <MessageBodyCaptureDescriptor>(cd => cd.CaptureMode == MessageBodyCaptureMode.Unclaimed)),
                Times.Once());
            trackingStreamMock.Verify(
                ts => ts.SetupCapture(It.Is <MessageBodyCaptureDescriptor>(cd => cd.CaptureMode == MessageBodyCaptureMode.Claimed), It.IsAny <Stream>()),
                Times.Never());
        }
        public void RequiresCheckInAndOutIsTrue()
        {
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(@"\\network\share");

            Assert.That(ClaimStore.RequiresCheckInAndOut);
        }
        public void RequiresCheckInAndOutIsCaseInsensitive()
        {
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath().ToUpper());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath().ToLower());

            Assert.That(ClaimStore.RequiresCheckInAndOut, Is.False);
        }
        public void RequiresCheckInAndOutIsTrailingBackslashInsensitive()
        {
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            // makes sure there is one trailing '\'
            .Returns(Path.GetTempPath().ToUpper().TrimEnd('\\') + '\\');
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            // makes sure there is no trailing '\'
            .Returns(Path.GetTempPath().ToLower().TrimEnd('\\'));

            Assert.That(ClaimStore.RequiresCheckInAndOut, Is.False);
        }
        public void CaptureMessageBodyWillHaveMessageClaimedButSsoApplicationDoesNotExist()
        {
            // setup a mock's callback to ensure that, even if the BizTalk.Factory SSO store is deployed, the call will look for an SSO store that does not exist
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Callback(() => _ssoSettingsReaderInstance.ReadString("NONEXISTENT_APPLICATION", BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());

            var trackingStreamMock = new Mock <TrackingStream>(FakeTextStream.Create(1024 * 1024))
            {
                CallBase = true
            };

            Assert.That(
                () => ClaimStore.Instance.SetupMessageBodyCapture(trackingStreamMock.Object, ActivityTrackingModes.Claim, null),
                Throws.TypeOf <COMException>().With.Message.EqualTo("The application does not exist.\r\n"));
        }
        public void ClaimMessageBody()
        {
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(@"\\network\share");

            using (var contentStream = FakeTextStream.Create(1024 * 1024))
                using (var trackingStream = new TrackingStream(contentStream))
                {
                    MessageMock.Object.BodyPart.Data = trackingStream;

                    ClaimStore.Instance.SetupMessageBodyCapture(trackingStream, ActivityTrackingModes.Claim, null);
                    ClaimStore.Instance.Claim(MessageMock.Object, ResourceTrackerMock.Object);

                    // message's actual body stream has been exhausted (i.e. saved to disk)
                    Assert.That(contentStream.Position, Is.EqualTo(contentStream.Length));

                    // message's body stream is replaced by a token message
                    using (var reader = new StreamReader(MessageMock.Object.BodyPart.Data))
                    {
                        Assert.That(reader.ReadToEnd(), Is.EqualTo(MessageFactory.CreateClaimCheckIn(trackingStream.CaptureDescriptor.Data).OuterXml));
                    }

                    // MessageType of token message is promoted in message context
                    var schemaMetadata = typeof(Claim.CheckIn).GetMetadata();
                    MessageMock.Verify(m => m.Promote(BtsProperties.MessageType, schemaMetadata.MessageType), Times.Once());
                    MessageMock.Verify(m => m.Promote(BtsProperties.SchemaStrongName, schemaMetadata.DocumentSpec.DocSpecStrongName), Times.Once());

                    // payload is claimed to disk and file extension is .chk
                    var captureDescriptor = trackingStream.CaptureDescriptor;
                    Assert.That(captureDescriptor.CaptureMode, Is.EqualTo(MessageBodyCaptureMode.Claimed));
                    Assert.That(captureDescriptor.Data, Does.StartWith(DateTime.Today.ToString(@"yyyyMMdd\\")));
                    Assert.That(File.Exists(Path.Combine(Path.GetTempPath(), captureDescriptor.Data.Replace("\\", "") + ".chk")), Is.True);
                }
        }
        public void RedeemClaimToken()
        {
            const string content = "dummy";
            const string url     = "cca95baa39ab4e25a3c54971ea170911";

            using (var file = File.CreateText(Path.Combine(Path.GetTempPath(), url)))
            {
                file.Write(content);
            }

            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_IN_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());
            SsoSettingsReaderMock
            .Setup(ssr => ssr.ReadString(BizTalkFactorySettings.APPLICATION_NAME, BizTalkFactorySettings.CLAIM_STORE_CHECK_OUT_DIRECTORY_PROPERTY_NAME))
            .Returns(Path.GetTempPath());

            using (var tokenStream = MessageFactory.CreateClaimCheck(url).AsStream())
            {
                MessageMock.Object.BodyPart.Data = tokenStream;

                ClaimStore.Instance.Redeem(MessageMock.Object, ResourceTrackerMock.Object);

                Assert.That(MessageMock.Object.BodyPart.Data, Is.TypeOf <TrackingStream>());
                // ReSharper disable once PossibleInvalidCastException
                var captureDescriptor = ((TrackingStream)MessageMock.Object.BodyPart.Data).CaptureDescriptor;
                Assert.That(captureDescriptor.CaptureMode, Is.EqualTo(MessageBodyCaptureMode.Claimed));
                // previously captured payload is reused and not captured/claimed anew
                Assert.That(captureDescriptor.Data, Is.EqualTo(url));
            }

            using (var reader = new StreamReader(MessageMock.Object.BodyPart.Data))
            {
                Assert.That(reader.ReadToEnd(), Is.EqualTo(content));
            }
        }