private List <TabularData> GetTabularDataFromQuestionsAndAnswers(IEnumerable <Question> questions, IEnumerable <Answer> answers)
        {
            var tabularDataList = new List <TabularData>();

            if (questions != null && answers != null)
            {
                foreach (var tabularQuestion in questions.Where(q => QuestionInputType.TabularData.Equals(q.InputType, StringComparison.OrdinalIgnoreCase)))
                {
                    var questionId = tabularQuestion.QuestionId;
                    var jsonAnswer = answers.FirstOrDefault(a => a.QuestionId == questionId)?.Value;

                    if (jsonAnswer != null)
                    {
                        try
                        {
                            var tabularData = JsonConvert.DeserializeObject <TabularData>(jsonAnswer);

                            if (questionId == RoatpWorkflowQuestionIds.ManagementHierarchy)
                            {
                                tabularData = ManagementHierarchyTransformer.Transform(tabularData);
                            }

                            tabularDataList.Add(tabularData);
                        }
                        catch
                        {
                            // safe to ignore.
                            _logger.LogWarning($"Expected TabularData but was something else. Question Id: {questionId}");
                        }
                    }
                }
            }

            return(tabularDataList);
        }
Пример #2
0
        public void Transformer_Produces_Expected_Result(string firstName, string lastName, string jobRole, string years, string months, string yearsAndMonths, string anotherOrg, string orgDetails, string dobMonth, string dobYear, string dob, string email, string contactNumber)
        {
            var input = new TabularData
            {
                HeadingTitles = _InputHeadingTitles,
                DataRows      = new List <TabularDataRow>
                {
                    new TabularDataRow
                    {
                        Columns = new List <string> {
                            firstName, lastName, jobRole, years, months, anotherOrg, orgDetails, dobMonth, dobYear, email, contactNumber
                        }
                    }
                }
            };

            var expectedResult = new TabularData
            {
                HeadingTitles = _TransformedHeadingTitles,
                DataRows      = new List <TabularDataRow>
                {
                    new TabularDataRow
                    {
                        Columns = new List <string> {
                            firstName, lastName, jobRole, yearsAndMonths, anotherOrg, !string.IsNullOrWhiteSpace(orgDetails) ? orgDetails : _NotApplicable, dob, email, contactNumber
                        }
                    }
                }
            };

            var actualResult = ManagementHierarchyTransformer.Transform(input);

            Assert.IsNotNull(actualResult);
            Assert.IsNotNull(actualResult.HeadingTitles);
            Assert.IsNotNull(actualResult.DataRows);
            CollectionAssert.AreEquivalent(expectedResult.HeadingTitles, actualResult.HeadingTitles);
            CollectionAssert.AreEquivalent(expectedResult.DataRows[0].Columns, actualResult.DataRows[0].Columns);
        }