public void MappingTest()
        {
            //Arrange
            var dataStorage = new Mock<IDataStorage>();
            var entity = new AdverseEventFormData() {
                Id = 1,
                AdverseExperience = new Question {Id = 2, Value = "2"},
                EndDate = new Question {Id = 3, Value = "3"},
                EndTime = new Question {Id = 4, Value = "4"},
                OnsetDate = new Question {Id = 5, Value = "5"},
                OnsetTime = new Question {Id = 6, Value = "6"},
                Outcome = new Question {Id = 7, Value = "7"},
                Intensity = new Question {Id = 8, Value = "8"},
                RelationshipToInvestigationalDrug = new Question {Id = 9, Value = "9"}
            };
            var updatedEntity = new AdverseEventFormData() {
                Id = 1,
                AdverseExperience = new Question {Id = 3, Value = "3"},
                EndDate = new Question {Id = 4, Value = "4"},
                EndTime = new Question {Id = 5, Value = "5"},
                OnsetDate = new Question {Id = 6, Value = "6"},
                OnsetTime = new Question {Id = 7, Value = "7"},
                Outcome = new Question {Id = 8, Value = "8"},
                Intensity = new Question {Id = 9, Value = "9"},
                RelationshipToInvestigationalDrug = new Question {Id = 10, Value = "10"}
            };
            var repository = new AdverseEventFormDataRepository(dataStorage.Object);

            dataStorage.Setup(ds => ds.GetData<AdverseEventFormData>()).Returns(new List<AdverseEventFormData> {entity});
            //Act
            repository.Edit(updatedEntity);
            //Assert
            //Values are equal
            Assert.That(entity.AdverseExperience.Value, Is.EqualTo(updatedEntity.AdverseExperience.Value));
            Assert.That(entity.EndDate.Value, Is.EqualTo(updatedEntity.EndDate.Value));
            Assert.That(entity.EndTime.Value, Is.EqualTo(updatedEntity.EndTime.Value));
            Assert.That(entity.OnsetDate.Value, Is.EqualTo(updatedEntity.OnsetDate.Value));
            Assert.That(entity.OnsetTime.Value, Is.EqualTo(updatedEntity.OnsetTime.Value));
            Assert.That(entity.Outcome.Value, Is.EqualTo(updatedEntity.Outcome.Value));
            Assert.That(entity.Intensity.Value, Is.EqualTo(updatedEntity.Intensity.Value));
            Assert.That(entity.RelationshipToInvestigationalDrug.Value,
                        Is.EqualTo(updatedEntity.RelationshipToInvestigationalDrug.Value));

            //Objects are not equal
            Assert.That(entity.AdverseExperience, Is.Not.EqualTo(updatedEntity.AdverseExperience));
            Assert.That(entity.EndDate, Is.Not.EqualTo(updatedEntity.EndDate));
            Assert.That(entity.EndTime, Is.Not.EqualTo(updatedEntity.EndTime));
            Assert.That(entity.OnsetDate, Is.Not.EqualTo(updatedEntity.OnsetDate));
            Assert.That(entity.OnsetTime, Is.Not.EqualTo(updatedEntity.OnsetTime));
            Assert.That(entity.Outcome, Is.Not.EqualTo(updatedEntity.Outcome));
            Assert.That(entity.Intensity, Is.Not.EqualTo(updatedEntity.Intensity));
            Assert.That(entity.RelationshipToInvestigationalDrug, Is.Not.EqualTo(updatedEntity.RelationshipToInvestigationalDrug));
        }
        public void AdverseEventFormDataToViewModel_FullData()
        {
            //Arrange
            var aFormData = new AdverseEventFormData {
                Id = 6,
                Form =
                    new Form {
                        Id = 16,
                        Caption = "Form16",
                        FormState = FormState.Completed,
                        Visit =
                            new Visit {Caption = "Visit16", Patient = new Patient {PatientNumber = 160}}
                    },
                AdverseExperience =
                    new Question {Id = 3, DataType = QuestionDataType.String, Value = "Broken legs"},
                OnsetDate =
                    new Question {
                        Id = 4,
                        DataType = QuestionDataType.Date,
                        Value =
                            new DateTime(2011, 11, 8, 9, 10, 11).ToString(
                                CultureInfo.InvariantCulture)
                    },
                OnsetTime =
                    new Question {
                        Id = 5,
                        DataType = QuestionDataType.Time,
                        Value =
                            new DateTime(2011, 11, 8, 9, 10, 11).ToString(
                                CultureInfo.InvariantCulture)
                    },
                EndDate =
                    new Question {
                        Id = 6,
                        DataType = QuestionDataType.Date,
                        Value =
                            new DateTime(2012, 1, 10, 11, 12, 13).ToString(
                                CultureInfo.InvariantCulture)
                    },
                EndTime =
                    new Question {
                        Id = 7,
                        DataType = QuestionDataType.Date,
                        Value =
                            new DateTime(2012, 1, 10, 11, 12, 13).ToString(
                                CultureInfo.InvariantCulture)
                    },
                Outcome = new Question {Id = 8, DataType = QuestionDataType.Enum, Value = "1"},
                Intensity = new Question {Id = 9, DataType = QuestionDataType.Enum, Value = "2"},
                RelationshipToInvestigationalDrug = new Question {Id = 10, DataType = QuestionDataType.Enum, Value = "3"}
            };
            //Act
            AdverseEventFormViewModel adverseEvent = formController.MapAdverseEventFormDataToViewModel(aFormData);

            //Assert
            Assert.That(adverseEvent.FormId, Is.EqualTo(16));
            Assert.That(adverseEvent.FormCaption, Is.EqualTo("Form16"));
            Assert.That(adverseEvent.IsCompleted, Is.True);
            Assert.That(adverseEvent.VisitName, Is.EqualTo("Visit16"));
            Assert.That(adverseEvent.PatientNumber, Is.EqualTo(160));
            Assert.That(adverseEvent.Id, Is.EqualTo(6));
            Assert.That(adverseEvent.AdverseExperience, Is.EqualTo("Broken legs"));

            Assert.That(adverseEvent.OnsetDate, Is.Not.Null);
            Assert.That(adverseEvent.OnsetDate.Value.ToShortDateString(),
                        Is.EqualTo(new DateTime(2011, 11, 8).ToShortDateString()));
            Assert.That(adverseEvent.OnsetTime, Is.Not.Null);
            Assert.That(adverseEvent.OnsetTime.Value.ToLongTimeString(),
                        Is.EqualTo(new DateTime(2011, 12, 5, 9, 10, 11).ToLongTimeString()));

            Assert.That(adverseEvent.EndDate, Is.Not.Null);
            Assert.That(adverseEvent.EndDate.Value.ToShortDateString(),
                        Is.EqualTo(new DateTime(2012, 1, 10).ToShortDateString()));
            Assert.That(adverseEvent.EndTime, Is.Not.Null);
            Assert.That(adverseEvent.EndTime.Value.ToLongTimeString(),
                        Is.EqualTo(new DateTime(2011, 12, 5, 11, 12, 13).ToLongTimeString()));

            Assert.That(adverseEvent.Outcome, Is.EqualTo(1));
            Assert.That(adverseEvent.Intensity, Is.EqualTo(2));
            Assert.That(adverseEvent.RelationshipToInvestigationalDrug, Is.EqualTo(3));

            Assert.That(adverseEvent.AdverseExperienceQuestionId, Is.EqualTo(aFormData.AdverseExperience.Id));
            Assert.That(adverseEvent.OnsetDateQuestionId, Is.EqualTo(aFormData.OnsetDate.Id));
            Assert.That(adverseEvent.OnsetTimeQuestionId, Is.EqualTo(aFormData.OnsetTime.Id));
            Assert.That(adverseEvent.EndDateQuestionId, Is.EqualTo(aFormData.EndDate.Id));
            Assert.That(adverseEvent.EndTimeQuestionId, Is.EqualTo(aFormData.EndTime.Id));
            Assert.That(adverseEvent.OutcomeQuestionId, Is.EqualTo(aFormData.Outcome.Id));
            Assert.That(adverseEvent.IntensityQuestionId, Is.EqualTo(aFormData.Intensity.Id));
            Assert.That(adverseEvent.RelationshipToInvestigationalDrugQuestionId,
                        Is.EqualTo(aFormData.RelationshipToInvestigationalDrug.Id));
        }
            private BaseFormData CreateTestCrf(FormType formType, int formId, int question1, int question2)
            {
                BaseFormData formData;

                if (formType == FormType.Demographics) {
                    formData = new DemographicFormData {
                        Id = formId,
                        Race = new Question {Value = question1.ToString()},
                        Sex = new Question {Value = question2.ToString()}
                    };
                }
                else {
                    formData = new AdverseEventFormData {
                        Id = formId,
                        Intensity = new Question {Value = question1.ToString()},
                        RelationshipToInvestigationalDrug = new Question {Value = question2.ToString()}
                    };
                }

                return formData;
            }
            public void PostEditAdverseEventFormData_ChangedValues_IncompleteForm_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Incomplete,
                    Caption = "TestForm",
                    Visit = new Visit { Caption = "TestVisit", Patient = new Patient { PatientNumber = 123 } }
                };

                var formData = new AdverseEventFormData {
                    Id = 12,
                    Form = form,
                    OnsetDate = new Question() { Id = 101, Value = new DateTime(2011, 10, 11).ToString(CultureInfo.InvariantCulture) },
                    OnsetTime = new Question() { Id = 102, Value = new DateTime(2010, 11, 12, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    EndDate = new Question() { Id = 103, Value = new DateTime(2012, 1, 2).ToString(CultureInfo.InvariantCulture) },
                    EndTime = new Question() { Id = 104, Value = new DateTime(2011, 9, 3, 3, 4, 5).ToString(CultureInfo.InvariantCulture) },
                    AdverseExperience = new Question() { Id = 105, Value = "Broken neck" },
                    Intensity = new Question() { Id = 106, Value = "1" },
                    Outcome = new Question() { Id = 107, Value = "2" },
                    RelationshipToInvestigationalDrug = new Question() { Id = 108, Value = "3" }
                };
                var incomingModel = new AdverseEventFormViewModel {
                    Id = 12,
                    FormId = 13,
                    OnsetDate = new DateTime(2010, 1, 2),
                    OnsetTime = new DateTime(2011, 1, 1, 5, 4, 3),
                    EndDate = new DateTime(2011, 11, 12),
                    EndTime = new DateTime(2012, 1, 1, 14, 15, 16),
                    Intensity = 4,
                    Outcome = 5,
                    RelationshipToInvestigationalDrug = 6
                };

                bool isValid = true;
                Mock<IHtmlEditorAdapter> htmlHelper = new Mock<IHtmlEditorAdapter>();
                htmlHelper.Setup(hh => hh.GetActiveView(It.IsAny<string>())).Returns(null);
                htmlHelper.Setup(hh => hh.GetHtmlView(It.IsAny<string>(),
                                                      It.IsAny<ASPxHtmlEditorHtmlEditingSettings>(),
                                                      It.IsAny<HtmlEditorValidationSettings>(),
                                                      It.IsAny<EventHandler<HtmlEditorValidationEventArgs>>(),
                                                      out isValid)).Returns("Broken brain");

                formController.HtmlEditorAdapter = htmlHelper.Object;

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);

                //here we return "formdata before editing" - this data should be overwritten from model
                afdRep.Setup(r => r.GetByKey(incomingModel.Id)).Returns(formData);

                //as checking of saved formdata is quite complicated, we will save the passed object and inspect it later
                AdverseEventFormData savedFormData = null;
                afdRep.Setup(r => r.Edit(It.IsAny<AdverseEventFormData>())).Callback<AdverseEventFormData>(fd => savedFormData = fd);

                Form savedForm = null;
                fRep.Setup(r => r.Edit(It.IsAny<Form>())).Callback<Form>(f => savedForm = f);

                //Act
                var result = formController.EditAdverseEventForm(incomingModel);

                //Assert
                CheckEditFormDataRedirectRouteValues(result, form);

                afdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                afdRep.Verify(r => r.Edit(It.IsAny<AdverseEventFormData>()), Times.Once());
                afdRep.Verify(r => r.Save(), Times.Once());

                fRep.Verify(r => r.GetByKey(incomingModel.FormId), Times.Once());
                fRep.Verify(r => r.Edit(It.IsAny<Form>()), Times.Once());
                fRep.Verify(r => r.Save(), Times.Once());

                Assert.That(savedFormData, Is.Not.Null);
                Assert.That(savedFormData.Id, Is.EqualTo(incomingModel.Id));
                Assert.That(savedFormData.OnsetDate.Value,
                            Is.EqualTo(new DateTime(2010, 1, 2).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.OnsetTime.Value,
                            Is.EqualTo(new DateTime(2011, 1, 1, 5, 4, 3).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.EndDate.Value,
                            Is.EqualTo(new DateTime(2011, 11, 12).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.EndTime.Value,
                            Is.EqualTo(new DateTime(2012, 1, 1, 14, 15, 16).ToString(CultureInfo.InvariantCulture)));
                Assert.That(savedFormData.AdverseExperience.Value, Is.EqualTo("Broken brain"));
                Assert.That(savedFormData.Intensity.Value, Is.EqualTo("4"));
                Assert.That(savedFormData.Outcome.Value, Is.EqualTo("5"));
                Assert.That(savedFormData.RelationshipToInvestigationalDrug.Value, Is.EqualTo("6"));

                Assert.That(savedForm, Is.Not.Null);
                Assert.That(savedForm.Id, Is.EqualTo(incomingModel.FormId));
                Assert.That(savedForm.FormState, Is.EqualTo(FormState.Completed));
            }
            public void PostEditAdverseEventFormData_ChangedUnconfirmedValues_Test()
            {
                //Arrange
                EmulateControllerContext(formController, false);
                var form = new Form {
                    Id = 13,
                    FormState = FormState.Completed,
                    Caption = "TestForm",
                    Visit = new Visit { Caption = "TestVisit", Patient = new Patient { PatientNumber = 123 } }
                };

                var formData = new AdverseEventFormData {
                    Id = 12,
                    Form = form,
                    OnsetDate = new Question() { Id = 101, Value = new DateTime(2011, 10, 11).ToString(CultureInfo.InvariantCulture) },
                    OnsetTime = new Question() { Id = 102, Value = new DateTime(2010, 11, 12, 12, 13, 14).ToString(CultureInfo.InvariantCulture) },
                    EndDate = new Question() { Id = 103, Value = new DateTime(2012, 1, 2).ToString(CultureInfo.InvariantCulture) },
                    EndTime = new Question() { Id = 104, Value = new DateTime(2011, 9, 3, 3, 4, 5).ToString(CultureInfo.InvariantCulture) },
                    AdverseExperience = new Question() { Id = 105, Value = "Broken neck" },
                    Intensity = new Question() { Id = 106, Value = "1" },
                    Outcome = new Question() { Id = 107, Value = "2" },
                    RelationshipToInvestigationalDrug = new Question() { Id = 108, Value = "3" }
                };
                var incomingModel = new AdverseEventFormViewModel {
                    Id = 12,
                    FormId = 13,
                    OnsetDate = new DateTime(2010, 1, 2),
                    OnsetTime = new DateTime(2011, 1, 1, 5, 4, 3),
                    EndDate = new DateTime(2011, 11, 12),
                    EndTime = new DateTime(2012, 1, 1, 14, 15, 16),
                    Intensity = 2,
                    Outcome = 1,
                    RelationshipToInvestigationalDrug = 2
                };

                bool isValid = true;
                Mock<IHtmlEditorAdapter> htmlHelper = new Mock<IHtmlEditorAdapter>();
                htmlHelper.Setup(hh => hh.GetActiveView(It.IsAny<string>())).Returns(null);
                htmlHelper.Setup(hh => hh.GetHtmlView(It.IsAny<string>(),
                                                      It.IsAny<ASPxHtmlEditorHtmlEditingSettings>(),
                                                      It.IsAny<HtmlEditorValidationSettings>(),
                                                      It.IsAny<EventHandler<HtmlEditorValidationEventArgs>>(),
                                                      out isValid)).Returns("Broken brain");

                formController.HtmlEditorAdapter = htmlHelper.Object;

                fRep.Setup(r => r.GetByKey(incomingModel.FormId)).Returns(form);
                //here we return "formdata before editing" - this data should not be overwritten from model as no any data change note provided
                afdRep.Setup(r => r.GetByKey(incomingModel.Id)).Returns(formData);

                //Act
                var result = formController.EditAdverseEventForm(incomingModel);

                //Assert
                afdRep.Verify(r => r.GetByKey(incomingModel.Id), Times.Once());
                afdRep.Verify(r => r.Edit(It.IsAny<AdverseEventFormData>()), Times.Never());
                afdRep.Verify(r => r.Save(), Times.Never());

                fRep.Verify(r => r.GetByKey(incomingModel.FormId), Times.Once());
                fRep.Verify(r => r.Edit(It.IsAny<Form>()), Times.Never());
                fRep.Verify(r => r.Save(), Times.Never());

                Assert.That(result, Is.Not.Null);
                Assert.That(result is ViewResultBase);
                var viewResultBase = result as ViewResultBase;
                Assert.That(viewResultBase.Model, Is.Not.Null);
                AdverseEventFormViewModel model = viewResultBase.Model as AdverseEventFormViewModel;
                Assert.That(model, Is.Not.Null);

                Assert.That(model.DataChangeReasonRequired);
                Assert.That(model.ChangeInfos, Is.Not.Null);
                Assert.That(model.ChangeInfos.Count, Is.EqualTo(8));

                AssertChangeInfo(formData.OnsetDate, DateTime.Parse(formData.OnsetDate.Value).ToShortDateString(), incomingModel.OnsetDate.Value.ToShortDateString(), model);
                AssertChangeInfo(formData.OnsetTime, "12:13 PM", "5:04 AM", model);

                AssertChangeInfo(formData.EndDate, DateTime.Parse(formData.EndDate.Value).ToShortDateString(), incomingModel.EndDate.Value.ToShortDateString(), model);
                AssertChangeInfo(formData.EndTime, "3:04 AM", "2:15 PM", model);

                AssertChangeInfo(formData.AdverseExperience, "Broken neck", "Broken brain", model);
                AssertChangeInfo(formData.Intensity, "Mild", "Moderate", model);
                AssertChangeInfo(formData.Outcome, "Ongoing", "Resolved", model);
                AssertChangeInfo(formData.RelationshipToInvestigationalDrug, "Suspected (reasonably possible)", "Unlikely", model);
            }
 private AdverseEventFormData AddAdverseEventFormData(Form form)
 {
     var formData = new AdverseEventFormData() {
         Form = form,
         AdverseExperience =
             new Question { Caption = "Adverse Experience", DataType = QuestionDataType.String, Value = null, Form = form },
         OnsetDate =
             new Question { Caption = "Onset Date", DataType = QuestionDataType.Date, Value = null, Form = form },
         OnsetTime =
             new Question { Caption = "Onset Time", DataType = QuestionDataType.Time, Value = null, Form = form },
         EndDate =
             new Question { Caption = "End Date", DataType = QuestionDataType.Date, Value = null, Form = form },
         EndTime =
             new Question { Caption = "End Time", DataType = QuestionDataType.Time, Value = null, Form = form },
         Outcome =
             new Question { Caption = "Outcome", DataType = QuestionDataType.Enum, Value = null, Form = form },
         Intensity =
             new Question { Caption = "Intensity", DataType = QuestionDataType.Enum, Value = null, Form = form },
         RelationshipToInvestigationalDrug =
             new Question { Caption = "Relationship to Investigational Drug", DataType = QuestionDataType.Enum, Value = null, Form = form }
     };
     AdverseEventFormDataRepository.Add(formData);
     QuestionRepository.Add(formData.AdverseExperience);
     QuestionRepository.Add(formData.OnsetDate);
     QuestionRepository.Add(formData.OnsetTime);
     QuestionRepository.Add(formData.EndDate);
     QuestionRepository.Add(formData.EndTime);
     QuestionRepository.Add(formData.Outcome);
     QuestionRepository.Add(formData.Intensity);
     QuestionRepository.Add(formData.RelationshipToInvestigationalDrug);
     return formData;
 }