public void GetSegments_Expected_CorrectSegmentCount() { JsonPath path = new JsonPath("EnumerableData().NestedData.Name", "EnumerableData.NestedData.Name"); int expected = 3; int actual = path.GetSegements().Count(); Assert.AreEqual(expected, actual); }
public void GetSegments_Expected_LastSegmentIsCorrect() { JsonPath path = new JsonPath("EnumerableData().NestedData.NestedData.Name", "EnumerableData.NestedData.NestedData.Name"); string expected = "Name"; string actual = path.GetSegements().Last().ToString(); Assert.AreEqual(expected, actual); }
public void ToStringOnScalarSegment_Expected_ScalarFormat() { JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment("Name"); const string expected = "Name"; string actual = segment.ToString(); Assert.AreEqual(expected, actual); }
public void ToStringOnEnumerableSegment_WhereEnumerablesAreConsidered_Expected_ScalarFormat() { JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment("Collection()"); const string expected = "Collection()"; string actual = segment.ToString(true); Assert.AreEqual(expected, actual); }
public void RecordsetListHelper_SplitRecordsetAndFieldNames_WhenPathContainsEndingField_MultiLevel_ShouldConvertToField() { //------------Setup for test-------------------------- var jsonPath = new JsonPath(); jsonPath.ActualPath = "OneRecordset().AnotherRecset().AndAnotherRecset"; //------------Execute Test--------------------------- Tuple<string, string> splitRecordsetAndFieldNames = RecordsetListHelper.SplitRecordsetAndFieldNames(jsonPath); //------------Assert Results------------------------- Assert.AreEqual("OneRecordset_AnotherRecset", splitRecordsetAndFieldNames.Item1); Assert.AreEqual("AndAnotherRecset", splitRecordsetAndFieldNames.Item2); }
public void CreateScalarPathSegmentFromJProperty_Expected_ScalarJsonPathSegment() { JProperty jProperty = new JProperty("ScalarProperty", "ScalarValue"); JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment(jProperty); bool expected = false; bool actual = segment.IsEnumarable; Assert.AreEqual(expected, actual); }
public void CreateEnumerablePathSegmentFromJProperty_Expected_EnumerableJsonPathSegment() { JProperty jProperty = new JProperty("EnumerableProperty", new JArray( new JObject(new JProperty("ScalarProperty", "ScalarValue"), new JProperty("ScalarProperty1", "ScalarValue1")))); JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment(jProperty); bool expected = true; bool actual = segment.IsEnumarable; Assert.AreEqual(expected, actual); }
IPath BuildPath(Stack <Tuple <JProperty, bool> > propertyStack, JProperty jProperty, JToken root) { var path = new JsonPath(); path.ActualPath = string.Join(JsonPath.SeperatorSymbol, propertyStack.Reverse().Select(p => path.CreatePathSegment(p.Item1).ToString(p.Item2))); var displayPathSegments = propertyStack.Reverse() .Select(p => new Tuple <IPathSegment, bool>(path.CreatePathSegment(p.Item1), p.Item2)) .ToList(); var recordsetEncountered = false; for (int i = displayPathSegments.Count - 1; i >= 0; i--) { var pathSegment = displayPathSegments[i]; if (recordsetEncountered) { pathSegment.Item1.IsEnumarable = false; } if (pathSegment.Item1.IsEnumarable && pathSegment.Item2) { recordsetEncountered = true; } } path.DisplayPath = string.Join(JsonPath.SeperatorSymbol, displayPathSegments.Select(p => p.Item1.ToString(p.Item2))); if (path.ActualPath != string.Empty) { path.ActualPath += JsonPath.SeperatorSymbol; } if (path.DisplayPath != string.Empty) { path.DisplayPath += JsonPath.SeperatorSymbol; } path.ActualPath += path.CreatePathSegment(jProperty); path.DisplayPath += path.CreatePathSegment(jProperty); path.SampleData += GetSampleData(root, path); return(path); }
public IEnumerable <object> SelectEnumerable(IPath path) { if (path == null) { throw new ArgumentNullException("path"); } JsonPath jsonPath = path as JsonPath; if (jsonPath == null) { throw new Exception(string.Format("Path of type '{0}' expected, path of type '{1}' received.", typeof(JsonPath), path.GetType())); } List <object> returnData; if (path.ActualPath == JsonPath.SeperatorSymbol) { returnData = new List <object> { Data }; } else if (path.ActualPath == JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol) { IEnumerable enumerableData = Data as IEnumerable; returnData = new List <object>(); if (enumerableData != null) { IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { returnData.Add(enumerator.Current); } } } else { returnData = new List <object>(SelectEnumberable(jsonPath.GetSegements().ToList(), Data as JToken).Select(o => o.ToString())); } return(returnData); }
public void SelectScalarValueUsingEnumerablePathFromJson_WherePathMapsToPrimitiveEnumerable_Expected_ScalarValue() { string testData = Given(); IPath namePath = new JsonPath("PrimitiveRecordset()", "PrimitiveRecordset"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = JsonNavigator.SelectScalar(namePath).ToString().Trim(); const string expected = "RandomData1"; Assert.AreEqual(expected, actual); }
public void SelectScalarValueUsingScalarPathFromJson_Expected_ScalarValue() { string testData = Given(); IPath namePath = new JsonPath("Name", "Name"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = JsonNavigator.SelectScalar(namePath).ToString(); const string expected = "Dev2"; Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainASinglePathWhichIsScalar_Expected_FlattenedDataWithValueFromScalarPath() { string testData = Given(); IPath path = new JsonPath("Name", "Name"); List<IPath> paths = new List<IPath> { path }; JsonNavigator JsonNavigator = new JsonNavigator(testData); Dictionary<IPath, IList<object>> data = JsonNavigator.SelectEnumerablesAsRelated(paths); const string expected = "Dev2"; string actual = string.Join("|", data[path].Select(s => s.ToString().Trim())); Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainASinglePathWhichIsEnumerable_Expected_FlattenedDataWithValuesFromEnumerablePath() { string testData = Given(); IPath path = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name"); List<IPath> paths = new List<IPath> { path }; JsonNavigator JsonNavigator = new JsonNavigator(testData); Dictionary<IPath, IList<object>> data = JsonNavigator.SelectEnumerablesAsRelated(paths); const string expected = "Brendon|Jayd|Bob|Joe"; string actual = string.Join("|", data[path].Select(s => s.ToString().Trim())); Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainNestedEnumerablePaths_Expected_FlattenedDataWithValuesFromOuterEnumerablePathRepeatingForEveryValueFromNestedEnumerablePath() { string testData = Given(); IPath path3 = new JsonPath("Name", "Name"); IPath path = new JsonPath("Departments().Name", "Departments.Name"); IPath path1 = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name"); IPath path2 = new JsonPath("PrimitiveRecordset()", "PrimitiveRecordset"); List<IPath> paths = new List<IPath> { path3, path, path1, path2 }; JsonNavigator JsonNavigator = new JsonNavigator(testData); Dictionary<IPath, IList<object>> data = JsonNavigator.SelectEnumerablesAsRelated(paths); const string expected = "Dev2|Dev2|Dev2|Dev2^Dev|Dev|Accounts|Accounts^Brendon|Jayd|Bob|Joe^RandomData\r\n ,\r\n RandomData1|||"; string actual = string.Join("|", data[path3].Select(s => s.ToString().Trim())) + "^" + string.Join("|", data[path].Select(s => s.ToString().Trim())) + "^" + string.Join("|", data[path1].Select(s => s.ToString().Trim())) + "^" + string.Join("|", data[path2].Select(s => s.ToString().Trim())); Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainUnrelatedEnumerablePaths_Expected_FlattenedDataWithValuesFromUnrelatedEnumerablePathsAtMatchingIndexes() { string testData = Given(); IPath path = new JsonPath("Departments().Name", "Departments.Name"); IPath path1 = new JsonPath("Contractors().Name", "Contractors.Name"); List<IPath> paths = new List<IPath> { path, path1 }; JsonNavigator JsonNavigator = new JsonNavigator(testData); Dictionary<IPath, IList<object>> data = JsonNavigator.SelectEnumerablesAsRelated(paths); const string expected = "Dev|Accounts||^Roofs Inc.|Glass Inc.|Doors Inc.|Cakes Inc."; string actual = string.Join("|", data[path].Select(s => s.ToString().Trim())) + "^" + string.Join("|", data[path1].Select(s => s.ToString().Trim())); Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainAScalarPath_Expected_FlattenedDataWithValueFromScalarPathRepeatingForEachEnumeration() { string testData = Given(); IPath path = new JsonPath("Name", "Name"); IPath path1 = new JsonPath("Departments().Name", "Departments.Name"); List<IPath> paths = new List<IPath> { path, path1 }; JsonNavigator JsonNavigator = new JsonNavigator(testData); Dictionary<IPath, IList<object>> data = JsonNavigator.SelectEnumerablesAsRelated(paths); const string expected = "Dev2|Dev2^Dev|Accounts"; string actual = string.Join("|", data[path].Select(s => s.ToString().Trim())) + "^" + string.Join("|", data[path1].Select(s => s.ToString().Trim())); Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesUsingEnumerablePathFromJson_WherePathMapsThroughNestedEnumerables_Expected_EnumerableValue() { string testData = Given(); IPath path = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim())); const string expected = "Brendon|Jayd|Bob|Joe"; Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesUsingScalarPathFromJson_Expected_EnumerableValue() { string testData = Given(); IPath path = new JsonPath("Motto", "Motto"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim())); const string expected = "Eat lots of cake"; Assert.AreEqual(expected, actual); }
public void CreateEnumerablePathSegmentFromSegmentText_Expected_EnumerableJsonPathSegment() { JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment("EnumerableData()"); bool expected = true; bool actual = segment.IsEnumarable; Assert.AreEqual(expected, actual); }
public void SelectScalarValueUsingJsonScalarPathFromJson_Expected_ScalarValue() { string testData = GivenJson(); IPath namePath = new JsonPath("Name", "Name"); IDataBrowser dataBrowser = DataBrowserFactory.CreateDataBrowser(); object data = dataBrowser.SelectScalar(namePath, testData); Assert.AreEqual(data, "Dev2"); }
public void RecordsetListHelper_SplitRecordsetAndFieldNames_WhenPathContainsScalar_SingleLevel_ShouldConvertToField() { //------------Setup for test-------------------------- var jsonPath = new JsonPath(); jsonPath.ActualPath = "ScalarValue"; //------------Execute Test--------------------------- Tuple<string, string> splitRecordsetAndFieldNames = RecordsetListHelper.SplitRecordsetAndFieldNames(jsonPath); //------------Assert Results------------------------- Assert.AreEqual("", splitRecordsetAndFieldNames.Item1); Assert.AreEqual("ScalarValue", splitRecordsetAndFieldNames.Item2); }
public void SelectScalarValueUsingEnumerablePathFromJson_Expected_ScalarValue() { string testData = Given(); IPath namePath = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = JsonNavigator.SelectScalar(namePath).ToString(); const string expected = "Joe"; Assert.AreEqual(expected, actual); }
private IPath BuildPath(Stack<Tuple<JProperty, bool>> propertyStack, JProperty jProperty, JToken root) { var path = new JsonPath(); path.ActualPath = string.Join(JsonPath.SeperatorSymbol, propertyStack.Reverse().Select(p => path.CreatePathSegment(p.Item1).ToString(p.Item2))); List<Tuple<IPathSegment, bool>> displayPathSegments = propertyStack.Reverse() .Select(p => new Tuple<IPathSegment, bool>(path.CreatePathSegment(p.Item1), p.Item2)) .ToList(); bool recordsetEncountered = false; for (int i = displayPathSegments.Count - 1; i >= 0; i--) { Tuple<IPathSegment, bool> pathSegment = displayPathSegments[i]; if (recordsetEncountered) { pathSegment.Item1.IsEnumarable = false; } if (pathSegment.Item1.IsEnumarable && pathSegment.Item2) recordsetEncountered = true; } path.DisplayPath = string.Join(JsonPath.SeperatorSymbol, displayPathSegments.Select(p => p.Item1.ToString(p.Item2))); if (path.ActualPath != string.Empty) { path.ActualPath += JsonPath.SeperatorSymbol; } if (path.DisplayPath != string.Empty) { path.DisplayPath += JsonPath.SeperatorSymbol; } path.ActualPath += path.CreatePathSegment(jProperty).ToString(); path.DisplayPath += path.CreatePathSegment(jProperty).ToString(); path.SampleData += GetSampleData(root, path); return path; }
public object SelectScalar(IPath path) { if (path == null) { throw new ArgumentNullException("path"); } JsonPath jsonPath = path as JsonPath; if (jsonPath == null) { throw new Exception(string.Format("Path of type '{0}' expected, path of type '{1}' received.", typeof(JsonPath), path.GetType())); } JToken currentData = Data as JToken; if (currentData == null) { throw new Exception(string.Format("Type of {0} was expected for data, type of {1} was found instead.", typeof(JToken), Data.GetType())); } if (path.ActualPath == JsonPath.SeperatorSymbol) { //nothing to do here yet } else if (path.ActualPath == JsonPath.EnumerableSymbol + JsonPath.SeperatorSymbol) { var enumerableData = currentData as IEnumerable; IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { currentData = enumerator.Current as JToken; } } else { List <IPathSegment> pathSegments = jsonPath.GetSegements().ToList(); int segmentIndex = 0; while (currentData != null && segmentIndex < pathSegments.Count) { if (pathSegments[segmentIndex].IsEnumarable) { IEnumerable enumerableData = GetEnumerableValueForPathSegment(pathSegments[segmentIndex], currentData); if (enumerableData == null) { currentData = null; } else { IEnumerator enumerator = enumerableData.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { currentData = enumerator.Current as JToken; } } } else { currentData = GetScalarValueForPathSegement(pathSegments[segmentIndex], currentData); } segmentIndex++; } } string returnVal = ""; if (currentData != null) { returnVal = currentData.ToString(); } return(returnVal); }
public void SelectEnumerableValuesUsingEnumerablePathFromJson_WherePathMapsToPrimitiveEnumerable_Expected_EnumerableValue() { string testData = Given(); IPath path = new JsonPath("PrimitiveRecordset()", "PrimitiveRecordset"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim())); const string expected = "RandomData|RandomData1"; Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesUsingEnumerablePathFromJson_Expected_EnumerableValue() { string testData = Given(); IPath path = new JsonPath("Departments().Name", "Departments.Name"); JsonNavigator JsonNavigator = new JsonNavigator(testData); string actual = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim())); const string expected = "Dev|Accounts"; Assert.AreEqual(expected, actual); }
public void SelectEnumerableValuesAsRelatedUsingJsonEnumerablePathsFromJson_Where_PathsContainNestedEnumerablePaths_Expected_FlattenedDataWithValuesFromOuterEnumerablePathRepeatingForEveryValueFromNestedEnumerablePath() { string testData = GivenJson(); IPath enumerableNamePath = new JsonPath("Departments().Name", "Departments.Name"); IPath nestedEnumerableNamePath = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name"); List<IPath> paths = new List<IPath> { enumerableNamePath, nestedEnumerableNamePath }; IDataBrowser dataBrowser = DataBrowserFactory.CreateDataBrowser(); Dictionary<IPath, IList<object>> data = dataBrowser.SelectEnumerablesAsRelated(paths, testData); const string expected = "Dev|Dev|Accounts|Accounts^Brendon|Jayd|Bob|Joe"; string actual = string.Join("|", data[enumerableNamePath]); actual += "^" + string.Join("|", data[nestedEnumerableNamePath]); Assert.AreEqual(expected, actual); }
public void CreateScalarPathSegmentFromSegmentText_Expected_ScalarJsonPathSegment() { JsonPath path = new JsonPath(); IPathSegment segment = path.CreatePathSegment("Name"); bool expected = false; bool actual = segment.IsEnumarable; Assert.AreEqual(expected, actual); }