@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()); }
/** must fail because value is a {@link #delta} larger than constraint */ @Test public void testMaxConstraintGreaterExplicit() { final CurrencyModel curr = createCurrencyForValidation(testedValue + delta); modelService.save(curr); assertEquals( BigDecimal.valueOf(curr.getConversion().doubleValue()), BigDecimal.valueOf(testedValue + delta)); createDecimalMaxConstraint(); assertFalse(validationService.validate(curr).isEmpty()); }
/** Test localized validation messages for attribute based validation. */ @Test public void testAttributeConstraintLocalization() { // create a DigitConstraint which defines format: integer=1, fraction=2 final DigitsConstraintModel constraint = modelService.create(DigitsConstraintModel.class); final AttributeDescriptorModel attribute = typeService.getAttributeDescriptor( typeService.getComposedType(CurrencyModel.class), CurrencyModel.CONVERSION); // constraint.setQualifier("conversion"); constraint.setTarget(CurrencyModel.class); constraint.setDescriptor(attribute); constraint.setId("digitConstraint"); constraint.setInteger(Integer.valueOf(1)); constraint.setFraction(Integer.valueOf(2)); constraint.setMessage( "Type {type} fails validation at property {field}. Valid format: integer={integer}, fractions={fraction}", Locale.UK); constraint.setMessage( "Typ {type} ist nicht valide bei {field}. Valides format: {integer}.{fraction}", Locale.GERMANY); modelService.save(constraint); validationService.reloadValidationEngine(); // create a CurrencyModel which gets validated final CurrencyModel currency = modelService.create(CurrencyModel.class); currency.setConversion(Double.valueOf(11.11)); currency.setIsocode("ERN"); // assert english and german localized messages // both are taken from 'message' attribute // placeholder {type} is processed assertLocalization( Locale.GERMAN, "Typ CurrencyModel ist nicht valide bei conversion. Valides format: 1.2", currency); assertLocalization( Locale.ENGLISH, "Type CurrencyModel fails validation at property conversion. Valid format: integer=1, fractions=2", currency); // nullify localized message for UK // this enabled ResourceBundle fallback constraint.setMessage(null, Locale.UK); // same as above but english localization is now taken from resource bundle assertLocalization( Locale.GERMAN, "Typ CurrencyModel ist nicht valide bei conversion. Valides format: 1.2", currency); assertLocalization( Locale.ENGLISH, "The attribute \"conversion\" has an invalid numeric syntax (<1 digits>.<2 digits> expected).", currency); }
@Test public void testGetSupportedJavaCurrencies() { final List<CurrencyModel> currencies = new ArrayList<CurrencyModel>(); final CurrencyModel currency1 = new CurrencyModel(); currency1.setIsocode("USD"); final CurrencyModel currency2 = new CurrencyModel(); currency2.setIsocode("EUR"); currencies.add(currency1); currencies.add(currency2); when(currencyDao.findCurrencies()).thenReturn(currencies); final List<Currency> supportedJavaCurrencies = new ArrayList<Currency>(i18NService.getSupportedJavaCurrencies()); assertEquals( "Wrong number of supported java currencies! Should be: '" + currencies.size() + "' but was: '" + supportedJavaCurrencies.size() + "'.", currencies.size(), supportedJavaCurrencies.size()); assertEquals( "Wrong number of supported java currencies! Should be: '" + NUMBER_OF_ELEMENTS + "' but was: '" + supportedJavaCurrencies.size() + "'.", NUMBER_OF_ELEMENTS, supportedJavaCurrencies.size()); assertEquals( "Wrong current java currency isocode! Should be: '" + currencies.get(0).getIsocode() + "' but was: '" + supportedJavaCurrencies.get(0).getCurrencyCode() + "'.", currencies.get(0).getIsocode(), supportedJavaCurrencies.get(0).getCurrencyCode()); assertEquals( "Wrong current java currency isocode! Should be: '" + currencies.get(1).getIsocode() + "' but was: '" + supportedJavaCurrencies.get(1).getCurrencyCode() + "'.", currencies.get(1).getIsocode(), supportedJavaCurrencies.get(1).getCurrencyCode()); verify(currencyDao, times(1)).findCurrencies(); }
@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()); }
/** Adjusts {@link DecimalFormat}'s fraction digits according to given {@link CurrencyModel}. */ protected DecimalFormat adjustDigits( final DecimalFormat format, final CurrencyModel currencyModel) { final int tempDigits = currencyModel.getDigits() == null ? 0 : currencyModel.getDigits().intValue(); final int digits = Math.max(0, tempDigits); format.setMaximumFractionDigits(digits); format.setMinimumFractionDigits(digits); if (digits == 0) { format.setDecimalSeparatorAlwaysShown(false); } return format; }
@Test public void testGetCurrentJavaCurrencyWithNonExistentJavaCurrency() { final CurrencyModel currency1 = new CurrencyModel(); currency1.setIsocode("BLABLA"); when(sessionService.getAttribute(I18NConstants.CURRENCY_SESSION_ATTR_KEY)) .thenReturn(currency1); try { i18NService.getCurrentJavaCurrency(); fail(); } catch (final ConfigurationException e) { // ok } verify(sessionService, times(1)).getAttribute(Mockito.anyString()); }
@Test public void testSetCurrentJavaCurrency() // NOPMD the assert(), fail() methods do not occur because the // sessionService.setAttribute doing all the work { final CurrencyModel currency1 = new CurrencyModel(); currency1.setIsocode("USD"); final List<CurrencyModel> currencyList = new ArrayList<CurrencyModel>(Collections.singletonList(currency1)); when(currencyDao.findCurrenciesByCode("USD")).thenReturn(currencyList); Currency.getInstance("USD"); i18NService.setCurrentJavaCurrency(Currency.getInstance("USD")); verify(currencyDao, times(1)).findCurrenciesByCode("USD"); verify(sessionService, times(1)) .setAttribute(I18NConstants.CURRENCY_SESSION_ATTR_KEY, currency1); }
@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()); }
@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 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()); }
/** 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; }
/** * @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(); }
private CurrencyModel createCurrencyForValidation(final double value) { final CurrencyModel curr = modelService.create(CurrencyModel.class); curr.setIsocode("curr"); curr.setActive(Boolean.TRUE); curr.setConversion(Double.valueOf(value)); curr.setSymbol("CUR"); curr.setDigits(Integer.valueOf(7)); return curr; }
@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; }