public void MapArrayResultsToField()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.Errors.json");
            JToken token = JToken.Parse(json);
            MappedFieldList fieldList = new MappedFieldList
                                        {
                                            new MappedField
                                            {
                                                MapType = MapType.Content,
                                                Target = "Test_field",
                                                Source = "Server_field"
                                            }
                                        };
            ApplicationData applicationData = new ApplicationData
                                              {
                                                  { "Test_field", null }
                                              };
            JObject mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, applicationData) as JObject;
            JToken first = mapped.First;

            Assert.AreEqual(2, first.Values().Count());
            Assert.AreEqual(2, applicationData.GetValue<string[]>("Test_field").Count());
        }
        public void SaveWriteableRepeaterChild()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            appData["write-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["write-field"] = "this field is writeable!";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, null);
            Assert.AreEqual("this field is writeable!", appData.GetValue<string>("write-repeater[0].write-field"));
        }
        public void SaveReadOnlyRepeaterRevertRowToPrevious()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            appData["read-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["write-field-2"] = "this row has been updated";

            ApplicationData previousAppData = new ApplicationData();
            Dictionary<string, object>[] previousRepeater = new Dictionary<string, object>[1];
            previousAppData["read-repeater"] = previousRepeater;
            previousRepeater[0] = new Dictionary<string, object>();
            previousRepeater[0]["write-field-2"] = "this row can not be updated";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, previousAppData);
            Assert.AreEqual("this row can not be updated", appData.GetValue<string>("read-repeater[0].write-field-2"));
        }
        public void SaveReadOnlyRepeaterRevertRowToEmpty()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            appData["read-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["write-field-2"] = "this row has been added";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, null);
            Assert.AreEqual(0, appData.GetValue<Dictionary<string, object>[]>("read-repeater").Length);
        }
        public void SaveReadOnlyRepeaterRemoveRow()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            appData["read-repeater"] = new Dictionary<string, object>[0];

            ApplicationData previousAppData = new ApplicationData();
            Dictionary<string, object>[] previousRepeater = new Dictionary<string, object>[1];
            previousAppData["read-repeater"] = previousRepeater;
            previousRepeater[0] = new Dictionary<string, object>();
            previousRepeater[0]["write-field-2"] = "this row can not be removed 1";
            previousRepeater[0]["read-field-with-default-2"] = "this row can not be removed 2";
            previousRepeater[0]["read-field-no-default-2"] = "this row can not be removed 3";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, previousAppData);
            Assert.AreEqual(1, appData.GetValue<Dictionary<string, object>[]>("read-repeater").Length);
        }
        public void SaveReadOnlyRepeaterChildRevertToPrevious()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            appData["write-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["read-field-no-default"] = "this field is not writeable!";

            ApplicationData previousAppData = new ApplicationData();
            Dictionary<string, object>[] previousRepeater = new Dictionary<string, object>[1];
            previousAppData["write-repeater"] = previousRepeater;
            previousRepeater[0] = new Dictionary<string, object>();
            previousRepeater[0]["read-field-no-default"] = "this is the previous value!";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, previousAppData);
            Assert.AreEqual("this is the previous value!", appData.GetValue<string>("write-repeater[0].read-field-no-default"));
        }
        public void SaveReadOnlyRepeaterChildRevertToDefault()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            appData["write-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["read-field-with-default"] = "this field is not writeable!";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, null);
            Assert.AreEqual("the two sweetest words in the english language", appData.GetValue<string>("write-repeater[0].read-field-with-default"));
        }
        public void MapSingleStringToField()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.Single.json");
            JToken token = JToken.Parse(json);
            MappedFieldList fieldList = new MappedFieldList
                                        {
                                            new MappedField
                                            {
                                                MapType = MapType.Content,
                                                Target = "Test_field",
                                                Source = "Server_field"
                                            }
                                        };
            ApplicationData applicationData = new ApplicationData
                                              {
                                                  { "Test_field", null }
                                              };
            JObject mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, applicationData) as JObject;

            Assert.AreEqual("This is a single response", applicationData.GetValue<string>("Test_field"));
        }
        public void SaveNonExistantRepeaterChild()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData input = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[1];
            input["write-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["bad-field"] = "this field is writeable!";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            ApplicationData output = verifier.VerifyApplicationData(input, null);

            Assert.IsTrue(input.ContainsKey("write-repeater"));
            Assert.IsNotNull(input.GetValue<string>("write-repeater[0].bad-field"));

            Assert.IsTrue(output.ContainsKey("write-repeater"));
            Assert.IsNull(output.GetValue<string>("write-repeater[0].bad-field"));
        }
            public void UnknownRepeater()
            {
                ApplicationData target = new ApplicationData();

                Dictionary<string, object>[] repeater = new Dictionary<string, object>[2];
                repeater[0] = new Dictionary<string, object>();
                repeater[1] = new Dictionary<string, object>();
                target["repeater1"] = repeater;

                repeater[0]["field1"] = "leave me alone!";
                repeater[0]["field2"] = "leave me alone too!";
                repeater[1]["field1"] = "leave me alone!";
                repeater[1]["field2"] = "i'm incorrect anyway - update me!";

                ApplicationData source = new ApplicationData();
                Dictionary<string, object>[] sourceRepeater = new Dictionary<string, object>[2];
                sourceRepeater[0] = new Dictionary<string, object>();
                sourceRepeater[1] = new Dictionary<string, object>();
                source["repeater1"] = sourceRepeater;

                sourceRepeater[1]["field2"] = "you've been overwritten!";
                sourceRepeater[1]["field3"] = "I am the unknown control!";

                target.UpdateFrom(source);
                Assert.IsNull(target.GetValue<string>("repeater1[1].field3"));
            }
            public void RetainRepeater()
            {
                ApplicationData target = new ApplicationData();

                Dictionary<string, object>[] repeater = new Dictionary<string, object>[2];
                repeater[0] = new Dictionary<string, object>();
                repeater[1] = new Dictionary<string, object>();
                target["repeater1"] = repeater;

                repeater[0]["field1"] = "leave me alone!";
                repeater[0]["field2"] = "leave me alone too!";
                repeater[1]["field1"] = "leave me alone!";
                repeater[1]["field2"] = "i'm incorrect anyway - update me!";

                ApplicationData source = new ApplicationData();
                Dictionary<string, object>[] sourceRepeater = new Dictionary<string, object>[2];
                sourceRepeater[0] = new Dictionary<string, object>();
                sourceRepeater[1] = new Dictionary<string, object>();
                source["repeater1"] = sourceRepeater;

                sourceRepeater[1]["field2"] = "you've been overwritten!";

                target.UpdateFrom(source);
                Assert.AreEqual("leave me alone too!", target.GetValue<string>("repeater1[0].field2"));
            }
        /// <summary>
        /// Writes the option value.
        /// </summary>
        /// <param name="sb">The sting builder.</param>
        /// <param name="controlWithOptions">The control with options.</param>
        /// <param name="applicationData">The application data.</param>
        private void WriteOptionValue(StringBuilder sb, ControlWithOptions controlWithOptions, ApplicationData applicationData)
        {
            List<string> valuesList = new List<string>();

            var dataValue = applicationData.GetValue<object>(controlWithOptions.Name);
            var stringValue = dataValue as string;
            var arrayValue = dataValue as string[];
            if (stringValue != null)
            {
                valuesList.Add(stringValue);
            }
            else if (arrayValue != null)
            {
                valuesList.AddRange(arrayValue);
            }

            var selectedOptions = controlWithOptions.OptionValues.Where(o => valuesList.Contains(o.Value)).ToList();
            var description = string.Join(", ", selectedOptions.Select(o => o.Description));

            if (!string.IsNullOrWhiteSpace(description))
            {
                this.WriteRow(sb, controlWithOptions.Label, description);
            }
        }
        /// <summary>
        /// Writes the likert.
        /// </summary>
        /// <param name="sb">The string builder.</param>
        /// <param name="likert">The likert.</param>
        /// <param name="applicationData">The application data.</param>
        private void WriteLikert(StringBuilder sb, LikertControl likert, ApplicationData applicationData)
        {
            this.WriteHeaderRow(sb, likert.Label, FormatterResources.RepeaterStyles);

            NestedDictionary likertVals = applicationData.GetValue<NestedDictionary>(likert.Name);
            foreach (var row in likertVals)
            {
                var question = likert.Questions.FirstOrDefault(q => q.Name == row.Key);

                int applicationDataVal;
                bool isValidSelection = int.TryParse(row.Value.ToString(), out applicationDataVal);
                var scaleVal = isValidSelection ? likert.Scale.FirstOrDefault(s => s.Value == applicationDataVal) : null;

                if (question != null)
                {
                    this.WriteRow(sb, question.Label, scaleVal == null ? string.Empty : scaleVal.Description);
                }
            }
        }
        public void MapArrayToRepeater()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.EmployeeList.json");
            JToken token = JToken.Parse(json);
            MappedFieldList fieldList = new MappedFieldList
                                        {
                                            new MappedField
                                            {
                                                MapType = MapType.Content,
                                                Target = "Employees",
                                                Source = "employees"
                                            }
                                        };
            ApplicationData applicationData = new ApplicationData
                                              {
                                                  { "Employees", null }
                                              };
            JObject mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, applicationData) as JObject;

            NestedDictionary[] repeater = applicationData.GetValue<NestedDictionary[]>("Employees");
            Assert.AreEqual(3, repeater.Count());
            Assert.AreEqual("Josh", repeater[0]["Name"]);
            Assert.AreEqual("Heath", repeater[1]["Name"]);
            Assert.AreEqual("Nalvinder", repeater[2]["Name"]);
        }
        public void SaveNonExistantField()
        {
            const string formId = "10";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData input = new ApplicationData();
            input["bad-field"] = "my legal value";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            ApplicationData output = verifier.VerifyApplicationData(input, null);

            Assert.IsTrue(input.ContainsKey("bad-field"));
            Assert.IsNotNull(input.GetValue<string>("bad-field"));

            Assert.IsFalse(output.ContainsKey("bad-field"));
        }
        public void SaveReadOnlyRepeaterAddRow()
        {
            const string formId = "11";
            const string userId = "007";
            ControlList controls = this.GetControlList(formId);
            List<ControlAccess> controlsAccess = this.GetControlAccess(formId, userId);

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeater = new Dictionary<string, object>[2];
            appData["read-repeater"] = repeater;
            repeater[0] = new Dictionary<string, object>();
            repeater[0]["write-field-2"] = "this row was always there";
            repeater[1] = new Dictionary<string, object>();
            repeater[1]["write-field-2"] = "there is no row";

            ApplicationData previousAppData = new ApplicationData();
            Dictionary<string, object>[] previousRepeater = new Dictionary<string, object>[1];
            previousAppData["read-repeater"] = previousRepeater;
            previousRepeater[0] = new Dictionary<string, object>();
            previousRepeater[0]["write-field-2"] = "this row was always there";

            ApplicationDataVerifier verifier = new ApplicationDataVerifier(controls, controlsAccess);
            appData = verifier.VerifyApplicationData(appData, previousAppData);
            Assert.AreEqual(1, appData.GetValue<Dictionary<string, object>[]>("read-repeater").Length);
        }
        /// <summary>
        /// Returns <see langword="true"/> if the request keys of <paramref name="dataSource"/> have changed,
        /// otherwise <see langword="false"/>.
        /// </summary>
        /// <param name="dataSource">The data source to interrogate.</param>
        /// <param name="postedData">The posted application data.</param>
        /// <param name="existingData">The saved application data.</param>
        /// <returns>
        /// <see langword="true"/> if the request keys of <paramref name="dataSource"/> have changed,
        /// otherwise <see langword="false"/>.
        /// </returns>
        private bool KeysHaveChanged(ProductDataSource dataSource, ApplicationData postedData, ApplicationData existingData)
        {
            if (existingData == null)
            {
                return true;
            }

            List<string> sourceFields = dataSource.RequestFieldMap.Where(map => map.MapType == MapType.Field).Select(map => map.Source).ToList();
            foreach (string sourceField in sourceFields)
            {
                string oldValue = existingData.GetValue<string>(sourceField);
                string newValue = postedData.GetValue<string>(sourceField);
                if (newValue != oldValue)
                {
                    return true;
                }
            }

            return false;
        }
        public void MapArrayWithArrayDataToRepeater()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.EmployeeListWithCheckbox.json");
            JToken token = JToken.Parse(json);
            MappedFieldList fieldList = new MappedFieldList
                                        {
                                            new MappedField
                                            {
                                                MapType = MapType.Content,
                                                Target = "Employees",
                                                Source = "employees"
                                            }
                                        };
            ApplicationData applicationData = new ApplicationData
                                              {
                                                  { "Employees", null }
                                              };
            JObject mapped = new HttpEndpointResponseMapper().MapToken(token, null, fieldList, applicationData) as JObject;

            NestedDictionary[] repeater = applicationData.GetValue<NestedDictionary[]>("Employees");
            Assert.AreEqual(3, repeater.Count());

            var employeeOneDetails = repeater[0]["Details"];
            Assert.AreEqual(3, ((string[])employeeOneDetails).Length);

            var employeeTwoDetails = repeater[1]["Details"];
            Assert.AreEqual(1, ((string[])employeeTwoDetails).Length);

            var employeeThreeDetails = repeater[2]["Details"];
            Assert.AreEqual(0, ((string[])employeeThreeDetails).Length);
        }