Skip to content

Commit 2226cb8

Browse files
committed
Merge pull request #1 from AshleyPoole/add-unit-tests
Refractor code to increase testability
2 parents b3a6a39 + b657371 commit 2226cb8

File tree

15 files changed

+167
-150
lines changed

15 files changed

+167
-150
lines changed

.nuget/packages.config

Lines changed: 0 additions & 4 deletions
This file was deleted.

SSLLWrapper.Tests/Class1.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

SSLLWrapper.Tests/InfoTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.IO;
2+
using System.Net;
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using SSLLWrapper.Interfaces;
7+
using SSLLWrapper.Models;
8+
using SSLLWrapper.Models.Response;
9+
10+
namespace SSLLWrapper.Tests
11+
//namespace given_that_I_make_a_info_request
12+
{
13+
[TestClass]
14+
public class when_the_api_is_online
15+
{
16+
private static Info _infoResponse;
17+
18+
[ClassInitialize]
19+
public static void Setup(TestContext testContext)
20+
{
21+
Stream responseStream = new MemoryStream();
22+
var mockedApiProvider = new Mock<IApiProvider>();
23+
var mockedHttpWebResponse = new Mock<HttpWebResponse>();
24+
var streamWriter = new StreamWriter(responseStream);
25+
26+
streamWriter.Write("{\"engineVersion\":\"1.11.4\",\"criteriaVersion\":\"2009i\",\"clientMaxAssessments\":5,\"notice\":\"Some notice goes here\"}");
27+
28+
mockedHttpWebResponse.Setup(x => x.GetResponseStream()).Returns(responseStream);
29+
mockedApiProvider.Setup(x => x.MakeGetRequest(new RequestModel())).Returns(mockedHttpWebResponse.Object);
30+
31+
var ssllService = new SSLLService("https://api.dev.ssllabs.com/api/fa78d5a4/", mockedApiProvider.Object);
32+
_infoResponse = ssllService.Info();
33+
}
34+
35+
[TestMethod]
36+
public void then_no_errors_should_be_created()
37+
{
38+
_infoResponse.Errors.Count.Should().Be(0);
39+
}
40+
41+
[TestMethod]
42+
public void then_the_api_should_be_marked_as_online()
43+
{
44+
_infoResponse.Online.Should().BeTrue();
45+
}
46+
47+
[TestMethod]
48+
public void then_the_info_response_should_be_populated_with_a_engine_number()
49+
{
50+
_infoResponse.engineVersion.Should().NotBeNullOrEmpty();
51+
}
52+
}
53+
}

SSLLWrapper.Tests/SSLLWrapper.Tests.csproj

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
43
<PropertyGroup>
54
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
65
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{96C1BF31-F13B-449B-96BB-BDC0E21A97D8}</ProjectGuid>
6+
<ProjectGuid>{3F2EFE11-F333-4413-9FB9-61BC7CA2D6E6}</ProjectGuid>
87
<OutputType>Library</OutputType>
98
<AppDesignerFolder>Properties</AppDesignerFolder>
109
<RootNamespace>SSLLWrapper.Tests</RootNamespace>
1110
<AssemblyName>SSLLWrapper.Tests</AssemblyName>
1211
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1312
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
1419
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1520
<RestorePackages>true</RestorePackages>
1621
</PropertyGroup>
@@ -32,49 +37,67 @@
3237
<WarningLevel>4</WarningLevel>
3338
</PropertyGroup>
3439
<ItemGroup>
35-
<Reference Include="FluentAssertions">
40+
<Reference Include="FluentAssertions, Version=3.2.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
41+
<SpecificVersion>False</SpecificVersion>
3642
<HintPath>..\packages\FluentAssertions.3.2.2\lib\net45\FluentAssertions.dll</HintPath>
3743
</Reference>
3844
<Reference Include="FluentAssertions.Core">
3945
<HintPath>..\packages\FluentAssertions.3.2.2\lib\net45\FluentAssertions.Core.dll</HintPath>
4046
</Reference>
41-
<Reference Include="FluentTests.Framework">
42-
<HintPath>..\packages\FluentTests.1.1.1\lib\FluentTests.Framework.dll</HintPath>
43-
</Reference>
44-
<Reference Include="FluentTests.NUnit">
45-
<HintPath>..\packages\FluentTests.NUnit.1.1.2\lib\FluentTests.NUnit.dll</HintPath>
46-
</Reference>
4747
<Reference Include="Moq">
4848
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>
4949
</Reference>
50-
<Reference Include="nunit.framework">
51-
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
52-
</Reference>
53-
<Reference Include="NUnit.Should">
54-
<HintPath>..\packages\NUnit.Should.1.0.2.0\lib\NET30\NUnit.Should.dll</HintPath>
50+
<Reference Include="MSTest.Fluent">
51+
<HintPath>..\packages\MSTest.Fluent.1.1.0\lib\net45\MSTest.Fluent.dll</HintPath>
5552
</Reference>
5653
<Reference Include="System" />
57-
<Reference Include="System.Core" />
58-
<Reference Include="System.Xml.Linq" />
59-
<Reference Include="System.Data.DataSetExtensions" />
60-
<Reference Include="Microsoft.CSharp" />
61-
<Reference Include="System.Data" />
6254
<Reference Include="System.Xml" />
55+
<Reference Include="System.Xml.Linq" />
6356
</ItemGroup>
57+
<Choose>
58+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
59+
<ItemGroup>
60+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
61+
</ItemGroup>
62+
</When>
63+
<Otherwise>
64+
<ItemGroup>
65+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
66+
</ItemGroup>
67+
</Otherwise>
68+
</Choose>
6469
<ItemGroup>
65-
<Compile Include="Class1.cs" />
70+
<Compile Include="InfoTests.cs" />
6671
<Compile Include="Properties\AssemblyInfo.cs" />
6772
</ItemGroup>
68-
<ItemGroup>
69-
<None Include="app.config" />
70-
<None Include="packages.config" />
71-
</ItemGroup>
7273
<ItemGroup>
7374
<ProjectReference Include="..\SSLLWrapper\SSLLWrapper.csproj">
74-
<Project>{9D2FF62D-01C0-41B0-A3BE-32005365290C}</Project>
75+
<Project>{9d2ff62d-01c0-41b0-a3be-32005365290c}</Project>
7576
<Name>SSLLWrapper</Name>
7677
</ProjectReference>
7778
</ItemGroup>
79+
<ItemGroup>
80+
<None Include="packages.config" />
81+
</ItemGroup>
82+
<Choose>
83+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
84+
<ItemGroup>
85+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
86+
<Private>False</Private>
87+
</Reference>
88+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
89+
<Private>False</Private>
90+
</Reference>
91+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
92+
<Private>False</Private>
93+
</Reference>
94+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
95+
<Private>False</Private>
96+
</Reference>
97+
</ItemGroup>
98+
</When>
99+
</Choose>
100+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
78101
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
79102
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
80103
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

SSLLWrapper.Tests/app.config

Lines changed: 0 additions & 15 deletions
This file was deleted.

SSLLWrapper.Tests/packages.config

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="FluentAssertions" version="3.2.2" targetFramework="net45" />
4-
<package id="FluentTests" version="1.1.1" targetFramework="net45" />
5-
<package id="FluentTests.NUnit" version="1.1.2" targetFramework="net45" />
64
<package id="Moq" version="4.2.1409.1722" targetFramework="net45" />
7-
<package id="NUnit" version="2.6.4" targetFramework="net45" />
8-
<package id="NUnit.Should" version="1.0.2.0" targetFramework="net45" />
5+
<package id="MSTest.Fluent" version="1.1.0" targetFramework="net45" />
96
</packages>

SSLLWrapper.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{06122B
99
ProjectSection(SolutionItems) = preProject
1010
.nuget\NuGet.Config = .nuget\NuGet.Config
1111
.nuget\NuGet.exe = .nuget\NuGet.exe
12-
.nuget\NuGet.targets = .nuget\NuGet.targets
1312
EndProjectSection
1413
EndProject
1514
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SSLLWrapper.Tests", "SSLLWrapper.Tests\SSLLWrapper.Tests.csproj", "{3F2EFE11-F333-4413-9FB9-61BC7CA2D6E6}"
Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,59 @@
11
using System.Net;
22
using Newtonsoft.Json;
3+
using SSLLWrapper.Models;
34
using SSLLWrapper.Models.Response;
5+
using SSLLWrapper.Models.Response.BaseSubModels;
46

57
namespace SSLLWrapper.Domain
68
{
79
class ResponsePopulation
810
{
911
public JsonSerializerSettings JsonSerializerSettings;
10-
private readonly WebResponseReader _webResponseReader;
1112

1213
public ResponsePopulation()
1314
{
1415
// Ignoring null values when serializing json objects
1516
JsonSerializerSettings = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore};
16-
17-
_webResponseReader = new WebResponseReader();
1817
}
1918

20-
public Info InfoModel(HttpWebResponse webResponse, Info infoModel)
19+
public Info InfoModel(WebResponseModel webResponse, Info infoModel)
2120
{
22-
var webResult = _webResponseReader.GetResponsePayload(webResponse);
23-
24-
infoModel = JsonConvert.DeserializeObject<Info>(webResult, JsonSerializerSettings);
25-
infoModel.Header.statusCode = _webResponseReader.GetStatusCode(webResponse);
26-
infoModel.Header.statusDescription = _webResponseReader.GetStatusDescription(webResponse);
21+
infoModel = JsonConvert.DeserializeObject<Info>(webResponse.Payloay, JsonSerializerSettings);
22+
infoModel.Header = PopulateHeader(infoModel.Header, webResponse);
2723

2824
return infoModel;
2925
}
3026

31-
public Analyze AnalyzeModel(HttpWebResponse webResponse, Analyze analyzeModel)
27+
public Analyze AnalyzeModel(WebResponseModel webResponse, Analyze analyzeModel)
3228
{
33-
var webResult = _webResponseReader.GetResponsePayload(webResponse);
34-
35-
analyzeModel = JsonConvert.DeserializeObject<Analyze>(webResult, JsonSerializerSettings);
36-
analyzeModel.Header.statusCode = _webResponseReader.GetStatusCode(webResponse);
37-
analyzeModel.Header.statusDescription = _webResponseReader.GetStatusDescription(webResponse);
29+
analyzeModel = JsonConvert.DeserializeObject<Analyze>(webResponse.Payloay, JsonSerializerSettings);
30+
analyzeModel.Header = PopulateHeader(analyzeModel.Header, webResponse);
3831

3932
return analyzeModel;
4033
}
4134

42-
public Endpoint EndpointModel(HttpWebResponse webResponse, Endpoint endpointModel)
35+
public Endpoint EndpointModel(WebResponseModel webResponse, Endpoint endpointModel)
4336
{
44-
var webResult = _webResponseReader.GetResponsePayload(webResponse);
45-
46-
endpointModel = JsonConvert.DeserializeObject<Endpoint>(webResult, JsonSerializerSettings);
47-
endpointModel.Header.statusCode = _webResponseReader.GetStatusCode(webResponse);
48-
endpointModel.Header.statusDescription = _webResponseReader.GetStatusDescription(webResponse);
37+
endpointModel = JsonConvert.DeserializeObject<Endpoint>(webResponse.Payloay, JsonSerializerSettings);
38+
endpointModel.Header = PopulateHeader(endpointModel.Header, webResponse);
4939

5040
return endpointModel;
5141
}
5242

53-
public StatusCodes StatusCodesModel(HttpWebResponse webResponse, StatusCodes statusCodes)
43+
public StatusCodes StatusCodesModel(WebResponseModel webResponse, StatusCodes statusCodes)
5444
{
55-
var webResult = _webResponseReader.GetResponsePayload(webResponse);
56-
57-
statusCodes = JsonConvert.DeserializeObject<StatusCodes>(webResult, JsonSerializerSettings);
58-
statusCodes.Header.statusCode = _webResponseReader.GetStatusCode(webResponse);
59-
statusCodes.Header.statusDescription = _webResponseReader.GetStatusDescription(webResponse);
45+
statusCodes = JsonConvert.DeserializeObject<StatusCodes>(webResponse.Payloay, JsonSerializerSettings);
46+
statusCodes.Header = PopulateHeader(statusCodes.Header, webResponse);
6047

6148
return statusCodes;
6249
}
50+
51+
public Header PopulateHeader(Header header, WebResponseModel webResponse)
52+
{
53+
header.statusCode = webResponse.StatusCode;
54+
header.statusDescription = webResponse.StatusDescription;
55+
56+
return header;
57+
}
6358
}
6459
}

SSLLWrapper/Domain/UrlValidation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,15 @@ public bool IsValid(string url)
1616

1717
return valid;
1818
}
19+
20+
public string Format(string url)
21+
{
22+
if (!url.EndsWith("/"))
23+
{
24+
url = url + "/";
25+
}
26+
27+
return url;
28+
}
1929
}
2030
}

SSLLWrapper/Domain/WebResponseReader.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)