public void PutAccountUpdateAccount() { IHttpActionResult result; CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult; OkNegotiatedContentResult<AccountModel> accountResult; //Arrange using (var accountsController = new AccountsController()) { //Build new AccountModel Object var newAccount = new AccountModel { AccountNumber = 21323, Balance = 213213, }; //Insert AccountModelObject into Database so //that I can take it out and test for update. result = accountsController.PostAccount(newAccount); //Cast result as Content Result so that I can gather information from ContentResult contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; } using (var SecondaccountsController = new AccountsController()) { //Result contains the customer I had JUST createad result = SecondaccountsController.GetAccount(contentResult.Content.AccountId); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<AccountModel>)); //Get AccountModel from 'result' accountResult = (OkNegotiatedContentResult<AccountModel>)result; } using (var thirdController = new AccountsController()) { var modifiedAccount = accountResult.Content; modifiedAccount.Balance += 5; //Act //The result of the Put Request result = thirdController.PutAccount(accountResult.Content.AccountId, modifiedAccount); //Assert Assert.IsInstanceOfType(result, typeof(StatusCodeResult)); } }
public void DeleteAccountDeletesAccount() { int customerIdForTest = 1; //Arrange: // Instantiate AccountsController so its methods can be called // Create a new account to be deleted, and get its account ID var accountController = new AccountsController(); var account = new AccountModel { CustomerId = customerIdForTest, CreatedDate = DateTime.Now, AccountNumber = "5555", Balance = 998877.66M }; IHttpActionResult result = accountController.PostAccount(account); CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; int accountIdToDelete = contentResult.Content.AccountId; //Act: Call DeleteAccount result = accountController.DeleteAccount(accountIdToDelete); //Assert: // Verify that HTTP result is OK // Verify that reading deleted account returns result not found Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<Account>)); result = accountController.GetAccount(accountIdToDelete); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public IHttpActionResult GetAccount(int id) { // Find Account object corresponding to account ID Account dbAccount = db.Accounts.Find(id); if (dbAccount == null) { return NotFound(); } // Populate new AccountModel object from Account object AccountModel modelAccount = new AccountModel { AccountId = dbAccount.AccountId, AccountNumber = dbAccount.AccountNumber, Balance = dbAccount.Balance, CreatedDate = dbAccount.CreatedDate, CustomerId = dbAccount.CustomerId }; return Ok(modelAccount); }
public void PostAccountUpdateAccount() { //Arrange var accountController = new AccountsController(); //Act var newAccount = new AccountModel { AccountNumber = 1231, Balance = 1222222 }; //Get the result of the post request IHttpActionResult result = accountController.PostAccount(newAccount); //If not 'true' Assert False Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<AccountModel>)); //Cast CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; //Check if Customer is posted to the database //Check to see if Customer ID is NOT equal to zero. If Customer Id us equal to zero, //then customer was NOT added to Database Assert.IsTrue(contentResult.Content.AccountId != 0); }
public void DeleteAccountRecord() { //Arrange //Create Controller var customersController = new AccountsController(); //Create a customer to be deleted var dbAccount = new AccountModel { AccountNumber = 21323, Balance = 213213, }; //Add 'new customer' to the DB using a POST //Save returned value as RESULT IHttpActionResult result = customersController.PostAccount(dbAccount); //Cast result as Content Result so that I can gather information from ContentResult CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; //Result contains the customer I had JUST created result = customersController.GetAccount(contentResult.Content.AccountId); //Get CustomerModel from 'result' OkNegotiatedContentResult<AccountModel> customerResult = (OkNegotiatedContentResult<AccountModel>)result; //Act //The result of the Delete Request result = customersController.DeleteAccount(customerResult.Content.AccountId); //Assert //If action returns: NotFound() Assert.IsNotInstanceOfType(result, typeof(NotFoundResult)); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<AccountModel>)); }
public void PostAccountCreatesAccount() { int customerIdForTest = 1; //Arrange: Instantiate AccountsController so its methods can be called var accountController = new AccountsController(); //Act: // Create a AccountModel object populated with test data, // and call PostAccount var newAccount = new AccountModel { AccountNumber = "223344", Balance = 888.77M, CreatedDate = DateTime.Now, CustomerId = customerIdForTest }; IHttpActionResult result = accountController.PostAccount(newAccount); //Assert: // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult // Verify that the HTTP result body contains a nonzero account ID Assert.IsInstanceOfType (result, typeof(CreatedAtRouteNegotiatedContentResult<AccountModel>)); CreatedAtRouteNegotiatedContentResult<AccountModel> contentResult = (CreatedAtRouteNegotiatedContentResult<AccountModel>)result; Assert.IsTrue(contentResult.Content.AccountId != 0); // Delete the test account result = accountController.DeleteAccount(contentResult.Content.AccountId); }
public IHttpActionResult PutAccount(int id, AccountModel account) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != account.AccountId) { return BadRequest(); } if (!AccountExists(id)) { return BadRequest(); } // Get the account record corresponding to the account ID, then // update its properties to the values in the input AccountModel object, // and then set indicator that the record has been modified var dbAccount = db.Accounts.Find(id); dbAccount.Update(account); db.Entry(dbAccount).State = EntityState.Modified; // Perform update by saving changes to DB try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!AccountExists(id)) { return NotFound(); } else { throw new Exception("Unable to update the account in the database."); } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostAccount(AccountModel account) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } // Set up new Account object, // and populate it with the values from // the input AccountModel object Account dbAccount = new Account(); dbAccount.Update(account); // Add the new Account object to the list of Account objects db.Accounts.Add(dbAccount); // Save the changes to the DB try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the account to the database."); } // Update the AccountModel object with the new account ID // that was placed in the Account object after the changes // were saved to the DB account.AccountId = dbAccount.AccountId; return CreatedAtRoute("DefaultApi", new { id = dbAccount.AccountId }, account); }