예제 #1
0
        public async Task Product_Update_BadRequest_scenario()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();
            var mockApiModel   = AutoMapper.Mapper.Map <ProductApiModel>(mockProductDto);

            mockApiModel.Price = -100;
            mockProductDto.Id  = Guid.NewGuid();
            mockIProductServices.Setup(m => m.UpdateAsync(It.IsAny <ProductDto>())).Returns(Task.FromResult(mockProductDto));

            target.ModelState.AddModelError("Price", "Price should be a positive value!");
            IHttpActionResult result = await target.Update(mockProductDto.Id, mockApiModel) as InvalidModelStateResult;

            AssertX.IsInstanceOfType(result, typeof(InvalidModelStateResult));
            CollectionAssert.Contains(((InvalidModelStateResult)result).ModelState.Keys.ToArray(), "Price");

            target.ModelState.Clear();
            result = await target.Update(Guid.Empty, mockApiModel) as BadRequestErrorMessageResult;

            AssertX.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult));

            mockIProductServices.Setup(m => m.UpdateAsync(It.IsAny <ProductDto>())).Throws(new EntityNotFoundException("Not found!"));
            result = await target.Update(mockProductDto.Id, mockApiModel) as NotFoundResult;

            AssertX.IsInstanceOfType(result, typeof(NotFoundResult));

            mockIProductServices.Setup(m => m.UpdateAsync(It.IsAny <ProductDto>())).Throws(new DbUpdateConcurrencyException());
            result = await target.Update(mockProductDto.Id, mockApiModel) as ConflictResult;

            AssertX.IsInstanceOfType(result, typeof(ConflictResult));
        }
예제 #2
0
        public async Task Discover_product_by_name_expect_two()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct1 = FixturesDto.ProductsBuildOne();

            // Bogus doesn't ensure unique names
            mockProduct1.Name = "Prod1";
            await target.CreateAsync(mockProduct1);

            var mockProduct2 = FixturesDto.ProductsBuildOne();

            await target.CreateAsync(mockProduct2);

            var mockProduct3 = FixturesDto.ProductsBuildOne();

            mockProduct3.Name = mockProduct2.Name;
            await target.CreateAsync(mockProduct3);


            var result = await target.GetByNameAsync(mockProduct2.Name);

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count(), $"Expected two products with name '{ mockProduct2.Name}' to be found!");
            Assert.AreEqual(mockProduct2.Name, result.First().Name);
        }
예제 #3
0
        public async Task Delete_product_fail()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct  = FixturesDto.ProductsBuildOne();
            var result       = Task.FromResult(target.CreateAsync(mockProduct)).Result;
            var newProductId = Guid.NewGuid();;
            var deleteResult = await target.DeleteByIdAsync(newProductId);

            Assert.IsFalse(deleteResult);
        }
예제 #4
0
        public async Task Product_GetById_notFound()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();

            mockProductDto.Id = Guid.NewGuid();
            mockIProductServices.Setup(m => m.GetByIdAsync(mockProductDto.Id)).Returns(Task.FromResult(mockProductDto));

            var result = await target.GetProduct(Guid.NewGuid()) as IHttpActionResult;

            AssertX.IsInstanceOfType(result, typeof(NotFoundResult));
        }
예제 #5
0
        public void BasicCreateAsyncTest()
        {
            var target      = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct = FixturesDto.ProductsBuildOne();
            var result      = Task.FromResult(target.CreateAsync(mockProduct)).Result;

            Assert.IsNull(result.Exception);
            Assert.IsNotNull(result.Result);
            // The row version is returned
            Assert.IsNotNull(result.Result.RowVersion);
            Assert.IsTrue(result.Result.RowVersion.Length > 0);
        }
예제 #6
0
        public async Task Product_GetById_success()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();

            mockProductDto.Id = Guid.NewGuid();

            mockIProductServices.Setup(m => m.GetByIdAsync(It.IsAny <Guid>())).Returns(Task.FromResult(mockProductDto));

            var result = await target.GetProduct(mockProductDto.Id) as OkNegotiatedContentResult <ProductApiModel>;

            AssertX.AreEqual(mockProductDto.Id, result.Content.Id);
        }
예제 #7
0
        public async Task Product_Delete_success()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();

            mockProductDto.Id = Guid.NewGuid();
            mockIProductServices.Setup(m => m.DeleteByIdAsync(It.IsAny <Guid>())).Returns(Task.FromResult(true));

            var result = await target.Delete(mockProductDto.Id) as StatusCodeResult;

            AssertX.IsInstanceOfType(result, typeof(StatusCodeResult));
            Assert.AreEqual(HttpStatusCode.NoContent, result.StatusCode);
        }
예제 #8
0
        public async Task Discover_product_by_id()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct1 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct1);

            var mockProduct2 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct2);

            var result = await target.GetByIdAsync(mockProduct2.Id);

            Assert.IsNotNull(result);
            Assert.AreEqual(mockProduct2.Id, result.Id);
        }
예제 #9
0
        public async Task Get_all_products_expected_two()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct1 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct1);

            var mockProduct2 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct2);

            var result = await target.GetAllAsync();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count(), "Expected two products to be found!");
        }
예제 #10
0
        public async Task Product_SearchByName_notFound()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();
            var retMock        = (new List <ProductDto>()
            {
                mockProductDto
            }).AsEnumerable();

            mockIProductServices.Setup(m => m.GetByNameAsync("Me")).Returns(Task.FromResult(retMock));

            var result = await target.SearchByName("Any") as IHttpActionResult;

            AssertX.IsInstanceOfType(result, typeof(NotFoundResult));
        }
예제 #11
0
        public async Task Product_searchByName_success()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();
            var retMock        = (new List <ProductDto>()
            {
                mockProductDto
            }).AsEnumerable();

            mockIProductServices.Setup(m => m.GetByNameAsync(It.IsAny <string>())).Returns(Task.FromResult(retMock));

            var result = await target.SearchByName("Any") as OkNegotiatedContentResult <IEnumerable <ProductApiModel> >;

            AssertX.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <IEnumerable <ProductApiModel> >));
        }
예제 #12
0
        public async Task Product_Update_success()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();

            mockProductDto.Id = Guid.NewGuid();
            var mockApiModel = AutoMapper.Mapper.Map <ProductApiModel>(mockProductDto);

            mockIProductServices.Setup(m => m.UpdateAsync(It.IsAny <ProductDto>())).Returns(Task.FromResult(mockProductDto));

            var result = await target.Update(mockProductDto.Id, mockApiModel) as OkNegotiatedContentResult <ProductApiModel>;

            AssertX.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <ProductApiModel>));
            Assert.AreEqual(mockProductDto.Id, result.Content.Id);
        }
예제 #13
0
        public async Task Product_Create_invalid_model()
        {
            var mockProductDto = FixturesDto.ProductsBuildOne();
            var mockApiModel   = AutoMapper.Mapper.Map <ProductApiModel>(mockProductDto);

            mockApiModel.Price = -100;
            mockProductDto.Id  = Guid.NewGuid();
            mockIProductServices.Setup(m => m.CreateAsync(It.IsAny <ProductDto>())).Returns(Task.FromResult(mockProductDto));

            target.ModelState.AddModelError("Price", "Price should be a positive value!");
            var result = await target.Create(mockApiModel) as InvalidModelStateResult;

            AssertX.IsInstanceOfType(result, typeof(InvalidModelStateResult));
            CollectionAssert.Contains(result.ModelState.Keys.ToArray(), "Price");
        }
예제 #14
0
        public async Task Update_product_success()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct1 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct1);

            var mockProduct2 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct2);

            var mockProduct3 = FixturesDto.ProductsBuildOne();
            var prodtoUpdate = await target.CreateAsync(mockProduct3);

            prodtoUpdate.Name = "UpdatedProd";
            var result = await target.UpdateAsync(prodtoUpdate);

            Assert.IsNotNull(result);
            Assert.AreEqual(prodtoUpdate.Name, result.Name);
            Assert.AreNotEqual(mockProduct3.Name, result.Name);
        }
예제 #15
0
        public async Task Update_product_conflict()
        {
            var target       = new ProductServices(Helper.CreateMemoryUow());
            var mockProduct1 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct1);

            var mockProduct2 = FixturesDto.ProductsBuildOne();
            await target.CreateAsync(mockProduct2);

            var mockProduct3 = FixturesDto.ProductsBuildOne();
            var prodtoUpdate = await target.CreateAsync(mockProduct3);

            prodtoUpdate.Name = "UpdatedProd";
            // Initial update
            var result = await target.UpdateAsync(prodtoUpdate);

            // need to revise
            prodtoUpdate.Name = "Confict";
            var result2 = Task.FromResult(target.UpdateAsync(prodtoUpdate)).Result;

            Assert.IsNotNull(result2.Exception);
            Assert.IsInstanceOfType(result2.Exception.InnerException, typeof(System.Data.Entity.Infrastructure.DbUpdateConcurrencyException));
        }