Ejemplo n.º 1
0
        public void CanCreateTest2()
        {
            //Arrange
            var service = GetDecisionService();
            CreateFormQuestion question = new CreateFormQuestion
            {
                AnniversaryDate = new DateTimeOffset(2017, 10, 31, 0, 0, 0, TimeSpan.Zero),
                Culture         = "en-US",
                SubjectId       = 12345,
                SvfDate         = new DateTimeOffset(2017, 5, 1, 0, 0, 0, TimeSpan.Zero),
                UserId          = 12345,
                Form            = "Initial Enrollment"
            };

            question.MostRecentForms.Add(new FormStatus
            {
                Form          = "Initial Enrollment",
                Status        = "IN_PROCESS",
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });

            //Act
            CreateFormAnswer answer = service.CanCreateForm(question);

            //Assert
            Assert.IsNotNull(answer);
            Assert.IsFalse(answer.Error);
            Assert.IsFalse(answer.CanCreate);
            Assert.IsFalse(String.IsNullOrWhiteSpace(answer.Explanation));
        }
        public static CreateFormFact ToFact(this CreateFormQuestion question)
        {
            if (String.IsNullOrWhiteSpace(question.Form))
            {
                throw new ArgumentException("question.Form was null or whitespace", "question");
            }
            CreateFormFact fact = new CreateFormFact();

            fact.Form            = question.Form.Trim();
            fact.AnniversaryDate = question.AnniversaryDate;
            fact.SvfDate         = question.SvfDate;

            if (question.MostRecentForms == null || question.MostRecentForms.Count == 0)
            {
                return(fact);
            }
            question.MostRecentForms.ForEach(x =>
            {
                if (String.IsNullOrWhiteSpace(x.Form))
                {
                    throw new ArgumentException("FormStatus.Form was null or whitespace", "question");
                }
                if (String.IsNullOrWhiteSpace(x.Status))
                {
                    throw new ArgumentException("FormStatus.Status was null or whitespace", "question");
                }
                string form   = x.Form.Trim();
                string status = x.Status.Trim();
                if (form.Equals(Form.INITIAL_ENROLLMENT, StringComparison.OrdinalIgnoreCase))
                {
                    fact.InitialEnrollmentStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.REENROLLMENT, StringComparison.OrdinalIgnoreCase))
                {
                    fact.ReenrollmentStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.SVF_WITH_CHANGES, StringComparison.OrdinalIgnoreCase))
                {
                    fact.SvfWithChangesStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else if (form.Equals(Form.SVF_NO_CHANGES, StringComparison.OrdinalIgnoreCase))
                {
                    fact.SvfNoChangesStatus = ValidateAndTransformSvfNoChangesStatus(status);
                }
                else if (form.Equals(Form.UPDATE_FORM, StringComparison.OrdinalIgnoreCase))
                {
                    fact.UpdateFormStatus = ValidateAndTransformAdapApplicationStatus(status);
                }
                else
                {
                    throw new Exception(String.Format("Don't know how to handle form {0}", form));
                }
            });

            return(fact);
        }
Ejemplo n.º 3
0
        public void CanCreateTest4()
        {
            //Arrange
            var service = GetDecisionService();
            CreateFormQuestion question = new CreateFormQuestion
            {
                AnniversaryDate = new DateTimeOffset(2017, 10, 31, 0, 0, 0, TimeSpan.Zero),
                Culture         = "en-US",
                SubjectId       = 12345,
                SvfDate         = new DateTimeOffset(2017, 5, 1, 0, 0, 0, TimeSpan.Zero),
                UserId          = 12345,
                Form            = Form.REENROLLMENT
            };

            question.MostRecentForms.Add(new FormStatus
            {
                Form          = Form.INITIAL_ENROLLMENT,
                Status        = AdapApplicationStatus.APPROVED,
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });
            question.MostRecentForms.Add(new FormStatus
            {
                Form          = Form.REENROLLMENT,
                Status        = AdapApplicationStatus.CANCELLED,
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });
            question.MostRecentForms.Add(new FormStatus
            {
                Form          = Form.SVF_WITH_CHANGES,
                Status        = AdapApplicationStatus.CANCELLED,
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });
            question.MostRecentForms.Add(new FormStatus
            {
                Form          = Form.SVF_NO_CHANGES,
                Status        = SvfNoChangesStatus.CANCELLED,
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });
            question.MostRecentForms.Add(new FormStatus
            {
                Form          = Form.UPDATE_FORM,
                Status        = AdapApplicationStatus.CANCELLED,
                StatusChanged = new DateTimeOffset(2017, 2, 27, 0, 0, 0, TimeSpan.Zero)
            });

            //Act
            CreateFormAnswer answer = service.CanCreateForm(question);

            //Assert
            Assert.IsNotNull(answer);
            Assert.IsFalse(answer.Error);
            Assert.IsFalse(answer.CanCreate);
            Assert.IsFalse(String.IsNullOrWhiteSpace(answer.Explanation));
        }
        public CreateFormAnswer CanCreateForm(CreateFormQuestion question)
        {
            if (question == null)
            {
                throw new ArgumentNullException("question");
            }
            CreateFormAnswer answer = new CreateFormAnswer();

            try
            {
                CreateFormFact fact  = question.ToFact();
                var            rules = mDecisionTable.Rows
                                       .Where(x => x.Form == fact.Form &&
                                              x.InitialEnrollmentStatus == fact.InitialEnrollmentStatus &&
                                              x.ReenrollmentStatus == fact.ReenrollmentStatus &&
                                              x.SvfWithChangesStatus == fact.SvfWithChangesStatus &&
                                              x.SvfNoChangesStatus == fact.SvfNoChangesStatus &&
                                              x.UpdateFormStatus == fact.UpdateFormStatus)
                                       .ToList();
                if (rules.Count == 0)
                {
                    answer.Error        = true;
                    answer.ErrorMessage = String.Format("Your question did not match any rules in the decision table. "
                                                        + "Question was converted to the following fact. {0}", JsonConvert.SerializeObject(fact));
                }
                if (rules.Count > 1)
                {
                    answer.Error = true;
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Your question matched more than 1 rule in the decision table.");
                    sb.AppendLine(String.Format("Question was converted to the following fact. {0}", JsonConvert.SerializeObject(fact)));
                    sb.AppendLine("The following rules matched the fact:");
                    foreach (var _rule in rules)
                    {
                        sb.AppendLine(JsonConvert.SerializeObject(_rule));
                    }
                }
                CreateFormDecisionRow rule = rules[0];
                if (rule.CheckReenrollmentWindow.Value == FunctionColumn.NO && rule.CheckSvfWindow.Value == FunctionColumn.NO)
                {
                    if (!rule.CanCreate.HasValue)
                    {
                        throw new Exception(String.Format("Rule.CanCreate does not have a value specified. {0}", JsonConvert.SerializeObject(rule)));
                    }
                    answer.CanCreate   = rule.CanCreate.Value;
                    answer.Explanation = rule.Explanation;
                }
                else
                {
                    bool   checkReenrollmentWindowResult      = false;
                    string checkReenrollmentWindowExplanation = null;
                    string checkReenrollmentWindow            = rule.CheckReenrollmentWindow.Value;
                    if (checkReenrollmentWindow == FunctionColumn.YES || checkReenrollmentWindow == FunctionColumn.NEGATE)
                    {
                        checkReenrollmentWindowResult = mDecisionTable.CheckAnniversaryWindow(fact, question.Culture,
                                                                                              checkReenrollmentWindow == FunctionColumn.NEGATE, out checkReenrollmentWindowExplanation);
                    }
                    bool   checkSvfWindowResult     = false;
                    string checkSvfWindowExplantion = "";
                    string checkSvfWindow           = rule.CheckSvfWindow.Value;
                    if (checkSvfWindow == FunctionColumn.YES || checkSvfWindow == FunctionColumn.NEGATE)
                    {
                        checkSvfWindowResult = mDecisionTable.CheckAnniversaryWindow(fact, question.Culture,
                                                                                     checkSvfWindow == FunctionColumn.NEGATE, out checkSvfWindowExplantion);
                    }
                    answer.CanCreate = checkReenrollmentWindowResult && checkSvfWindowResult;
                    StringBuilder sb = new StringBuilder();
                    if (!String.IsNullOrWhiteSpace(checkReenrollmentWindowExplanation))
                    {
                        sb.Append(checkReenrollmentWindowExplanation);
                    }
                    if (!String.IsNullOrWhiteSpace(checkSvfWindowExplantion))
                    {
                        if (!String.IsNullOrWhiteSpace(checkReenrollmentWindowExplanation))
                        {
                            sb.Append(" ");
                        }
                        sb.Append(checkSvfWindowExplantion);
                    }
                    answer.Explanation = sb.ToString();
                }
            }
            catch (Exception ex)
            {
                string errorMessage = String.Format("An error occurred while evaluating the question. {0}", ex.ToStringVerbose());
                mLogger.LogError(errorMessage);
                answer.Error        = true;
                answer.ErrorMessage = errorMessage;
            }
            return(answer);
        }