Skip to content

Commit 235f252

Browse files
committed
Units tests
1 parent 25f650f commit 235f252

File tree

10 files changed

+119
-9
lines changed

10 files changed

+119
-9
lines changed

.DS_Store

0 Bytes
Binary file not shown.

FakeApiTest/ConfigIOTests.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using FakeApi;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56
using Newtonsoft.Json;
@@ -26,7 +27,7 @@ public void GetConfigShouldThrowExceptionWhenConfigFileIsNotValid()
2627
{
2728
Assert.ThrowsException<JsonReaderException>(() =>
2829
{
29-
ConfigIO.GetConfig("DownloadFile.txt");
30+
ConfigIO.GetConfig("Files/DownloadFile.txt");
3031
});
3132
}
3233

@@ -35,11 +36,11 @@ public void GetConfigShouldThrowExceptionWhenConfigIsNull()
3536
{
3637
var ex = Assert.ThrowsException<FileLoadException>(() =>
3738
{
38-
ConfigIO.GetConfig("BadConfigFile.json");
39+
ConfigIO.GetConfig("Files/BadConfigFile.json");
3940
});
4041

4142
//Assert
42-
Assert.AreEqual("An error occured when deserialized file BadConfigFile.json", ex.Message);
43+
Assert.AreEqual("An error occured when deserialized file Files/BadConfigFile.json", ex.Message);
4344
}
4445

4546
[TestMethod]
@@ -52,6 +53,16 @@ public void WriteConfig_ShouldThrowExceptionIfConfigIsNull()
5253

5354
//Assert
5455
Assert.AreEqual("config", ex.ParamName);
56+
}
57+
58+
[TestMethod]
59+
public void ShouldMergeApisConfigFiles()
60+
{
61+
//Act
62+
var config = ConfigIO.GetConfig("Files/Config/Api/api.cfg.json");
63+
64+
//Assert
65+
Assert.AreEqual(4, config.Apis.Count());
5566
}
5667
}
5768
}

FakeApiTest/FakeApiTest.csproj

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<None Update="DownloadFile.txt">
17+
<None Update="Files\BadConfigFile.json">
1818
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1919
</None>
20-
<None Update="BadConfigFile.json">
20+
<None Update="Files\DownloadFile.txt">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Files\Config\Api\usersApi.cfg.json">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
<None Update="Files\Config\Api\api.cfg.json">
27+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
</None>
29+
<None Update="Files\Config\Api\ordersApi.cfg.json">
2130
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2231
</None>
2332
</ItemGroup>
2433
<ItemGroup>
2534
<ProjectReference Include="..\Src\FakeApi\FakeApi.csproj" />
2635
</ItemGroup>
36+
<ItemGroup>
37+
<Folder Include="Files\" />
38+
<Folder Include="Files\Config\" />
39+
<Folder Include="Files\Config\Api\" />
40+
</ItemGroup>
2741
</Project>

FakeApiTest/FakeHttpRequesterTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void ShoulReturnDataFromFile()
201201
var responseCfg = new HttpResponseMock();
202202
api.Responses = new[] { responseCfg };
203203
responseCfg.Active = true;
204-
responseCfg.File = "DownloadFile.txt";
204+
responseCfg.File = "Files/DownloadFile.txt";
205205

206206
ConfigIO.WriteConfig(config, filePath);
207207

@@ -216,7 +216,7 @@ public void ShoulReturnDataFromFile()
216216
using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
217217
{
218218
var content = streamReader.ReadToEnd();
219-
Assert.AreEqual(File.ReadAllText("DownloadFile.txt"), content);
219+
Assert.AreEqual(File.ReadAllText("Files/DownloadFile.txt"), content);
220220
}
221221
}
222222

@@ -290,7 +290,7 @@ public void ShouldThrowCustomException()
290290
public void ShouldThrowExceptionWhenRequestIsNull()
291291
{
292292
//Arrange
293-
var requester = new FakeHttpRequester("DownloadFile.txt");
293+
var requester = new FakeHttpRequester("Files/DownloadFile.txt");
294294

295295
//Act
296296
var exception = Assert.ThrowsException<ArgumentNullException>(() =>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"defaultMethod": "GET",
3+
"apisDirectories": [
4+
"Files/Config/Api"
5+
]
6+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"apis": [
3+
{
4+
"url": "https://localhost/api/orders/{orderId}",
5+
"responses": [
6+
{
7+
"active": 1,
8+
"file": "Response/User/getOrderById.json"
9+
},
10+
{
11+
"active": 1,
12+
"method": "PUT",
13+
"file": "Response/User/putOrder.json"
14+
},
15+
{
16+
"active": 1,
17+
"method": "DELETE",
18+
"httpCode": 400,
19+
"customApiException": {
20+
"fullTypeName": "App.CustomWebException, App",
21+
"constructorArgs": [
22+
"Order not found"
23+
]
24+
}
25+
}
26+
]
27+
}
28+
]
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"apis": [
3+
{
4+
"url": "https://localhost/api/users/{userId}",
5+
"responses": [
6+
{
7+
"active": 1,
8+
"file": "Config/Api/User/Response/getUserById.json"
9+
},
10+
{
11+
"active": 1,
12+
"method": "PUT",
13+
"file": "Config/Api/User/Response/putUser.json"
14+
}
15+
]
16+
},
17+
{
18+
"url": "https://localhost/api/users",
19+
"responses": [
20+
{
21+
"active": 1,
22+
"method": "POST",
23+
"file": "Config/Api/User/Response/postUser1.json"
24+
},
25+
{
26+
"active": 0,
27+
"method": "POST",
28+
"file": "Config/Api/User/Response/postUser2.json"
29+
}
30+
]
31+
},
32+
{
33+
"url": "https://localhost/api/users?pIndex={0}&pSize={1}",
34+
"responses": [
35+
{
36+
"active": 1,
37+
"file": "Config/Api/User/Response/searchUsersPage0.json"
38+
},
39+
{
40+
"active": 0,
41+
"file": "Config/Api/User/Response/searchUsersPage1.json"
42+
}
43+
]
44+
}
45+
]
46+
}

Src/FakeApi/ConfigIO.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void WriteConfig(Config config, string configFilePath)
6161
File.WriteAllText(configFilePath, JsonConvert.SerializeObject(config));
6262
}
6363

64-
private static void MergeApis(Config config, string configSource)
64+
public static void MergeApis(Config config, string configSource)
6565
{
6666
if (config.ApisDirectories == null)
6767
{
@@ -78,6 +78,10 @@ private static void MergeApis(Config config, string configSource)
7878
foreach (var file in Directory.GetFiles(directory).Except(new List<string> { configSource }))
7979
{
8080
var apis = LoadConfig(file).Apis;
81+
if(config.Apis == null)
82+
{
83+
config.Apis = new List<ApiConfig>();
84+
}
8185
config.Apis = new List<ApiConfig>(config.Apis.Union(apis));
8286
}
8387
}

0 commit comments

Comments
 (0)