public void AddProduct_BareBones() { //Arrange var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "A62037", StandardCost = 324.23M }; const int productId = 99; _mockProvider.Setup(p => p.Add(It.IsAny<Product>())).Callback<Product>((p) => { p.ProductID = productId; }); //Act var result = _service.AddProduct(productModel); //Assert _mockProvider.VerifyAll(); Assert.IsTrue(result.Success); Assert.IsTrue(productModel.Id == productId); }
public void AddProduct_BareBones() { //Arrange var product = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "A62037", StandardCost = 324.23M }; using (new TransactionScope()) { //Act Result<int?> added = _service.AddProduct(product); //Assert Assert.IsTrue(added.Success); var result = _service.GetProductById(product.Id); Assert.IsTrue(result.Success); Assert.IsNotNull(result.Value); Assert.AreEqual(result.Value.Name, product.Name); Assert.AreEqual(result.Value.ListPrice, product.ListPrice); Assert.AreEqual(result.Value.StandardCost, product.StandardCost); Assert.IsTrue(result.Value.LastModifiedDate < DateTime.Now && result.Value.LastModifiedDate > DateTime.MinValue); } }
public HttpResponseMessage Post(ProductModelCreateRequest product) { return Invoke(Request, () => { var result = _service.AddProduct(product); var response = Request.CreatePostResponseFor(result, WebApiConfig.ProductsRouteName, new { id = result.Value != null ? result.Value.ToString() : string.Empty }); return response; }); }
public async Task AddProduct_BareBones() { //Arrange var product = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "A62037", StandardCost = 324.23M }; //Act var added = await _productClient.PostWithResult<ProductModelCreateRequest, string>(product); //Assert Assert.IsTrue(added.Success); Assert.IsNotNull(added.Value); }
public void AddProduct_ModelValidationFails() { //Arrange var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "1", StandardCost = 324.23M }; //Act var result = _service.AddProduct(productModel); //Assert Assert.IsTrue(result.Failure); Assert.IsTrue(result.Messages.Any()); Assert.IsTrue(result.Messages.Any(m => m.Code == (int)MessageCodes.ValidationError)); Assert.IsTrue(result.Messages.Any(m => String.Format(Models.Products.Resx.Strings.ProductModelCreateRequest_ValidationError_1, productModel.StandardCost, productModel.ProductNumber) == m.Phrase)); }
public void AddProduct_ValidationFailsDatabaseLookup() { //Arrange var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "HL-U509", StandardCost = 324.23M }; using (new TransactionScope()) { //Act var result = _service.AddProduct(productModel); //Assert Assert.IsTrue(result.Failure); Assert.IsTrue(result.Messages.Any()); Assert.IsTrue(result.Messages.Any(m => m.Code == (int) MessageCodes.ValidationError)); Assert.IsTrue(result.Messages.Any(m => String.Format(Resx.Strings.ProductsService_AddProduct_ValidationError_1, productModel.ProductNumber) == m.Phrase)); } }
public void DeleteProduct_ValidProductId() { //Arrange var product = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "A62037", StandardCost = 324.23M }; using (new TransactionScope()) { //Act _service.AddProduct(product); //Assert var deleteResult = _service.DeleteProduct(product.Id); Assert.IsTrue(deleteResult.Success); var result = _service.GetProductById(product.Id); Assert.IsTrue(result.NotFound); Assert.IsTrue(result.Failure); Assert.IsTrue(result.Messages.Any(m => m.Code == (int)MessageCodes.NotFound)); } }
public async Task AddProduct_ValidationFailsDatabaseLookup() { //Arrange var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "HL-U509", StandardCost = 324.23M }; //Act var result = await _productClient.PostWithUri(productModel); //Assert Assert.IsTrue(result.Failure); Assert.IsTrue(result.Messages.Any()); Assert.IsTrue(result.Messages.Any(m => m.Code == (int) MessageCodes.ValidationError)); Assert.IsTrue(result.Messages.Any(m => String.Format(Services.Resx.Strings.ProductsService_AddProduct_ValidationError_1, productModel.ProductNumber) == m.Phrase)); }
public Result<int?> AddProduct(ProductModelCreateRequest product) { var watch = new Stopwatch(); watch.Start(); using (new OperationLogger(Log, m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, String.Format("with product {0}", product.ToJson())))) { Console.Out.WriteLine("A - " + watch.ElapsedMilliseconds); // validates the model using data annotations and the IValidatableObject interface method. var results = Validate(product); if (results.Any()) return Result<int?>.CreateValidationErrors(results.ToArray()); Console.Out.WriteLine("B - " + watch.ElapsedMilliseconds); // note: you might want to call out to some provider to do something. this is how you can do it! var widgets = _externalProvider.GetWidgets(); var utcNow = DateTime.UtcNow; var entity = Mapper.Map<Product>(product); entity.ModifiedDate = utcNow; entity.SellStartDate = utcNow; Console.Out.WriteLine("C - " + watch.ElapsedMilliseconds); try { using (var uow = UoW()) { Console.Out.WriteLine("D - " + watch.ElapsedMilliseconds); // more complex validation - requires a database lookup. if (uow.Products.GetAll().Any(p => p.ProductNumber == product.ProductNumber)) { Console.Out.WriteLine("E - " + watch.ElapsedMilliseconds); return Result<int?>.CreateValidationErrors(new ValidationResult(String.Format(Strings.ProductsService_AddProduct_ValidationError_1, product.ProductNumber), new[] { "ProductNumber" })); } Console.Out.WriteLine("F - " + watch.ElapsedMilliseconds); uow.Products.Add(entity); uow.Commit(); Console.Out.WriteLine("G - " + watch.ElapsedMilliseconds); Log.Info(m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, String.Format(" product added - id = {0}", entity.ProductID))); Console.Out.WriteLine("H - " + watch.ElapsedMilliseconds); } } catch (ProviderException e) { Console.Out.WriteLine("I - " + watch.ElapsedMilliseconds); Log.Exception(m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, e.Errors.ToJson()), e); Console.Out.WriteLine("J - " + watch.ElapsedMilliseconds); return ResultExtensions.Create<int?>(e, (int) ServiceMessages.Codes.ProviderError); } product.Id = entity.ProductID; Console.Out.WriteLine("K - " + watch.ElapsedMilliseconds); return Result<int?>.Create(entity.ProductID); } }
public void AddProduct_ValidationFailsDatabaseLookup() { //Arrange var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "HL-U509", StandardCost = 324.23M }; _mockProvider.Setup(p => p.GetAll()).Returns(new []{ new Product { ProductNumber ="HL-U509" }}.AsQueryable()); //Act var result = _service.AddProduct(productModel); //Assert Assert.IsTrue(result.Failure); Assert.IsTrue(result.Messages.Any()); Assert.IsTrue(result.Messages.Any(m => m.Code == (int)MessageCodes.ValidationError)); Assert.IsTrue(result.Messages.Any(m => String.Format(Strings.ProductsService_AddProduct_ValidationError_1, productModel.ProductNumber) == m.Phrase)); }
public void AddProduct_ViolatesSqlConstraints() { var productModel = new ProductModelCreateRequest { Name = "iPhone 7", ListPrice = 999.90M, ProductNumber = "A62037", StandardCost = 324.23M }; _mockProvider.Setup(p => p.Add(It.IsAny<Product>())).Throws(new ProviderException("Provider Exception", new[] { "Error 1", "Error 2" })); //Act var result = _service.AddProduct(productModel); _mockProvider.VerifyAll(); }