подготовка ко 2 миграции

This commit is contained in:
2025-04-09 14:37:39 +04:00
parent 064a7c88b2
commit c9793000e3
19 changed files with 641 additions and 69 deletions

View File

@@ -2,6 +2,8 @@
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Extensions; using MagicCarpetContracts.Extensions;
using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetContracts.StoragesContracts; using MagicCarpetContracts.StoragesContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
@@ -12,13 +14,15 @@ using System.Threading.Tasks;
namespace MagicCarpetBusinessLogic.Implementations; namespace MagicCarpetBusinessLogic.Implementations;
public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,ISaleStorageContract saleStorageContract, public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageContract,ISaleStorageContract saleStorageContract,
IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, ILogger logger) : ISalaryBusinessLogicContract IPostStorageContract postStorageContract, IEmployeeStorageContract employeeStorageContract, ILogger logger, IConfigurationSalary сonfiguration) : ISalaryBusinessLogicContract
{ {
private readonly ILogger _logger = logger; private readonly ILogger _logger = logger;
private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract; private readonly ISalaryStorageContract _salaryStorageContract = salaryStorageContract;
private readonly ISaleStorageContract _saleStorageContract = saleStorageContract; private readonly ISaleStorageContract _saleStorageContract = saleStorageContract;
private readonly IPostStorageContract _postStorageContract = postStorageContract; private readonly IPostStorageContract _postStorageContract = postStorageContract;
private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract; private readonly IEmployeeStorageContract _employeeStorageContract = employeeStorageContract;
private readonly IConfigurationSalary _salaryConfiguration = сonfiguration;
private readonly Lock _lockObject = new();
public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate) public List<SalaryDataModel> GetAllSalariesByPeriod(DateTime fromDate, DateTime toDate)
{ {
_logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate); _logger.LogInformation("GetAllSalaries params: {fromDate}, {toDate}", fromDate, toDate);
@@ -59,7 +63,13 @@ public class SalaryBusinessLogicContract(ISalaryStorageContract salaryStorageCon
throw new NullListException(); throw new NullListException();
var post = _postStorageContract.GetElementById(employee.PostId) ?? var post = _postStorageContract.GetElementById(employee.PostId) ??
throw new NullListException(); throw new NullListException();
var salary = post.Salary + sales * 0.1; var salary = post.ConfigurationModel switch
{
null => 0,
TravelAgentPostConfiguration cpc => CalculateSalaryForTravelAgent(sales, startDate, finishDate, cpc),
ChiefPostConfiguration spc => CalculateSalaryForChief(startDate, finishDate, spc),
PostConfiguration pc => pc.Rate,
};
_logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary); _logger.LogDebug("The employee {employeeId} was paid a salary of {salary}", employee.Id, salary);
_salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, DateTime.SpecifyKind(finishDate, DateTimeKind.Utc), salary)); _salaryStorageContract.AddElement(new SalaryDataModel(employee.Id, DateTime.SpecifyKind(finishDate, DateTimeKind.Utc), salary));
} }

View File

@@ -16,5 +16,5 @@ public class PostBindingModel
public string? PostType { get; set; } public string? PostType { get; set; }
public double Salary { get; set; } public string? ConfigurationJson { get; set; }
} }

View File

@@ -2,6 +2,10 @@
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Extensions; using MagicCarpetContracts.Extensions;
using MagicCarpetContracts.Infrastructure; using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -10,12 +14,26 @@ using System.Threading.Tasks;
namespace MagicCarpetContracts.DataModels; namespace MagicCarpetContracts.DataModels;
public class PostDataModel(string postId, string postName, PostType postType, double salary) : IValidation public class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
{ {
public string Id { get; private set; } = postId; public string Id { get; private set; } = postId;
public string PostName { get; private set; } = postName; public string PostName { get; private set; } = postName;
public PostType PostType { get; private set; } = postType; public PostType PostType { get; private set; } = postType;
public double Salary { get; private set; } = salary; public PostConfiguration ConfigurationModel { get; private set; } = configuration;
public PostDataModel(string postId, string postName, PostType postType, string configurationJson) : this(postId, postName, postType, (PostConfiguration)null)
{
var obj = JToken.Parse(configurationJson);
if (obj is not null)
{
ConfigurationModel = obj.Value<string>("Type") switch
{
nameof(TravelAgentPostConfiguration) => JsonConvert.DeserializeObject<TravelAgentPostConfiguration>(configurationJson)!,
nameof(ChiefPostConfiguration) => JsonConvert.DeserializeObject<ChiefPostConfiguration>(configurationJson)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
};
}
}
public void Validate() public void Validate()
{ {
@@ -27,7 +45,9 @@ public class PostDataModel(string postId, string postName, PostType postType, do
throw new ValidationException("Field PostName is empty"); throw new ValidationException("Field PostName is empty");
if (PostType == PostType.None) if (PostType == PostType.None)
throw new ValidationException("Field PostType is empty"); throw new ValidationException("Field PostType is empty");
if (Salary <= 0) if (ConfigurationModel is null)
throw new ValidationException("Field Salary is empty"); throw new ValidationException("Field ConfigurationModel is not initialized");
if (ConfigurationModel!.Rate <= 0)
throw new ValidationException("Field Rate is less or equal zero");
} }
} }

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.Infrastructure;
public interface IConfigurationSalary
{
double ExtraMadeSum { get; }
int MaxConcurrentThreads { get; }
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.Infrastructure.PostConfigurations;
public class ChiefPostConfiguration : PostConfiguration
{
public override string Type => nameof(ChiefPostConfiguration);
public double PersonalCountTrendPremium { get; set; }
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.Infrastructure.PostConfigurations;
public class PostConfiguration
{
public virtual string Type => nameof(PostConfiguration);
public double Rate { get; set; }
}

View File

@@ -0,0 +1,15 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetContracts.Infrastructure.PostConfigurations;
public class TravelAgentPostConfiguration : PostConfiguration
{
public override string Type => nameof(TravelAgentPostConfiguration);
public double PersonalCount { get; set; }
public double BonusForExtraSales { get; set; }
}

View File

@@ -14,5 +14,5 @@ public class PostViewModel
public required string PostType { get; set; } public required string PostType { get; set; }
public double Salary { get; set; } public required string Configuration { get; set; }
} }

View File

@@ -13,6 +13,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.3" />
</ItemGroup> </ItemGroup>

View File

@@ -1,6 +1,9 @@
using MagicCarpetContracts.Infrastructure; using MagicCarpetContracts.Infrastructure;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetDatabase.Models; using MagicCarpetDatabase.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -43,6 +46,13 @@ public class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase)
.HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE"); .HasFilter($"\"{nameof(Post.IsActual)}\" = TRUE");
modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId }); modelBuilder.Entity<SaleTour>().HasKey(x => new { x.SaleId, x.TourId });
modelBuilder.Entity<Post>()
.Property(x => x.Configuration)
.HasColumnType("jsonb")
.HasConversion(
x => SerializePostConfiguration(x),
x => DeserialzePostConfiguration(x));
} }
public DbSet<Client> Clients { get; set; } public DbSet<Client> Clients { get; set; }
@@ -60,4 +70,12 @@ public class MagicCarpetDbContext(IConfigurationDatabase configurationDatabase)
public DbSet<SaleTour> SaleTours { get; set; } public DbSet<SaleTour> SaleTours { get; set; }
public DbSet<Employee> Employees { get; set; } public DbSet<Employee> Employees { get; set; }
private static string SerializePostConfiguration(PostConfiguration postConfiguration) => JsonConvert.SerializeObject(postConfiguration);
private static PostConfiguration DeserialzePostConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
{
nameof(TravelAgentPostConfiguration) => JsonConvert.DeserializeObject<TravelAgentPostConfiguration>(jsonString)!,
nameof(ChiefPostConfiguration) => JsonConvert.DeserializeObject<ChiefPostConfiguration>(jsonString)!,
_ => JsonConvert.DeserializeObject<PostConfiguration>(jsonString)!,
};
} }

View File

@@ -1,4 +1,5 @@
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
@@ -14,7 +15,7 @@ public class Post
public required string PostId { get; set; } public required string PostId { get; set; }
public required string PostName { get; set; } public required string PostName { get; set; }
public PostType PostType { get; set; } public PostType PostType { get; set; }
public double Salary { get; set; } public required PostConfiguration Configuration { get; set; }
public bool IsActual { get; set; } public bool IsActual { get; set; }
public DateTime ChangeDate { get; set; } public DateTime ChangeDate { get; set; }
} }

View File

@@ -2,6 +2,7 @@
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetContracts.StoragesContracts; using MagicCarpetContracts.StoragesContracts;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
@@ -38,9 +39,9 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
var listOriginal = new List<PostDataModel>() var listOriginal = new List<PostDataModel>()
{ {
new(Guid.NewGuid().ToString(),"name 1", PostType.Manager, 10), new(Guid.NewGuid().ToString(),"name 1", PostType.TravelAgent, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 2", PostType.Manager, 10), new(Guid.NewGuid().ToString(), "name 2", PostType.TravelAgent, new PostConfiguration() { Rate = 10 }),
new(Guid.NewGuid().ToString(), "name 3", PostType.Manager, 10), new(Guid.NewGuid().ToString(), "name 3", PostType.TravelAgent, new PostConfiguration() { Rate = 10 }),
}; };
_postStorageContract.Setup(x => x.GetList()).Returns(listOriginal); _postStorageContract.Setup(x => x.GetList()).Returns(listOriginal);
//Act //Act
@@ -94,8 +95,8 @@ internal class PostBusinessLogicContractTests
var postId = Guid.NewGuid().ToString(); var postId = Guid.NewGuid().ToString();
var listOriginal = new List<PostDataModel>() var listOriginal = new List<PostDataModel>()
{ {
new(postId, "name 1", PostType.Manager, 10), new(postId, "name 1", PostType.TravelAgent, new PostConfiguration() { Rate = 10 }),
new(postId, "name 2", PostType.Manager, 10) new(postId, "name 2", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })
}; };
_postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal); _postStorageContract.Setup(x => x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
//Act //Act
@@ -159,7 +160,7 @@ internal class PostBusinessLogicContractTests
{ {
//Arrange //Arrange
var id = Guid.NewGuid().ToString(); var id = Guid.NewGuid().ToString();
var record = new PostDataModel(id, "name", PostType.Manager, 10); var record = new PostDataModel(id, "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementById(id)).Returns(record); _postStorageContract.Setup(x => x.GetElementById(id)).Returns(record);
//Act //Act
var element = _postBusinessLogicContract.GetPostByData(id); var element = _postBusinessLogicContract.GetPostByData(id);
@@ -174,7 +175,7 @@ internal class PostBusinessLogicContractTests
{ {
//Arrange //Arrange
var postName = "name"; var postName = "name";
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.Manager, 10); var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record); _postStorageContract.Setup(x => x.GetElementByName(postName)).Returns(record);
//Act //Act
var element = _postBusinessLogicContract.GetPostByData(postName); var element = _postBusinessLogicContract.GetPostByData(postName);
@@ -230,11 +231,11 @@ internal class PostBusinessLogicContractTests
{ {
//Arrange //Arrange
var flag = false; var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10); var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())) _postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) => .Callback((PostDataModel x) =>
{ {
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary; flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
}); });
//Act //Act
_postBusinessLogicContract.InsertPost(record); _postBusinessLogicContract.InsertPost(record);
@@ -249,7 +250,7 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data")); _postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10)), Throws.TypeOf<ElementExistsException>()); Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once); _postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
} }
@@ -265,7 +266,7 @@ internal class PostBusinessLogicContractTests
public void InsertPost_InvalidRecord_ThrowException_Test() public void InsertPost_InvalidRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.Manager, 10)), Throws.TypeOf<ValidationException>()); Assert.That(() => _postBusinessLogicContract.InsertPost(new PostDataModel("id", "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never); _postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
} }
@@ -275,7 +276,7 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
_postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException())); _postStorageContract.Setup(x => x.AddElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10)), Throws.TypeOf<StorageException>()); Assert.That(() => _postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once); _postStorageContract.Verify(x => x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
} }
@@ -284,11 +285,11 @@ internal class PostBusinessLogicContractTests
{ {
//Arrange //Arrange
var flag = false; var flag = false;
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 10); var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())) _postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>()))
.Callback((PostDataModel x) => .Callback((PostDataModel x) =>
{ {
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.Salary == record.Salary; flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ConfigurationModel.Rate == record.ConfigurationModel.Rate;
}); });
//Act //Act
_postBusinessLogicContract.UpdatePost(record); _postBusinessLogicContract.UpdatePost(record);
@@ -303,7 +304,7 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException("")); _postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementNotFoundException(""));
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10)), Throws.TypeOf<ElementNotFoundException>()); Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementNotFoundException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once); _postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
} }
@@ -313,7 +314,7 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data")); _postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new ElementExistsException("Data", "Data"));
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.Manager, 10)), Throws.TypeOf<ElementExistsException>()); Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "anme", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ElementExistsException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once); _postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
} }
@@ -329,7 +330,7 @@ internal class PostBusinessLogicContractTests
public void UpdatePost_InvalidRecord_ThrowException_Test() public void UpdatePost_InvalidRecord_ThrowException_Test()
{ {
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.Manager, 10)), Throws.TypeOf<ValidationException>()); Assert.That(() => _postBusinessLogicContract.UpdatePost(new PostDataModel("id", "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<ValidationException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never); _postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
} }
@@ -339,7 +340,7 @@ internal class PostBusinessLogicContractTests
//Arrange //Arrange
_postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException())); _postStorageContract.Setup(x => x.UpdElement(It.IsAny<PostDataModel>())).Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert
Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.Manager, 10)), Throws.TypeOf<StorageException>()); Assert.That(() => _postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 })), Throws.TypeOf<StorageException>());
_postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once); _postStorageContract.Verify(x => x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
} }

View File

@@ -2,7 +2,9 @@
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetContracts.StoragesContracts; using MagicCarpetContracts.StoragesContracts;
using MagicCarpetTests.Infrastructure;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
using System; using System;
@@ -10,6 +12,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace MagicCarpetTests.BusinessLogicContractsTests; namespace MagicCarpetTests.BusinessLogicContractsTests;
@@ -21,6 +24,7 @@ internal class SalaryBusinessLogicContractTests
private Mock<ISaleStorageContract> _saleStorageContract; private Mock<ISaleStorageContract> _saleStorageContract;
private Mock<IPostStorageContract> _postStorageContract; private Mock<IPostStorageContract> _postStorageContract;
private Mock<IEmployeeStorageContract> _employeeStorageContract; private Mock<IEmployeeStorageContract> _employeeStorageContract;
private readonly ConfigurationSalaryTest _salaryConfigurationTest = new();
[OneTimeSetUp] [OneTimeSetUp]
public void OneTimeSetUp() public void OneTimeSetUp()
@@ -30,7 +34,7 @@ internal class SalaryBusinessLogicContractTests
_postStorageContract = new Mock<IPostStorageContract>(); _postStorageContract = new Mock<IPostStorageContract>();
_employeeStorageContract = new Mock<IEmployeeStorageContract>(); _employeeStorageContract = new Mock<IEmployeeStorageContract>();
_salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object, _salaryBusinessLogicContract = new SalaryBusinessLogicContract(_salaryStorageContract.Object,
_saleStorageContract.Object, _postStorageContract.Object, _employeeStorageContract.Object, new Mock<ILogger>().Object); _postStorageContract.Object, _employeeStorageContract.Object, new Mock<ILogger>().Object, _salaryConfigurationTest);
} }
[TearDown] [TearDown]
@@ -191,16 +195,16 @@ internal class SalaryBusinessLogicContractTests
{ {
//Arrange //Arrange
var employeeId = Guid.NewGuid().ToString(); var employeeId = Guid.NewGuid().ToString();
var saleSum = 1.2 * 5; var saleSum = 200;
var postSalary = 2000.0; var postSalary = 2000.0;
var rate = 2000.0;
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) _saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])]); .Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [new SaleTourDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 1.2)])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, postSalary)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = rate }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); .Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0; var sum = 0.0;
var expectedSum = postSalary + saleSum * 0.1;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>())) _salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) => .Callback((SalaryDataModel x) =>
{ {
@@ -209,7 +213,7 @@ internal class SalaryBusinessLogicContractTests
//Act //Act
_salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow); _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow);
//Assert //Assert
Assert.That(sum, Is.EqualTo(expectedSum)); Assert.That(sum, Is.EqualTo(rate));
} }
[Test] [Test]
@@ -231,7 +235,7 @@ internal class SalaryBusinessLogicContractTests
new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, []), new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, []),
new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, [])]); new SaleDataModel(Guid.NewGuid().ToString(), employee3Id, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns(list); .Returns(list);
//Act //Act
@@ -244,16 +248,16 @@ internal class SalaryBusinessLogicContractTests
public void CalculateSalaryByMounth_WithoutSalesByEmployee_Test() public void CalculateSalaryByMounth_WithoutSalesByEmployee_Test()
{ {
//Arrange //Arrange
var postSalary = 2000.0; var rate = 2000.0;
var employeeId = Guid.NewGuid().ToString(); var employeeId = Guid.NewGuid().ToString();
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) _saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([]); .Returns([]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, postSalary)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = rate }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); .Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
var sum = 0.0; var sum = 0.0;
var expectedSum = postSalary; var expectedSum = rate;
_salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>())) _salaryStorageContract.Setup(x => x.AddElement(It.IsAny<SalaryDataModel>()))
.Callback((SalaryDataModel x) => .Callback((SalaryDataModel x) =>
{ {
@@ -271,7 +275,7 @@ internal class SalaryBusinessLogicContractTests
//Arrange //Arrange
var employeeId = Guid.NewGuid().ToString(); var employeeId = Guid.NewGuid().ToString();
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); .Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert //Act&Assert
@@ -299,7 +303,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) _saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]); .Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 100 }));
//Act&Assert //Act&Assert
Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>()); Assert.That(() => _salaryBusinessLogicContract.CalculateSalaryByMounth(DateTime.UtcNow), Throws.TypeOf<NullListException>());
} }
@@ -312,7 +316,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) _saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException())); .Throws(new StorageException(new InvalidOperationException()));
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]); .Returns([new EmployeeDataModel(employeeId, "Test", "123@gmail.com", Guid.NewGuid().ToString(), DateTime.UtcNow, DateTime.UtcNow, false)]);
//Act&Assert //Act&Assert
@@ -342,7 +346,7 @@ internal class SalaryBusinessLogicContractTests
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())) _saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]); .Returns([new SaleDataModel(Guid.NewGuid().ToString(), employeeId, null, DiscountType.None, false, [])]);
_postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>())) _postStorageContract.Setup(x => x.GetElementById(It.IsAny<string>()))
.Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 2000)); .Returns(new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 100 }));
_employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>())) _employeeStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>(), It.IsAny<DateTime?>()))
.Throws(new StorageException(new InvalidOperationException())); .Throws(new StorageException(new InvalidOperationException()));
//Act&Assert //Act&Assert

View File

@@ -1,6 +1,7 @@
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -14,41 +15,48 @@ internal class PostDataModelTests
[Test] [Test]
public void IdIsNullOrEmptyTest() public void IdIsNullOrEmptyTest()
{ {
var post = CreateDataModel(null, "name", PostType.Manager, 10); var post = CreateDataModel(null, "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(string.Empty, "name", PostType.Manager, 10); post = CreateDataModel(string.Empty, "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void IdIsNotGuidTest() public void IdIsNotGuidTest()
{ {
var post = CreateDataModel("id", "name", PostType.Manager, 10); var post = CreateDataModel("id", "name", PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void PostNameIsEmptyTest() public void PostNameIsEmptyTest()
{ {
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.Manager, 10); var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.Manager, 10); manufacturer = CreateDataModel(Guid.NewGuid().ToString(), string.Empty, PostType.TravelAgent, new PostConfiguration() { Rate = 10 });
Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => manufacturer.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void PostTypeIsNoneTest() public void PostTypeIsNoneTest()
{ {
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, 10); var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.None, new PostConfiguration() { Rate = 10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
[Test] [Test]
public void SalaryIsLessOrZeroTest() public void ConfigurationModelIsNullTest()
{ {
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, 0); var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, null);
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.Manager, -10); }
[Test]
public void RateIsLessOrZeroTest()
{
var post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = 0 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
post = CreateDataModel(Guid.NewGuid().ToString(), "name", PostType.TravelAgent, new PostConfiguration() { Rate = -10 });
Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>()); Assert.That(() => post.Validate(), Throws.TypeOf<ValidationException>());
} }
@@ -58,18 +66,18 @@ internal class PostDataModelTests
var postId = Guid.NewGuid().ToString(); var postId = Guid.NewGuid().ToString();
var postName = "name"; var postName = "name";
var postType = PostType.Manager; var postType = PostType.Manager;
var salary = 10; var configuration = new PostConfiguration() { Rate = 10 };
var post = CreateDataModel(postId, postName, postType, salary); var post = CreateDataModel(postId, postName, postType, configuration);
Assert.That(() => post.Validate(), Throws.Nothing); Assert.That(() => post.Validate(), Throws.Nothing);
Assert.Multiple(() => Assert.Multiple(() =>
{ {
Assert.That(post.Id, Is.EqualTo(postId)); Assert.That(post.Id, Is.EqualTo(postId));
Assert.That(post.PostName, Is.EqualTo(postName)); Assert.That(post.PostName, Is.EqualTo(postName));
Assert.That(post.PostType, Is.EqualTo(postType)); Assert.That(post.PostType, Is.EqualTo(postType));
Assert.That(post.Salary, Is.EqualTo(salary)); Assert.That(post.ConfigurationModel.Rate, Is.EqualTo(configuration.Rate));
}); });
} }
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, double salary) => private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, PostConfiguration configuration) =>
new(id, postName, postType, salary); new(id, postName, postType, configuration);
} }

View File

@@ -0,0 +1,14 @@
using MagicCarpetContracts.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetTests.Infrastructure;
class ConfigurationSalaryTest : IConfigurationSalary
{
public double ExtraMadeSum => 10;
public int MaxConcurrentThreads => 4;
}

View File

@@ -1,4 +1,5 @@
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetDatabase; using MagicCarpetDatabase;
using MagicCarpetDatabase.Models; using MagicCarpetDatabase.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -20,9 +21,9 @@ internal static class MagicCarpetDbContextExtensions
return client; return client;
} }
public static Post InsertPostToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.Manager, double salary = 10, bool isActual = true, DateTime? changeDate = null) public static Post InsertPostToDatabaseAndReturn(this MagicCarpetDbContext dbContext, string? id = null, string postName = "test", PostType postType = PostType.TravelAgent, PostConfiguration? config = null, bool isActual = true, DateTime? changeDate = null)
{ {
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow }; var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id ?? Guid.NewGuid().ToString(), PostName = postName, PostType = postType, Configuration = config ?? new PostConfiguration() { Rate = 100 }, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
dbContext.Posts.Add(post); dbContext.Posts.Add(post);
dbContext.SaveChanges(); dbContext.SaveChanges();
return post; return post;

View File

@@ -0,0 +1,332 @@
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetDatabase.Implementations;
using MagicCarpetDatabase.Models;
using MagicCarpetTests.Infrastructure;
using MagicCarpetTests.StoragesContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts;
[TestFixture]
internal class PostStorageContractTests : BaseStorageContractTest
{
private PostStorageContract _postStorageContract;
[SetUp]
public void SetUp()
{
_postStorageContract = new PostStorageContract(MagicCarpetDbContext);
}
[TearDown]
public void TearDown()
{
MagicCarpetDbContext.RemovePostsFromDatabase();
}
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2");
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3");
var list = _postStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
AssertElement(list.First(x => x.Id == post.PostId), post);
}
[Test]
public void Try_GetList_WhenNoRecords_Test()
{
var list = _postStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.Empty);
}
[Test]
public void Try_GetList_WhenDifferentConfigTypes_Test()
{
var postSimple = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
var postBuilder = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new TravelAgentPostConfiguration() { PersonalCount = 500 });
var postLoader = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new ChiefPostConfiguration() { PersonalCountTrendPremium = 20 });
var list = _postStorageContract.GetList();
Assert.That(list, Is.Not.Null);
AssertElement(list.First(x => x.Id == postSimple.PostId), postSimple);
AssertElement(list.First(x => x.Id == postBuilder.PostId), postBuilder);
AssertElement(list.First(x => x.Id == postLoader.PostId), postLoader);
}
[Test]
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
{
var postId = Guid.NewGuid().ToString();
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
var list = _postStorageContract.GetPostWithHistory(postId);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
}
[Test]
public void Try_GetPostWithHistory_WhenNoRecords_Test()
{
var postId = Guid.NewGuid().ToString();
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1", isActual: true);
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
var list = _postStorageContract.GetPostWithHistory(Guid.NewGuid().ToString());
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
}
[Test]
public void Try_GetElementById_WhenHaveRecord_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
}
[Test]
public void Try_GetElementById_WhenNoRecord_Test()
{
MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
}
[Test]
public void Try_GetElementById_WhenRecordWasDeleted_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
}
[Test]
public void Try_GetElementById_WhenTrySearchById_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
}
[Test]
public void Try_GetElementByName_WhenHaveRecord_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
}
[Test]
public void Try_GetElementByName_WhenNoRecord_Test()
{
MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
}
[Test]
public void Try_GetElementByName_WhenRecordWasDeleted_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
Assert.That(() => _postStorageContract.GetElementById(post.PostName), Is.Null);
}
[Test]
public void Try_AddElement_Test()
{
var post = CreateModel(Guid.NewGuid().ToString());
_postStorageContract.AddElement(post);
AssertElement(MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id), post);
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), "name unique");
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName, isActual: true);
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSamePostId_Test()
{
var post = CreateModel(Guid.NewGuid().ToString());
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_AddElement_WithTravelAgentPostConfiguration_Test()
{
var salePercent = 10;
var post = CreateModel(Guid.NewGuid().ToString(), config: new TravelAgentPostConfiguration() { PersonalCount = salePercent });
_postStorageContract.AddElement(post);
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
});
}
[Test]
public void Try_AddElement_WithChiefPostConfiguration_Test()
{
var trendPremium = 20;
var post = CreateModel(Guid.NewGuid().ToString(), config: new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium });
_postStorageContract.AddElement(post);
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.Id);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public void Try_UpdElement_Test()
{
var post = CreateModel(Guid.NewGuid().ToString());
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: true);
_postStorageContract.UpdElement(post);
var posts = MagicCarpetDbContext.GetPostsFromDatabaseByPostId(post.Id);
Assert.That(posts, Is.Not.Null);
Assert.That(posts, Has.Length.EqualTo(2));
AssertElement(posts[0], CreateModel(post.Id));
AssertElement(posts[^1], CreateModel(post.Id));
}
[Test]
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _postStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
{
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, postName: "name");
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: post.PostName);
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_UpdElement_WhenRecordWasDeleted_Test()
{
var post = CreateModel(Guid.NewGuid().ToString());
MagicCarpetDbContext.InsertPostToDatabaseAndReturn(post.Id, isActual: false);
Assert.That(() => _postStorageContract.UpdElement(post), Throws.TypeOf<ElementDeletedException>());
}
[Test]
public void Try_UpdElement_WithTravelAgentPostConfiguration_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
var salePercent = 10;
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new TravelAgentPostConfiguration() { PersonalCount = salePercent }));
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
});
}
[Test]
public void Try_UpdElement_WithChiefPostConfiguration_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
var trendPremium = 20;
_postStorageContract.UpdElement(CreateModel(post.PostId, config: new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium }));
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test]
public void Try_DelElement_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: true);
_postStorageContract.DelElement(post.PostId);
Assert.That(MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId), Is.Null);
}
[Test]
public void Try_DelElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _postStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_DelElement_WhenRecordWasDeleted_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
Assert.That(() => _postStorageContract.DelElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
}
[Test]
public void Try_ResElement_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: false);
_postStorageContract.ResElement(post.PostId);
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(post.PostId);
Assert.Multiple(() =>
{
Assert.That(element, Is.Not.Null);
Assert.That(element!.IsActual);
});
}
[Test]
public void Try_ResElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _postStorageContract.ResElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
{
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(isActual: true);
Assert.That(() => _postStorageContract.ResElement(post.PostId), Throws.Nothing);
}
private static void AssertElement(PostDataModel? actual, Post expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
Assert.That(actual.ConfigurationModel.Rate, Is.EqualTo(expected.Configuration.Rate));
});
}
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.TravelAgent, PostConfiguration? config = null)
=> new(postId, postName, postType, config ?? new PostConfiguration() { Rate = 100 });
private static void AssertElement(Post? actual, PostDataModel expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
Assert.That(actual.Configuration.Rate, Is.EqualTo(expected.ConfigurationModel.Rate));
});
}
}

View File

@@ -1,13 +1,16 @@
using MagicCarpetContracts.BindingModels; using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.Enums; using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Infrastructure.PostConfigurations;
using MagicCarpetContracts.ViewModels; using MagicCarpetContracts.ViewModels;
using MagicCarpetDatabase.Models; using MagicCarpetDatabase.Models;
using MagicCarpetTests.Infrastructure; using MagicCarpetTests.Infrastructure;
using System.Text.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MagicCarpetTests.WebApiControllersTests; namespace MagicCarpetTests.WebApiControllersTests;
@@ -82,6 +85,24 @@ internal class PostControllerTests : BaseWebApiControllerTest
}); });
} }
[Test]
public async Task GetRecords_WhenDifferentConfigTypes_ShouldSuccess_Test()
{
//Arrange
var postSimple = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 1");
var postTravelAgent = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 2", config: new TravelAgentPostConfiguration() { PersonalCount = 500 });
var postLoader = MagicCarpetDbContext.InsertPostToDatabaseAndReturn(postName: "name 3", config: new ChiefPostConfiguration() { PersonalCountTrendPremium = 20 });
//Act
var response = await HttpClient.GetAsync("/api/posts");
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
var data = await GetModelFromResponseAsync<List<PostViewModel>>(response);
Assert.That(data, Is.Not.Null);
AssertElement(data.First(x => x.Id == postSimple.PostId), postSimple);
AssertElement(data.First(x => x.Id == postTravelAgent.PostId), postTravelAgent);
AssertElement(data.First(x => x.Id == postLoader.PostId), postLoader);
}
[Test] [Test]
public async Task GetHistory_WhenWrongData_ShouldBadRequest_Test() public async Task GetHistory_WhenWrongData_ShouldBadRequest_Test()
{ {
@@ -200,10 +221,10 @@ internal class PostControllerTests : BaseWebApiControllerTest
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test() public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
{ {
//Arrange //Arrange
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 }; var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.TravelAgent.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 }; var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.TravelAgent.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 }; var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 }; var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.TravelAgent.ToString(), ConfigurationJson = null };
//Act //Act
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect)); var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect)); var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
@@ -237,6 +258,44 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
} }
[Test]
public async Task Post_WithTravelAgentPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var salePercent = 10;
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new TravelAgentPostConfiguration() { PersonalCount = salePercent, Rate = 10 }));
//Act
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
});
}
[Test]
public async Task Post_WithLoaderPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var trendPremium = 20;
var postModel = CreateModel(configuration: JsonSerializer.Serialize(new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium, Rate = 10 }));
//Act
var response = await HttpClient.PostAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test] [Test]
public async Task Put_ShouldSuccess_Test() public async Task Put_ShouldSuccess_Test()
{ {
@@ -292,10 +351,10 @@ internal class PostControllerTests : BaseWebApiControllerTest
public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test() public async Task Put_WhenDataIsIncorrect_ShouldBadRequest_Test()
{ {
//Arrange //Arrange
var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.Manager.ToString(), Salary = 10 }; var postModelWithIdIncorrect = new PostBindingModel { Id = "Id", PostName = "name", PostType = PostType.TravelAgent.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = 10 }; var postModelWithNameIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.TravelAgent.ToString(), ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, Salary = 10 }; var postModelWithPostTypeIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = string.Empty, ConfigurationJson = JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 }) };
var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.Manager.ToString(), Salary = -10 }; var postModelWithSalaryIncorrect = new PostBindingModel { Id = Guid.NewGuid().ToString(), PostName = string.Empty, PostType = PostType.TravelAgent.ToString(), ConfigurationJson = null };
//Act //Act
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect)); var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithIdIncorrect));
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect)); var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/posts", MakeContent(postModelWithNameIncorrect));
@@ -329,6 +388,48 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest)); Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
} }
[Test]
public async Task Put_WithTravelAgentPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var salePercent = 10;
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new TravelAgentPostConfiguration() { PersonalCount = salePercent, Rate = 10 }));
//Act
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
MagicCarpetDbContext.ChangeTracker.Clear();
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(TravelAgentPostConfiguration).Name));
Assert.That((element.Configuration as TravelAgentPostConfiguration)!.PersonalCount, Is.EqualTo(salePercent));
});
}
[Test]
public async Task Put_WithSupervisorPostConfiguration_ShouldSuccess_Test()
{
//Arrange
var trendPremium = 20;
var post = MagicCarpetDbContext.InsertPostToDatabaseAndReturn();
var postModel = CreateModel(post.PostId, configuration: JsonSerializer.Serialize(new ChiefPostConfiguration() { PersonalCountTrendPremium = trendPremium, Rate = 10 }));
//Act
var response = await HttpClient.PutAsync($"/api/posts", MakeContent(postModel));
//Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
MagicCarpetDbContext.ChangeTracker.Clear();
var element = MagicCarpetDbContext.GetPostFromDatabaseByPostId(postModel.Id!);
Assert.That(element, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(element.Configuration.Type, Is.EqualTo(typeof(ChiefPostConfiguration).Name));
Assert.That((element.Configuration as ChiefPostConfiguration)!.PersonalCountTrendPremium, Is.EqualTo(trendPremium));
});
}
[Test] [Test]
public async Task Delete_ShouldSuccess_Test() public async Task Delete_ShouldSuccess_Test()
{ {
@@ -433,17 +534,17 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(actual.Id, Is.EqualTo(expected.PostId)); Assert.That(actual.Id, Is.EqualTo(expected.PostId));
Assert.That(actual.PostName, Is.EqualTo(expected.PostName)); Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
Assert.That(actual.PostType, Is.EqualTo(expected.PostType.ToString())); Assert.That(actual.PostType, Is.EqualTo(expected.PostType.ToString()));
Assert.That(actual.Salary, Is.EqualTo(expected.Salary)); Assert.That(JsonNode.Parse(actual.Configuration)!["Type"]!.GetValue<string>(), Is.EqualTo(expected.Configuration.Type));
}); });
} }
private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Manager, double salary = 10) private static PostBindingModel CreateModel(string? postId = null, string postName = "name", PostType postType = PostType.Manager, string? configuration = null)
=> new() => new()
{ {
Id = postId ?? Guid.NewGuid().ToString(), Id = postId ?? Guid.NewGuid().ToString(),
PostName = postName, PostName = postName,
PostType = postType.ToString(), PostType = postType.ToString(),
Salary = salary ConfigurationJson = configuration ?? JsonSerializer.Serialize(new PostConfiguration() { Rate = 10 })
}; };
private static void AssertElement(Post? actual, PostBindingModel expected) private static void AssertElement(Post? actual, PostBindingModel expected)
@@ -454,7 +555,7 @@ internal class PostControllerTests : BaseWebApiControllerTest
Assert.That(actual.PostId, Is.EqualTo(expected.Id)); Assert.That(actual.PostId, Is.EqualTo(expected.Id));
Assert.That(actual.PostName, Is.EqualTo(expected.PostName)); Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
Assert.That(actual.PostType.ToString(), Is.EqualTo(expected.PostType)); Assert.That(actual.PostType.ToString(), Is.EqualTo(expected.PostType));
Assert.That(actual.Salary, Is.EqualTo(expected.Salary)); Assert.That(actual.Configuration.Type, Is.EqualTo(JsonNode.Parse(expected.ConfigurationJson!)!["Type"]!.GetValue<string>()));
}); });
} }
} }

View File

@@ -6,6 +6,7 @@ using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels; using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels; using MagicCarpetContracts.ViewModels;
using System.Text.Json;
namespace MagicCarpetWebApi.Adapters; namespace MagicCarpetWebApi.Adapters;
@@ -17,6 +18,11 @@ public class PostAdapter : IPostAdapter
private readonly Mapper _mapper; private readonly Mapper _mapper;
private readonly JsonSerializerOptions JsonSerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};
public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, ILogger<PostAdapter> logger) public PostAdapter(IPostBusinessLogicContract postBusinessLogicContract, ILogger<PostAdapter> logger)
{ {
_postBusinessLogicContract = postBusinessLogicContract; _postBusinessLogicContract = postBusinessLogicContract;
@@ -24,7 +30,8 @@ public class PostAdapter : IPostAdapter
var config = new MapperConfiguration(cfg => var config = new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<PostBindingModel, PostDataModel>(); cfg.CreateMap<PostBindingModel, PostDataModel>();
cfg.CreateMap<PostDataModel, PostViewModel>(); cfg.CreateMap<PostDataModel, PostViewModel>()
.ForMember(x => x.Configuration, x => x.MapFrom(src => JsonSerializer.Serialize(src.ConfigurationModel, JsonSerializerOptions)));
}); });
_mapper = new Mapper(config); _mapper = new Mapper(config);
} }