using qdr.fnd.core.pqueue.Exceptions;
using qdr.fnd.core.pqueue.Storages;
using qdr.fnd.core.pqueue.test.Mocks;
using qdr.fnd.core.pqueue.test.Storages.Mocks;

namespace qdr.fnd.core.pqueue.test.Storages
{
    public class MemoryStorageProviderTest
    {
        private LoggerMock _logger;


        private string _id = "id";
        private string _owner = "owner";
        private string _ownerRef = "ownerRef";
        private string _providerClass = "providerClass";
        private int _initialAttempts = 10;
        private DateTime _created = DateTime.Now;

        [SetUp]
        public void Setup()
        {
            _logger = new LoggerMock();
        }

        [Test(TestOf=typeof(MemoryStorageProvider))]
        public void ConstructorOk()
        {
            var provider = new MemoryStorageProvider(_logger);
        }

        [Test(TestOf=typeof(MemoryStorageProvider))]
        public void CreateItemOk()
        {
            var provider = new MemoryStorageProvider(_logger);
            var itemId = provider.CreateItem(_owner, _ownerRef, _providerClass, _initialAttempts, _created);
            Assert.That(string.IsNullOrEmpty(itemId), Is.False);
        }
        [Test(TestOf=typeof(MemoryStorageProvider))]
        public void ContainsItemOk()
        {
            var provider = new MemoryStorageProvider(_logger);
            var itemId = provider.CreateItem(_owner, _ownerRef, _providerClass, _initialAttempts, _created);
            Assert.That(provider.ContainsItem(itemId), Is.True);
            Assert.That(provider.ContainsItem("not-exist"), Is.False);
        }

        [Test(TestOf=typeof(MemoryStorageProvider))]
        public void DeleteItemOk()
        {
            var provider = new MemoryStorageProvider(_logger);
            var itemId = provider.CreateItem(_owner, _ownerRef, _providerClass, _initialAttempts, _created);
            provider.DeleteItem(itemId);
            Assert.That(provider.ContainsItem(itemId), Is.False);
        }

        [Test(TestOf = typeof(MemoryStorageProvider))]
        public void DeleteItemFail()
        {
            var provider = new MemoryStorageProvider(_logger);
            var itemId = provider.CreateItem(_owner, _ownerRef, _providerClass, _initialAttempts, _created);
            provider.DeleteItem(itemId);
            Assert.Throws<ArgumentNullException>(() => provider.DeleteItem(string.Empty));
            var psEx = Assert.Throws<PSStorageException>(() => provider.DeleteItem(itemId));
            Assert.That(psEx, Is.Not.Null);
            Assert.That(psEx.Code, Is.EqualTo(PSStorageException.ErrorCodes.PsStorageItemNotFound));
        }



    }
}
