public async void Post_Returns_Expected_Total() { // Arrange var expectedTotal = new decimal(14); var resourceServiceClient = new Mock <ITrolleyCalculator>(); resourceServiceClient .Setup(x => x.Calculate(It.IsAny <TrolleyTotalRequest>())) .ReturnsAsync(expectedTotal); var controller = new TrolleyTotalController(resourceServiceClient.Object); // Act var result = await controller.Post(_trolleyTotalRequest); //Assert Assert.Equal(expectedTotal, result); }
public void TestCalculateTrolleyTotalWithValidJObject() { // Prepare double expectedResult = 2.8; string trolleyStr = @" { 'products': [ { 'name': 'string', 'price': 3 } ], 'specials': [ { 'quantities': [ { 'name': 'string', 'quantity': 1 } ], 'total': 1.4 } ], 'quantities': [ { 'name': 'string', 'quantity': 2 } ] }"; JObject trolley = JObject.Parse(trolleyStr); // Set up behavior Mock <IResourceService> resourceService = new Mock <IResourceService>(); resourceService.Setup(r => r.GetMinimumTotal(trolley)).Returns(expectedResult); // Call method and assert TrolleyTotalController trolleyController = new TrolleyTotalController(resourceService.Object); Assert.AreEqual(expectedResult, trolleyController.Post(trolley).Value); }
public async void GetTrolleyTotal_WhenCalled_ReturnsOkResult() { //Arrange var _serviceContextMock = new Mock <IServiceContext>(); _serviceContextMock.Setup(a => a.GetTrolleyTotalAsync(GetProductsTrolleyItems())).Returns(CalculateTrolleyTotal()); IProductRepository _productRepository = new ProductRepository(_serviceContextMock.Object); var result = _productRepository.GetTrolleyTotalAsync(GetProductsTrolleyItems()); TrolleyTotalController _trolleyTotalController = new TrolleyTotalController(_productRepository); //Act var actionResult = await _trolleyTotalController.GetTrolleyTotal(GetProductsTrolleyItems()) as OkObjectResult; //Assert Assert.NotNull(actionResult); }
public async Task TrolleyTotal_QuantitiesAreNull_ShouldReturn_InternalServerError() { var logger = Mock.Of <ILogger <TrolleyTotalController> >(); var mapper = new Mock <IMapper>(); mapper.Setup(x => x.Map <CalculateTrolleyTotalCommand.RequestTrolley>(It.IsAny <TrolleyInputModel>())) .Returns(new CalculateTrolleyTotalCommand.RequestTrolley { Products = new List <CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct>(), Quantities = null }); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <CalculateTrolleyTotalCommand.RequestTrolley>(), It.IsAny <CancellationToken>())) .ReturnsAsync(new Exception("unknown")); var controller = new TrolleyTotalController(mediator.Object, mapper.Object, logger) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; var response = await controller.TrolleyTotal(new TrolleyInputModel()); response.ShouldNotBeNull(); response.ShouldBeOfType <ObjectResult>(); var data = response as ObjectResult; data.StatusCode.ShouldBe((int)HttpStatusCode.BadRequest); data.Value.ShouldBeOfType <ProblemDetails>(); var details = data.Value as ProblemDetails; details.Detail.ShouldBe("Value cannot be null. (Parameter 'Quantities')"); details.Title.ShouldBe("Invalid Trolley State"); details.Status.ShouldBe((int)HttpStatusCode.BadRequest); }
public async Task TrolleyTotal_MapperFails_Returns_BadRequest() { var logger = Mock.Of <ILogger <TrolleyTotalController> >(); var mapper = new Mock <IMapper>(); mapper.Setup(x => x.Map <CalculateTrolleyTotalCommand>(It.IsAny <TrolleyInputModel>())) .Throws(new Exception("Mapping failed")); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <GetUserQuery>(), It.IsAny <CancellationToken>())) .ThrowsAsync(new ApplicationSettingsInvalidException("missing")); var controller = new TrolleyTotalController(mediator.Object, mapper.Object, logger) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; var response = await controller.TrolleyTotal(It.IsAny <TrolleyInputModel>()); response.ShouldNotBeNull(); response.ShouldBeOfType <ObjectResult>(); var data = response as ObjectResult; data.StatusCode.ShouldBe((int)HttpStatusCode.BadRequest); data.Value.ShouldBeOfType <ProblemDetails>(); var details = data.Value as ProblemDetails; details.Detail.ShouldBe("Value cannot be null. (Parameter 'trolley')"); details.Title.ShouldBe("Invalid Trolley State"); details.Status.ShouldBe((int)HttpStatusCode.BadRequest); }
public void UseSpecialDynamicProgramming2() { var priceDic = new Dictionary <string, double> { { "Apple", 1.0 }, { "Banana", 1.1 }, { "Cola", 3.0 }, { "Orange", 11.0 }, }; var quantity = new Dictionary <string, int> { { "Apple", 2 }, { "Banana", 3 } }; var specials = new[] { new TrolleyCalculatorSpecial { Total = 5.0, Quantities = new[] { new TrolleyCalculatorQuantity("Apple", 2), new TrolleyCalculatorQuantity("Cola", 3) } }, new TrolleyCalculatorSpecial { Total = 5.0, Quantities = new[] { new TrolleyCalculatorQuantity("Orange", 2), new TrolleyCalculatorQuantity("Banana", 3) } }, }; var sub = new TrolleyTotalController(); var total = sub.UseSpecial(quantity, priceDic, specials, 0, 0); Assert.Equal((decimal)5.3, total); }
public async Task TrolleyTotal_WhenDataIsFound_ShouldReturn_OkSuccess() { var logger = Mock.Of <ILogger <TrolleyTotalController> >(); var mapper = new Mock <IMapper>(); mapper.Setup(x => x.Map <CalculateTrolleyTotalCommand.RequestTrolley>(It.IsAny <TrolleyInputModel>())) .Returns(new CalculateTrolleyTotalCommand.RequestTrolley { Products = new List <CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct>() { new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "a", Price = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "b", Price = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "c", Price = 1 } }, Quantities = new List <CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity> { new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "a", Quantity = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "b", Quantity = 2 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "c", Quantity = 1 } } }); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <CalculateTrolleyTotalCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync((decimal)123.1); var controller = new TrolleyTotalController(mediator.Object, mapper.Object, logger) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; var response = await controller.TrolleyTotal(new TrolleyInputModel()); response.ShouldNotBeNull(); response.ShouldBeOfType <OkObjectResult>(); var data = response as OkObjectResult; data.StatusCode.ShouldBe((int)HttpStatusCode.OK); data.Value.ShouldBeOfType <decimal>(); var details = data.Value; details.ShouldBe(123.1); }
public async Task TrolleyTotal_MediatorReturnsTrolleyFailed_ShouldReturn_InternalServerError() { var logger = Mock.Of <ILogger <TrolleyTotalController> >(); var mapper = new Mock <IMapper>(); mapper.Setup(x => x.Map <CalculateTrolleyTotalCommand.RequestTrolley>(It.IsAny <TrolleyInputModel>())) .Returns(new CalculateTrolleyTotalCommand.RequestTrolley { Products = new List <CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct>() { new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "a", Price = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "b", Price = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProduct { Name = "c", Price = 1 } }, Quantities = new List <CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity> { new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "a", Quantity = 1 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "b", Quantity = 2 }, new CalculateTrolleyTotalCommand.RequestTrolley.TrolleyProductQuantity { Name = "c", Quantity = 1 } } }); var mediator = new Mock <IMediator>(); mediator.Setup(x => x.Send(It.IsAny <CalculateTrolleyTotalCommand>(), It.IsAny <CancellationToken>())) .ThrowsAsync(new TrolleyCalculationRemoteException(HttpStatusCode.Ambiguous, "trolley")); var controller = new TrolleyTotalController(mediator.Object, mapper.Object, logger) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() } }; var response = await controller.TrolleyTotal(new TrolleyInputModel()); response.ShouldNotBeNull(); response.ShouldBeOfType <ObjectResult>(); var data = response as ObjectResult; data.StatusCode.ShouldBe((int)HttpStatusCode.Ambiguous); data.Value.ShouldBeOfType <ProblemDetails>(); var details = data.Value as ProblemDetails; details.Detail.ShouldBe("trolley"); details.Title.ShouldBe("Remote Error"); details.Status.ShouldBe((int)HttpStatusCode.Ambiguous); }