Skip to content

Commit bd76071

Browse files
committed
Refactor and add more info tests
1 parent 99f6873 commit bd76071

File tree

10 files changed

+160
-60
lines changed

10 files changed

+160
-60
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SSLLWrapper.Models.Response;
4+
5+
namespace SSLLWrapper.Tests.AnalyzeSharedTests
6+
{
7+
public abstract class NegativeTests
8+
{
9+
public static Analyze AnalyzeResponse;
10+
11+
[TestMethod]
12+
public void then_the_error_count_should_be_greater_than_zero()
13+
{
14+
AnalyzeResponse.Errors.Count.Should().BeGreaterOrEqualTo(1);
15+
}
16+
17+
[TestMethod]
18+
public void then_the_HasErrorOccurred_should_be_true()
19+
{
20+
AnalyzeResponse.HasErrorOccurred.Should().BeTrue();
21+
}
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SSLLWrapper.Models.Response;
4+
5+
namespace SSLLWrapper.Tests.AnalyzeSharedTests
6+
{
7+
public abstract class PositiveTests
8+
{
9+
public static Analyze AnalyzeResponse;
10+
public static string TestHost;
11+
12+
[TestMethod]
13+
public void then_the_error_count_should_be_zero()
14+
{
15+
AnalyzeResponse.Errors.Count.Should().Be(0);
16+
}
17+
18+
[TestMethod]
19+
public void then_the_HasErrorOccurred_should_be_false()
20+
{
21+
AnalyzeResponse.HasErrorOccurred.Should().BeFalse();
22+
}
23+
24+
[TestMethod]
25+
public void then_the_host_should_match_the_host_passed_into_analyze()
26+
{
27+
AnalyzeResponse.host.Should().Be(TestHost);
28+
}
29+
}
30+
}

SSLLWrapper.Tests/AnalyzeTests.cs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
using SSLLWrapper.Interfaces;
88
using SSLLWrapper.Models;
99
using SSLLWrapper.Models.Response;
10+
using SSLLWrapper.Tests.AnalyzeSharedTests;
1011

1112
namespace given_that_I_make_a_analyze_request
1213
{
1314
[TestClass]
14-
public class when_the_api_is_online_and_a_valid_request_is_made
15+
public class when_the_api_is_online_and_a_valid_request_is_made : PositiveTests
1516
{
16-
private static Analyze _analyzeResponse;
17-
private const string TestHost = "https://www.ashleypoole.co.uk";
18-
1917
[ClassInitialize]
2018
public static void Setup(TestContext testContext)
2119
{
2220
var mockedApiProvider = new Mock<IApiProvider>();
21+
TestHost = "www.ashleypoole.co.uk";
2322
var webResponseModel = new WebResponseModel()
2423
{
2524
Payloay = "{\"host\":\"www.ashleypoole.co.uk\",\"port\":443,\"protocol\":\"HTTPS\",\"isPublic\":true,\"status\":\"READY\",\"" +
@@ -36,31 +35,13 @@ public static void Setup(TestContext testContext)
3635
mockedApiProvider.Setup(x => x.MakeGetRequest(It.IsAny<RequestModel>())).Returns(webResponseModel);
3736

3837
var ssllService = new SSLLService("https://api.dev.ssllabs.com/api/fa78d5a4/", mockedApiProvider.Object);
39-
_analyzeResponse = ssllService.Analyze(TestHost);
40-
}
41-
42-
[TestMethod]
43-
public void then_the_error_count_should_be_zero()
44-
{
45-
_analyzeResponse.Errors.Count.Should().Be(0);
46-
}
47-
48-
[TestMethod]
49-
public void then_HasErrorOccurred_should_be_false()
50-
{
51-
_analyzeResponse.HasErrorOccurred.Should().BeFalse();
38+
AnalyzeResponse = ssllService.Analyze(TestHost);
5239
}
5340

5441
[TestMethod]
5542
public void then_the_header_status_code_should_be_200()
5643
{
57-
_analyzeResponse.Header.statusCode.Should().Be(200);
58-
}
59-
60-
[TestMethod]
61-
public void then_the_host_should_match_the_host_passed_into_analyze()
62-
{
63-
_analyzeResponse.host.Should().Be(TestHost);
44+
AnalyzeResponse.Header.statusCode.Should().Be(200);
6445
}
6546
}
6647
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SSLLWrapper.Interfaces;
4+
5+
namespace SSLLWrapper.Tests
6+
{
7+
public class GenericNegativeTests<T> where T : IBaseResponse
8+
{
9+
public static T Response;
10+
11+
[TestMethod]
12+
public void then_the_error_count_should_be_greater_than_zero()
13+
{
14+
Response.Errors.Count.Should().BeGreaterOrEqualTo(1);
15+
}
16+
17+
[TestMethod]
18+
public void then_the_HasErrorOccurred_should_be_true()
19+
{
20+
Response.HasErrorOccurred.Should().BeTrue();
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using FluentAssertions;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using SSLLWrapper.Interfaces;
4+
5+
namespace SSLLWrapper.Tests
6+
{
7+
public class GenericPositiveTests<T> where T : IBaseResponse
8+
{
9+
public static T Response;
10+
11+
[TestMethod]
12+
public void then_the_error_count_should_be_zero()
13+
{
14+
Response.Errors.Count.Should().Be(0);
15+
}
16+
17+
[TestMethod]
18+
public void then_the_HasErrorOccurred_should_be_false()
19+
{
20+
Response.HasErrorOccurred.Should().BeFalse();
21+
}
22+
}
23+
}

SSLLWrapper.Tests/InfoTests.cs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
using System.IO;
2-
using System.Net;
3-
using FluentAssertions;
1+
using FluentAssertions;
42
using Microsoft.VisualStudio.TestTools.UnitTesting;
53
using Moq;
64
using SSLLWrapper;
75
using SSLLWrapper.Interfaces;
86
using SSLLWrapper.Models;
97
using SSLLWrapper.Models.Response;
8+
using SSLLWrapper.Tests;
109

1110
namespace given_that_I_make_a_info_request
1211
{
1312
[TestClass]
14-
public class when_the_api_is_online
13+
public class when_the_api_is_online : GenericPositiveTests<Info>
1514
{
16-
private static Info _infoResponse;
17-
1815
[ClassInitialize]
1916
public static void Setup(TestContext testContext)
2017
{
@@ -30,45 +27,31 @@ public static void Setup(TestContext testContext)
3027
mockedApiProvider.Setup(x => x.MakeGetRequest(It.IsAny<RequestModel>())).Returns(webResponseModel);
3128

3229
var ssllService = new SSLLService("https://api.dev.ssllabs.com/api/fa78d5a4/", mockedApiProvider.Object);
33-
_infoResponse = ssllService.Info();
34-
}
35-
36-
[TestMethod]
37-
public void then_the_error_count_should_be_zero()
38-
{
39-
_infoResponse.Errors.Count.Should().Be(0);
40-
}
41-
42-
[TestMethod]
43-
public void then_the_HasErrorOccurred_should_be_false()
44-
{
45-
_infoResponse.HasErrorOccurred.Should().BeFalse();
30+
Response = ssllService.Info();
4631
}
4732

4833
[TestMethod]
4934
public void then_the_api_should_be_marked_as_online()
5035
{
51-
_infoResponse.Online.Should().BeTrue();
36+
Response.Online.Should().BeTrue();
5237
}
5338

5439
[TestMethod]
5540
public void then_the_info_response_should_be_populated_with_a_engine_number()
5641
{
57-
_infoResponse.engineVersion.Should().NotBeNullOrEmpty();
42+
Response.engineVersion.Should().NotBeNullOrEmpty();
5843
}
5944

6045
[TestMethod]
6146
public void then_the_info_response_header_status_code_should_be_200()
6247
{
63-
_infoResponse.Header.statusCode.Should().Be(200);
48+
Response.Header.statusCode.Should().Be(200);
6449
}
6550
}
6651

6752
[TestClass]
68-
public class when_the_api_is_offline
53+
public class when_the_api_is_offline : SharedNegativeTests
6954
{
70-
private static Info _infoResponse;
71-
7255
[ClassInitialize]
7356
public static void Setup(TestContext testContext)
7457
{
@@ -84,31 +67,50 @@ public static void Setup(TestContext testContext)
8467
mockedApiProvider.Setup(x => x.MakeGetRequest(It.IsAny<RequestModel>())).Returns(webResponseModel);
8568

8669
var ssllService = new SSLLService("https://api.dev.ssllabs.com/api/fa78d5a4/", mockedApiProvider.Object);
87-
_infoResponse = ssllService.Info();
70+
Response = ssllService.Info();
8871
}
8972

9073
[TestMethod]
91-
public void then_the_error_count_should_be_zero()
74+
public void then_the_info_response_header_status_code_should_be_400()
9275
{
93-
_infoResponse.Errors.Count.Should().BeGreaterOrEqualTo(1);
76+
Response.Header.statusCode.Should().Be(400);
9477
}
9578

9679
[TestMethod]
97-
public void then_the_HasErrorOccurred_should_be_true()
80+
public void then_the_info_response_header_status_description_should_match_api()
9881
{
99-
_infoResponse.HasErrorOccurred.Should().BeTrue();
82+
Response.Header.statusDescription.Should().Be("Bad Request");
10083
}
84+
}
10185

102-
[TestMethod]
103-
public void then_the_api_should_be_marked_as_offline()
86+
[TestClass]
87+
public class when_the_api_url_is_invalid : SharedNegativeTests
88+
{
89+
[ClassInitialize]
90+
public static void Setup(TestContext testContext)
10491
{
105-
_infoResponse.Online.Should().BeFalse();
92+
var mockedApiProvider = new Mock<IApiProvider>();
93+
var webResponseModel = new WebResponseModel()
94+
{
95+
Payloay = "",
96+
StatusCode = 0,
97+
StatusDescription = "",
98+
Url = ""
99+
};
100+
101+
mockedApiProvider.Setup(x => x.MakeGetRequest(It.IsAny<RequestModel>())).Returns(webResponseModel);
102+
103+
var ssllService = new SSLLService("https://blah-blah.dev.ssllabs.com/api/blah/", mockedApiProvider.Object);
104+
Response = ssllService.Info();
106105
}
106+
}
107107

108+
public abstract class SharedNegativeTests : GenericNegativeTests<Info>
109+
{
108110
[TestMethod]
109-
public void then_the_info_response_header_status_code_should_be_400()
111+
public void then_the_api_should_be_marked_as_offline()
110112
{
111-
_infoResponse.Header.statusCode.Should().Be(400);
113+
Response.Online.Should().BeFalse();
112114
}
113115
}
114116
}

SSLLWrapper.Tests/SSLLWrapper.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@
6767
</Otherwise>
6868
</Choose>
6969
<ItemGroup>
70+
<Compile Include="AnalyzeSharedTests\PositiveTests.cs" />
71+
<Compile Include="AnalyzeSharedTests\NegativeTests.cs" />
7072
<Compile Include="AnalyzeTests.cs" />
73+
<Compile Include="GenericPositiveTests.cs" />
74+
<Compile Include="GenericNegativeTests.cs" />
7175
<Compile Include="InfoTests.cs" />
7276
<Compile Include="Properties\AssemblyInfo.cs" />
7377
</ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using SSLLWrapper.Models.Response.BaseSubModels;
3+
4+
namespace SSLLWrapper.Interfaces
5+
{
6+
public interface IBaseResponse
7+
{
8+
Header Header { get; set; }
9+
bool HasErrorOccurred { get; set; }
10+
List<Error> Errors { get; set; }
11+
}
12+
}

SSLLWrapper/Models/Response/BaseModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Collections.Generic;
2+
using SSLLWrapper.Interfaces;
23
using SSLLWrapper.Models.Response.BaseSubModels;
34

45
namespace SSLLWrapper.Models.Response
56
{
6-
public class BaseModel
7+
public class BaseModel : IBaseResponse
78
{
89
public Header Header { get; set; }
910
public bool HasErrorOccurred { get; set; }

SSLLWrapper/SSLLWrapper.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
</ItemGroup>
4646
<ItemGroup>
4747
<Compile Include="External\SSLLabsApi.cs" />
48+
<Compile Include="Interfaces\IBaseResponse.cs" />
4849
<Compile Include="Models\Response\StatusCodes.cs" />
4950
<Compile Include="Models\WebResponseModel.cs" />
5051
<Compile Include="SSLLService.cs" />

0 commit comments

Comments
 (0)