383 lines
15 KiB
C#
Raw Normal View History

2025-02-26 23:49:16 +04:00
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using CandyHouseBase.DataModels;
using CandyHouseBase.Exceptions;
using CandyHouseBase.Interfaces.StoragesContracts;
using Microsoft.Extensions.Logging;
using CandyHouseBase.Implementations;
namespace CandyHouseTests.BusinessLogicsContractsTests
{
[TestFixture]
internal class IngredientBusinessLogicContractTests
{
private IngredientBusinessLogicContract _ingredientBusinessLogicContract;
private Mock<IIngredientStorageContact> _ingredientStorageContact;
[OneTimeSetUp]
public void OneTimeSetUp()
{
_ingredientStorageContact = new Mock<IIngredientStorageContact>();
_ingredientBusinessLogicContract =
new IngredientBusinessLogicContract(_ingredientStorageContact.Object, new Mock<ILogger>().Object);
}
[SetUp]
public void SetUp()
{
_ingredientStorageContact.Reset();
}
[Test]
public void GetAllIngredients_ReturnsListOfRecords_Test()
{
// Arrange
var ingredients = new List<IngredientDataModel>
{
new(Guid.NewGuid().ToString(), "Sugar", "kg", 100),
new(Guid.NewGuid().ToString(), "Flour", "kg", 200),
new(Guid.NewGuid().ToString(), "Cocoa", "kg", 50)
};
_ingredientStorageContact.Setup(x => x.GetList()).Returns(ingredients);
// Act
var list = _ingredientBusinessLogicContract.GetAllIngredients();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.EquivalentTo(ingredients));
}
[Test]
public void GetAllIngredients_ReturnsEmptyList_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetList()).Returns(new List<IngredientDataModel>());
// Act
var list = _ingredientBusinessLogicContract.GetAllIngredients();
// Assert
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(0));
_ingredientStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllIngredients_ReturnsNull_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetList()).Returns((List<IngredientDataModel>)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf<NullListException>());
_ingredientStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetAllIngredients_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetList())
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetAllIngredients(), Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetList(), Times.Once);
}
[Test]
public void GetIngredientById_ReturnsRecord_Test()
{
// Arrange
var id = Guid.NewGuid().ToString();
var ingredient = new IngredientDataModel(id, "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns(ingredient);
// Act
var element = _ingredientBusinessLogicContract.GetIngredientByData(id);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Id, Is.EqualTo(id));
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_ReturnsRecord_Test()
{
// Arrange
var name = "Sugar";
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), name, "kg", 100);
_ingredientStorageContact.Setup(x => x.GetElementByName(name)).Returns(ingredient);
// Act
var element = _ingredientBusinessLogicContract.GetIngredientByData(name);
// Assert
Assert.That(element, Is.Not.Null);
Assert.That(element.Name, Is.EqualTo(name));
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientById_NotFoundRecord_ThrowException_Test()
{
// Arrange
var id = "nonexistent";
_ingredientStorageContact.Setup(x => x.GetElementById(id)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(id),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_NotFoundRecord_ThrowException_Test()
{
// Arrange
var name = "Nonexistent";
_ingredientStorageContact.Setup(x => x.GetElementByName(name)).Returns((IngredientDataModel)null);
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(name),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientById_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetElementById(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetElementById(It.IsAny<string>()), Times.Once);
}
[Test]
public void GetIngredientByName_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.GetElementByName(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.GetIngredientByData("Sugar"),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
}
[Test]
public void AddIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.AddElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == ingredient.Id && x.Name == ingredient.Name &&
x.Cost == ingredient.Cost && x.Unit == ingredient.Unit;
});
// Act
_ingredientBusinessLogicContract.InsertIngredient(ingredient);
// Assert
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void AddIngredient_RecordWithExistsData_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
_ingredientStorageContact.Setup(x => x.AddElement(ingredient))
.Throws(new ElementExistsException("ID", ingredient.Id));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<ElementExistsException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void AddIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.InsertIngredient(null),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void AddIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(new IngredientDataModel("", null, "", -1)),
Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void AddIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.AddElement(It.IsAny<IngredientDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.InsertIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.AddElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void UpdateIngredient_CorrectRecord_Test()
{
// Arrange
var flag = false;
var ingredient = new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100);
_ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny<IngredientDataModel>()))
.Callback((IngredientDataModel x) =>
{
flag = x.Id == ingredient.Id && x.Name == ingredient.Name &&
x.Cost == ingredient.Cost && x.Unit == ingredient.Unit;
});
// Act
_ingredientBusinessLogicContract.UpdateIngredient(ingredient);
// Assert
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
Assert.That(flag);
}
[Test]
public void UpdateIngredient_RecordNotFound_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient))
.Throws(new ElementNotFoundException(ingredient.ToString()));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void UpdateIngredient_NullRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.UpdateIngredient(null),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void UpdateIngredient_InvalidRecord_ThrowException_Test()
{
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(new IngredientDataModel("", null, "", -1)),
Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Never);
}
[Test]
public void UpdateIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.UpdateElement(It.IsAny<IngredientDataModel>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(
() => _ingredientBusinessLogicContract.UpdateIngredient(
new IngredientDataModel(Guid.NewGuid().ToString(), "Sugar", "kg", 100)),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.UpdateElement(It.IsAny<IngredientDataModel>()), Times.Once);
}
[Test]
public void DeleteIngredient_CorrectId_Test()
{
// Arrange
var id = "1";
var flag = false;
_ingredientStorageContact.Setup(x => x.DeleteElement(It.Is((string x) => x == id)))
.Callback(() => { flag = true; });
// Act
_ingredientBusinessLogicContract.DeleteIngredient(id);
// Assert
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
Assert.That(flag);
}
[Test]
public void DeleteIngredient_RecordNotFound_ThrowException_Test()
{
var ingredient = It.IsAny<IngredientDataModel>();
// Arrange
_ingredientStorageContact.Setup(x => x.UpdateElement(ingredient))
.Throws(new ElementNotFoundException(ingredient.ToString()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("nonexistent"),
Throws.TypeOf<ElementNotFoundException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
}
[Test]
public void DeleteIngredient_NullOrEmptyId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(null),
Throws.TypeOf<ArgumentNullException>());
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(string.Empty),
Throws.TypeOf<ArgumentNullException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteIngredient_InvalidId_ThrowException_Test()
{
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient("invalid"),
Throws.TypeOf<ValidationException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Never);
}
[Test]
public void DeleteIngredient_StorageThrowError_ThrowException_Test()
{
// Arrange
_ingredientStorageContact.Setup(x => x.DeleteElement(It.IsAny<string>()))
.Throws(new StorageException(new InvalidOperationException()));
// Act & Assert
Assert.That(() => _ingredientBusinessLogicContract.DeleteIngredient(Guid.NewGuid().ToString()),
Throws.TypeOf<StorageException>());
_ingredientStorageContact.Verify(x => x.DeleteElement(It.IsAny<string>()), Times.Once);
}
}
}