@Test
  public void testPopulate() {
    // create search result values
    final SearchResultValueData searchResultValueData = new SearchResultValueData();
    final Map<String, Object> searchValueMap = new HashMap<String, Object>();
    searchValueMap.put(BillingPlanModel.BILLINGFREQUENCY, "monthly");
    searchValueMap.put(ProductModel.SOLDINDIVIDUALLY, Boolean.TRUE);
    searchValueMap.put(SubscriptionTermModel.TERMOFSERVICERENEWAL, "yearly");
    searchValueMap.put("termLimit", "18 months");
    searchValueMap.put("lowestBundlePriceValue", Double.valueOf(1.99));
    searchResultValueData.setValues(searchValueMap);

    final CurrencyModel currency = new CurrencyModel();
    currency.setIsocode("USD");
    final PriceData priceData = new PriceData();
    priceData.setValue(BigDecimal.valueOf(1.99));
    priceData.setCurrencyIso(currency.getIsocode());
    given(commonI18NService.getCurrentCurrency()).willReturn(currency);
    given(
            priceDataFactory.create(
                PriceDataType.BUY, BigDecimal.valueOf(1.99), currency.getIsocode()))
        .willReturn(priceData);

    final ProductData productData = new ProductData();
    searchProductTelcoPopulator.populate(searchResultValueData, productData);

    assertNotNull("", productData.getSubscriptionTerm());
    assertNotNull("", productData.getSubscriptionTerm().getBillingPlan());
    assertNotNull("", productData.getSubscriptionTerm().getBillingPlan().getBillingTime());
    assertEquals(
        "",
        searchProductTelcoPopulator.getValue(searchResultValueData, "billingTime"),
        productData.getSubscriptionTerm().getBillingPlan().getBillingTime().getName());
    assertEquals(
        "",
        searchProductTelcoPopulator.getValue(searchResultValueData, ProductModel.SOLDINDIVIDUALLY),
        Boolean.valueOf(productData.isSoldIndividually()));
    assertNotNull("", productData.getSubscriptionTerm().getTermOfServiceFrequency());
    assertEquals(
        "",
        searchProductTelcoPopulator.getValue(searchResultValueData, "termLimit"),
        productData.getSubscriptionTerm().getTermOfServiceFrequency().getName());
    assertNotNull("", productData.getLowestBundlePrice());
    assertEquals(
        "",
        BigDecimal.valueOf(
            ((Double)
                    searchProductTelcoPopulator.getValue(
                        searchResultValueData, "lowestBundlePriceValue"))
                .doubleValue()),
        productData.getLowestBundlePrice().getValue());
  }
  @Test
  public void testConvert() {
    final CurrencyModel currencyModel = mock(CurrencyModel.class);

    given(currencyModel.getName()).willReturn(ISOCODE);
    given(currencyModel.getIsocode()).willReturn(ISOCODE);
    given(currencyModel.getActive()).willReturn(Boolean.TRUE);
    given(currencyModel.getSymbol()).willReturn(ISOCODE);

    final CurrencyData currencyData = currencyConverter.convert(currencyModel);

    Assert.assertEquals(currencyModel.getIsocode(), currencyData.getIsocode());
    Assert.assertEquals(currencyModel.getActive(), Boolean.valueOf(currencyData.isActive()));
    Assert.assertEquals(currencyModel.getSymbol(), currencyData.getSymbol());
    Assert.assertEquals(currencyModel.getName(), currencyData.getName());
  }
  @Test
  public void testPopulateAll() {
    final CustomerData customerData = mock(CustomerData.class);
    final CustomerModel customerModel = new CustomerModel();

    final CurrencyData currencyData = mock(CurrencyData.class);
    final LanguageData languageData = mock(LanguageData.class);
    final CurrencyModel currencyModel = mock(CurrencyModel.class);
    final LanguageModel languageModel = mock(LanguageModel.class);

    given(customerData.getFirstName()).willReturn("firstName");
    given(customerData.getLastName()).willReturn("lastName");
    given(customerData.getTitleCode()).willReturn(null);
    given(customerNameStrategy.getName("firstName", "lastName")).willReturn("firstName lastName");

    given(customerData.getCurrency()).willReturn(currencyData);
    given(currencyData.getIsocode()).willReturn("USD");
    given(currencyModel.getIsocode()).willReturn("USD");
    given(commonI18NService.getCurrency("USD")).willReturn(currencyModel);

    given(customerData.getLanguage()).willReturn(languageData);
    given(languageData.getIsocode()).willReturn("en");
    given(languageModel.getIsocode()).willReturn("en");
    given(commonI18NService.getLanguage("en")).willReturn(languageModel);

    customerReversePopulator.populate(customerData, customerModel);
    Assert.assertEquals("firstName lastName", customerModel.getName());
    Assert.assertNull(customerModel.getTitle());
    Assert.assertEquals("USD", customerModel.getSessionCurrency().getIsocode());
    Assert.assertEquals("en", customerModel.getSessionLanguage().getIsocode());
  }
  @Test
  public void testGetCurrentJavaCurrency() {
    final CurrencyModel currency1 = new CurrencyModel();
    currency1.setIsocode("USD");

    when(sessionService.getAttribute(I18NConstants.CURRENCY_SESSION_ATTR_KEY))
        .thenReturn(currency1);
    final Currency currentJavaCurrency = i18NService.getCurrentJavaCurrency();

    assertEquals(
        "Wrong current java currency isocode! Should be: '"
            + currency1.getIsocode()
            + "' but was: '"
            + currentJavaCurrency.getCurrencyCode()
            + "'.",
        currency1.getIsocode(),
        currentJavaCurrency.getCurrencyCode());

    verify(sessionService, times(1)).getAttribute(Mockito.anyString());
  }
  /**
   * @param locale
   * @param currency
   * @return A clone of {@link NumberFormat} from the instance in the local cache, if the cache does
   *     not contain an instance of a NumberFormat for a given locale and currency one would be
   *     added.
   */
  protected NumberFormat createCurrencyFormat(final Locale locale, final CurrencyModel currency) {
    final String key = locale.getISO3Country() + "_" + currency.getIsocode();

    NumberFormat numberFormat = currencyFormats.get(key);
    if (numberFormat == null) {
      final NumberFormat currencyFormat = createNumberFormat(locale, currency);
      numberFormat = currencyFormats.putIfAbsent(key, currencyFormat);
      if (numberFormat == null) {
        numberFormat = currencyFormat;
      }
    }
    // don't allow multiple references
    return (NumberFormat) numberFormat.clone();
  }
  @Override
  public PriceData create(
      final PriceDataType priceType, final BigDecimal value, final CurrencyModel currency) {
    Assert.notNull(priceType, "Parameter priceType cannot be null.");
    Assert.notNull(value, "Parameter value cannot be null.");
    Assert.notNull(currency, "Parameter currency cannot be null.");

    final PriceData priceData = createPriceData();

    priceData.setPriceType(priceType);
    priceData.setValue(value);
    priceData.setCurrencyIso(currency.getIsocode());
    priceData.setFormattedValue(formatPrice(value, currency));

    return priceData;
  }
  @Test
  public void testBillingFreqContainer() {
    final CurrencyModel currencyModel = mock(CurrencyModel.class);
    final PriceData priceData = mock(PriceData.class);
    final DeliveryModeModel deliveryMode = mock(DeliveryModeModel.class);
    given(cartModel.getDeliveryMode()).willReturn(deliveryMode);

    given(cartModel.getDeliveryCost()).willReturn(Double.valueOf(3.4));
    given(cartModel.getCurrency()).willReturn(currencyModel);
    given(currencyModel.getIsocode()).willReturn("isoCode");
    given(priceDataFactory.create(PriceDataType.BUY, BigDecimal.valueOf(3.4), currencyModel))
        .willReturn(priceData);

    given(cartModel.getBillingTime()).willReturn(billingTimeModel);
    given(cartModel.getBillingTime().getCode()).willReturn("paynow");

    subsOrderPopulator.addTotals(cartModel, cartData);
    Assert.assertEquals(priceData, cartData.getDeliveryCost());
  }
 /** Adjusts {@link DecimalFormat}'s symbol according to given {@link CurrencyModel}. */
 protected DecimalFormat adjustSymbol(
     final DecimalFormat format, final CurrencyModel currencyModel) {
   final String symbol = currencyModel.getSymbol();
   if (symbol != null) {
     final DecimalFormatSymbols symbols = format.getDecimalFormatSymbols(); // does cloning
     final String iso = currencyModel.getIsocode();
     boolean changed = false;
     if (!iso.equalsIgnoreCase(symbols.getInternationalCurrencySymbol())) {
       symbols.setInternationalCurrencySymbol(iso);
       changed = true;
     }
     if (!symbol.equals(symbols.getCurrencySymbol())) {
       symbols.setCurrencySymbol(symbol);
       changed = true;
     }
     if (changed) {
       format.setDecimalFormatSymbols(symbols);
     }
   }
   return format;
 }