folder rename

This commit is contained in:
2017-02-02 01:55:21 +02:00
parent e10e704724
commit 8d240ae27d
11 changed files with 0 additions and 0 deletions
@@ -0,0 +1,173 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using LanBackup.DataCore;
using LanBackup.ModelsCore;
using LanBackup.WebApp.Controllers;
using Microsoft.AspNetCore.Mvc;
using AutoMapper;
using LanBackup.WebApp.Models.Telemetry;
using LanBackup.WebApp.Models.DTO;
namespace LanBackup.WebApp.Tests.IntegrationTests
{
public class ApiLogsControllerTests : IClassFixture<TestFixture<LanBackup.WebApp.Startup>>
{
private readonly HttpClient _client;
private ITestOutputHelper _output;
private ITelemetryLogger _tele;
public ApiLogsControllerTests(TestFixture<LanBackup.WebApp.Startup> fixture, ITestOutputHelper output)
{
_output = output;
_client = fixture.Client;
Mapper.Initialize(cfg => cfg.AddProfiles(typeof(LanBackup.WebApp.Startup)));
_tele = new MockTelemetry();
}
[Fact]
public async Task GetAllLogRecords()
{
// Arrange
// Act
var response = await _client.GetAsync("/api/logs");
// Assert
response.EnsureSuccessStatusCode();
var jsonStr = await response.Content.ReadAsStringAsync();
var logList = JsonConvert.DeserializeObject<List<LanBackup.ModelsCore.BackupLog>>(jsonStr);
Assert.NotEmpty(logList);
}
#region SqliteInMemory DB tests
[Fact]
public async void WithSqliteInmemoryTestMethod()
{
using (var mctx = new GetSqliteMemoryContext().GetContext())
{
var service = new LogsController(Mapper.Instance, mctx, _tele);
var res = Assert.IsType<OkObjectResult>(await service.Get(null, null));
var val1 = Assert.IsType<List<BackupLogDTO>>(res.Value);
Assert.Equal(5, val1.Count());
}
}
[Fact]
public async void GetByIdWithSqliteInmemoryTestMethod()
{
using (var mctx = new GetSqliteMemoryContext().GetContext())
{
var recId = 1;
var service = new LogsController(Mapper.Instance, mctx, _tele);
var res = Assert.IsType<OkObjectResult>(service.Get(recId));
var val1 = Assert.IsType<BackupLogDTO>(res.Value);
Assert.NotNull(val1);
var res2 = Assert.IsType<NotFoundResult>(service.Get(10000));
}
}
[Fact]
public async void GetByClientIdWithSqliteInmemoryTestMethod()
{
using (var mctx = new GetSqliteMemoryContext().GetContext())
{
var clientId = "192.168.1.100";
var service = new LogsController(Mapper.Instance, mctx, _tele);
var res = Assert.IsType<OkObjectResult>(service.GetByCientID(clientId));
var val1 = Assert.IsType<List<BackupLogDTO>>(res.Value);
Assert.Equal(2, val1.Count());
}
}
private class GetSqliteMemoryContext : IDisposable
{
private SqliteConnection connection;
private BackupsContext _context;
private DbContextOptions<BackupsContext> options;
public GetSqliteMemoryContext()
{
connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
options = new DbContextOptionsBuilder<BackupsContext>()
.UseSqlite(connection)
.Options;
// Create the schema in the database
using (var context = new BackupsContext(options))
{
context.Database.EnsureCreated();
}
// Insert seed data into the database using one instance of the context
using (var context = new BackupsContext(options))
{
int id = 1;
context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.Add(new BackupLog { ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ });
context.SaveChanges();
}
_context = new BackupsContext(options);
}
public void Dispose()
{
_context.Dispose();
connection.Close();
connection.Dispose();
}
internal BackupsContext GetContext()
{
return _context;
}
}
#endregion
}
}
@@ -0,0 +1,16 @@
using System.Net.Http;
using Xunit;
namespace LanBackup.WebApp.Tests.IntegrationTests
{
public class HomeControllerTests : IClassFixture<TestFixture<LanBackup.WebApp.Startup>>
{
private readonly HttpClient _client;
public HomeControllerTests(TestFixture<LanBackup.WebApp.Startup> fixture)
{
_client = fixture.Client;
}
}
}
@@ -0,0 +1,68 @@
using LanBackup.DataCore;
using LanBackup.ModelsCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using Xunit;
namespace LanBackup.WebApp.Tests.IntegrationTests
{
public class MssqlTests : IDisposable
{
BackupsContext _context;
public MssqlTests()
{
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.BuildServiceProvider();
var builder = new DbContextOptionsBuilder<BackupsContext>();
builder.UseSqlServer($"Server=(localdb)\\mssqllocaldb;Database=lanbackup_db_{Guid.NewGuid()};Trusted_Connection=True;MultipleActiveResultSets=true")
.UseInternalServiceProvider(serviceProvider);
_context = new BackupsContext(builder.Options);
_context.Database.EnsureCreated();
//_context.Database.Migrate();
}
public void Dispose()
{
_context.Database.EnsureDeleted();
}
#region MSSQL server integration tests
[Trait("Category", "DbIntegration")]
[Fact]
public async void MssqlTestMethod()
{
//Add some monsters before querying
_context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
_context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
_context.Add(new BackupLog { ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
_context.Add(new BackupLog { ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
_context.Add(new BackupLog { ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK"});
_context.SaveChanges();
//Execute the query
var res = _context.Logs.FromSql("SELECT ID, Description, ClientIP, ConfigurationID, LogError, Status, DateTime, RowVersion FROM Logs").ToList();
//Verify the results
Assert.Equal(5, res.Count());
}
#endregion
}
}
@@ -0,0 +1,101 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using System;
using System.IO;
using System.Net.Http;
using Microsoft.Extensions.PlatformAbstractions;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ViewComponents;
namespace LanBackup.WebApp.Tests
{
public class TestFixture<TStartup> : IDisposable
{
private const string SolutionName = "LanBackup.sln";
private readonly TestServer _server;
public HttpClient Client;
public TestFixture() : this(Path.Combine("src"))
{
}
protected TestFixture(string solutionRelativeTargetProjectParentDir)
{
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var contentRoot = GetProjectPath(solutionRelativeTargetProjectParentDir, startupAssembly);
var builder = new WebHostBuilder()
.UseContentRoot(contentRoot)
.ConfigureServices(InitializeServices)
.UseEnvironment("Development")
.UseStartup(typeof(TStartup));
_server = new TestServer(builder);
Client = _server.CreateClient();
}
public void Dispose()
{
Client.Dispose();
_server.Dispose();
}
protected virtual void InitializeServices(IServiceCollection services)
{
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
// Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd().
var manager = new ApplicationPartManager();
manager.ApplicationParts.Add(new AssemblyPart(startupAssembly));
manager.FeatureProviders.Add(new ControllerFeatureProvider());
manager.FeatureProviders.Add(new ViewComponentFeatureProvider());
services.AddSingleton(manager);
}
/// <summary>
/// Gets the full path to the target project path that we wish to test
/// </summary>
/// <param name="solutionRelativePath">
/// The parent directory of the target project.
/// e.g. src, samples, test, or test/Websites
/// </param>
/// <param name="startupAssembly">The target project's assembly.</param>
/// <returns>The full path to the target project.</returns>
private static string GetProjectPath(string solutionRelativePath, Assembly startupAssembly)
{
// Get name of the target project which we want to test
var projectName = startupAssembly.GetName().Name;
// Get currently executing test project path
var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath;
// Find the folder which contains the solution file. We then use this information to find the target
// project which we want to test.
var directoryInfo = new DirectoryInfo(applicationBasePath);
do
{
var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName));
if (solutionFileInfo.Exists)
{
return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName));
}
directoryInfo = directoryInfo.Parent;
}
while (directoryInfo.Parent != null);
throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
}
}
}
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.25420" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.25420</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>f124a292-3711-4fd1-9c54-63d5cf613d37</ProjectGuid>
<RootNamespace>LanBackup.WebAppSpa.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
@@ -0,0 +1,18 @@
using System;
using LanBackup.WebApp.Models.Telemetry;
namespace LanBackup.WebApp.Tests.IntegrationTests
{
internal class MockTelemetry : ITelemetryLogger
{
public void TrackEvent(string eventmsg)
{
//DO NOTHING
}
public void TrackException(Exception ex)
{
//DO NOTHING
}
}
}
@@ -0,0 +1,14 @@
using System;
using Xunit.Abstractions;
namespace LanBackup.WebApp.Tests
{
public static class TestOutputHelperExtender
{
public static void Log(this ITestOutputHelper output, string msg)
{
output.WriteLine(msg);
Console.WriteLine(msg);
}
}
}
@@ -0,0 +1,14 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-LanBackup.WebApp-d5f6c3b5-64d7-4d41-8000-1e69f75536f7;Trusted_Connection=True;MultipleActiveResultSets=true",
"BackupsConnectionString": "Server=(localdb)\\mssqllocaldb;Database=BackupsLAN;Trusted_Connection=True;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
+41
View File
@@ -0,0 +1,41 @@
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"copyToOutput": {
"includeFiles": [
"appsettings.json",
"xunit.runner.json"
]
}
},
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.3.0",
"xunit": "2.2.0-beta4-build3444",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.DotNet.InternalAbstractions": "1.0.1-beta-003206",
"Microsoft.AspNetCore.TestHost": "1.1.0",
"Microsoft.AspNetCore.Http.Abstractions": "1.1.0",
"Microsoft.Data.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",
"FluentAssertions": "4.18.0",
"LanBackup.WebApp": "1.0.0-*",
"Moq": "4.6.38-alpha",
"System.Diagnostics.TraceSource": "4.0.0"
},
"testRunner": "xunit",
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
}
}
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<assemblies>
<assembly name="LanBackup.WebApp.Tests.dll" environment="64-bit .NET (unknown version) [collection-per-class, parallel (8 threads)]" test-framework="xUnit.net 2.2.0.3444" run-date="2017-01-10" run-time="21:35:28" total="1" passed="1" failed="0" skipped="0" time="3.650" errors="0">
<errors />
<collection total="1" passed="1" failed="0" skipped="0" name="Test collection for LanBackup.WebApp.Tests.IntegrationTests.ApiLogsControllerTests" time="0.580">
<test name="LanBackup.WebApp.Tests.IntegrationTests.ApiLogsControllerTests.GetAllLogRecords" type="LanBackup.WebApp.Tests.IntegrationTests.ApiLogsControllerTests" method="GetAllLogRecords" time="0.5799872" result="Pass">
<output><![CDATA[jsonStr: [{"id":1,"rowVersion":"AAAAAAABQFE=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 04:58:55","error":null,"status":"OK"},{"id":2,"rowVersion":"AAAAAAABQFI=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 04:59:10","error":null,"status":"OK"},{"id":3,"rowVersion":"AAAAAAABQFM=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 04:59:25","error":null,"status":"OK"},{"id":4,"rowVersion":"AAAAAAABQFQ=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 04:59:40","error":null,"status":"OK"},{"id":5,"rowVersion":"AAAAAAABQFU=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 04:59:55","error":null,"status":"OK"},{"id":6,"rowVersion":"AAAAAAABQFY=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 05:00:10","error":null,"status":"OK"},{"id":7,"rowVersion":"AAAAAAABQFc=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 05:01:49","error":null,"status":"OK"},{"id":8,"rowVersion":"AAAAAAABQFg=","clientIP":"169.254.80.80","configurationID":null,"description":"Time on the agent is 10/01/2017 05:02:04","error":null,"status":"OK"}]
]]></output>
</test>
</collection>
</assembly>
</assemblies>
@@ -0,0 +1,3 @@
{
"diagnosticMessages": true
}