Skip to content

Commit

Permalink
Refactorign TDD side and fixed standart method
Browse files Browse the repository at this point in the history
  • Loading branch information
Elyor Latipov committed Mar 30, 2020
1 parent e1e7c77 commit 974ad86
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 106 deletions.
12 changes: 6 additions & 6 deletions CT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CT.Api", "src\CT.Api\CT.Api
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CT.Unit.Test", "test\CT.Unit.Test\CT.Unit.Test.csproj", "{C873F671-F5B3-4CD4-A2BA-39B76909D9C6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CT.Integration.Test", "test\CT.Integration.Test\CT.Integration.Test.csproj", "{E45ED139-F854-4397-8DE0-580D1D8CB6D6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CT.Integration.Test", "test\CT.Integration.Test\CT.Integration.Test.csproj", "{30C73AB1-868B-419F-B3AC-3C69437CCE43}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -39,10 +39,10 @@ Global
{C873F671-F5B3-4CD4-A2BA-39B76909D9C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C873F671-F5B3-4CD4-A2BA-39B76909D9C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C873F671-F5B3-4CD4-A2BA-39B76909D9C6}.Release|Any CPU.Build.0 = Release|Any CPU
{E45ED139-F854-4397-8DE0-580D1D8CB6D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E45ED139-F854-4397-8DE0-580D1D8CB6D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E45ED139-F854-4397-8DE0-580D1D8CB6D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E45ED139-F854-4397-8DE0-580D1D8CB6D6}.Release|Any CPU.Build.0 = Release|Any CPU
{30C73AB1-868B-419F-B3AC-3C69437CCE43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30C73AB1-868B-419F-B3AC-3C69437CCE43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30C73AB1-868B-419F-B3AC-3C69437CCE43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30C73AB1-868B-419F-B3AC-3C69437CCE43}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -52,7 +52,7 @@ Global
{D8A1996D-C9EE-43E5-A3DA-6C286DFF5FD9} = {3F01CD45-1A93-469A-BC7E-77BB528B5B78}
{35B072D7-F875-4CD8-B68F-7A8D258EBB98} = {3F01CD45-1A93-469A-BC7E-77BB528B5B78}
{C873F671-F5B3-4CD4-A2BA-39B76909D9C6} = {11390D1D-84BB-41F0-8B4B-E5D1C9E3E18B}
{E45ED139-F854-4397-8DE0-580D1D8CB6D6} = {11390D1D-84BB-41F0-8B4B-E5D1C9E3E18B}
{30C73AB1-868B-419F-B3AC-3C69437CCE43} = {11390D1D-84BB-41F0-8B4B-E5D1C9E3E18B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83BE496A-D0AE-4E65-BB4D-312AF5AF0CAA}
Expand Down
2 changes: 2 additions & 0 deletions src/CT.Api/CT.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
<ProjectReference Include="..\CT.Service\CT.Service.csproj" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>

</Project>
7 changes: 3 additions & 4 deletions src/CT.Api/Controllers/AirportController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CT.Api.Models;
Expand All @@ -10,6 +9,7 @@

namespace CT.Api.Controllers
{

[Route("api/[controller]/[action]")]
[ApiController]
public class AirportController : ControllerBase
Expand All @@ -21,7 +21,7 @@ public AirportController(RestService service)
_service = service;
}

[HttpPost("calc-dist")]
[HttpPost]
public async Task<IActionResult> CaculateDistance([FromBody]DistanceInputModel model)
{
if (!ModelState.IsValid)
Expand All @@ -31,10 +31,9 @@ public async Task<IActionResult> CaculateDistance([FromBody]DistanceInputModel m

var sourceAir = await _service.GetAirport(model.SourceCode) ?? throw new AppException("Source entry is null");
var targetAir = await _service.GetAirport(model.TargetCode) ?? throw new AppException("Target entry is null");

var betweenDist = sourceAir.location.GetDistance(targetAir.location);

return Ok($"{betweenDist:0.######}");
return Ok(betweenDist);
}

}
Expand Down
8 changes: 7 additions & 1 deletion src/CT.Api/Models/DistanceInputModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
Expand All @@ -17,5 +18,10 @@ public class DistanceInputModel
[MinLength(3)]
public string TargetCode { get; set; }

public override string ToString()
{
return JsonConvert.SerializeObject(this,Formatting.None);
}

}
}
7 changes: 4 additions & 3 deletions src/CT.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Startup(IConfiguration configuration)

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
public virtual void ConfigureServices(IServiceCollection services)
{
//config
var config = new AppConfig();
Expand All @@ -38,8 +38,9 @@ public void ConfigureServices(IServiceCollection services)
a.BaseAddress = new Uri(config.SourceUrl);
});

services.AddCors(a => a.AddPolicy("All", b => b.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()));
services.AddMvc();
services.AddHttpClient();
services.AddCors(a => a.AddPolicy("All", b => b.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials()));

//swagger
services.AddSwaggerGen(c =>
Expand All @@ -50,7 +51,7 @@ public void ConfigureServices(IServiceCollection services)
Version = "v1"
});
});

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down
6 changes: 1 addition & 5 deletions src/CT.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@
"System": "Information",
"Microsoft": "Information"
}
},
"AppConfig": {
"ServerUrl": "http://localhost:5000",
"SourceUrl": "https://places-dev.cteleport.com"
}
}
}
6 changes: 1 addition & 5 deletions src/CT.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@
"Default": "Warning"
}
},
"AllowedHosts": "*",
"AppConfig": {
"ServerUrl": "http://127.0.0.1:5000",
"SourceUrl": "https://places-dev.cteleport.com"
}
"AllowedHosts": "*"
}
6 changes: 2 additions & 4 deletions src/CT.Common/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ namespace CT.Common
{
public class AppConfig
{

public string ServerUrl { get; set; }

public string SourceUrl { get; set; }

public string SourceUrl { get; set; } = "https://places-dev.cteleport.com";

}
}
62 changes: 0 additions & 62 deletions test/CT.Integration.Test/ApiTest.cs

This file was deleted.

4 changes: 2 additions & 2 deletions test/CT.Integration.Test/CT.Integration.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
Expand Down
12 changes: 0 additions & 12 deletions test/CT.Integration.Test/Properties/launchSettings.json

This file was deleted.

47 changes: 47 additions & 0 deletions test/CT.Integration.Test/RestTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CT.Api;
using CT.Api.Models;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CT.Integration.Test
{

[TestClass]
public class RestTest
{

[TestMethod]
public async Task Calculation_Distance_Test()
{
// Arrange
var webHostBuilder = WebHost.CreateDefaultBuilder()
.UseEnvironment("Testing")
.UseStartup<Startup>();
var expected = 4158;

using (var server = new TestServer(webHostBuilder))
using (var client = server.CreateClient())
{
// Act
var inputModel = new DistanceInputModel
{
SourceCode = "AMS",
TargetCode = "USA"
};
var response = await client.PostAsJsonAsync("/api/Airport/CaculateDistance", inputModel);
var actual = await response.Content.ReadAsAsync<double>();

// Assert
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(Math.Round((double)actual), expected);
}
}
}
}
2 changes: 1 addition & 1 deletion test/CT.Unit.Test/CT.Unit.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace CT.Unit.Test
{
[TestClass]
public class CommonTest
public class ExtensionsTest
{
[TestMethod]
public void Calculation_Distance_Test()
Expand Down

0 comments on commit 974ad86

Please sign in to comment.