| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | | using ValidateLib.Metadata.Properties; |
| | 5 | |
|
| | 6 | | namespace NormalizationTests |
| | 7 | | { |
| | 8 | | public class CommonPropertiesTests |
| | 9 | | { |
| | 10 | | public readonly string testFilesDirectory; |
| 1 | 11 | | public CommonPropertiesTests() |
| 1 | 12 | | { |
| 1 | 13 | | testFilesDirectory = Path.Combine(GetProjectDirectory(), "TestFiles","CommonPropertiesFiles"); |
| 1 | 14 | | } |
| | 15 | | static string GetProjectDirectory() |
| 1 | 16 | | { |
| 1 | 17 | | string? currentDirectory = Directory.GetCurrentDirectory(); |
| | 18 | |
|
| 1 | 19 | | while (!string.IsNullOrEmpty(currentDirectory)) |
| 1 | 20 | | { |
| 1 | 21 | | string[] projectFiles = Directory.GetFiles(currentDirectory, "*.csproj"); |
| | 22 | |
|
| 1 | 23 | | if (projectFiles.Length > 0) |
| 1 | 24 | | return currentDirectory; |
| 1 | 25 | | currentDirectory = Directory.GetParent(currentDirectory)?.FullName; |
| 1 | 26 | | } |
| | 27 | |
|
| 0 | 28 | | throw new Exception("Could not find project directory."); |
| | 29 | |
|
| | 30 | |
|
| 1 | 31 | | } |
| | 32 | | [Fact] |
| | 33 | | public void CommonPropertyStringValueToObjectWithValuePropertyTest() |
| 1 | 34 | | { |
| 1 | 35 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile1.json"); |
| 1 | 36 | | string jsonString = File.ReadAllText(testFilePath); |
| 1 | 37 | | JToken jToken = JObject.Parse(jsonString); |
| 1 | 38 | | var warnings = new List<Warning>(); |
| 1 | 39 | | Context context = Context.GetContextFromJToken(jToken, warnings, testFilePath); |
| 1 | 40 | | var property = ((JObject)jToken).Property("dc:title"); |
| 1 | 41 | | CommonProperty.Normalize(property!.Value, context,property); |
| | 42 | |
|
| 1 | 43 | | Assert.Equal(typeof(JObject), property.Value.GetType()); |
| 1 | 44 | | Assert.Equal("The title of this Table",property?.Value?["@value"]?.ToString()); |
| 1 | 45 | | Assert.Equal("en",property?.Value?["@language"]?.ToString()); |
| | 46 | |
|
| 1 | 47 | | } |
| | 48 | |
|
| | 49 | | [Fact] |
| | 50 | | public void CommonPropertyArrayPropertyTest() |
| 1 | 51 | | { |
| 1 | 52 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile3.json"); |
| 1 | 53 | | string jsonString = File.ReadAllText(testFilePath); |
| 1 | 54 | | JToken jToken = JObject.Parse(jsonString); |
| 1 | 55 | | var warnings = new List<Warning>(); |
| 1 | 56 | | Context context = Context.GetContextFromJToken(jToken, warnings,testFilePath); |
| 1 | 57 | | var property = ((JObject)jToken).Property("dc:title"); |
| 1 | 58 | | CommonProperty.Normalize(property!.Value, context, property); |
| | 59 | |
|
| 1 | 60 | | Assert.Equal(typeof(JArray), property.Value.GetType()); |
| | 61 | |
|
| 1 | 62 | | var object1 = property.Value[0]; |
| 1 | 63 | | var object2 = property.Value[1]; |
| 1 | 64 | | Assert.Equal("The title of this Table", object1?["@value"]?.ToString()); |
| 1 | 65 | | Assert.Equal("en", object1?["@language"]?.ToString()); |
| | 66 | |
|
| 1 | 67 | | Assert.Equal("Der Titel dieser Tabelle", object2?["@value"]?.ToString()); |
| 1 | 68 | | Assert.Equal("de", object2?["@language"]?.ToString()); |
| | 69 | |
|
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | [Fact] |
| | 73 | | public void CommonPropertyNormalizeAgainstBaseIdPropertyTest() |
| 1 | 74 | | { |
| 1 | 75 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile4.json"); |
| 1 | 76 | | string jsonString = File.ReadAllText(testFilePath); |
| 1 | 77 | | JToken jToken = JObject.Parse(jsonString); |
| 1 | 78 | | var warnings = new List<Warning>(); |
| 1 | 79 | | Context context = Context.GetContextFromJToken(jToken, warnings, testFilePath); |
| 1 | 80 | | var property = ((JObject)jToken).Property("schema:url"); |
| 1 | 81 | | CommonProperty.Normalize(property?.Value!, context, property); |
| | 82 | |
|
| 1 | 83 | | Assert.Equal(typeof(JObject), property!.Value.GetType()); |
| | 84 | |
|
| 1 | 85 | | Assert.Equal("http://example.com/table.csv", property?.Value?["@id"]!.ToString()); |
| | 86 | |
|
| | 87 | |
|
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | [Fact] |
| | 91 | | public void CommonPropertyNormalizeEmbeddedObjectInArrayTest() |
| 1 | 92 | | { |
| 1 | 93 | | string testFilePath = Path.Combine(testFilesDirectory, "testFile5.json"); |
| 1 | 94 | | string jsonString = File.ReadAllText(testFilePath); |
| 1 | 95 | | JToken jToken = JObject.Parse(jsonString); var warnings = new List<Warning>(); |
| 1 | 96 | | Context context = Context.GetContextFromJToken(jToken, warnings, testFilePath); |
| 1 | 97 | | var property = ((JObject)jToken).Property("dc:publisher"); |
| 1 | 98 | | CommonProperty.Normalize(property?.Value!, context, property); |
| | 99 | |
|
| 1 | 100 | | var object1 = property!.Value[0]; |
| | 101 | |
|
| 1 | 102 | | Assert.Equal(typeof(JArray), property.Value.GetType()); |
| | 103 | |
|
| 1 | 104 | | Assert.Equal("Example Municipality", object1?["schema:name"]!["@value"]!.ToString()); |
| | 105 | |
|
| | 106 | |
|
| 1 | 107 | | } |
| | 108 | | } |
| | 109 | | } |