public async Task PostThirdParties_ShouldReturnOk() { // Arrange var model = new SegmentThirdPartyCreateViewModel { ParentRtbSegmentId = "prefix:parent", RtbSegmentId = "prefix:child", Name = "WJsHome", Description = "WJsHome Description" }; var segment = new Segment(); Mock.Mock <ISegmentService>().Setup(x => x.GetSegment(model.ParentRtbSegmentId)) .Returns(Task.FromResult(segment)); Mock.Mock <ISegmentService>().Setup(x => x.GetSegment(It.IsAny <int>())) .Returns(Task.FromResult(segment)); // Act var retVal = await Controller.PostThirdParties(model); // Assert Assert.That(retVal, Is.Not.Null); Assert.That(retVal, Is.TypeOf <CreatedAtRouteNegotiatedContentResult <SegmentViewModel> >()); Mock.Mock <ISegmentService>().Verify(x => x.CreateSegment(It.IsAny <Segment>()), Times.Once); }
public async Task <IHttpActionResult> PostThirdParties(SegmentThirdPartyCreateViewModel model) { if (model == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (string.IsNullOrEmpty(model.ParentRtbSegmentId)) { var claim = Request.GetOwinContext().Authentication.User.Claims.Where(x => x.Type == StandardClaimType.SegmentThirdPartyParent) .FirstOrDefault(x => x.Value.StartsWith(model.RtbSegmentId.Split(':')[0])); if (claim == null) { return(BadRequest($"The default parent segment for {model.RtbSegmentId} was not found.")); } // default parent model.ParentRtbSegmentId = claim.Value; } var parentSegment = await _segmentService.GetSegment(model.ParentRtbSegmentId).ConfigureAwait(false); if (parentSegment == null) { return(BadRequest("The specified parent segment was not found.")); } if (model.ParentRtbSegmentId.Split(':')[0] != model.RtbSegmentId.Split(':')[0]) { return(BadRequest("The rtb segment prefix must be the same as its parent.")); } var segment = _mapping.Map <Segment>(model, options => options.Items.Add(nameof(parentSegment), parentSegment)); await _segmentService.CreateSegment(segment).ConfigureAwait(false); segment = await _segmentService.GetSegment(segment.SegmentId).ConfigureAwait(false); var segmentViewModel = _mapping.Map <SegmentViewModel>(segment); return(CreatedAtRoute("Segments.GetById", new { Id = segmentViewModel.SegmentId }, segmentViewModel)); }