@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(); }
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; }
/** 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 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 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 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()); }