forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
297 lines
10 KiB
C#
297 lines
10 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.BusinessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using MagicCarpetWebApi.Adapters;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.WebApiAdapterTests;
|
|
|
|
[TestFixture]
|
|
internal class SuppliesAdapterTests
|
|
{
|
|
private Mock<ISuppliesBusinessLogicContract> _suppliesBusinessLogicContract;
|
|
private SuppliesAdapter _adapter;
|
|
private Mock<ILogger<SuppliesAdapter>> _logger;
|
|
|
|
[OneTimeSetUp]
|
|
public void OneTimeSetUp()
|
|
{
|
|
_suppliesBusinessLogicContract = new Mock<ISuppliesBusinessLogicContract>();
|
|
_logger = new Mock<ILogger<SuppliesAdapter>>();
|
|
_adapter = new SuppliesAdapter(_suppliesBusinessLogicContract.Object, _logger.Object);
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllComponents_WhenSuppliesExist_ReturnOk()
|
|
{
|
|
// Arrange
|
|
var supplies = new List<SuppliesDataModel>
|
|
{
|
|
new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now, 5, []),
|
|
new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now.AddDays(1), 10, [])
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Returns(supplies);
|
|
// Act
|
|
var result = _adapter.GetAllComponents();
|
|
var list = (List<SuppliesViewModel>)result.Result!;
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
Assert.That(list.Count, Is.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllComponents_WhenListNull_ReturnNotFound()
|
|
{
|
|
// Arrange
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new NullListException());
|
|
// Act
|
|
var result = _adapter.GetAllComponents();
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public void GetAllComponents_WhenStorageException_ReturnsInternalServerError()
|
|
{
|
|
// Arrange
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetAllComponents()).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act
|
|
var result = _adapter.GetAllComponents();
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
|
}
|
|
|
|
[Test]
|
|
public void GetComponentByData_WhenSupplies_ReturnOK()
|
|
{
|
|
var supply = new SuppliesDataModel(Guid.NewGuid().ToString(), TourType.Ski, DateTime.Now, 5, []);
|
|
// Arrange
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(supply.Id)).Returns(supply);
|
|
// Act
|
|
var result = _adapter.GetComponentByData(supply.Id);
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
|
var resultData = (SuppliesViewModel)result.Result!;
|
|
Assert.That(resultData.Id!, Is.EqualTo(supply.Id));
|
|
}
|
|
|
|
[Test]
|
|
public void GetComponentByData_WhenComponentNotFound_ReturnsNotFound()
|
|
{
|
|
// Arrange
|
|
var data = "test";
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new ElementNotFoundException(""));
|
|
// Act
|
|
var result = _adapter.GetComponentByData(data);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
|
|
}
|
|
|
|
[Test]
|
|
public void GetComponentByData_WhenStorageException_ReturnInternalServerError()
|
|
{
|
|
// Arrange
|
|
var data = "test";
|
|
_suppliesBusinessLogicContract.Setup(x => x.GetComponentByData(data)).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act
|
|
var result = _adapter.GetComponentByData(data);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.InternalServerError));
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponent_WhenValidData_ReturnsNoContent()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.Ski,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = [new TourSuppliesBindingModel { TourId = Guid.NewGuid().ToString(), SuppliesId = Guid.NewGuid().ToString(), Count = 5 }]
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>()));
|
|
// Act
|
|
var result = _adapter.InsertComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponent_WhenDataIsNull_ReturnsBadRequest()
|
|
{
|
|
// Arrange
|
|
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(null)).Throws(new ArgumentNullException());
|
|
// Act
|
|
var result = _adapter.InsertComponent(null);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponent_WhenValidationError_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.None,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = []
|
|
};
|
|
// Act
|
|
var result = _adapter.InsertComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponent_WhenElementExistException_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply1 = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.None,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = []
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
// Act
|
|
var result = _adapter.InsertComponent(supply1);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void InsertComponent_WhenStorageException_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.Ski,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = new List<TourSuppliesBindingModel>()
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.InsertComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act
|
|
var result = _adapter.InsertComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenValidData_ReturnsNoContent()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>()));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenDataIsNull_ReturnsBadRequest()
|
|
{
|
|
// Arrange
|
|
var component = new SuppliesBindingModel { Id = Guid.NewGuid().ToString() };
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(component);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenValidationError_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.Ski,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = []
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ValidationException(""));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenElementNotFoundException_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.None,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = []
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementNotFoundException(""));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenElementExistException_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply1 = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.None,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = []
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new ElementExistsException("Data", "Data"));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(supply1);
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateComponent_WhenStorageException_ReturnBadRequest()
|
|
{
|
|
// Arrange
|
|
var supply = new SuppliesBindingModel
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
TourType = TourType.Ski,
|
|
ProductuionDate = DateTime.Now,
|
|
Count = 5,
|
|
Tours = new List<TourSuppliesBindingModel>()
|
|
};
|
|
_suppliesBusinessLogicContract.Setup(x => x.UpdateComponent(It.IsAny<SuppliesDataModel>())).Throws(new StorageException(new InvalidOperationException()));
|
|
// Act
|
|
var result = _adapter.UpdateComponent(supply);
|
|
// Assert
|
|
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
|
|
}
|
|
}
|