public void Dispatch_HttpRequest_BodyEmpty()
        {
            // arrange
            int          callCount = 0;
            AttachmentId id        = AttachmentId.NewId();
            Mock <ITelemetryAttachmentTransmitter> transmitter = new Mock <ITelemetryAttachmentTransmitter>();

            transmitter
            .Setup(t => t.Enqueue(It.IsAny <AttachmentDescriptor>()))
            .Callback((AttachmentDescriptor d) =>
            {
                if (d.Id == id)
                {
                    Interlocked.Increment(ref callCount);
                }
            });

            HashSet <ITelemetryAttachmentTransmitter> transmitters = new HashSet <ITelemetryAttachmentTransmitter>
            {
                transmitter.Object
            };
            AttachmentDispatcher dispatcher = new AttachmentDispatcher(transmitters);
            string      payloadName         = "Name-4141";
            HttpRequest payloadValue        = new HttpRequest();

            // act
            Action verify = () => dispatcher.Dispatch(id, payloadName, payloadValue);

            // assert
            Assert.Null(Record.Exception(verify));
            Assert.Equal(1, callCount);
        }
        public void Detach_Success()
        {
            // arrange
            int callCount = 0;
            Mock <IAttachment> attachment = new Mock <IAttachment>();
            Mock <ITelemetryAttachmentTransmitter> transmitter = new Mock <ITelemetryAttachmentTransmitter>();

            transmitter
            .Setup(t => t.Enqueue(It.IsAny <AttachmentDescriptor>()))
            .Callback(() => Interlocked.Increment(ref callCount));

            HashSet <ITelemetryAttachmentTransmitter> transmitters = new HashSet <ITelemetryAttachmentTransmitter>
            {
                transmitter.Object
            };
            AttachmentDispatcher dispatcher = new AttachmentDispatcher(transmitters);

            // act
            dispatcher.Detach(transmitter.Object);

            // assert
            dispatcher.Dispatch(attachment.Object);

            Assert.Equal(0, callCount);
        }
        public void Detach_TransmitterNull()
        {
            // arrange
            ITelemetryAttachmentTransmitter           transmitter  = null;
            HashSet <ITelemetryAttachmentTransmitter> transmitters = new HashSet <ITelemetryAttachmentTransmitter>();
            AttachmentDispatcher dispatcher = new AttachmentDispatcher(transmitters);

            // act
            Action validate = () => dispatcher.Detach(transmitter);

            // assert
            Assert.Throws <ArgumentNullException>("transmitter", validate);
        }
        public void Dispatch_AttachmentsEmpty()
        {
            // arrange
            IAttachment[] attachments = new IAttachment[0];
            HashSet <ITelemetryAttachmentTransmitter> transmitters = new HashSet <ITelemetryAttachmentTransmitter>();
            AttachmentDispatcher dispatcher = new AttachmentDispatcher(transmitters);

            // act
            Action validate = () => dispatcher.Dispatch(attachments);

            // assert
            AssertEx.NotThrow(validate);
        }
        public void Dispatch_AttachmentsEmpty()
        {
            // arrange
            IAttachment[] attachments = new IAttachment[0];
            HashSet <ITelemetryAttachmentTransmitter> transmitters = new HashSet <ITelemetryAttachmentTransmitter>();
            AttachmentDispatcher dispatcher = new AttachmentDispatcher(transmitters);

            // act
            Action validate = () => dispatcher.Dispatch(attachments);

            // assert
            Assert.Throws <ArgumentOutOfRangeException>("attachments", validate);
        }
        public void Dispatch_HttpRequest_PayloadValueNull()
        {
            // arrange
            AttachmentId         id         = AttachmentId.NewId();
            AttachmentDispatcher dispatcher = AttachmentDispatcher.Instance;
            string      payloadName         = "Name-5151";
            HttpRequest payloadValue        = null;

            // act
            Action verify = () => dispatcher.Dispatch(id, payloadName, payloadValue);

            // assert
            Assert.Null(Record.Exception(verify));
        }
        public void Dispatch_HttpRequest_IdEmpty()
        {
            // arrange
            AttachmentId         id         = AttachmentId.Empty;
            AttachmentDispatcher dispatcher = AttachmentDispatcher.Instance;
            string      payloadName         = "Name-6161";
            HttpRequest payloadValue        = new HttpRequest();

            // act
            Action verify = () => dispatcher.Dispatch(id, payloadName, payloadValue);

            // assert
            Assert.Null(Record.Exception(verify));
        }
        public void Dispatch_HttpResponse_DispatcherNull()
        {
            // arrange
            AttachmentId         id         = AttachmentId.NewId();
            AttachmentDispatcher dispatcher = null;
            string       payloadName        = "Name-7272";
            HttpResponse payloadValue       = new HttpResponse();

            // act
            Action verify = () => dispatcher.Dispatch(id, payloadName, payloadValue);

            // assert
            Assert.Null(Record.Exception(verify));
        }
        /// <summary>
        /// Dispatches an <see cref="HttpRequest"/>.
        /// </summary>
        /// <param name="dispatcher">A dispatcher instance.</param>
        /// <param name="id">A correlation id.</param>
        /// <param name="payloadName">A payload name.</param>
        /// <param name="payloadValue">A HTTP request.</param>
        /// <remarks>This method may not break because it is called from EventSources.</remarks>
        public static void Dispatch(this AttachmentDispatcher dispatcher, AttachmentId id,
                                    string payloadName, HttpRequest payloadValue)
        {
            if (dispatcher != null && id != AttachmentId.Empty &&
                !string.IsNullOrWhiteSpace(payloadName) && payloadValue != null)
            {
                HttpRequestAttachment request = AttachmentFactory
                                                .Create <HttpRequestAttachment, HttpRequest>(id, payloadName, payloadValue);

                dispatcher.Dispatch(request);

                if (payloadValue.Body != null && payloadValue.Body.Length > 0)
                {
                    HttpRequestBodyAttachment requestBody = AttachmentFactory
                                                            .Create <HttpRequestBodyAttachment>(id, payloadName,
                                                                                                payloadValue.Body);

                    dispatcher.Dispatch(requestBody);
                }
            }
        }
        /// <summary>
        /// Dispatches an <see cref="HttpResponse"/>.
        /// </summary>
        /// <param name="dispatcher">A dispatcher instance.</param>
        /// <param name="id">A correlation id.</param>
        /// <param name="payloadName">A payload name.</param>
        /// <param name="payloadValue">A HTTP response.</param>
        /// <remarks>This method may not break because it is called from EventSources.</remarks>
        public static void Dispatch(this AttachmentDispatcher dispatcher, AttachmentId id,
                                    string payloadName, HttpResponse payloadValue)
        {
            if (dispatcher != null && id != AttachmentId.Empty &&
                !string.IsNullOrWhiteSpace(payloadName) && payloadValue != null)
            {
                HttpResponseAttachment response = AttachmentFactory
                                                  .Create <HttpResponseAttachment, HttpResponse>(id, payloadName, payloadValue);

                dispatcher.Dispatch(response);

#pragma warning disable S125 // Sections of code should not be "commented out"
                //if (payloadValue.Body != null && payloadValue.Body.Length > 0)
                //{
                //    HttpResponseBodyAttachment responseBody = AttachmentFactory
                //        .Create<HttpResponseBodyAttachment>(id, payloadName,
                //            payloadValue.Body);
                //
                //    dispatcher.Dispatch(responseBody);
                //}
#pragma warning disable S125 // Sections of code should not be "commented out"
            }
        }