@ -0,0 +1,284 @@
using Moq ;
using NUnit.Framework ;
using PapaCarloBusinessLogic.Implementations ;
using PapaCarloContracts.BusinessLogicContracts ;
using PapaCarloContracts.DataModels ;
using PapaCarloContracts.Exceptions ;
using PapaCarloContracts.StoragesContracts ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace PapaCarloTests.BusinessLogicsContracts ;
[TestFixture]
internal class BlankBusinessLogicContractTests
{
private IBlankBusinessLogicContract _blankBusinessLogicContract ;
private Mock < IBlankStorageContract > _blankStorageContract ;
[OneTimeSetUp]
public void OneTimeSetUp ( )
{
_blankStorageContract = new Mock < IBlankStorageContract > ( ) ;
_blankBusinessLogicContract = new BlankBusinessLogicContract ( _blankStorageContract . Object ) ;
}
[SetUp]
public void SetUp ( )
{
_blankStorageContract . Reset ( ) ;
}
[Test]
public void GetAllBlanks_ReturnListOfRecords_Test ( )
{
var listOriginal = new List < BlankDataModel > ( )
{
new ( Guid . NewGuid ( ) . ToString ( ) , "Mahogany" , "" , "" ) ,
new ( Guid . NewGuid ( ) . ToString ( ) , "Birch" , "" , "" ) ,
new ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" )
} ;
_blankStorageContract . Setup ( x = > x . GetList ( ) ) . Returns ( listOriginal ) ;
var list = _blankBusinessLogicContract . GetAllBlanks ( ) ;
Assert . That ( list , Is . Not . Null ) ;
Assert . That ( list , Is . EquivalentTo ( listOriginal ) ) ;
}
[Test]
public void GetAllBlanks_ReturnEmptyList_Test ( )
{
_blankStorageContract . Setup ( x = > x . GetList ( ) ) . Returns ( [ ] ) ;
var list = _blankBusinessLogicContract . GetAllBlanks ( ) ;
Assert . That ( list , Is . Not . Null ) ;
Assert . That ( list , Has . Count . EqualTo ( 0 ) ) ;
_blankStorageContract . Verify ( x = > x . GetList ( ) , Times . Once ) ;
}
[Test]
public void GetAllBlanks_ReturnNull_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . GetAllBlanks ( ) , Throws . TypeOf < NullListException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetList ( ) , Times . Once ) ;
}
[Test]
public void GetAllBlanks_StorageThrowError_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . GetList ( ) ) . Throws ( new StorageException ( new InvalidOperationException ( ) ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . GetAllBlanks ( ) , Throws . TypeOf < StorageException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetList ( ) , Times . Once ) ;
}
[Test]
public void GetBlankByData_GetById_ReturnRecord_Test ( )
{
var id = Guid . NewGuid ( ) . ToString ( ) ;
var record = new BlankDataModel ( id , "Spruce" , "" , "" ) ;
_blankStorageContract . Setup ( x = > x . GetElementById ( id ) ) . Returns ( record ) ;
var element = _blankBusinessLogicContract . GetBlankByData ( id ) ;
Assert . That ( element , Is . Not . Null ) ;
Assert . That ( element . Id , Is . EqualTo ( id ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementById ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
[Test]
public void GetBlankByData_GetByName_ReturnRecord_Test ( )
{
var name = "Spruce" ;
var record = new BlankDataModel ( Guid . NewGuid ( ) . ToString ( ) , name , "" , "" ) ;
_blankStorageContract . Setup ( x = > x . GetElementByName ( name ) ) . Returns ( record ) ;
var element = _blankBusinessLogicContract . GetBlankByData ( name ) ;
Assert . That ( element , Is . Not . Null ) ;
Assert . That ( element . BlankName , Is . EqualTo ( name ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
public void GetBlankByData_GetByOldName_ReturnRecord_Test ( )
{
var oldName = "Spruce" ;
var record = new BlankDataModel ( Guid . NewGuid ( ) . ToString ( ) , "Birch" , oldName , "" ) ;
_blankStorageContract . Setup ( x = > x . GetElementByOldName ( oldName ) ) . Returns ( record ) ;
var element = _blankBusinessLogicContract . GetBlankByData ( oldName ) ;
Assert . That ( element , Is . Not . Null ) ;
Assert . That ( element . PrevBlankName , Is . EqualTo ( oldName ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementByOldName ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
[Test]
public void GetBlankByData_EmptyData_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( null ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( String . Empty ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementById ( It . IsAny < string > ( ) ) , Times . Never ) ;
_blankStorageContract . Verify ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) , Times . Never ) ;
_blankStorageContract . Verify ( x = > x . GetElementByOldName ( It . IsAny < string > ( ) ) , Times . Never ) ;
}
[Test]
public void GetBlankByData_GetById_NotFoundRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( Guid . NewGuid ( ) . ToString ( ) ) , Throws . TypeOf < ElementNotFoundException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementById ( It . IsAny < string > ( ) ) , Times . Once ) ;
_blankStorageContract . Verify ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) , Times . Never ) ;
_blankStorageContract . Verify ( x = > x . GetElementByOldName ( It . IsAny < string > ( ) ) , Times . Never ) ;
}
[Test]
public void GetBlankByData_GetByName_NotFoundRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( "Name" ) , Throws . TypeOf < ElementNotFoundException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementById ( It . IsAny < string > ( ) ) , Times . Never ) ;
_blankStorageContract . Verify ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) , Times . Once ) ;
_blankStorageContract . Verify ( x = > x . GetElementByOldName ( It . IsAny < string > ( ) ) , Times . Never ) ;
}
[Test]
public void GetBlankByData_StorageThrowError_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . GetElementById ( It . IsAny < string > ( ) ) ) . Throws ( new StorageException ( new InvalidOperationException ( ) ) ) ;
_blankStorageContract . Setup ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) ) . Throws ( new StorageException ( new InvalidOperationException ( ) ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( Guid . NewGuid ( ) . ToString ( ) ) , Throws . TypeOf < StorageException > ( ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . GetBlankByData ( "Name" ) , Throws . TypeOf < StorageException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . GetElementById ( It . IsAny < string > ( ) ) , Times . Once ) ;
_blankStorageContract . Verify ( x = > x . GetElementByName ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
[Test]
public void InsertBlank_CorrectRecord_Test ( )
{
var flag = false ;
var record = new BlankDataModel ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" ) ;
_blankStorageContract . Setup ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) )
. Callback ( ( BlankDataModel x ) = >
{
flag = x . Id = = record . Id & & x . BlankName = = record . BlankName ;
} ) ;
_blankBusinessLogicContract . InsertBlank ( record ) ;
_blankStorageContract . Verify ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Once ) ;
Assert . That ( flag ) ;
}
[Test]
public void InsertBlank_RecordWithExistData_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) ) . Throws ( new ElementExistsException ( "Data" , "Data" ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . InsertBlank ( new ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" ) ) , Throws . TypeOf < ElementExistsException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Once ) ;
}
[Test]
public void InsertBlank_NullRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . InsertBlank ( null ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Never ) ;
}
[Test]
public void InsertBlank_InvalidRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . InsertBlank ( new BlankDataModel ( "id" , "name" , "" , "" ) ) , Throws . TypeOf < ValidationException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Never ) ;
}
[Test]
public void InsertBlank_StorageThrowError_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) ) . Throws ( new StorageException ( new InvalidOperationException ( ) ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . InsertBlank ( new ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" ) ) , Throws . TypeOf < StorageException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . AddElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Once ) ;
}
[Test]
public void UpdateBlank_RecordWithIncorrectData_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) ) . Throws ( new ElementNotFoundException ( "" ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . UpdateBlank ( new ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" ) ) , Throws . TypeOf < ElementNotFoundException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Once ) ;
}
[Test]
public void UpdateBlank_RecordWithExistData_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) ) . Throws ( new ElementExistsException ( "Data" , "Data" ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . UpdateBlank ( new ( Guid . NewGuid ( ) . ToString ( ) , "Spruce" , "" , "" ) ) , Throws . TypeOf < ElementNotFoundException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Once ) ;
}
[Test]
public void UpdateBlank_NullRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . UpdateBlank ( null ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Never ) ;
}
[Test]
public void UpdateBlank_InvalidRecord_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . UpdateBlank ( new ( "id" , "Spruce" , "" , "" ) ) , Throws . TypeOf < ValidationException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . UpdElement ( It . IsAny < BlankDataModel > ( ) ) , Times . Never ) ;
}
[Test]
public void DeleteBlank_CorrectRecord_Test ( )
{
var id = Guid . NewGuid ( ) . ToString ( ) ;
var flag = false ;
_blankStorageContract . Setup ( x = > x . DelElement ( It . Is ( ( string x ) = > x = = id ) ) ) . Callback ( ( ) = > { flag = true ; } ) ;
_blankBusinessLogicContract . DeleteBlank ( id ) ;
_blankStorageContract . Verify ( x = > x . DelElement ( It . IsAny < string > ( ) ) , Times . Once ) ;
Assert . That ( flag ) ;
}
[Test]
public void DeleteBlank_RecordWithIncorrectId_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . DelElement ( It . IsAny < string > ( ) ) ) . Throws ( new ElementNotFoundException ( "" ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . DeleteBlank ( Guid . NewGuid ( ) . ToString ( ) ) , Throws . TypeOf < ElementNotFoundException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . DelElement ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
[Test]
public void DeleteBlank_IdIsNullOrEmpty_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . DeleteBlank ( null ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . DeleteBlank ( string . Empty ) , Throws . TypeOf < ArgumentNullException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . DelElement ( It . IsAny < string > ( ) ) , Times . Never ) ;
}
[Test]
public void DeleteBlank_IdIsNotGuid_ThrowException_Test ( )
{
Assert . That ( ( ) = > _blankBusinessLogicContract . DeleteBlank ( "id" ) , Throws . TypeOf < ValidationException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . DelElement ( It . IsAny < string > ( ) ) , Times . Never ) ;
}
[Test]
public void DeleteBlank_StorageThrowError_ThrowException_Test ( )
{
_blankStorageContract . Setup ( x = > x . DelElement ( It . IsAny < string > ( ) ) ) . Throws ( new StorageException ( new InvalidOperationException ( ) ) ) ;
Assert . That ( ( ) = > _blankBusinessLogicContract . DeleteBlank ( Guid . NewGuid ( ) . ToString ( ) ) , Throws . TypeOf < StorageException > ( ) ) ;
_blankStorageContract . Verify ( x = > x . DelElement ( It . IsAny < string > ( ) ) , Times . Once ) ;
}
}