public void CreateBranchWithAddress_Should_ReturnBadRequest_When_ModelStateIsInvalid() { // Arrange var branchCreation = new BranchWithAddressCreationDto(); _sut.ModelState.AddModelError(nameof(branchCreation.Branch), $"The {nameof(branchCreation.Branch)} field is required."); _sut.ModelState.AddModelError(nameof(branchCreation.Address), $"The {nameof(branchCreation.Address)} field is required."); // Act var badRequestResult = _sut.CreateBranchWithAddress(branchCreation).Result as BadRequestObjectResult; // Assert Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(branchCreation.Branch))); Assert.IsTrue(error.ContainsKey(nameof(branchCreation.Address))); var branchErrorValues = error[nameof(branchCreation.Branch)] as string[]; Assert.IsNotNull(branchErrorValues); Assert.IsTrue(branchErrorValues.Single() == $"The {nameof(branchCreation.Branch)} field is required."); var addressErrorValues = error[nameof(branchCreation.Address)] as string[]; Assert.IsNotNull(addressErrorValues); Assert.IsTrue(addressErrorValues.Single() == $"The {nameof(branchCreation.Address)} field is required."); }
public void CreateBranchWithAddress_Should_CreateBranchWithAddress_And_ReturnBranchDto_When_ModelStateIsValid() { // Arrange var branchCreation = new BranchWithAddressCreationDto { Branch = new BranchCreationDto { BranchCode = "002" }, Address = new AddressCreationDto { Country = "United States", City = "New York", Street = "Glenwood Ave", HouseNumber = "10", ApartmentNumber = "11", PostalCode = "10028" } }; // Act var createdAtRouteResult = _sut.CreateBranchWithAddress(branchCreation).Result as CreatedAtRouteResult; // Assert Assert.IsNotNull(createdAtRouteResult); Assert.IsInstanceOfType(createdAtRouteResult.Value, typeof(BranchDto)); var branchDto = createdAtRouteResult.Value as BranchDto; Assert.IsNotNull(branchDto); Assert.AreEqual(branchCreation.Branch.BranchCode, branchDto.BranchCode); Assert.AreEqual(branchCreation.Address.Country, branchDto.BranchAddress.Country); Assert.AreEqual(branchCreation.Address.City, branchDto.BranchAddress.City); Assert.AreEqual(branchCreation.Address.Street, branchDto.BranchAddress.Street); Assert.AreEqual(branchCreation.Address.HouseNumber, branchDto.BranchAddress.HouseNumber); Assert.AreEqual(branchCreation.Address.ApartmentNumber, branchDto.BranchAddress.ApartmentNumber); Assert.AreEqual(branchCreation.Address.PostalCode, branchDto.BranchAddress.PostalCode); var branchFromDb = _context.Branches.SingleOrDefault(b => b.Id == branchDto.Id); Assert.IsNotNull(branchFromDb); Assert.AreEqual(branchCreation.Branch.BranchCode, branchFromDb.BranchCode); Assert.AreEqual(branchCreation.Address.Country, branchFromDb.BranchAddress.Country); Assert.AreEqual(branchCreation.Address.City, branchFromDb.BranchAddress.City); Assert.AreEqual(branchCreation.Address.Street, branchFromDb.BranchAddress.Street); Assert.AreEqual(branchCreation.Address.HouseNumber, branchFromDb.BranchAddress.HouseNumber); Assert.AreEqual(branchCreation.Address.ApartmentNumber, branchFromDb.BranchAddress.ApartmentNumber); Assert.AreEqual(branchCreation.Address.PostalCode, branchFromDb.BranchAddress.PostalCode); }
public void CreateBranchWithAddress_Should_ReturnBadRequest_When_BranchCodeIsAlreadyInUse() { // Arrange var branchCreation = new BranchWithAddressCreationDto { Branch = new BranchCreationDto { BranchCode = "000" }, Address = new AddressCreationDto { Country = "United States", City = "New York", Street = "Glenwood Ave", HouseNumber = "10", ApartmentNumber = "11", PostalCode = "10028" } }; // Act var result = _sut.CreateBranchWithAddress(branchCreation); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result.Result, typeof(BadRequestObjectResult)); var badRequestResult = result.Result as BadRequestObjectResult; Assert.IsNotNull(badRequestResult); Assert.IsInstanceOfType(badRequestResult.Value, typeof(SerializableError)); var error = badRequestResult.Value as SerializableError; Assert.IsNotNull(error); Assert.IsTrue(error.ContainsKey(nameof(branchCreation.Branch.BranchCode))); var branchCodeErrorValues = error[nameof(branchCreation.Branch.BranchCode)] as string[]; Assert.IsNotNull(branchCodeErrorValues); Assert.IsTrue(branchCodeErrorValues.Single() == "Branch code is already in use."); }
public ActionResult <BranchDto> CreateBranchWithAddress([FromBody] BranchWithAddressCreationDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_context.Branches.FirstOrDefault(b => b.BranchCode == model.Branch.BranchCode) != null) { ModelState.AddModelError(nameof(model.Branch.BranchCode), "Branch code is already in use."); return(BadRequest(ModelState)); } var branch = _mapper.Map <Branch>(model); _context.Branches.Add(branch); _context.SaveChanges(); var branchDto = _mapper.Map <Branch, BranchDto>(branch); return(CreatedAtRoute("GetBranch", new { branchId = branchDto.Id }, branchDto)); }