public void Post_GivenViewModel_ReturnsCreatedMeteredSpaceGETAtRoute() { //arrange _mockMeteredSpacesService .Setup(m => m.AddOrUpdate(It.IsAny <MeteredSpacePOST>())) .Returns <MeteredSpacePOST>(vm => new MeteredSpace { MeterId = vm.PoleSerialNumber, Active = vm.Status == 1 } ); var viewModelCollection = new MeteredSpacePOSTCollection { new MeteredSpacePOST { PoleSerialNumber = "Pole1", Status = 1 } }; //act IHttpActionResult actionResult = _controller.Post(viewModelCollection); var okResult = actionResult as OkNegotiatedContentResult <MeteredSpaceGET>; //assert Assert.IsNotNull(okResult); //Assert.AreEqual("MeteredSpaces", createdResult.RouteName); //Assert.AreEqual("Pole1", createdResult.RouteValues["id"]); Assert.IsNotNull(okResult.Content); Assert.AreEqual(true, okResult.Content.Active); Assert.AreEqual("Pole1", okResult.Content.MeterId); }
public IHttpActionResult Post([FromBody] MeteredSpacePOSTCollection postedMeteredSpaces) { if (postedMeteredSpaces == null) { Logger.Warning("POST to {0} with null model", RequestContext.RouteData.Route.RouteTemplate); return(BadRequest("Incoming data parsed to null entity model.")); } if (postedMeteredSpaces.Count == 0) { Logger.Warning("POST to {0} with empty model", RequestContext.RouteData.Route.RouteTemplate); return(BadRequest("Incoming data parsed to empty entity model.")); } MeteredSpace lastEntity = null; Exception lastException = null; for (int index = 0; index < postedMeteredSpaces.Count; index++) { try { lastEntity = _meteredSpacesService.AddOrUpdate(postedMeteredSpaces[index]); } catch (Exception ex) { lastException = ex; Logger.Error( ex, String.Format( "Server error on POST to {0} with model:{1}{2}", RequestContext.RouteData.Route.RouteTemplate, Environment.NewLine, postedMeteredSpaces[index].ToXmlString() ) ); } } //temporary because WebApi routes are registered with Route.Name = null, hence cannot be looked up by name //we should return CreatedAtRoute (201 with a location header) //instead we just return 200 with the entity //return CreatedAtRoute( // "MeteredSpaces", // new { id = lastEntity.MeterId }, // _meteredSpacesService.ConvertToViewModel(lastEntity) //); if (lastException == null) { return(Ok(_meteredSpacesService.ConvertToViewModel(lastEntity))); } else { return(InternalServerError(lastException)); } }
public void Post_GivenEmptyViewModel_ReturnsBadRequest() { var emptyViewModelCollection = new MeteredSpacePOSTCollection(); //act IHttpActionResult actionResult = _controller.Post(emptyViewModelCollection); //assert Assert.IsInstanceOf <BadRequestErrorMessageResult>(actionResult); }
public void Post_ReturnsInternalServerError_WhenServiceFails() { //arrnage var exception = new Exception("This is the exception mock"); _mockMeteredSpacesService .Setup(m => m.AddOrUpdate(It.IsAny <MeteredSpacePOST>())) .Throws(exception); var viewModelCollection = new MeteredSpacePOSTCollection { new MeteredSpacePOST() }; //act IHttpActionResult actionResult = _controller.Post(viewModelCollection); //assert Assert.IsInstanceOf <ExceptionResult>(actionResult); }
public void PolesCollectionXml_DeserializesTo_MeteredSpacePOSTCollection() { var serializer = new DataContractSerializer(typeof(MeteredSpacePOSTCollection)); string xml = @"<Poles> <Pole> <Area>WILSHIRE</Area> <Lat>34.026239</Lat> <Long>-118.489714</Long> <PoleSerialNumber>WIL1301</PoleSerialNumber> <Status>1</Status> <SubArea>1301 WILSHIRE BLVD</SubArea> <Zone>Santa Monica, CA Default Zone</Zone> </Pole> <Pole> <Area>WILSHIRE</Area> <Lat>34.026239</Lat> <Long>-118.489715</Long> <PoleSerialNumber>WIL1302</PoleSerialNumber> <Status>1</Status> <SubArea>1302 WILSHIRE BLVD</SubArea> <Zone>Santa Monica, CA Default Zone</Zone> </Pole> </Poles> "; MeteredSpacePOSTCollection poles = null; using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { poles = serializer.ReadObject(ms) as MeteredSpacePOSTCollection; } Assert.NotNull(poles); Assert.AreEqual(2, poles.Count); Assert.AreEqual("WIL1301", poles.First().PoleSerialNumber); Assert.AreEqual("WIL1302", poles.Last().PoleSerialNumber); }
public void Post_GivenViewModel_ReturnsCreatedMeteredSpaceGETAtRoute() { //arrange _mockMeteredSpacesService .Setup(m => m.AddOrUpdate(It.IsAny<MeteredSpacePOST>())) .Returns<MeteredSpacePOST>(vm => new MeteredSpace { MeterId = vm.PoleSerialNumber, Active = vm.Status == 1 } ); var viewModelCollection = new MeteredSpacePOSTCollection { new MeteredSpacePOST { PoleSerialNumber = "Pole1", Status = 1 } }; //act IHttpActionResult actionResult = _controller.Post(viewModelCollection); var okResult = actionResult as OkNegotiatedContentResult<MeteredSpaceGET>; //assert Assert.IsNotNull(okResult); //Assert.AreEqual("MeteredSpaces", createdResult.RouteName); //Assert.AreEqual("Pole1", createdResult.RouteValues["id"]); Assert.IsNotNull(okResult.Content); Assert.AreEqual(true, okResult.Content.Active); Assert.AreEqual("Pole1", okResult.Content.MeterId); }
public void Post_ReturnsInternalServerError_WhenServiceFails() { //arrnage var exception = new Exception("This is the exception mock"); _mockMeteredSpacesService .Setup(m => m.AddOrUpdate(It.IsAny<MeteredSpacePOST>())) .Throws(exception); var viewModelCollection = new MeteredSpacePOSTCollection { new MeteredSpacePOST() }; //act IHttpActionResult actionResult = _controller.Post(viewModelCollection); //assert Assert.IsInstanceOf<ExceptionResult>(actionResult); }
public void Post_GivenEmptyViewModel_ReturnsBadRequest() { var emptyViewModelCollection = new MeteredSpacePOSTCollection(); //act IHttpActionResult actionResult = _controller.Post(emptyViewModelCollection); //assert Assert.IsInstanceOf<BadRequestErrorMessageResult>(actionResult); }