public async Task ValidationCacheService_GetValidKeyWithListWithSingleValue_ShouldReturnANewCopyIt()
        {
            // Arrange
            string key       = "some key";
            var    valueMock = new Mock <IValidationCacheEntity>();
            var    value     = valueMock.Object;
            var    list      = new IValidationCacheEntity[] { value };

            var repositoryMock = new Mock <IValidationCacheDataRepository>();

            repositoryMock
            .Setup(r => r.GetAll(key))
            .Returns(list);

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            var service = new ValidationCacheService(repositoryMock.Object, dateTimeProviderMock.Object);

            // Act
            var result = await service.Get(key);

            // Asset
            Assert.IsNotNull(result);
            Assert.AreNotSame(value, result);

            Assert.AreEqual(value.Content, result.Content);
            Assert.AreEqual(value.LastUpdate, result.LastUpdate);
            Assert.AreEqual(value.Status, result.Status);

            repositoryMock.Verify(r => r.GetAll(key), Times.Once);
            dateTimeProviderMock.VerifyGet(p => p.Now, Times.Never);
        }
        public async Task <object> Remove(string key, IValidationCacheEntity value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var dbmodel = await this.collection.Find(o => o.Id == key).FirstOrDefaultAsync();

            if (dbmodel == null)
            {
                return(false);
            }

            var result = dbmodel.Values.Remove(new ValidationCacheEntity(value));

            var response = await this.collection.ReplaceOneAsync(o => o.Id == key, dbmodel);

            return(result && response.IsAcknowledged);
        }
        public ValidationCacheEntity(IValidationCacheEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            this.Content    = entity.Content;
            this.LastUpdate = entity.LastUpdate;
            this.Status     = entity.Status;
        }
        public async Task ValidationCacheService_GetValidKeyWithListWithTwoValues_ShouldReturnANewCopyOfTheLastUpdatedOne()
        {
            // Arrange
            string key = "some key";
            var    now = DateTime.Now;

            var value1Mock = new Mock <IValidationCacheEntity>();

            value1Mock
            .SetupGet(v => v.LastUpdate)
            .Returns(now);

            var value2Mock = new Mock <IValidationCacheEntity>();

            value2Mock
            .SetupGet(v => v.LastUpdate)
            .Returns(now + TimeSpan.FromHours(1));

            var expectedValue = value2Mock.Object;

            var list = new IValidationCacheEntity[] { value1Mock.Object, value2Mock.Object };

            var repositoryMock = new Mock <IValidationCacheDataRepository>();

            repositoryMock
            .Setup(r => r.GetAll(key))
            .Returns(list);

            var dateTimeProviderMock = new Mock <IDateTimeProvider>();

            var service = new ValidationCacheService(repositoryMock.Object, dateTimeProviderMock.Object);

            // Act
            var result = await service.Get(key);

            // Asset
            Assert.IsNotNull(result);
            Assert.AreNotSame(value1Mock.Object, result);
            Assert.AreNotSame(value2Mock.Object, result);

            Assert.AreEqual(expectedValue.Content, result.Content);
            Assert.AreEqual(expectedValue.LastUpdate, result.LastUpdate);
            Assert.AreEqual(expectedValue.Status, result.Status);

            repositoryMock.Verify(r => r.GetAll(key), Times.Once);
            dateTimeProviderMock.VerifyGet(p => p.Now, Times.Never);
        }
        public async Task <object> Add(string key, IValidationCacheEntity value)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            return(await this.collection.UpdateOneAsync(
                       Builders <ValidatedObject> .Filter
                       .Eq(o => o.Id, key),
                       Builders <ValidatedObject> .Update
                       .AddToSet(o => o.Values, new ValidationCacheEntity(value)),
                       this.updateOptions));
        }