// Updates hero public IHttpActionResult Put(string id, HeroPropertiesModel model) { Trace.TraceInformation($"Updating hero {id}: {model}"); if (!ModelState.IsValid) { return BadRequest(); } // Try to resolve hero Guid heroId = _cryptographyService.DecryptGuid(id); Hero hero = _heroRepository.GetHero(heroId); if (hero == null) { return NotFound(); } hero.Name = model.Name; hero.Type = model.Type; hero.State = model.State; hero.Price = model.Price.GetValueOrDefault(); Validate(hero); // Adds validation to ModelState if (!ModelState.IsValid) { return BadRequest(); } _heroRepository.Save(hero); HeroViewModel heroViewModel = new HeroViewModel(hero, _cryptographyService); return Ok(heroViewModel); }
public void HeroPropertiesModelBinderValueTest() { // Arrange Hero hero = HeroTest.Create(); HeroPropertiesModel expected = new HeroPropertiesModel(hero.Name, hero.Type, hero.State, hero.Price); const string encryptedTypeId = "encryptedTypeId"; const string encryptedStateId = "encryptedStateId"; Mock<ICryptographyService> cryptographyServiceMock = new Mock<ICryptographyService>(); cryptographyServiceMock .Setup(cs => cs.DecryptByte(It.Is<string>(m => m == encryptedTypeId))) .Returns(hero.Type.Id); cryptographyServiceMock .Setup(cs => cs.DecryptByte(It.Is<string>(m => m == encryptedStateId))) .Returns(hero.State.Id); Mock<IHeroRepository> heroRepositoryMock = new Mock<IHeroRepository>(); heroRepositoryMock .Setup(hr => hr.HeroTypes) .Returns(HeroRepository.Current.HeroTypes); heroRepositoryMock .Setup(hr => hr.HeroStates) .Returns(HeroRepository.Current.HeroStates); HttpActionContext httpActionContext = new HttpActionContext(); ModelBindingContext modelBindingContext = new ModelBindingContext(); Dictionary<string, string> values = new Dictionary<string, string> { { nameof(HeroPropertiesModel.Name), hero.Name }, { nameof(HeroPropertiesModel.Type), encryptedTypeId }, { nameof(HeroPropertiesModel.State), encryptedStateId }, { nameof(HeroPropertiesModel.Price), hero.Price.ToString() }, }; modelBindingContext.ValueProvider = new NameValuePairsValueProvider(values, CultureInfo.InvariantCulture); modelBindingContext.ModelMetadata = new ModelMetadata(new EmptyModelMetadataProvider(), null, () => null, typeof(HeroPropertiesModel), null); // Act bool bindingResult = new HeroPropertiesModelBinder(heroRepositoryMock.Object, cryptographyServiceMock.Object) .BindModel(httpActionContext, modelBindingContext); HeroPropertiesModel actual = modelBindingContext.Model as HeroPropertiesModel; // Assert Assert.IsTrue(bindingResult); Assert.IsNotNull(actual); Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.Type, actual.Type); Assert.AreEqual(expected.State, actual.State); }
// Creates and returns a hero. The hero is created with the initial state of Inactive public IHttpActionResult Post(HeroPropertiesModel model) { Trace.TraceInformation($"Creating hero {model}"); if (!ModelState.IsValid) { return BadRequest(); } HeroState inactive = _heroRepository.HeroStates[HeroState.Inactive]; Hero hero = new Hero(model.Name, model.Type, inactive, model.Price.GetValueOrDefault()); Validate(hero); // Adds validation to ModelState if (!ModelState.IsValid) { return BadRequest(); } _heroRepository.Save(hero); HeroViewModel heroViewModel = new HeroViewModel(hero, _cryptographyService); return Ok(heroViewModel); }
public void HeroControllerPostTest() { // Arrange DateTime now = DateTime.Now; Hero expectedHero = HeroTest.Create(); Hero actualHero = null; Mock<IHeroRepository> heroRepositoryMock = new Mock<IHeroRepository>(); heroRepositoryMock .Setup(hr => hr.Save(It.IsAny<Hero>())) .Callback<Hero>(h => actualHero = h); heroRepositoryMock .Setup(hr => hr.HeroStates) .Returns(HeroRepository.Current.HeroStates); // Use the actual lookup values here Mock<ICryptographyService> cryptographyServiceMock = new Mock<ICryptographyService>(); HeroPropertiesModel model = new HeroPropertiesModel(expectedHero.Name, expectedHero.Type, expectedHero.State, expectedHero.Price); // Act OkNegotiatedContentResult<HeroViewModel> actual = new HeroController(heroRepositoryMock.Object, cryptographyServiceMock.Object) { Configuration = new HttpConfiguration() } .Post(model) as OkNegotiatedContentResult<HeroViewModel>; // Assert Assert.IsNotNull(actual); Assert.IsNotNull(actual.Content); Assert.AreEqual(expectedHero.Name, actualHero.Name); Assert.AreEqual(expectedHero.Name, actual.Content.Name); Assert.AreEqual(expectedHero.Type, actualHero.Type); Assert.AreEqual(expectedHero.Price, actualHero.Price); Assert.AreEqual(expectedHero.Price, actual.Content.Price); }