public void POST_Edit_Success() { //Arrange var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "123"), }); var principal = new ClaimsPrincipal(identity); var context = new Mock <HttpContextBase>(); context.SetupGet(x => x.User).Returns(principal); var routeData = new RouteData(); _controller.ControllerContext = new ControllerContext(context.Object, routeData, _controller); var viewModel = new EditNbrSurveillanceViewModel(); _patientService.Setup(x => x.IsClosed(It.IsAny <int>())).Returns(false); _nbrSurveillanceService.Setup(x => x.Update(It.IsAny <EditNbrSurveillanceViewModel>())).Returns(true); _nbrSurveillanceService.Setup(x => x.Log(It.IsAny <OperationType>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())); //Act var result = (RedirectToRouteResult)_controller.Edit(1, viewModel); //Assert Assert.That(result.RouteValues["action"], Is.EqualTo("View")); }
public void Update() { //Arrange var surveillance = _dataContext.NbrSurveillances .Include(s => s.CnsExploration) .Include(s => s.Analysis) .First(); var viewModel = new EditNbrSurveillanceViewModel(surveillance) { Id = surveillance.Id, Eeg = Electroencephalogram.Slow, CnsExploration = { Reflexes = Reflexes.Irregular }, Analysis = { Cpk = 20 } }; //Act var updated = _nbrSurveillanceService.Update(viewModel); surveillance = _nbrSurveillanceService.FindById(viewModel.Id); //Assert Assert.That(updated, Is.True); Assert.That(surveillance.Eeg, Is.EqualTo(Electroencephalogram.Slow)); Assert.That(surveillance.CnsExploration.Reflexes, Is.EqualTo(Reflexes.Irregular)); Assert.That(surveillance.Analysis.Cpk, Is.EqualTo(20)); }
public void Create_RepeatedTimeSlot() { //Arrange var viewModel = new EditNbrSurveillanceViewModel { TimeSlot = TimeSlot.H72, Eeg = Electroencephalogram.Normal, AEeg = AElectroencephalogram.BurstSupression, TfUltrasound = TransfontanellarUltrasound.Normal, CnsExploration = new CnsExplorationViewModel(), Analysis = new AnalysisViewModel(), ModelState = new ModelStateDictionary() }; var patientId = _dataContext.Patients.First().Id; _nbrSurveillanceService.Create(viewModel, patientId); //Act _nbrSurveillanceService.Create(viewModel, patientId); var surveillances = _nbrSurveillanceService.FindByPatientId(patientId); //Assert Assert.That(viewModel.ModelState.IsValid, Is.False); Assert.That(surveillances.Count, Is.EqualTo(1)); }
/// <summary> /// Save a new NbrSurveillance and it's related CnsExploration to the database with the data from /// a viewmodel. If the TimeSlot is repeated, an error will be added to the modelstate of the /// viewmodel and the operation will not be completed. /// </summary> /// <param name="viewModel">Viewmodel with the new surveillance data.</param> /// <param name="patientId">Id of the patient for which the surveillance is being created.</param> public void Create(EditNbrSurveillanceViewModel viewModel, int patientId) { //Check that the time slot for the surveillance is not repeated var repeated = Repository.NbrSurveillances .Where(s => s.PatientId == patientId) .Any(s => s.TimeSlot == viewModel.TimeSlot); //If repeated show error to the user if (repeated) { viewModel.ModelState.AddModelError("TimeSlot", Strings.ErrorRepeated); } else { //Create a new surveillance from the viewmodel var surveillance = viewModel.ToNewModel(); surveillance.PatientId = patientId; //Create the associated CnsExploration var explorationId = _cnsExplorationService.Create(viewModel.CnsExploration); surveillance.CnsExplorationId = explorationId; //Create the associated Analysis var analysisId = _analysisService.Create(viewModel.Analysis); surveillance.AnalysisId = analysisId; Repository.NbrSurveillances.Add(surveillance); Save(); } }
public ActionResult Create(int fromId, EditNbrSurveillanceViewModel nbrSurveillanceViewModel) { if (!ModelState.IsValid) { return(View(nbrSurveillanceViewModel)); } //Check that the tracking is not closed if (_patientService.IsClosed(fromId)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } nbrSurveillanceViewModel.ModelState = ModelState; _nbrSurveillanceService.Create(nbrSurveillanceViewModel, fromId); //Check validation again for repeated time slots if (!ModelState.IsValid) { return(View(nbrSurveillanceViewModel)); } //Check if patient status needs to be updated _patientService.UpdateStatus(fromId); _nbrSurveillanceService.Log(OperationType.NbrSurveillanceCreate, User.Identity.GetUserId <int>(), fromId); return(RedirectToAction("View", "NbrSurveillance", new { id = fromId })); }
public ActionResult Edit(int fromId, EditNbrSurveillanceViewModel nbrSurveillance) { if (!ModelState.IsValid) { return(View(nbrSurveillance)); } //Check that the tracking is not closed if (_patientService.IsClosed(nbrSurveillance.Id)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var updated = _nbrSurveillanceService.Update(nbrSurveillance); //Update failed because of a wrong id if (!updated) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //Check if patient status needs to be updated _patientService.UpdateStatus(fromId); //Update successful, log operation and redirect to view nbr surveillances _nbrSurveillanceService.Log(OperationType.NbrSurveillanceUpdate, User.Identity.GetUserId <int>(), nbrSurveillance.Id); return(RedirectToAction("View", new { id = fromId })); }
public void POST_Create_Closed() { //Arrange var viewModel = new EditNbrSurveillanceViewModel(); _patientService.Setup(x => x.IsClosed(It.IsAny <int>())).Returns(true); //Act var result = (HttpStatusCodeResult)_controller.Create(1, viewModel); //Assert Assert.That(result.StatusCode, Is.EqualTo((int)HttpStatusCode.BadRequest)); }
public void POST_Create_InvalidModel() { //Arrange var viewModel = new EditNbrSurveillanceViewModel(); _controller.ModelState.AddModelError("Error", @"ModelError"); //Act var result = (ViewResult)_controller.Create(1, viewModel); //Assert Assert.That(result.ViewName, Is.Empty); }
public void Update_NotFound() { //Arrange var viewModel = new EditNbrSurveillanceViewModel { Id = -123 }; //Act var updated = _nbrSurveillanceService.Update(viewModel); //Assert Assert.That(updated, Is.False); }
/// <summary> /// Update the NbrSurveillance with the data from the given viewmodel. /// </summary> /// <param name="viewModel">Viewmodel with the modified data.</param> /// <returns>bool indicating if the update was successful.</returns> public bool Update(EditNbrSurveillanceViewModel viewModel) { var nbrSurveillance = Repository.NbrSurveillances .SingleOrDefault(n => n.Id == viewModel.Id); if (nbrSurveillance == null) { return(false); } //Update the surveillance with the viewmodel data viewModel.ToModel(nbrSurveillance); Repository.Entry(nbrSurveillance).State = EntityState.Modified; //Update associated CnsExploration _cnsExplorationService.Update(viewModel.CnsExploration); //Update associated Analysis _analysisService.Update(viewModel.Analysis); Save(); return(true); }