Skip to content

Commit 21d9976

Browse files
Microsoft Graph DevX ToolingMicrosoft Graph DevX Tooling
authored andcommitted
Updated test to cinclude file from custom folder
1 parent f4df01f commit 21d9976

File tree

3 files changed

+155
-1
lines changed

3 files changed

+155
-1
lines changed

tools/Custom/JsonExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace Microsoft.Graph.PowerShell.JsonUtilities
22
{
33
using Newtonsoft.Json.Linq;
44
using System.Linq;
5-
using static Microsoft.Graph.PowerShell.Runtime.Extensions;
65

76
public static class JsonExtensions
87
{
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
namespace JsonUtilitiesTest;
2+
using System;
3+
using Newtonsoft.Json.Linq;
4+
using Xunit;
5+
using Microsoft.Graph.PowerShell.JsonUtilities;
6+
7+
public class JsonExtensionsTests
8+
{
9+
[Fact]
10+
public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValues()
11+
{
12+
// Arrange
13+
JObject json = JObject.Parse(@"{
14+
""displayname"": ""Tim"",
15+
""position"": ""defaultnull"",
16+
""salary"": 2000000,
17+
""team"": ""defaultnull""
18+
}");
19+
20+
// Act
21+
string cleanedJson = json.RemoveDefaultNullProperties();
22+
JObject result = JObject.Parse(cleanedJson);
23+
24+
// Assert
25+
Assert.False(result.ContainsKey("position"));
26+
Assert.False(result.ContainsKey("team"));
27+
Assert.Equal("Tim", result["displayname"]?.ToString());
28+
Assert.Equal(2000000, result["salary"]?.ToObject<int>());
29+
}
30+
31+
[Fact]
32+
public void RemoveDefaultNullProperties_ShouldConvertStringNullToJsonNull()
33+
{
34+
// Arrange
35+
JObject json = JObject.Parse(@"{
36+
""displayname"": ""Tim"",
37+
""position"": ""null"",
38+
""salary"": 2000000,
39+
""team"": """"
40+
}");
41+
42+
// Act
43+
string cleanedJson = json.RemoveDefaultNullProperties();
44+
JObject result = JObject.Parse(cleanedJson);
45+
46+
// Assert
47+
Assert.Null(result["position"]?.Value<string>());
48+
Assert.Null(result["team"]?.Value<string>());
49+
Assert.Equal("Tim", result["displayname"]?.ToString());
50+
Assert.Equal(2000000, result["salary"]?.ToObject<int>());
51+
}
52+
53+
[Fact]
54+
public void RemoveDefaultNullProperties_ShouldHandleNestedObjects()
55+
{
56+
// Arrange
57+
JObject json = JObject.Parse(@"{
58+
""displayname"": ""Tim"",
59+
""metadata"": {
60+
""phone"": ""defaultnull"",
61+
""location"": ""Nairobi""
62+
}
63+
}");
64+
65+
// Act
66+
string cleanedJson = json.RemoveDefaultNullProperties();
67+
JObject result = JObject.Parse(cleanedJson);
68+
69+
// Assert
70+
Assert.False(result["metadata"]?.ToObject<JObject>()?.ContainsKey("phone"));
71+
Assert.Equal("Nairobi", result["metadata"]?["location"]?.ToString());
72+
}
73+
74+
[Fact]
75+
public void RemoveDefaultNullProperties_ShouldHandleEmptyJsonObject()
76+
{
77+
// Arrange
78+
JObject json = JObject.Parse(@"{}");
79+
80+
// Act
81+
string cleanedJson = json.RemoveDefaultNullProperties();
82+
JObject result = JObject.Parse(cleanedJson);
83+
84+
// Assert
85+
Assert.Empty(result);
86+
}
87+
88+
[Fact]
89+
public void RemoveDefaultNullProperties_ShouldHandleJsonArrays()
90+
{
91+
// Arrange
92+
JObject json = JObject.Parse(@"{
93+
""users"": [
94+
{ ""displayname"": ""Tim"", ""email"": ""defaultnull"" },
95+
{ ""displayname"": ""Mayabi"", ""email"": ""mayabi@example.com"" }
96+
]
97+
}");
98+
99+
// Act
100+
string cleanedJson = json.RemoveDefaultNullProperties();
101+
JObject result = JObject.Parse(cleanedJson);
102+
103+
// Assert
104+
Assert.Equal("Tim", result["users"]?[0]?["displayname"]?.ToString());
105+
Assert.Equal("mayabi@example.com", result["users"]?[1]?["email"]?.ToString());
106+
}
107+
108+
[Fact]
109+
public void RemoveDefaultNullProperties_ShouldNotAlterValidData()
110+
{
111+
// Arrange
112+
JObject json = JObject.Parse(@"{
113+
""displayname"": ""Tim"",
114+
""email"": ""mayabi@example.com"",
115+
""salary"": 2000000
116+
}");
117+
118+
// Act
119+
string cleanedJson = json.RemoveDefaultNullProperties();
120+
JObject result = JObject.Parse(cleanedJson);
121+
122+
// Assert
123+
Assert.Equal("Tim", result["displayname"]?.ToString());
124+
Assert.Equal("mayabi@example.com", result["email"]?.ToString());
125+
Assert.Equal(2000000, result["salary"]?.ToObject<int>());
126+
}
127+
}
128+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
13+
<PackageReference Include="xunit" Version="2.9.2" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Using Include="Xunit" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Compile Include="../../Custom/JsonExtensions.cs">
23+
<Link>../../Custom/JsonExtensions.cs</Link>
24+
</Compile>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)