diff options
| author | mo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64> | 2008-01-06 22:17:42 +0000 |
|---|---|---|
| committer | mo.khan <mo.khan@a0a4a051-f042-0410-9e78-9fae330bdb64> | 2008-01-06 22:17:42 +0000 |
| commit | 4be9b7516d14867f5e70100c5a93714617724659 (patch) | |
| tree | c4ffa4687d83609012ba563ea8f892cc29a81694 | |
| parent | 45647e8b046f8fcc4df81df09bd7dc8aed899262 (diff) | |
Added Lease requests test to LeaseTasks
git-svn-id: http://mokhan.googlecode.com/svn/trunk@11 a0a4a051-f042-0410-9e78-9fae330bdb64
4 files changed, 146 insertions, 16 deletions
diff --git a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/Interfaces/ILeaseDuration.cs b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/Interfaces/ILeaseDuration.cs index a146411..402398d 100644 --- a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/Interfaces/ILeaseDuration.cs +++ b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/Interfaces/ILeaseDuration.cs @@ -4,5 +4,7 @@ using Marina.Infrastructure; namespace Marina.Domain.Interfaces {
public interface ILeaseDuration : IDomainObject, ISpecification< IDateRange > {
DateTime CalculateExpiryDateFrom( DateTime startDate );
+
+ string Name();
}
}
\ No newline at end of file diff --git a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/LeaseDurations.cs b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/LeaseDurations.cs index d4f3ca9..e87e567 100644 --- a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/LeaseDurations.cs +++ b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Domain/LeaseDurations.cs @@ -4,23 +4,30 @@ using Marina.Domain.Interfaces; namespace Marina.Domain {
public class LeaseDurations {
- public static readonly ILeaseDuration Daily = new LeaseDuration( 1, 1 );
- public static readonly ILeaseDuration Weekly = new LeaseDuration( 2, 7 );
- public static readonly ILeaseDuration Monthly = new LeaseDuration( 3, 30 );
- public static readonly ILeaseDuration Yearly = new LeaseDuration( 4, 365 );
- private static readonly ILeaseDuration NoDuration = new LeaseDuration( -1, 0 );
+ public static readonly ILeaseDuration Daily = new LeaseDuration( "Daily", 1, 1 );
+ public static readonly ILeaseDuration Weekly = new LeaseDuration( "Weekly", 2, 7 );
+ public static readonly ILeaseDuration Monthly = new LeaseDuration( "Monthly", 3, 30 );
+ public static readonly ILeaseDuration Yearly = new LeaseDuration( "Yearly", 4, 365 );
+ private static readonly ILeaseDuration NoDuration = new LeaseDuration( "No Duration", -1, 0 );
+ private static readonly ILeaseDuration UnknownDuration = new LeaseDuration( "Unknown", -1, 0 );
private class LeaseDuration : DomainObject, ILeaseDuration {
private readonly int _days;
+ private readonly string _name;
- public LeaseDuration( long id, int days ) : base( id ) {
+ public LeaseDuration( string name, long id, int days ) : base( id ) {
_days = days;
+ _name = name;
}
public DateTime CalculateExpiryDateFrom( DateTime startDate ) {
return startDate.AddDays( _days ).Date.AddHours( 11 );
}
+ public string Name() {
+ return _name;
+ }
+
public bool IsSatisfiedBy( IDateRange range ) {
TimeSpan daysInRange = range.End( ).Subtract( range.Start( ) );
return _days <= daysInRange.Days;
@@ -42,5 +49,14 @@ namespace Marina.Domain { yield return Weekly;
yield return Daily;
}
+
+ public static ILeaseDuration FindBy( string duration ) {
+ foreach ( ILeaseDuration leaseDuration in AllDurations( ) ) {
+ if ( string.Compare( leaseDuration.Name( ), duration, true ) == 0 ) {
+ return leaseDuration;
+ }
+ }
+ return UnknownDuration;
+ }
}
}
\ No newline at end of file diff --git a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Task/LeaseTasks.cs b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Task/LeaseTasks.cs index 183747f..3d3deef 100644 --- a/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Task/LeaseTasks.cs +++ b/Sait/Cmpp299/Assignment1/trunk/src/app/Marina/Task/LeaseTasks.cs @@ -1,4 +1,6 @@ using System.Collections.Generic;
+using Marina.Domain;
+using Marina.Domain.Exceptions;
using Marina.Domain.Interfaces;
using Marina.Domain.Repositories;
using Marina.Infrastructure;
@@ -7,21 +9,30 @@ using Marina.Task.Mappers; namespace Marina.Task {
public class LeaseTasks : ILeaseTasks {
- public LeaseTasks( ICustomerRepository repository, ILeaseToDtoMapper mapper ) {
- _repository = repository;
+ public LeaseTasks( ICustomerRepository customers, ISlipsRepository slips, ILeaseToDtoMapper mapper ) {
+ _customers = customers;
+ _slips = slips;
_mapper = mapper;
}
public IEnumerable< DisplayLeaseDTO > FindAllLeasesFor( long customerId ) {
return new EnumerableMapper< ISlipLease, DisplayLeaseDTO >( _mapper )
- .MapFrom( _repository.FindBy( customerId ).Leases( ) );
+ .MapFrom( _customers.FindBy( customerId ).Leases( ) );
}
public DisplayResponseLineDTO RequestLeaseUsing( SubmitLeaseRequestDTO request ) {
- return null;
+ ICustomer customer = _customers.FindBy( request.CustomerId );
+ try {
+ customer.Lease( _slips.FindBy( request.SlipId ), LeaseDurations.FindBy( request.Duration ) );
+ return new DisplayResponseLineDTO( "Success!" );
+ }
+ catch ( SlipIsAlreadyLeasedException ) {
+ return new DisplayResponseLineDTO( "Slip is already leased!" );
+ }
}
- private readonly ICustomerRepository _repository;
+ private readonly ICustomerRepository _customers;
+ private readonly ISlipsRepository _slips;
private readonly ILeaseToDtoMapper _mapper;
}
}
\ No newline at end of file diff --git a/Sait/Cmpp299/Assignment1/trunk/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs b/Sait/Cmpp299/Assignment1/trunk/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs index 2da8be3..a2991a2 100644 --- a/Sait/Cmpp299/Assignment1/trunk/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs +++ b/Sait/Cmpp299/Assignment1/trunk/src/test/Marina.Test/Unit/Task/LeaseTasksTest.cs @@ -1,3 +1,5 @@ +using Marina.Domain;
+using Marina.Domain.Exceptions;
using Marina.Domain.Interfaces;
using Marina.Domain.Repositories;
using Marina.Infrastructure;
@@ -11,18 +13,20 @@ namespace Marina.Test.Unit.Task { [TestFixture]
public class LeaseTasksTest {
private MockRepository _mockery;
- private ICustomerRepository _repository;
+ private ICustomerRepository _customers;
private ILeaseToDtoMapper _mapper;
+ private ISlipsRepository _slips;
[SetUp]
public void Setup() {
_mockery = new MockRepository( );
- _repository = _mockery.DynamicMock< ICustomerRepository >( );
+ _customers = _mockery.DynamicMock< ICustomerRepository >( );
+ _slips = _mockery.DynamicMock< ISlipsRepository >( );
_mapper = _mockery.DynamicMock< ILeaseToDtoMapper >( );
}
public ILeaseTasks CreateSUT() {
- return new LeaseTasks( _repository, _mapper );
+ return new LeaseTasks( _customers, _slips, _mapper );
}
[Test]
@@ -30,7 +34,7 @@ namespace Marina.Test.Unit.Task { long customerId = 87;
using ( _mockery.Record( ) ) {
- Expect.Call( _repository.FindBy( customerId ) ).Return( _mockery.DynamicMock< ICustomer >( ) );
+ Expect.Call( _customers.FindBy( customerId ) ).Return( _mockery.DynamicMock< ICustomer >( ) );
}
using ( _mockery.Playback( ) ) {
@@ -47,7 +51,7 @@ namespace Marina.Test.Unit.Task { using ( _mockery.Record( ) ) {
SetupResult.For( customer.Leases( ) ).Return( ListFactory.For( lease ) );
- SetupResult.For( _repository.FindBy( customerId ) ).Return( customer );
+ SetupResult.For( _customers.FindBy( customerId ) ).Return( customer );
Expect.Call( _mapper.MapFrom( lease ) ).Return( dto );
}
@@ -57,5 +61,102 @@ namespace Marina.Test.Unit.Task { 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 );
+ }
+ }
}
}
\ No newline at end of file |
