Skip to content

Commit 6da60a0

Browse files
author
Michael Mantos
committed
Update test app
1 parent 44b67ec commit 6da60a0

File tree

9 files changed

+184
-6
lines changed

9 files changed

+184
-6
lines changed

Src/StackifyLib/Config.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ public static void ReadStackifyJSONConfig(string filePath)
228228

229229
public static void SetStackifyObj(JObject obj)
230230
{
231-
AppName = TryGetValue(obj, "AppName");
232-
Environment = TryGetValue(obj, "Environment");
233-
ApiKey = TryGetValue(obj, "ApiKey");
231+
AppName = TryGetValue(obj, "AppName") ?? AppName;
232+
Environment = TryGetValue(obj, "Environment") ?? Environment;
233+
ApiKey = TryGetValue(obj, "ApiKey") ?? ApiKey;
234234
}
235235

236236
private static string TryGetValue(JToken jToken, string key)
@@ -240,14 +240,23 @@ private static string TryGetValue(JToken jToken, string key)
240240
try
241241
{
242242
var val = jToken[key];
243-
if (val != null)
243+
if (val == null)
244244
{
245-
r = val.ToString();
245+
StackifyAPILogger.Log($"#Config #TryGetValue #Json failed - Property is null or empty - Property: {key}");
246+
return r;
246247
}
248+
249+
if (val.Type != JTokenType.String || (val.Type == JTokenType.String && string.IsNullOrEmpty(val.ToString())))
250+
{
251+
StackifyAPILogger.Log($"#Config #TryGetValue #Json failed - Property is not a string - Property: {key}");
252+
return r;
253+
}
254+
255+
r = val.ToString();
247256
}
248257
catch (Exception ex)
249258
{
250-
StackifyAPILogger.Log("#Config #TryGetValue failed", ex);
259+
StackifyAPILogger.Log("#Config #TryGetValue #Json failed", ex);
251260
}
252261

253262
return r;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Reflection;
6+
using System.Text;
7+
using FluentAssertions;
8+
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace StackifyLib.UnitTests
14+
{
15+
public class ConfigJSON_Tests
16+
{
17+
private readonly ITestOutputHelper output;
18+
private readonly string AppName = "TestAppName";
19+
private readonly string Environment = "TestAppName";
20+
private readonly string ApiKey = "TestAppName";
21+
22+
public ConfigJSON_Tests(ITestOutputHelper output)
23+
{
24+
this.output = output;
25+
}
26+
27+
28+
[Fact]
29+
public void Should_Ignore_Invalid_Format_Json_File()
30+
{
31+
ResetConfig();
32+
33+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
34+
string jsonPath = Path.Combine(baseDirectory, "Fixtures", "JsonConfig", "Stackify-Invalid-Format.json");
35+
36+
StackifyLib.Config.ReadStackifyJSONConfig(jsonPath);
37+
38+
Assert.Equal(StackifyLib.Config.AppName, AppName);
39+
Assert.Equal(StackifyLib.Config.Environment, Environment);
40+
Assert.Equal(StackifyLib.Config.ApiKey, ApiKey);
41+
}
42+
43+
[Fact]
44+
public void Should_Ignore_Invalid_Property_Type_Json_File()
45+
{
46+
ResetConfig();
47+
48+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
49+
string jsonPath = Path.Combine(baseDirectory, "Fixtures", "JsonConfig", "Stackify-Invalid-Property-Type.json");
50+
51+
StackifyLib.Config.ReadStackifyJSONConfig(jsonPath);
52+
53+
Assert.Equal(StackifyLib.Config.AppName, AppName);
54+
Assert.Equal(StackifyLib.Config.Environment, Environment);
55+
Assert.Equal(StackifyLib.Config.ApiKey, ApiKey);
56+
}
57+
58+
[Fact]
59+
public void Should_Ignore_Empty_Json_File()
60+
{
61+
ResetConfig();
62+
63+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
64+
string jsonPath = Path.Combine(baseDirectory, "Fixtures", "JsonConfig", "Stackify-Empty.json");
65+
66+
StackifyLib.Config.ReadStackifyJSONConfig(jsonPath);
67+
68+
69+
Assert.Equal(StackifyLib.Config.AppName, AppName);
70+
Assert.Equal(StackifyLib.Config.Environment, Environment);
71+
Assert.Equal(StackifyLib.Config.ApiKey, ApiKey);
72+
}
73+
74+
[Fact]
75+
public void Should_Read_Valid_Format_Json_File()
76+
{
77+
/*
78+
{
79+
"AppName": "CoreConsoleApp",
80+
"Environment": "Dev",
81+
"ApiKey": "sampleKey"
82+
}
83+
*/
84+
ResetConfig();
85+
86+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
87+
string jsonPath = Path.Combine(baseDirectory, "Fixtures", "JsonConfig", "Stackify.json");
88+
89+
StackifyLib.Config.ReadStackifyJSONConfig(jsonPath);
90+
91+
Assert.Equal(StackifyLib.Config.AppName, "CoreConsoleApp");
92+
Assert.Equal(StackifyLib.Config.Environment, "Dev");
93+
Assert.Equal(StackifyLib.Config.ApiKey, "sampleKey");
94+
}
95+
96+
[Fact]
97+
public void Should_Read_Valid_Format_With_Comment_Json_File()
98+
{
99+
/*
100+
{
101+
"AppName": "CoreConsoleApp",
102+
"Environment": "Dev",
103+
"ApiKey": "sampleKey"
104+
}
105+
*/
106+
ResetConfig();
107+
108+
string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
109+
string jsonPath = Path.Combine(baseDirectory, "Fixtures", "JsonConfig", "Stackify-With-Comment.json");
110+
111+
StackifyLib.Config.ReadStackifyJSONConfig(jsonPath);
112+
113+
Assert.Equal(StackifyLib.Config.AppName, "CoreConsoleApp");
114+
Assert.Equal(StackifyLib.Config.Environment, "Dev");
115+
Assert.Equal(StackifyLib.Config.ApiKey, "sampleKey");
116+
}
117+
118+
private void ResetConfig()
119+
{
120+
StackifyLib.Config.AppName = AppName;
121+
StackifyLib.Config.Environment = Environment;
122+
StackifyLib.Config.ApiKey = ApiKey;
123+
}
124+
}
125+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Invalid
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"AppName": true,
3+
"Environment": {
4+
"Test": "Test"
5+
},
6+
"ApiKey": [
7+
"Test"
8+
]
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"AppName": "CoreConsoleApp",
3+
"Environment": "Dev",
4+
"ApiKey": "sampleKey"
5+
// "AppName": "CoreConsoleApp2",
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"AppName": "CoreConsoleApp",
3+
"Environment": "Dev",
4+
"ApiKey": "sampleKey"
5+
}

test/StackifyLib.UnitTests/StackifyLib.UnitTests.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,22 @@
1717
<ProjectReference Include="..\..\Src\StackifyLib\StackifyLib.csproj" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<None Update="Fixtures\JsonConfig\Stackify-With-Comment.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
24+
<None Update="Fixtures\JsonConfig\Stackify-Invalid-Property-Type.json">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
</None>
27+
<None Update="Fixtures\JsonConfig\Stackify-Invalid-Format.json">
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
</None>
30+
<None Update="Fixtures\JsonConfig\Stackify-Empty.json">
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</None>
33+
<None Update="Fixtures\JsonConfig\Stackify.json">
34+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
35+
</None>
36+
</ItemGroup>
37+
2038
</Project>

0 commit comments

Comments
 (0)