using JustSaying.AwsTools.MessageHandling;
using JustSaying.Messaging.MessageHandling;
using JustSaying.TestingFramework;
using NUnit.Framework;

namespace JustSaying.AwsTools.UnitTests.MessageHandling
{
    [TestFixture]
    public class HandlerMetadataTests
    {
        [Test]
        public void UnadornedHandler_DoesNotHaveExactlyOnce()
        {
            var handler = new UnadornedHandlerAsync();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.Enabled, Is.False);
        }

        [Test]
        public void OnceTestHandlerAsync_DoesHaveExactlyOnce()
        {
            var handler = new OnceTestHandlerAsync();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.Enabled, Is.True);
        }

        [Test]
        public void OnceTestHandlerAsync_HasCorrectTimeout()
        {
            var handler = new OnceTestHandlerAsync();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.GetTimeOut(), Is.EqualTo(42));
        }

        [Test, Ignore("reverted")]
        public void OnceHandlerWithImplicitTimeoutAsync_DefaultsToMaximum()
        {
            var handler = new OnceHandlerWithImplicitTimeoutAsync();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.GetTimeOut(), Is.EqualTo(int.MaxValue));
        }

        [Test]
        public void OnceTestHandler_DoesHaveExactlyOnce()
        {
            var handler = new OnceTestHandler();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.Enabled, Is.True);
        }

        [Test]
        public void OnceTestHandler_HasCorrectTimeout()
        {
            var handler = new OnceTestHandler();
            var reader = HandlerMetadata.ReadExactlyOnce(handler);

            Assert.That(reader.GetTimeOut(), Is.EqualTo(23));
        }

        [Test]
        public void WrappedHandler_DoesHaveExactlyOnce()
        {
#pragma warning disable 618
            var wrapped = new BlockingHandler<GenericMessage>(new OnceTestHandler());
#pragma warning restore 618

            var reader = HandlerMetadata.ReadExactlyOnce(wrapped);

            Assert.That(reader.Enabled, Is.True);
        }
    }
}
