Add doctor test

This commit is contained in:
Martijn Scheepers
2020-08-24 14:33:11 +02:00
parent 3f324b4771
commit 1c17893e79
5 changed files with 269 additions and 20 deletions

View File

@@ -0,0 +1,229 @@
using Arrestanten_planbord.Data;
using Arrestanten_planbord.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Arrestanten_planbord.UnitTest
{
[TestClass]
public class DoctorServiceTest
{
private static Mock<ConfigFileHelper> _configFileHelperMoq;
private static string TestFolder = "DoctorServiceTest";
[ClassInitialize]
public static void MyClassInitialize(TestContext testContext)
{
Mock<ILogger<ConfigFileHelper>> configLoggerMoq = new Mock<ILogger<ConfigFileHelper>>();
IConfiguration testAppsettings = new ConfigurationBuilder().AddJsonFile("appsettings.test.json").Build();
_configFileHelperMoq = new Mock<ConfigFileHelper>(configLoggerMoq.Object, testAppsettings);
string directory = testAppsettings.GetValue<string>("DatastoreLocation") + "\\" + TestFolder;
if (Directory.Exists(directory))
{
Directory.Delete(directory, true);
}
}
[ClassCleanup]
public static void MyClassClassCleanup()
{
}
[TestMethod]
public void StoreDoctordata_DoctorData_Null_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
var called = false;
doctorService.DoctorChangeEvent += () => { called = true; };
var result = doctorService.StoreDoctorData(null, TestFolder, "StoreDoctordata_DoctorData_Null_Test");
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(DoctorService.StoreDoctorData) doctorData == null", result.Message);
doctorLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(DoctorService.StoreDoctorData) doctorData == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreDoctordata_Location_Null_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
var called = false;
doctorService.DoctorChangeEvent += () => { called = true; };
DoctorData doctorData = new DoctorData();
doctorData.Lines.Add("Doctor data line 1");
doctorData.Lines.Add("Doctor data line 2");
var result = doctorService.StoreDoctorData(doctorData, null, "StoreDoctordata_Location_Null_Test");
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(DoctorService.StoreDoctorData) location == null", result.Message);
doctorLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(DoctorService.StoreDoctorData) location == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreDoctordata_Filename_Null_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
var called = false;
doctorService.DoctorChangeEvent += () => { called = true; };
DoctorData doctorData = new DoctorData();
doctorData.Lines.Add("Doctor data line 1");
doctorData.Lines.Add("Doctor data line 2");
var result = doctorService.StoreDoctorData(doctorData, TestFolder, null);
Assert.IsFalse(called);
Assert.IsFalse(result.Succeeded);
Assert.AreEqual("(DoctorService.StoreDoctorData) filename == null", result.Message);
doctorLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(DoctorService.StoreDoctorData) filename == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void GetDoctordata_Location_Null_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
var called = false;
doctorService.DoctorChangeEvent += () => { called = true; };
DoctorData doctorData = doctorService.GetDoctorData(2, null, "GetDoctordata_Location_Null_Test");
Assert.IsFalse(called);
Assert.IsNull(doctorData);
doctorLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(DoctorService.GetDoctorData) location == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void GetDoctordata_Filename_Null_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
var called = false;
doctorService.DoctorChangeEvent += () => { called = true; };
DoctorData doctorData = doctorService.GetDoctorData(2, TestFolder, null);
Assert.IsFalse(called);
Assert.IsNull(doctorData);
doctorLoggerMoq.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => string.Equals("(DoctorService.GetDoctorData) filename == null", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Once);
}
[TestMethod]
public void StoreDoctordata_DoctorData_Parrallel_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
//write read parrallel
Parallel.For(0, 100, i =>
{
DoctorData doctorData = new DoctorData();
doctorData.Lines.Add("Doctor data line 1");
doctorData.Lines.Add("Doctor data line 2");
doctorData.Lines.Add("Doctor data line 3");
doctorData.Lines.Add("Doctor data line 4");
doctorData.Lines.Add("Doctor data line 5");
var result = doctorService.StoreDoctorData(doctorData, TestFolder, $"StoreDoctordata_DoctorData_Parrallel_Test-{i}");
Assert.IsTrue(result.Succeeded);
});
//read parrallel all files
Parallel.For(0, 100, i =>
{
var data = doctorService.GetDoctorData(3, TestFolder, $"StoreDoctordata_DoctorData_Parrallel_Test-{i}");
Assert.AreEqual("Doctor data line 1", data.Lines[0]);
Assert.AreEqual("Doctor data line 2", data.Lines[1]);
Assert.AreEqual("Doctor data line 3", data.Lines[2]);
Assert.AreEqual("Doctor data line 4", data.Lines[3]);
Assert.AreEqual("Doctor data line 5", data.Lines[4]);
});
}
[TestMethod]
public void StoreDoctordata_DoctorData_Single_Parrallel_Test()
{
Mock<ILogger<DoctorService>> doctorLoggerMoq = new Mock<ILogger<DoctorService>>();
var doctorService = new DoctorService(_configFileHelperMoq.Object, doctorLoggerMoq.Object);
//write read file
DoctorData doctorData = new DoctorData();
doctorData.Lines.Add("Doctor data line 1");
doctorData.Lines.Add("Doctor data line 2");
doctorData.Lines.Add("Doctor data line 3");
doctorData.Lines.Add("Doctor data line 4");
doctorData.Lines.Add("Doctor data line 5");
var result = doctorService.StoreDoctorData(doctorData, TestFolder, "StoreDoctordata_DoctorData_Single_Parrallel_Test");
Assert.IsTrue(result.Succeeded);
//read parrallel one file
Parallel.For(0, 1000, i =>
{
var data = doctorService.GetDoctorData(3, TestFolder, "StoreDoctordata_DoctorData_Single_Parrallel_Test");
Assert.AreEqual("Doctor data line 1", data.Lines[0]);
Assert.AreEqual("Doctor data line 2", data.Lines[1]);
Assert.AreEqual("Doctor data line 3", data.Lines[2]);
Assert.AreEqual("Doctor data line 4", data.Lines[3]);
Assert.AreEqual("Doctor data line 5", data.Lines[4]);
});
}
}
}

View File

@@ -184,7 +184,7 @@ namespace Arrestanten_planbord.UnitTest
}
[TestMethod]
public void StoreCelldata_GetCellData_Parrallel_Test()
public void StoreTransportdata_TransportData_Parrallel_Test()
{
Mock<ILogger<TransportService>> transportLoggerMoq = new Mock<ILogger<TransportService>>();
var transportService = new TransportService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
@@ -197,14 +197,14 @@ namespace Arrestanten_planbord.UnitTest
transData.Transports.Add(new TransportLineData { Id = 2, Arrestant = "Bus", Bestemming = "Bus", Datum = DateTime.Now, TransportType = TransportType.Bus });
transData.Transports.Add(new TransportLineData { Id = 3, Arrestant = "Car", Bestemming = "Car", Datum = DateTime.Now, TransportType = TransportType.Car });
var result = transportService.StoreTransportdata(transData, TestFolder, $"StoreCelldata_GetCellData_Parrallel_Test-{i}");
var result = transportService.StoreTransportdata(transData, TestFolder, $"StoreTransportdata_TransportData_Parrallel_Test-{i}");
Assert.IsTrue(result.Succeeded);
});
//read parrallel all files
Parallel.For(0, 100, i =>
{
var data = transportService.GetTransportData(3, TestFolder, $"StoreCelldata_GetCellData_Parrallel_Test-{i}");
var data = transportService.GetTransportData(3, TestFolder, $"StoreTransportdata_TransportData_Parrallel_Test-{i}");
Assert.AreEqual(1, data.Transports[0].Id);
Assert.AreEqual("Onbekend", data.Transports[0].Arrestant);
@@ -224,7 +224,7 @@ namespace Arrestanten_planbord.UnitTest
}
[TestMethod]
public void StoreCelldata_GetCellData_Single_Parrallel_Test()
public void StoreTransportdata_TransportData_Single_Parrallel_Test()
{
Mock<ILogger<TransportService>> transportLoggerMoq = new Mock<ILogger<TransportService>>();
var transportService = new TransportService(_configFileHelperMoq.Object, transportLoggerMoq.Object);
@@ -235,14 +235,14 @@ namespace Arrestanten_planbord.UnitTest
transData.Transports.Add(new TransportLineData { Id = 2, Arrestant = "Bus", Bestemming = "Bus", Datum = DateTime.Now, TransportType = TransportType.Bus });
transData.Transports.Add(new TransportLineData { Id = 3, Arrestant = "Car", Bestemming = "Car", Datum = DateTime.Now, TransportType = TransportType.Car });
var result = transportService.StoreTransportdata(transData, TestFolder, "StoreCelldata_GetCellData_Single_Parrallel_Test");
var result = transportService.StoreTransportdata(transData, TestFolder, "StoreTransportdata_TransportData_Single_Parrallel_Test");
Assert.IsTrue(result.Succeeded);
//read parrallel one file
Parallel.For(0, 1000, i =>
{
var data = transportService.GetTransportData(3, TestFolder, "StoreCelldata_GetCellData_Single_Parrallel_Test");
var data = transportService.GetTransportData(3, TestFolder, "StoreTransportdata_TransportData_Single_Parrallel_Test");
Assert.AreEqual(1, data.Transports[0].Id);
Assert.AreEqual("Onbekend", data.Transports[0].Arrestant);

View File

@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Arrestanten_planbord.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -9,11 +10,6 @@ using System.Threading.Tasks;
namespace Arrestanten_planbord.Data
{
public class DoctorData
{
public List<String> Lines { get; set; } = new List<string>();
}
public class DoctorService
{
public event Action DoctorChangeEvent;
@@ -28,6 +24,17 @@ namespace Arrestanten_planbord.Data
public DoctorData GetDoctorData(int lines, string location, string filename)
{
if (location == null)
{
_logger.LogError("(DoctorService.GetDoctorData) location == null");
return null;
}
if (filename == null)
{
_logger.LogError("(DoctorService.GetDoctorData) filename == null");
return null;
}
string filePath = _configFileHelper.GetFilePath(filename, location);
if (File.Exists(filePath) == false)
@@ -42,28 +49,28 @@ namespace Arrestanten_planbord.Data
return _configFileHelper.ReadConfigFile<DoctorData>(filePath) as DoctorData;
}
public void StoreDoctorData(DoctorData doctorData, string location, string filename)
public ResultModel StoreDoctorData(DoctorData doctorData, string location, string filename)
{
if (doctorData == null)
{
_logger.LogError("(DoctorService.StoreDoctorData) doctorData == null");
return;
return new ResultModel { Succeeded = false, Message = "(DoctorService.StoreDoctorData) doctorData == null" };
}
if (location == null)
{
_logger.LogError("location == null");
return;
_logger.LogError("(DoctorService.StoreDoctorData) location == null");
return new ResultModel { Succeeded = false, Message = "(DoctorService.StoreDoctorData) location == null" };
}
if (filename == null)
{
_logger.LogError("filename == null");
return;
_logger.LogError("(DoctorService.StoreDoctorData) filename == null");
return new ResultModel { Succeeded = false, Message = "(DoctorService.StoreDoctorData) filename == null" };
}
string filePath = _configFileHelper.GetFilePath(filename, location);
_configFileHelper.WriteConfigFile(doctorData, filePath);
var result = _configFileHelper.WriteConfigFile(doctorData, filePath);
DoctorChangeEvent?.Invoke();
return result;
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Arrestanten_planbord.Models
{
public class DoctorData
{
public List<String> Lines { get; set; } = new List<string>();
}
}

View File

@@ -1,4 +1,5 @@
@using Arrestanten_planbord.Data
@using Arrestanten_planbord.Models
@using System.Diagnostics
@implements IDisposable
@inject DoctorService doctorService