Advertisement
spkenny

Junit Clock

Jul 3rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. @Test
  2.     public void testGetDeliveryDateEightDaysAndHoliday ()
  3.     {
  4.  
  5.         new NonStrictExpectations( product )
  6.         {
  7.             {
  8.                 product.getDeliveryDays( 1, 1 );
  9.                 result = 8;
  10.  
  11.             }
  12.         };
  13.  
  14.         Clock clock = Clock.fixed( Instant.parse( "2017-06-29T12:30:00.00Z" ), ZoneId.of( "UTC" ) );
  15.  
  16.         Date deliveryDate = product.getDeliveryDate( 1, 1, clock );
  17.  
  18.         Calendar cal = Calendar.getInstance();
  19.         cal.setTime( deliveryDate );
  20.         int day = cal.get( Calendar.DAY_OF_MONTH );
  21.  
  22.         Assert.assertEquals( 11, day );
  23.  
  24.     }
  25.  
  26.  
  27. public Date getDeliveryDate ( final int contextId, final int quantity, Clock clock )
  28.     {
  29.         Date returnValue = null;
  30.         int daysToAdd = quantity > 0 ? getDeliveryDays( contextId, quantity ) : getDeliveryDays( contextId );
  31.  
  32.         final DeliveryBlock block = DeliveryBlockManager.getInstance().getDeliveryBlock( getClientId(), contextId );
  33.  
  34.        
  35.         LocalDateTime localDateTime = LocalDateTime.now(clock);
  36.         if ( block != null && block.isValid( Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()) ) )
  37.         {
  38.             daysToAdd = localDateTime.getHour() < DELIVERY_TIME_LAST_HOUR ? daysToAdd - 1 : daysToAdd;
  39.         }
  40.  
  41.         if ( daysToAdd <= 0 )
  42.         {
  43.             returnValue = Date.from( getNextDate( localDateTime, block ).atZone( ZoneId.systemDefault() ).toInstant() );
  44.         }
  45.         else
  46.         {
  47.             LocalDateTime deliveryDate = Stream
  48.                     .iterate( getNextDate( localDateTime, block ), t -> getNextDate( t, block ) ).limit( daysToAdd )
  49.                     .reduce( ( a, b ) -> b ).orElse( getNextDate( LocalDateTime.now(), block ) );
  50.             returnValue = Date.from( deliveryDate.atZone( ZoneId.systemDefault() ).toInstant() );
  51.         }
  52.  
  53.         return returnValue;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement