APMAvailabilityRangeStrategy implements APMAvailabilityStrategy { @Override public boolean isAvailable(final WorldpayAPMConfigurationModel apmConfiguration, final CartModel cartModel) { if (CollectionUtils.isEmpty(apmConfiguration.getCurrencyRanges())) { return true; } final String cartCurrencyIso = cartModel.getCurrency().getIsocode(); for (final WorldpayCurrencyRangeModel currencyRangeModel : apmConfiguration.getCurrencyRanges()) { if (cartCurrencyIso.equals(currencyRangeModel.getCurrency().getIsocode())) { return isCartTotalWithinRange(cartModel.getTotalPrice(), currencyRangeModel.getMin(), currencyRangeModel.getMax()); } } return true; }  @Override boolean isAvailable(final WorldpayAPMConfigurationModel apmConfiguration, final CartModel cartModel);  }
@Test public void isAvailableReturnsTrueIfNoRangesConfigured() { when(apmConfigurationMock.getCurrencyRanges()).thenReturn(Collections.emptySet()); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsTrueIfRangeIsNullInConfiguration() { when(apmConfigurationMock.getCurrencyRanges()).thenReturn(null); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsTrueIfApmConfigHasRangesButAreNotInTheCartCurrency() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.ITALY).getCurrencyCode()); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsTrueIfApmConfigHasRangesInTheCartCurrencyAndWithinTheSpecifiedRange() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(15d); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsFalseIfApmConfigHasRangesInTheCartCurrencyAndBelowWithinTheSpecifiedRange() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(5d); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertFalse(result); }
@Test public void isAvailableReturnsFalseIfApmConfigHasRangesInTheCartCurrencyAndAboveWithinTheSpecifiedRange() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(25d); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertFalse(result); }
@Test public void isAvailableReturnsTrueIfApmConfigHasRangesInTheCartCurrencyEqualToHigherBoundWithinTheSpecifiedRange() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(20d); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsTrueIfApmConfigHasRangesInTheCartCurrencyEqualToLowerBoundWithinTheSpecifiedRange() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(10d); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }
@Test public void isAvailableReturnsTrueIfApmConfigHasRangesInTheCartCurrencyWhereLowerRangeIsNullAndHigherIsNull() { when(cartModelMock.getCurrency().getIsocode()).thenReturn(Currency.getInstance(Locale.UK).getCurrencyCode()); when(cartModelMock.getTotalPrice()).thenReturn(10d); when(worldpayCurrencyRangeModelUkMock.getMin()).thenReturn(null); when(worldpayCurrencyRangeModelUkMock.getMax()).thenReturn(null); final boolean result = testObj.isAvailable(apmConfigurationMock, cartModelMock); assertTrue(result); }