From 1dfdccb8118aeaa3cd844ac8de2a672c93312166 Mon Sep 17 00:00:00 2001 From: mokhan Date: Sat, 21 Feb 2009 21:44:27 +0000 Subject: git-svn-id: http://svn.xp-dev.com/svn/mokhan-sait@2 da190166-9cfc-4ee1-ae03-434a172be219 --- .../test/Marina.Test/Unit/Task/CatalogTasksTest.cs | 106 ++++++++++ .../test/Marina.Test/Unit/Task/LeaseTasksTest.cs | 181 +++++++++++++++++ .../Marina.Test/Unit/Task/RegistrationTasksTest.cs | 217 +++++++++++++++++++++ 3 files changed, 504 insertions(+) create mode 100644 slips/src/test/Marina.Test/Unit/Task/CatalogTasksTest.cs create mode 100644 slips/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs create mode 100644 slips/src/test/Marina.Test/Unit/Task/RegistrationTasksTest.cs (limited to 'slips/src/test/Marina.Test/Unit/Task') diff --git a/slips/src/test/Marina.Test/Unit/Task/CatalogTasksTest.cs b/slips/src/test/Marina.Test/Unit/Task/CatalogTasksTest.cs new file mode 100644 index 0000000..1eae132 --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/Task/CatalogTasksTest.cs @@ -0,0 +1,106 @@ +using System.Collections.Generic; +using Marina.Domain.Interfaces; +using Marina.Domain.Repositories; +using Marina.Infrastructure; +using Marina.Presentation.DTO; +using Marina.Task; +using Marina.Task.Mappers; +using Marina.Test.Utility; +using MbUnit.Framework; +using Rhino.Mocks; + +namespace Marina.Test.Unit.Task { + [TestFixture] + public class CatalogTasksTest { + private MockRepository _mockery; + private ISlipsRepository _slipRepository; + private ISlipsToDisplayDTOMapper _slipMapper; + private IDockRepository _dockRepository; + private IDockToDisplayDTOMapper _dockMapper; + + [SetUp] + public void Setup() { + _mockery = new MockRepository( ); + _slipRepository = _mockery.DynamicMock< ISlipsRepository >( ); + _dockRepository = _mockery.DynamicMock< IDockRepository >( ); + _slipMapper = _mockery.DynamicMock< ISlipsToDisplayDTOMapper >( ); + _dockMapper = _mockery.DynamicMock< IDockToDisplayDTOMapper >( ); + } + + public ICatalogTasks CreateSUT() { + return new CatalogTasks( _slipRepository, _slipMapper, _dockRepository, _dockMapper ); + } + + [Test] + public void Should_leverage_mapper_to_map_all_slips() { + IList< ISlip > availableSlips = new List< ISlip >( ); + ISlip slip = _mockery.DynamicMock< ISlip >( ); + + availableSlips.Add( slip ); + + SlipDisplayDTO slipDTO = ObjectMother.SlipDisplayDTO( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( _slipRepository.AllAvailableSlips( ) ).Return( availableSlips ); + Expect.Call( _slipMapper.MapFrom( slip ) ).Return( slipDTO ); + } + + using ( _mockery.Playback( ) ) { + IEnumerable< SlipDisplayDTO > allAvailableSlips = CreateSUT( ).GetAllAvailableSlips( ); + Assert.IsTrue( ListFactory.From( allAvailableSlips ).Contains( slipDTO ) ); + } + } + + [Test] + public void Should_leverage_repository_to_find_dock_by_id() { + long dockId = 1; + IDock dock = _mockery.DynamicMock< IDock >( ); + + using ( _mockery.Record( ) ) { + Expect.Call( _dockRepository.FindBy( dockId ) ).Return( dock ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).GetDockInformationBy( dockId ); + } + } + + [Test] + public void Should_leverage_mapper_to_return_dto() { + long dockId = 1; + IDock dock = _mockery.DynamicMock< IDock >( ); + + DockDisplayDTO dto = ObjectMother.DockDisplayDTO( ); + using ( _mockery.Record( ) ) { + SetupResult.For( _dockRepository.FindBy( dockId ) ).Return( dock ); + Expect.Call( _dockMapper.MapFrom( dock ) ).Return( dto ); + } + + using ( _mockery.Playback( ) ) { + Assert.AreEqual( dto, CreateSUT( ).GetDockInformationBy( dockId ) ); + } + } + + [Test] + public void Should_leverage_repository_to_find_dock() { + long dockId = 1; + IDock dock = _mockery.DynamicMock< IDock >( ); + ISlip slip = _mockery.DynamicMock< ISlip >( ); + + IList< ISlip > availableSlipsForDock = new List< ISlip >( ); + availableSlipsForDock.Add( slip ); + + SlipDisplayDTO dto = ObjectMother.SlipDisplayDTO( ); + using ( _mockery.Record( ) ) { + Expect.Call( _dockRepository.FindBy( dockId ) ).Return( dock ); + Expect.Call( _slipRepository.AllAvailableSlipsFor( dock ) ).Return( availableSlipsForDock ); + Expect.Call( _slipMapper.MapFrom( slip ) ).Return( dto ); + } + + using ( _mockery.Playback( ) ) { + IRichList< SlipDisplayDTO > slipsFound = ListFactory.From( CreateSUT( ).GetAvailableSlipsForDockBy( dockId ) ); + Assert.IsTrue( slipsFound.Contains( dto ) ); + } + } + } +} \ No newline at end of file diff --git a/slips/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs b/slips/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs new file mode 100644 index 0000000..e645640 --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs @@ -0,0 +1,181 @@ +using Marina.Domain; +using Marina.Domain.Exceptions; +using Marina.Domain.Interfaces; +using Marina.Domain.Repositories; +using Marina.Infrastructure; +using Marina.Presentation.DTO; +using Marina.Task; +using Marina.Task.Mappers; +using MbUnit.Framework; +using Rhino.Mocks; + +namespace Marina.Test.Unit.Task { + [TestFixture] + public class LeaseTasksTest { + private MockRepository _mockery; + private ICustomerRepository _customers; + private ILeaseToDtoMapper _mapper; + private ISlipsRepository _slips; + + [SetUp] + public void Setup() { + _mockery = new MockRepository( ); + _customers = _mockery.DynamicMock< ICustomerRepository >( ); + _slips = _mockery.DynamicMock< ISlipsRepository >( ); + _mapper = _mockery.DynamicMock< ILeaseToDtoMapper >( ); + } + + public ILeaseTasks CreateSUT() { + return new LeaseTasks( _customers, _slips, _mapper ); + } + + [Test] + public void Should_leverage_repository_to_find_customer() { + long customerId = 87; + + using ( _mockery.Record( ) ) { + Expect.Call( _customers.FindBy( customerId ) ).Return( _mockery.DynamicMock< ICustomer >( ) ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).FindAllLeasesFor( customerId ); + } + } + + [Test] + public void Should_leverage_mapper_to_convert_to_dto() { + long customerId = 99; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + ISlipLease lease = _mockery.DynamicMock< ISlipLease >( ); + DisplayLeaseDTO dto = new DisplayLeaseDTO( "", "", "" ); + + using ( _mockery.Record( ) ) { + SetupResult.For( customer.Leases( ) ).Return( ListFactory.For( lease ) ); + SetupResult.For( _customers.FindBy( customerId ) ).Return( customer ); + Expect.Call( _mapper.MapFrom( lease ) ).Return( dto ); + } + + using ( _mockery.Playback( ) ) { + IRichList< DisplayLeaseDTO > returnedDtos = ListFactory.From( CreateSUT( ).FindAllLeasesFor( customerId ) ); + Assert.AreEqual( 1, returnedDtos.Count ); + Assert.IsTrue( returnedDtos.Contains( dto ) ); + } + } + + [Test] + public void Should_lookup_customer_from_repository_when_requesting_a_lease() { + long customerId = 87; + long slipId = 32; + SubmitLeaseRequestDTO request = new SubmitLeaseRequestDTO( customerId, slipId, "daily" ); + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + + using ( _mockery.Record( ) ) { + Expect.Call( _customers.FindBy( customerId ) ).Return( customer ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).RequestLeaseUsing( request ); + } + } + + [RowTest] + [Row( 99 )] + [Row( 87 )] + public void Should_lookup_slip_from_repository_when_requesting_a_lease( long slipId ) { + long customerId = 87; + SubmitLeaseRequestDTO request = new SubmitLeaseRequestDTO( customerId, slipId, "weekly" ); + ISlip slip = _mockery.DynamicMock< ISlip >( ); + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( _customers.FindBy( 0 ) ).IgnoreArguments( ).Return( customer ); + Expect.Call( _slips.FindBy( slipId ) ).Return( slip ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).RequestLeaseUsing( request ); + } + } + + [Test] + public void customer_should_attempt_to_lease_slip() { + long customerId = 87; + long slipId = 32; + + string duration = "weekly"; + SubmitLeaseRequestDTO request = new SubmitLeaseRequestDTO( customerId, slipId, duration ); + ISlip slip = _mockery.DynamicMock< ISlip >( ); + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( _customers.FindBy( customerId ) ).Return( customer ); + SetupResult.For( _slips.FindBy( slipId ) ).Return( slip ); + + customer.Lease( slip, LeaseDurations.FindBy( duration ) ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).RequestLeaseUsing( request ); + } + } + + [Test] + public void should_return_success_response_message() { + long customerId = 87; + long slipId = 32; + + string duration = LeaseDurations.Daily.Name( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( _customers.FindBy( customerId ) ).Return( _mockery.DynamicMock< ICustomer >( ) ); + } + + using ( _mockery.Playback( ) ) { + DisplayResponseLineDTO response = + CreateSUT( ).RequestLeaseUsing( new SubmitLeaseRequestDTO( customerId, slipId, duration ) ); + Assert.AreEqual( response.Message, "Success!" ); + } + } + + [Test] + public void Should_return_error_message_if_the_slip_is_already_leased() { + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + using ( _mockery.Record( ) ) { + SetupResult + .For( _customers.FindBy( 0 ) ) + .IgnoreArguments( ) + .Return( customer ); + + customer.Lease( null, null ); + LastCall + .IgnoreArguments( ) + .Throw( new SlipIsAlreadyLeasedException( ) ); + } + + using ( _mockery.Playback( ) ) { + SubmitLeaseRequestDTO request = new SubmitLeaseRequestDTO( 1, 2, "weekly" ); + DisplayResponseLineDTO response = CreateSUT( ).RequestLeaseUsing( request ); + Assert.AreEqual( "Slip is already leased!", response.Message ); + } + } + + [Test] + public void Should_save_customer_back_to_repository() { + long customerId = 87; + long slipId = 32; + + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + + using ( _mockery.Record( ) ) { + SetupResult + .For( _customers.FindBy( customerId ) ) + .Return( customer ); + _customers.Save( customer ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).RequestLeaseUsing( new SubmitLeaseRequestDTO( customerId, slipId, LeaseDurations.Daily.Name( ) ) ); + } + } + } +} \ No newline at end of file diff --git a/slips/src/test/Marina.Test/Unit/Task/RegistrationTasksTest.cs b/slips/src/test/Marina.Test/Unit/Task/RegistrationTasksTest.cs new file mode 100644 index 0000000..226fae0 --- /dev/null +++ b/slips/src/test/Marina.Test/Unit/Task/RegistrationTasksTest.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; +using Marina.Domain.Interfaces; +using Marina.Domain.Repositories; +using Marina.Infrastructure; +using Marina.Presentation.DTO; +using Marina.Task; +using Marina.Task.Mappers; +using MbUnit.Framework; +using Rhino.Mocks; + +namespace Marina.Test.Unit.Task { + [TestFixture] + public class RegistrationTasksTest { + private MockRepository _mockery; + private ICustomerRepository _mockCustomerRepository; + private IBrokenRulesToDisplayItemMapper _mockMapper; + + [SetUp] + public void Setup() { + _mockery = new MockRepository( ); + _mockCustomerRepository = _mockery.DynamicMock< ICustomerRepository >( ); + _mockMapper = _mockery.DynamicMock< IBrokenRulesToDisplayItemMapper >( ); + } + + public IRegistrationTasks CreateSUT() { + return new RegistrationTasks( _mockMapper, _mockCustomerRepository ); + } + + [Test] + public void Should_leverage_repository_to_create_a_new_customer() { + string username = "username"; + string password = "password"; + string firstName = "mo"; + string lastName = "khan"; + string phoneNumber = "4036813389"; + string city = "calgary"; + RegisterCustomerDTO customerDTO = + new RegisterCustomerDTO( username, password, firstName, lastName, phoneNumber, city ); + + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + IRegistration registration = _mockery.DynamicMock< IRegistration >( ); + using ( _mockery.Record( ) ) { + SetupResult.For( customer.Registration( ) ).Return( registration ); + SetupResult.For( registration.IsValid( ) ).Return( true ); + + Expect.Call( _mockCustomerRepository.NewCustomer( ) ).Return( customer ); + customer.RegisterAccount( username, password, firstName, lastName, phoneNumber, city ); + } + + using ( _mockery.Playback( ) ) { + ListFactory.From( CreateSUT( ).RegisterNew( customerDTO ) ); + } + } + + private RegisterCustomerDTO RegisterCustomerDTO() { + string username = "username"; + string password = "password"; + string firstName = "mo"; + string lastName = "khan"; + string phoneNumber = "4036813389"; + string city = "calgary"; + return new RegisterCustomerDTO( username, password, firstName, lastName, phoneNumber, city ); + } + + [Test] + public void Should_return_registration_messages() { + IRegistration registration = _mockery.DynamicMock< IRegistration >( ); + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + + IList< DisplayResponseLineDTO > brokenRulesDtos = new List< DisplayResponseLineDTO >( ); + IList< IBrokenRule > brokenRules = new List< IBrokenRule >( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( _mockCustomerRepository.NewCustomer( ) ).Return( customer ); + SetupResult.For( customer.Registration( ) ).Return( registration ); + + using ( _mockery.Ordered( ) ) { + Expect.Call( registration.IsValid( ) ).Return( false ); + Expect.Call( registration.BrokenRules( ) ).Return( brokenRules ); + Expect.Call( _mockMapper.MapFrom( brokenRules ) ).Return( brokenRulesDtos ); + } + } + + using ( _mockery.Playback( ) ) { + Assert.AreEqual( brokenRulesDtos, CreateSUT( ).RegisterNew( RegisterCustomerDTO( ) ) ); + } + } + + [Test] + public void Should_return_a_success_message_if_there_are_no_broken_rules() { + IRegistration registration = _mockery.CreateMock< IRegistration >( ); + + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + using ( _mockery.Record( ) ) { + Expect.Call( _mockCustomerRepository.NewCustomer( ) ).Return( customer ); + SetupResult.For( customer.Registration( ) ).Return( registration ); + Expect + .Call( registration.IsValid( ) ) + .Return( true ); + } + + using ( _mockery.Playback( ) ) { + IRichList< DisplayResponseLineDTO > lineItems = + ListFactory.From( CreateSUT( ).RegisterNew( RegisterCustomerDTO( ) ) ); + Assert.IsTrue( lineItems.Contains( new DisplayResponseLineDTO( "Success!" ) ) ); + } + } + + [Test] + public void Should_lookup_customer_from_repository_using_customer_id() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + using ( _mockery.Record( ) ) { + Expect.Call( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).AddNewBoatUsing( new BoatRegistrationDTO( "reg#", "YAMAHA", "2007", "100", customerId ) ); + } + } + + [Test] + public void Should_register_boat_with_customer() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + using ( _mockery.Record( ) ) { + SetupResult.For( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + customer.RegisterBoat( "reg#", "YAMAHA", new DateTime( 2007, 01, 01 ), 100 ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).AddNewBoatUsing( new BoatRegistrationDTO( "reg#", "YAMAHA", "2007", "100", customerId ) ); + } + } + + [Test] + public void Should_save_the_changed_customer_to_the_repository() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + using ( _mockery.Record( ) ) { + using ( _mockery.Ordered( ) ) { + SetupResult.For( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + customer.RegisterBoat( "reg#", "YAMAHA", new DateTime( 2007, 01, 01 ), 100 ); + _mockCustomerRepository.Save( customer ); + } + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).AddNewBoatUsing( new BoatRegistrationDTO( "reg#", "YAMAHA", "2007", "100", customerId ) ); + } + } + + [Test] + public void Should_leverage_repository_to_find_customer() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + IRegistration registration = _mockery.DynamicMock< IRegistration >( ); + using ( _mockery.Record( ) ) { + SetupResult.For( registration.FirstName( ) ).Return( "mo" ); + SetupResult.For( registration.Username( ) ).Return( "mokhan" ); + SetupResult.For( registration.LastName( ) ).Return( "khan" ); + SetupResult.For( registration.PhoneNumber( ) ).Return( "4036813389" ); + SetupResult.For( registration.City( ) ).Return( "calgary" ); + + SetupResult.For( customer.Registration( ) ).Return( registration ); + Expect.Call( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + } + + using ( _mockery.Playback( ) ) { + Assert.AreEqual( + new CustomerRegistrationDisplayDTO( "1", "mokhan", "mo", "khan", "4036813389", "calgary" ), + CreateSUT( ).LoadRegistrationFor( customerId ) ); + } + } + + [Test] + public void Should_leverage_repository_to_update_the_customer_information() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + IRegistration registration = _mockery.DynamicMock< IRegistration >( ); + using ( _mockery.Record( ) ) { + SetupResult.For( customer.Registration( ) ).Return( registration ); + SetupResult.For( registration.IsValid( ) ).Return( true ); + + using ( _mockery.Ordered( ) ) { + Expect.Call( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + customer.UpdateRegistrationTo( "mokhan", "password", "mo", "khan", "4036813389", "calgary" ); + _mockCustomerRepository.Save( customer ); + } + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).UpdateRegistrationFor( + new UpdateCustomerRegistrationDTO( 1, "mokhan", "password", "mo", "khan", "4036813389", "calgary" ) ); + } + } + + [Test] + public void Should_not_save_customer_if_registration_information_is_incorrect() { + int customerId = 1; + ICustomer customer = _mockery.DynamicMock< ICustomer >( ); + IRegistration registration = _mockery.DynamicMock< IRegistration >( ); + + using ( _mockery.Record( ) ) { + SetupResult.For( customer.Registration( ) ).Return( registration ); + SetupResult.For( registration.IsValid( ) ).Return( false ); + SetupResult.For( _mockCustomerRepository.FindBy( customerId ) ).Return( customer ); + } + + using ( _mockery.Playback( ) ) { + CreateSUT( ).UpdateRegistrationFor( + new UpdateCustomerRegistrationDTO( 1, "mokhan", "password", "mo", "khan", "4036813389", "calgary" ) ); + } + } + } +} \ No newline at end of file -- cgit v1.2.3