add openhack files
This commit is contained in:
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using poi.Data;
|
||||
using IntegrationTests.Utilities;
|
||||
using System.IO;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CustomWebApplicationFactory<TStartup>
|
||||
: WebApplicationFactory<poi.Startup>
|
||||
{
|
||||
protected override IWebHostBuilder CreateWebHostBuilder(){
|
||||
//used to read env variables for host/port
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
var host = new WebHostBuilder()
|
||||
.UseKestrel()
|
||||
.UseConfiguration(configuration)
|
||||
.UseIISIntegration()
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
{
|
||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
logging.AddConsole();
|
||||
logging.AddDebug();
|
||||
})
|
||||
.UseStartup<IntegrationTests.Startup>()
|
||||
.UseUrls("http://localhost:8080");
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
// Create a new service provider.
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddEntityFrameworkInMemoryDatabase()
|
||||
.BuildServiceProvider();
|
||||
|
||||
// Add a database context (ApplicationDbContext) using an in-memory
|
||||
// database for testing.
|
||||
services.AddDbContext<POIContext>(options =>
|
||||
{
|
||||
options.UseInMemoryDatabase("InMemoryDbForTesting");
|
||||
options.UseInternalServiceProvider(serviceProvider);
|
||||
});
|
||||
|
||||
// Build the service provider.
|
||||
var sp = services.BuildServiceProvider();
|
||||
|
||||
// Create a scope to obtain a reference to the database
|
||||
// context (ApplicationDbContext).
|
||||
using (var scope = sp.CreateScope())
|
||||
{
|
||||
var scopedServices = scope.ServiceProvider;
|
||||
var db = scopedServices.GetRequiredService<POIContext>();
|
||||
var logger = scopedServices
|
||||
.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
|
||||
|
||||
// Ensure the database is created.
|
||||
db.Database.EnsureCreated();
|
||||
|
||||
try
|
||||
{
|
||||
// Seed the database with test data.
|
||||
DatabaseHelpers.InitializeDbForTests(db);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"An error occurred seeding the " +
|
||||
"database with test POIs. Error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
30
apis/poi/tests/IntegrationTests/IntegrationTests.csproj
Normal file
30
apis/poi/tests/IntegrationTests/IntegrationTests.csproj
Normal file
@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\web\poi.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
</Project>
|
54
apis/poi/tests/IntegrationTests/POITests.cs
Normal file
54
apis/poi/tests/IntegrationTests/POITests.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
using poi.Controllers;
|
||||
using poi.Models;
|
||||
using poi;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class POIIntegrationTests: IClassFixture<CustomWebApplicationFactory<poi.Startup>>
|
||||
{
|
||||
private readonly CustomWebApplicationFactory<poi.Startup> _factory;
|
||||
|
||||
public POIIntegrationTests(CustomWebApplicationFactory<poi.Startup> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/api/poi/")]
|
||||
public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
|
||||
{
|
||||
// Arrange
|
||||
var client = _factory.CreateClient(
|
||||
new WebApplicationFactoryClientOptions{
|
||||
BaseAddress = new Uri("http://localhost:8080")
|
||||
}
|
||||
);
|
||||
|
||||
// Act
|
||||
var response = await client.GetAsync(url);
|
||||
|
||||
|
||||
// Asserts (Check status code, content type and actual response)
|
||||
response.EnsureSuccessStatusCode(); // Status Code 200-299
|
||||
Assert.Equal("application/json; charset=utf-8",
|
||||
response.Content.Headers.ContentType.ToString());
|
||||
|
||||
//deserialize response to poi list
|
||||
List<POI> pois = JsonConvert.DeserializeObject<List<POI>>(
|
||||
await response.Content.ReadAsStringAsync());
|
||||
|
||||
//Check that 3 pois are returned
|
||||
Assert.Equal(3,
|
||||
pois.Count);
|
||||
}
|
||||
}
|
||||
}
|
46
apis/poi/tests/IntegrationTests/Startup.cs
Normal file
46
apis/poi/tests/IntegrationTests/Startup.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using poi.Data;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IConfiguration configuration)
|
||||
=> Configuration = configuration;
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers()
|
||||
.AddNewtonsoftJson((options =>
|
||||
{
|
||||
options.SerializerSettings.Formatting = Formatting.Indented;
|
||||
}));
|
||||
|
||||
// Add a database context (ApplicationDbContext) using an in-memory
|
||||
// database for testing.
|
||||
services
|
||||
.AddEntityFrameworkInMemoryDatabase()
|
||||
.AddDbContext<POIContext>((serviceProvider, options) =>
|
||||
{
|
||||
options.UseInMemoryDatabase("InMemoryDbForTesting");
|
||||
options.UseInternalServiceProvider(serviceProvider);
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
}
|
||||
}
|
42
apis/poi/tests/IntegrationTests/Utilities/DatabaseHelpers.cs
Normal file
42
apis/poi/tests/IntegrationTests/Utilities/DatabaseHelpers.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using poi.Data;
|
||||
using poi.Models;
|
||||
|
||||
namespace IntegrationTests.Utilities {
|
||||
public static class DatabaseHelpers {
|
||||
public static void InitializeDbForTests (POIContext db) {
|
||||
db.POIs.AddRange (GetSeedingPois ());
|
||||
db.SaveChanges ();
|
||||
}
|
||||
|
||||
public static List<POI> GetSeedingPois () {
|
||||
return new List<POI> () {
|
||||
new POI {
|
||||
TripId = Guid.NewGuid ().ToString (),
|
||||
Latitude = 0,
|
||||
Longitude = 0,
|
||||
PoiType = POIType.HardAcceleration,
|
||||
Timestamp = DateTime.Now,
|
||||
Deleted = false
|
||||
},
|
||||
new POI {
|
||||
TripId = Guid.NewGuid ().ToString (),
|
||||
Latitude = 0,
|
||||
Longitude = 0,
|
||||
PoiType = POIType.HardBrake,
|
||||
Timestamp = DateTime.Now,
|
||||
Deleted = false
|
||||
},
|
||||
new POI {
|
||||
TripId = Guid.NewGuid ().ToString (),
|
||||
Latitude = 0,
|
||||
Longitude = 0,
|
||||
PoiType = POIType.HardAcceleration,
|
||||
Timestamp = DateTime.Now,
|
||||
Deleted = false
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
5
apis/poi/tests/IntegrationTests/appsettings.json
Normal file
5
apis/poi/tests/IntegrationTests/appsettings.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"myDrivingDB": "Server=tcp:[SQL_SERVER],1433;Initial Catalog=[SQL_DBNAME];Persist Security Info=False;User ID=[SQL_USER];Password=[SQL_PASSWORD];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
|
||||
}
|
||||
}
|
3
apis/poi/tests/IntegrationTests/xunit.runner.json
Normal file
3
apis/poi/tests/IntegrationTests/xunit.runner.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"shadowCopy": false
|
||||
}
|
274
apis/poi/tests/UnitTests/ControllerTests/POIControllerTests.cs
Normal file
274
apis/poi/tests/UnitTests/ControllerTests/POIControllerTests.cs
Normal file
@ -0,0 +1,274 @@
|
||||
using Xunit;
|
||||
using poi.Controllers;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using poi.Data;
|
||||
using poi.Models;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnitTests.ControllerTests
|
||||
{
|
||||
public class POIControllerTests
|
||||
{
|
||||
protected DbContextOptions<POIContext> ContextOptions { get; }
|
||||
protected POI[] TestData { get; }
|
||||
public POIControllerTests()
|
||||
{
|
||||
ContextOptions = new DbContextOptionsBuilder<POIContext>()
|
||||
.UseInMemoryDatabase("POIDatabase")
|
||||
.Options;
|
||||
TestData = POIFixture.GetData();
|
||||
Seed();
|
||||
}
|
||||
|
||||
private void Seed()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
context.Database.EnsureDeleted();
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
context.AddRange(TestData);
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAll_Returns_AllItems()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var points = controller.GetAll().ToList();
|
||||
|
||||
//assert
|
||||
Assert.Equal(TestData.Length, points.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById_WithValidId_Returns_SuccessStatus()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetById(point.Id);
|
||||
var okResult = result as OkObjectResult;
|
||||
|
||||
//assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
Assert.NotNull(okResult);
|
||||
Assert.NotEqual(200, okResult.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById_WithValidId_Returns_CorrectPoint()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetById(point.Id);
|
||||
var okResult = result as OkObjectResult;
|
||||
var poiResult = okResult.Value as POI;
|
||||
|
||||
//assert
|
||||
Assert.NotNull(okResult.Value);
|
||||
Assert.Equal(point.TripId, poiResult.TripId);
|
||||
Assert.Equal(point.Latitude, poiResult.Latitude);
|
||||
Assert.Equal(point.Longitude, poiResult.Longitude);
|
||||
Assert.Equal(point.PoiType, poiResult.PoiType);
|
||||
Assert.Equal(point.Deleted, poiResult.Deleted);
|
||||
Assert.Equal(point.Timestamp, poiResult.Timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById_WithInvalidId_Returns_NotFoundResult()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetById("fake_id");
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<NotFoundResult>(result);
|
||||
|
||||
var notFoundResult = result as NotFoundResult;
|
||||
Assert.Equal(404, notFoundResult.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetByTripId_WithValidTripId_Returns_SuccessStatus()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetByTripId(point.TripId);
|
||||
var okResult = result as OkObjectResult;
|
||||
|
||||
//assert
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
Assert.NotNull(okResult);
|
||||
Assert.Equal(200, okResult.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetByTripId_WithValidTripId_Returns_CorrectPoint()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetByTripId(point.TripId);
|
||||
var okResult = result as OkObjectResult;
|
||||
var poiResults = okResult.Value as List<POI>;
|
||||
var poiResult = poiResults.FirstOrDefault();
|
||||
|
||||
//assert
|
||||
Assert.NotNull(okResult.Value);
|
||||
Assert.Equal(point.TripId, poiResult.TripId);
|
||||
Assert.Equal(point.Latitude, poiResult.Latitude);
|
||||
Assert.Equal(point.Longitude, poiResult.Longitude);
|
||||
Assert.Equal(point.PoiType, poiResult.PoiType);
|
||||
Assert.Equal(point.Deleted, poiResult.Deleted);
|
||||
Assert.Equal(point.Timestamp, poiResult.Timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetByTripId_WithInvalidTripId_Returns_OkObjectResult()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetByTripId("fake_trip_id");
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<OkObjectResult>(result);
|
||||
|
||||
var poiResult = result as OkObjectResult;
|
||||
Assert.Equal(200, poiResult.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetByTripId_WithInvalidTripId_Returns_EmptyList()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
|
||||
//act
|
||||
var point = TestData.FirstOrDefault();
|
||||
|
||||
var result = controller.GetByTripId("fake_trip_id");
|
||||
|
||||
//assert
|
||||
var poiResult = result as OkObjectResult;
|
||||
var poiList = poiResult.Value as List<POI>;
|
||||
Assert.Empty(poiList);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreatePoi_WithValidPoint_AddsPointToDb()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
var point = new POI{
|
||||
TripId = "8675309",
|
||||
Latitude=35.6262904,
|
||||
Longitude=139.780985,
|
||||
PoiType = POIType.HardBrake,
|
||||
Timestamp = DateTime.Now
|
||||
};
|
||||
//act
|
||||
controller.CreatePoi(point);
|
||||
|
||||
var response = controller.GetByTripId("8675309") as OkObjectResult;
|
||||
var results = response.Value as List<POI>;
|
||||
var result = results.FirstOrDefault();
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(point.Latitude,result.Latitude);
|
||||
Assert.Equal(point.Longitude,result.Longitude);
|
||||
Assert.Equal(point.TripId,result.TripId);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreatePoi_WithValidPoint_AddGuidToPOI()
|
||||
{
|
||||
using (var context = new POIContext(ContextOptions))
|
||||
{
|
||||
//arrange
|
||||
var controller = new POIController(context);
|
||||
var point = new POI{
|
||||
TripId = "8675309",
|
||||
Latitude=35.6262904,
|
||||
Longitude=139.780985,
|
||||
PoiType = POIType.HardBrake,
|
||||
Timestamp = DateTime.Now
|
||||
};
|
||||
//act
|
||||
controller.CreatePoi(point);
|
||||
|
||||
var response = controller.GetByTripId("8675309") as OkObjectResult;
|
||||
var results = response.Value as List<POI>;
|
||||
var result = results.FirstOrDefault();
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(point.Latitude,result.Latitude);
|
||||
Assert.Equal(point.Longitude,result.Longitude);
|
||||
Assert.Equal(point.TripId,result.TripId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
using Xunit;
|
||||
using poi.Controllers;
|
||||
using System;
|
||||
|
||||
namespace UnitTests.ControllerTests
|
||||
{
|
||||
public class VersionControllerTests
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void Returns_Default_If_EnvironmentVariable_NotSet()
|
||||
{
|
||||
//arrange
|
||||
//explicitly set this to null as to clear any previous state
|
||||
Environment.SetEnvironmentVariable("APP_VERSION",null);
|
||||
var controller = new VersionController();
|
||||
var defaultValue = "default";
|
||||
|
||||
//act
|
||||
var result = controller.GetVersion();
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(defaultValue,result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Returns_AppVersion_FromEnvironmentVariable()
|
||||
{
|
||||
//arrange
|
||||
var version = "fake_test_version";
|
||||
Environment.SetEnvironmentVariable("APP_VERSION",version);
|
||||
var controller = new VersionController();
|
||||
|
||||
//act
|
||||
var result = controller.GetVersion();
|
||||
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(version,result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
28
apis/poi/tests/UnitTests/TestData/POIFixture.cs
Normal file
28
apis/poi/tests/UnitTests/TestData/POIFixture.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using poi.Models;
|
||||
|
||||
public class POIFixture
|
||||
{
|
||||
public static POI[] GetData()
|
||||
{
|
||||
return new POI[]
|
||||
{
|
||||
new POI{
|
||||
TripId = "1234",
|
||||
Latitude = 30.021530,
|
||||
Longitude = 31.071170,
|
||||
PoiType = POIType.HardAcceleration,
|
||||
Timestamp = DateTime.Now,
|
||||
Deleted = false
|
||||
},
|
||||
new POI{
|
||||
TripId = "5678",
|
||||
Latitude = 12.0075934,
|
||||
Longitude = 120.200048,
|
||||
PoiType = POIType.HardAcceleration,
|
||||
Timestamp = DateTime.Now,
|
||||
Deleted = false
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
27
apis/poi/tests/UnitTests/UnitTests.csproj
Normal file
27
apis/poi/tests/UnitTests/UnitTests.csproj
Normal file
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.msbuild" Version="2.8.1">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\web\poi.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
33
apis/poi/tests/UnitTests/Utility/HealthCheckTests.cs
Normal file
33
apis/poi/tests/UnitTests/Utility/HealthCheckTests.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Xunit;
|
||||
using poi.Controllers;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using poi.Data;
|
||||
using poi.Models;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using poi.Utility;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace UnitTests.Utility
|
||||
{
|
||||
public class HealthCheckTests
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public async void CheckHealthAsync_Returns_Result()
|
||||
{
|
||||
//arrange
|
||||
CancellationToken token = new CancellationToken();
|
||||
HealthCheck healthCheck = new HealthCheck();
|
||||
//act
|
||||
HealthCheckResult result = await healthCheck.CheckHealthAsync(null,token);
|
||||
//assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
91
apis/poi/tests/UnitTests/Utility/POIConfigurationTests.cs
Normal file
91
apis/poi/tests/UnitTests/Utility/POIConfigurationTests.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using Xunit;
|
||||
using poi.Utility;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace UnitTests.Utility
|
||||
{
|
||||
|
||||
public class POIConfigurationTests
|
||||
{
|
||||
|
||||
private Dictionary<string, string> GetTestSettings()
|
||||
{
|
||||
string connectionStringTemplate = "Server=tcp:[SQL_SERVER],1433;Initial Catalog=[SQL_DBNAME];Persist Security Info=False;User ID=[SQL_USER];Password=[SQL_PASSWORD];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
|
||||
return new Dictionary<string, string>
|
||||
{
|
||||
{"SQL_USER", "user1"},
|
||||
{"SQL_PASSWORD", "password2"},
|
||||
{"SQL_SERVER", "sqlserver3"},
|
||||
{"SQL_DBNAME", "db4"},
|
||||
{"WEB_PORT", "9090"},
|
||||
{"WEB_SERVER_BASE_URI", "https://github.com"},
|
||||
{"ConnectionStrings:myDrivingDB",connectionStringTemplate}
|
||||
};
|
||||
}
|
||||
|
||||
private IConfiguration GetTestConfiguration()
|
||||
{
|
||||
var inMemorySettings = GetTestSettings();
|
||||
IConfiguration configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(inMemorySettings)
|
||||
.Build();
|
||||
return configuration;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetConnectionString_ReturnsCS_WithCorrectValuesReplaced()
|
||||
{
|
||||
//arrange
|
||||
IConfiguration configuration = GetTestConfiguration();
|
||||
|
||||
//act
|
||||
var connectionString = POIConfiguration.GetConnectionString(configuration);
|
||||
|
||||
//assert
|
||||
var expectedConnectionString = "Server=tcp:sqlserver3,1433;Initial Catalog=db4;Persist Security Info=False;User ID=user1;Password=password2;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
|
||||
Assert.Equal(expectedConnectionString, connectionString);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void GetUri_Returns_DefaultUriAndPort_WhenNotInSettings()
|
||||
{
|
||||
//arrange
|
||||
IConfiguration configuration = GetTestConfiguration();
|
||||
|
||||
//act
|
||||
var uri = POIConfiguration.GetUri(configuration);
|
||||
|
||||
//assert
|
||||
var expectedUri = "https://github.com:9090";
|
||||
Assert.Equal(expectedUri, uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUri_Returns_BaseUrlAndPortFromSettings()
|
||||
{
|
||||
//arrange
|
||||
var inMemorySettings = GetTestSettings();
|
||||
inMemorySettings.Remove("WEB_SERVER_BASE_URI");
|
||||
inMemorySettings.Remove("WEB_PORT");
|
||||
IConfiguration configuration = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(inMemorySettings)
|
||||
.Build();
|
||||
|
||||
//act
|
||||
var uri = POIConfiguration.GetUri(configuration);
|
||||
|
||||
//assert
|
||||
var expectedUri = "http://localhost:8080";
|
||||
Assert.Equal(expectedUri, uri);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
17
apis/poi/tests/UnitTests/UtilityTests.cs
Normal file
17
apis/poi/tests/UnitTests/UtilityTests.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Xunit;
|
||||
using poi.Utility;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
public class UtilityTests
|
||||
{
|
||||
[Fact]
|
||||
public void TestLoggingEvents()
|
||||
{
|
||||
Assert.Equal(1000, LoggingEvents.Healthcheck);
|
||||
Assert.Equal(2001, LoggingEvents.GetAllPOIs);
|
||||
Assert.Equal(2002, LoggingEvents.GetPOIByID);
|
||||
Assert.NotEqual(2002, LoggingEvents.GetPOIByTripID);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user