/// <summary> /// Builds and sends a request to add a expenditure to the database. /// </summary> /// <param name="model">The model.</param> /// <param name="username">The username.</param> /// <returns> /// The response object. /// </returns> public async Task <OrchestratorResponseWrapper <ExpenditureViewModel> > AddExpenditure( ExpenditureViewModel model, string username) { var response = new OrchestratorResponseWrapper <ExpenditureViewModel>(); try { var request = assembler.NewAddExpenditureRequest(model, username); var apiResponse = await dataAccess.AddExpenditure(request); if (!apiResponse.Success) { response.AddErrors(apiResponse.Errors); return(response); } response.AddWarnings(apiResponse.Warnings); response.Model = assembler.NewExpenditureViewModel(apiResponse); } catch (Exception ex) { var err = ErrorHelper.Create(ex, username, GetType(), "AddExpenditure"); response.AddError(err); } return(response); }
public void NewExpenditureViewModel_AddExpenditureResponse_NullParams_ThrowsArgumentNullException() { Assert.Throws <ArgumentNullException>( delegate { AddExpenditureResponse response = null; // ReSharper disable once ExpressionIsAlwaysNull assembler.NewExpenditureViewModel(response); }); }
public void SetUp() { validViewModel = new ExpenditureViewModel { Amount = 10, Category = "TEST", Id = Guid.NewGuid(), UserId = Guid.NewGuid() }; validExpenditureProxy = new ExpenditureProxy { Amount = 10, Category = new CategoryProxy { Id = Guid.NewGuid(), Name = "TEST" }, CategoryId = Guid.NewGuid(), Id = Guid.NewGuid(), UserId = Guid.NewGuid() }; validTrackExpenditureViewModel = new TrackExpenditureViewModel(); validDeleteExpenditureRequest = new DeleteExpenditureRequest { ExpenditureId = validExpenditureProxy.Id }; validDeleteExpenditureResponse = new DeleteExpenditureResponse { DeleteSuccess = true }; validAddExpenditureRequest = new AddExpenditureRequest { Expenditure = validExpenditureProxy }; validAddExpenditureResponse = new AddExpenditureResponse { Expenditure = validExpenditureProxy }; validGetExpenditureRequest = new GetExpenditureRequest { ExpenditureId = validExpenditureProxy.Id }; validGetExpenditureResponse = new GetExpenditureResponse { Expenditure = validExpenditureProxy }; validGetExpenditureForUserRequest = new GetExpenditureForUserRequest { UserId = validExpenditureProxy.Id }; validGetExpenditureForUserResponse = new GetExpenditureForUserResponse { Expenditure = new List <ExpenditureProxy> { validExpenditureProxy } }; validEditExpenditureRequest = new EditExpenditureRequest { Expenditure = validExpenditureProxy }; validEditExpenditureResponse = new EditExpenditureResponse { Expenditure = validExpenditureProxy }; validGetExpenditureForUserForMonthRequest = new GetExpenditureForUserForMonthRequest { UserId = validExpenditureProxy.Id }; validGetExpenditureForUserForMonthResponse = new GetExpenditureForUserForMonthResponse { Data = new List <ExpenditureProxy> { validExpenditureProxy } }; invalidExpenditureViewModel = new ExpenditureViewModel { Id = Guid.NewGuid() }; invalidAddExpenditureRequest = new AddExpenditureRequest(); invalidAddExpenditureResponse = new AddExpenditureResponse { Errors = { new ResponseErrorWrapper() } }; invalidGetExpenditureRequest = new GetExpenditureRequest(); invalidGetExpenditureResponse = new GetExpenditureResponse { Errors = { new ResponseErrorWrapper() } }; invalidDeleteExpenditureRequest = new DeleteExpenditureRequest(); invalidDeleteExpenditureResponse = new DeleteExpenditureResponse { Errors = { new ResponseErrorWrapper() } }; invalidGetExpenditureForUserRequest = new GetExpenditureForUserRequest(); invalidGetExpenditureForUserResponse = new GetExpenditureForUserResponse { Errors = { new ResponseErrorWrapper() } }; invalidEditExpenditureRequest = new EditExpenditureRequest(); invalidEditExpenditureResponse = new EditExpenditureResponse { Errors = { new ResponseErrorWrapper() } }; invalidGetExpenditureForUserForMonthRequest = new GetExpenditureForUserForMonthRequest(); invalidGetExpenditureForUserForMonthResponse = new GetExpenditureForUserForMonthResponse { Errors = { new ResponseErrorWrapper() } }; assembler = Substitute.For <IExpenditureAssembler>(); dataAccess = Substitute.For <IExpenditureDataAccess>(); errorHelper = Substitute.For <IErrorHelper>(); errorHelper.Create(Arg.Any <Exception>(), Arg.Any <string>(), Arg.Any <Type>(), Arg.Any <string>()) .Returns(new ResponseErrorWrapper()); errorHelper.Create(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Type>(), Arg.Any <string>()) .Returns(new ResponseErrorWrapper()); assembler.NewAddExpenditureRequest(validViewModel, validUsername).Returns(validAddExpenditureRequest); assembler.NewAddExpenditureRequest(invalidExpenditureViewModel, validUsername) .Returns(invalidAddExpenditureRequest); assembler.NewAddExpenditureRequest(null, validUsername).Throws(new Exception("TEST EXCEPTION")); assembler.NewExpenditureViewModel(validAddExpenditureResponse).Returns(validViewModel); assembler.NewExpenditureViewModel(validEditExpenditureResponse).Returns(validViewModel); assembler.NewDeleteExpenditureRequest(validViewModel.Id, validUsername) .Returns(validDeleteExpenditureRequest); assembler.NewDeleteExpenditureRequest(invalidExpenditureViewModel.Id, validUsername) .Returns(invalidDeleteExpenditureRequest); assembler.NewDeleteExpenditureRequest(Guid.Empty, validUsername).Throws(new Exception("TEST EXCEPTION")); assembler.NewGetExpenditureRequest(validViewModel.Id, validUsername).Returns(validGetExpenditureRequest); assembler.NewGetExpenditureRequest(invalidExpenditureViewModel.Id, validUsername) .Returns(invalidGetExpenditureRequest); assembler.NewGetExpenditureRequest(Guid.Empty, validUsername).Throws(new Exception("TEST EXCEPTION")); assembler.NewGetExpenditureForUserRequest(validViewModel.Id, validUsername) .Returns(validGetExpenditureForUserRequest); assembler.NewGetExpenditureForUserRequest(invalidExpenditureViewModel.Id, validUsername) .Returns(invalidGetExpenditureForUserRequest); assembler.NewGetExpenditureForUserRequest(Guid.Empty, validUsername) .Throws(new Exception("TEST EXCEPTION")); assembler.NewEditExpenditureRequest(validViewModel, validUsername).Returns(validEditExpenditureRequest); assembler.NewEditExpenditureRequest(invalidExpenditureViewModel, validUsername) .Returns(invalidEditExpenditureRequest); assembler.NewEditExpenditureRequest(null, validUsername).Throws(new Exception("TEST EXCEPTION")); assembler.NewGetExpenditureForUserForMonthRequest(1, validViewModel.Id, validUsername) .Returns(validGetExpenditureForUserForMonthRequest); assembler.NewGetExpenditureForUserForMonthRequest(1, invalidExpenditureViewModel.Id, validUsername) .Returns(invalidGetExpenditureForUserForMonthRequest); assembler.NewTrackExpenditureViewModel(validGetExpenditureForUserResponse) .Returns(validTrackExpenditureViewModel); assembler.NewExportViewModel(Arg.Any <IList <ExpenditureProxy> >(), ExportType.Json) .Throws(new Exception("TEST")); dataAccess.AddExpenditure(validAddExpenditureRequest).Returns(validAddExpenditureResponse); dataAccess.AddExpenditure(invalidAddExpenditureRequest).Returns(invalidAddExpenditureResponse); dataAccess.DeleteExpenditure(validDeleteExpenditureRequest).Returns(validDeleteExpenditureResponse); dataAccess.DeleteExpenditure(invalidDeleteExpenditureRequest).Returns(invalidDeleteExpenditureResponse); dataAccess.GetExpenditure(validGetExpenditureRequest).Returns(validGetExpenditureResponse); dataAccess.GetExpenditure(invalidGetExpenditureRequest).Returns(invalidGetExpenditureResponse); dataAccess.GetExpenditureForUser(validGetExpenditureForUserRequest) .Returns(validGetExpenditureForUserResponse); dataAccess.GetExpenditureForUser(invalidGetExpenditureForUserRequest) .Returns(invalidGetExpenditureForUserResponse); dataAccess.EditExpenditure(validEditExpenditureRequest).Returns(validEditExpenditureResponse); dataAccess.EditExpenditure(invalidEditExpenditureRequest).Returns(invalidEditExpenditureResponse); dataAccess.GetExpenditureForUserForMonth(validGetExpenditureForUserForMonthRequest) .Returns(validGetExpenditureForUserForMonthResponse); dataAccess.GetExpenditureForUserForMonth(invalidGetExpenditureForUserForMonthRequest) .Returns(invalidGetExpenditureForUserForMonthResponse); assembler.NewExportViewModel(new List <ExpenditureProxy> { validExpenditureProxy }, ExportType.Csv) .Returns(new ExportViewModel()); orchestrator = new ExpenditureOrchestrator(assembler, dataAccess, errorHelper); }