public async Task GivenProductServiceWhenEditWithProductFoundThenProductIsUpdated()
        {
            Product          productDb    = FakeBuilder.GetProductFake();
            ProductViewModel productModel = FakeBuilder.GetProductViewModelFake(5);

            this.mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>()))
            .ReturnsAsync(productDb);

            productDb.Name.Should().NotBe(productModel.Name);
            productDb.Description.Should().NotBe(productModel.Description);
            productDb.Family.Type.Should().NotBe(productModel.Type);
            productDb.Family.Subtype.Should().NotBe(productModel.Subtype);
            productDb.Availability.Start.Should().NotBeSameDateAs(productModel.Start);
            productDb.Availability.End.Should().NotBeSameDateAs(productModel.End);

            await this.productService.Edit(productModel);

            productDb.Name.Should().Be(productModel.Name);
            productDb.Description.Should().Be(productModel.Description);
            productDb.Family.Type.Should().Be(productModel.Type);
            productDb.Family.Subtype.Should().Be(productModel.Subtype);
            productDb.Availability.Start.Should().BeSameDateAs(productModel.Start);
            productDb.Availability.End.Should().BeSameDateAs(productModel.End);

            this.mockProductRepository.Verify(mock => mock.Save(It.IsAny <Product>()), Times.Once);
        }
        private Photo GetPhotoFake()
        {
            User    user          = FakeBuilder.GetOwnerFake();
            Product productDomain = FakeBuilder.GetProductFake(user);

            return(new Photo(productDomain, "http://www.almacenfotos.com/foto"));
        }
        public async Task GivenProductServiceWhenDeletePhotoWithPhotoNotFoundThenThrowArgumentException()
        {
            Product productDb = FakeBuilder.GetProductFake();

            productDb.AddPhotos(new List <string>()
            {
                "url1", "url2"
            });

            this.mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>())).ReturnsAsync(productDb);
            await Assert.ThrowsAsync <PhotoNotFoundException>(() => this.productService.DeletePhoto(1, 3));

            this.mockProductRepository.Verify(mock => mock.Save(It.IsAny <Product>()), Times.Never);
        }
        public async Task GivenProductServiceWhenDeleteWithProductIdWithProductFoundThenDeleteOk()
        {
            Product productDb = FakeBuilder.GetProductFake();

            productDb.AddPhotos(new List <string>()
            {
                "url1", "url2"
            });

            this.mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>())).ReturnsAsync(productDb);
            await this.productService.Delete(1);

            this.mockProductRepository.Verify(mock => mock.Delete(It.IsAny <Product>()), Times.Once);
        }
        public async Task GivenBorrowServiceWhenCreateWithDataValidThenCreateNewBorrow()
        {
            int             productId = 0;
            BorrowViewModel model     = FakeBuilder.GetBorrowViewModelFake(0, productId);

            this._mockUserManager.Setup(mock => mock.FindUserDomain(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult(new User(Guid.NewGuid().ToString())));
            this._mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>()))
            .Returns(Task.FromResult(FakeBuilder.GetProductFake()));
            await this._borrowService.Create(new ClaimsPrincipal(), model);

            this._mockBorrowRepository.Verify(mock => mock.Save(It.IsAny <Borrow>()), Times.Once);
            Assert.True(true);
        }
        public async Task GivenProductServiceWhenChangeStatusThenChangeStatusOk()
        {
            Product productDb = FakeBuilder.GetProductFake();

            this.mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>())).ReturnsAsync(productDb);
            Assert.True(productDb.IsShary());
            await this.productService.ChangeStatus(productDb.ProductId);

            this.mockProductRepository.Verify(mock => mock.Save(It.IsAny <Product>()), Times.Once);
            Assert.True(productDb.IsUnshary());
            await this.productService.ChangeStatus(productDb.ProductId);

            this.mockProductRepository.Verify(mock => mock.Save(It.IsAny <Product>()), Times.Exactly(2));
            Assert.True(productDb.IsShary());
        }
        public async Task GivenBorrowServiceWhenCreateWithUnavailableProductDateThenProductNoAvailableException()
        {
            Product productDomain = FakeBuilder.GetProductFake();

            productDomain.EditAvailability(new Term(DateTime.Now.AddYears(2), DateTime.Now.AddYears(4)));
            BorrowViewModel model = FakeBuilder.GetBorrowViewModelFake(0, productDomain.ProductId);

            this._mockUserManager.Setup(mock => mock.FindUserDomain(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult(new User(Guid.NewGuid().ToString())));

            this._mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>()))
            .Returns(Task.FromResult(productDomain));

            await Assert.ThrowsAsync <ProductNoAvailableException>(() => this._borrowService.Create(new ClaimsPrincipal(), model));

            this._mockBorrowRepository.Verify(mock => mock.Save(It.IsAny <Borrow>()), Times.Never);
        }
        public async Task GivenBorrowServiceWhenCreateWithProductUnsharyThenStatusInvalidException()
        {
            Product productDomain = FakeBuilder.GetProductFake();

            productDomain.UnShary();
            Assert.True(productDomain.IsUnshary());

            BorrowViewModel model = FakeBuilder.GetBorrowViewModelFake(0, productDomain.ProductId);

            this._mockUserManager.Setup(mock => mock.FindUserDomain(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult(new User(Guid.NewGuid().ToString())));

            this._mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>()))
            .Returns(Task.FromResult(productDomain));

            await Assert.ThrowsAsync <StatusInvalidException>(() => this._borrowService.Create(new ClaimsPrincipal(), model));

            this._mockBorrowRepository.Verify(mock => mock.Save(It.IsAny <Borrow>()), Times.Never);
        }
        public async Task GivenBorrowServiceWhenCreateWithBorrowOwnerIsEqualProductOwnerThenInvalidOperationException()
        {
            User            user          = FakeBuilder.GetOwnerFake();
            Product         productDomain = FakeBuilder.GetProductFake(user);
            BorrowViewModel model         = FakeBuilder.GetBorrowViewModelFake(0, productDomain.ProductId);

            model.BorrowerId     = productDomain.OwnerId;
            model.ProductOwnerId = productDomain.OwnerId;

            this._mockUserManager.Setup(mock => mock.FindUserDomain(It.IsAny <ClaimsPrincipal>()))
            .Returns(Task.FromResult(user));

            this._mockProductRepository.Setup(mock => mock.Get(It.IsAny <int>()))
            .Returns(Task.FromResult(productDomain));

            await Assert.ThrowsAsync <BorrowerIsSameOwnerProductException>(() => this._borrowService.Create(new ClaimsPrincipal(), model));

            this._mockBorrowRepository.Verify(mock => mock.Save(It.IsAny <Borrow>()), Times.Never);
        }
        public async Task GivenProductServiceWhenGetWithProductIdExistThenReturnProductNotNull()
        {
            int?    productId = 1;
            Product productDb = FakeBuilder.GetProductFake();

            this.mockProductRepository.Setup(repo => repo.Get(It.IsAny <int>()))
            .ReturnsAsync(productDb);

            ProductViewModel productViewModel = await this.productService.Get(productId);

            Assert.NotNull(productViewModel);
            productViewModel.ProductId.Should().Be(productDb.ProductId);
            productViewModel.Name.Should().Be(productDb.Name);
            productViewModel.Description.Should().Be(productDb.Description);
            productViewModel.Status.Should().Be(productDb.Status.ToString());
            productViewModel.Type.Should().Be(productDb.Family.Type);
            productViewModel.Subtype.Should().Be(productDb.Family.Subtype);
            productViewModel.Start.Should().Be(productDb.Availability.Start);
            productViewModel.End.Should().Be(productDb.Availability.End);
        }