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
|
||||
}
|
Reference in New Issue
Block a user