public IHttpActionResult Patch([FromODataUri] int key, Delta<Product> patch) { if (!ModelState.IsValid) { return BadRequest(ModelState); } Delta<V2VM.Product> v2Patch = new Delta<V2VM.Product>(); foreach (string name in patch.GetChangedPropertyNames()) { object value; if (patch.TryGetPropertyValue(name, out value)) { v2Patch.TrySetPropertyValue(name, value); } } var v2Product = _repository.Patch((long)key, v2Patch, Request); return Updated(Mapper.Map<Product>(v2Product)); }
public void CanPut_OpenType() { // Arrange var address = new SimpleOpenAddress { City = "City", Street = "Street", Properties = new Dictionary <string, object> { { "IntProp", 9 }, { "ListProp", new List <int> { 1, 2, 3 } } } }; PropertyInfo propertyInfo = typeof(SimpleOpenAddress).GetProperty("Properties"); var delta = new Delta <SimpleOpenAddress>(typeof(SimpleOpenAddress), null, propertyInfo); delta.TrySetPropertyValue("City", "ChangedCity"); delta.TrySetPropertyValue("IntProp", 1); // Act delta.Put(address); // Assert Assert.Equal("ChangedCity", address.City); Assert.Null(address.Street); Assert.Equal(1, address.Properties["IntProp"]); Assert.False(address.Properties.ContainsKey("ListProp")); }
public void RoundTrip_Properties(string propertyName, object value) { Delta <DeltaModel> delta = new Delta <DeltaModel>(); Type propertyType; Assert.True(delta.TryGetPropertyType(propertyName, out propertyType)); Assert.True(delta.TrySetPropertyValue(propertyName, value)); object retrievedValue; delta.TryGetPropertyValue(propertyName, out retrievedValue); Assert.Equal(value, retrievedValue); }
public void Put_DoesNotClear_NonUpdatableProperties() { // Arrange string expectedString = "hello, world"; int expectedInt = 24; var delta = new Delta <Base>(typeof(Base), new[] { "BaseInt" }); delta.TrySetPropertyValue("BaseInt", expectedInt); Base entity = new Base { BaseInt = 42, BaseString = expectedString }; // Act delta.Put(entity); // Assert Assert.Equal(expectedInt, entity.BaseInt); Assert.Equal(expectedString, entity.BaseString); }
public async Task CompaniesController_Patch_WhenICallPatch() { // Given var companiesController = _container.Resolve<CompaniesController>(); // When const int id = 2; var delta = new Delta<TranslatedCompany>(); delta.TrySetPropertyValue("Logo", Guid.NewGuid().ToString()); delta.TrySetPropertyValue("Headquarters", Guid.NewGuid().ToString()); var updateResult = await companiesController.Patch(id, delta) as UpdatedODataResult<TranslatedCompany>; // Then Assert.NotNull(updateResult); Assert.NotNull(updateResult.Entity); var company = delta.GetEntity(); Assert.Equal(id, updateResult.Entity.Id); Assert.Equal(company.Logo, updateResult.Entity.Logo); Assert.Equal(company.Headquarters, updateResult.Entity.Headquarters); }
public IHttpActionResult Patch(int key, Delta<Window> delta) { delta.TrySetPropertyValue("Id", key); // It is the key property, and should not be updated. Window window = _windows.FirstOrDefault(e => e.Id == key); if (window == null) { window = new Window(); delta.Patch(window); return Created(window); } delta.Patch(window); return Ok(window); }
public IHttpActionResult PatchPremiumAccount(int key, Delta<PremiumAccount> delta) { var originalAccount = _dataSource.Accounts.Single(a => a.AccountID == key) as PremiumAccount; delta.TrySetPropertyValue("AccountID", originalAccount.AccountID); // It is the key property, and should not be updated. delta.Patch(originalAccount); return Ok(originalAccount); }
private async Task PutIMEIToCallsign(int id, string callsign, string imei, VehicleType? type, ResultType expectedResult = ResultType.Success) { var logService = CreateMockLogService(); var service = CreateMockIMEIService(); var controller = new IMEIController(service.Object, logService.Object); var config = new Mock<HttpConfiguration>(); var principal = MockHelpers.CreateMockPrincipal(TestUsername); controller.User = principal.Object; controller.Configuration = config.Object; var delta = new Delta<IMEIToCallsign>(); if (!string.IsNullOrEmpty(imei)) delta.TrySetPropertyValue("IMEI", imei); if (!string.IsNullOrEmpty(callsign)) delta.TrySetPropertyValue("CallSign", callsign); if (type != null) delta.TrySetPropertyValue("Type", type); var res = await controller.Put(id, delta); switch (expectedResult) { case ResultType.ModelError: Assert.IsType<InvalidModelStateResult>(res); logService.Verify(l => l.LogIMEIRegistered(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<VehicleType>()), Times.Never); break; case ResultType.NotFoundError: Assert.IsType<NotFoundResult>(res); logService.Verify(l => l.LogIMEIRegistered(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<VehicleType>()), Times.Never); break; case ResultType.Success: Assert.IsType<UpdatedODataResult<IMEIToCallsign>>(res); service.Verify(i => i.RegisterCallsign(imei, callsign, type)); logService.Verify(l => l.LogIMEIRegistered(TestUsername, imei, callsign, type ?? VehicleType.Unknown)); break; } }