public static void main(final String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: <catalog filepath>"); System.exit(0); } File file = new File(args[0]); if(!file.exists()) { System.err.println("Error: '" + args[0] + "' does not exist"); } StandaloneCatalog catalog = XMLLoader.getObjectFromUri(file.toURI(), StandaloneCatalog.class); if (catalog != null) { System.out.println("Success: Catalog loads!"); } }
public static <T extends ValidatingConfig<T>> T getObjectFromString(final String uri, final Class<T> objectType) throws Exception { if (uri == null) { return null; } log.info("Initializing an object of class " + objectType.getName() + " from xml file at: " + uri); return getObjectFromStream(new URI(uri), UriAccessor.accessUri(uri), objectType); }
@Test(groups = "fast") public void testCatalogLoad() { try { XMLLoader.getObjectFromString(Resources.getResource("WeaponsHire.xml").toExternalForm(), StandaloneCatalog.class); XMLLoader.getObjectFromString(Resources.getResource("WeaponsHireSmall.xml").toExternalForm(), StandaloneCatalog.class); XMLLoader.getObjectFromString(Resources.getResource("SpyCarBasic.xml").toExternalForm(), StandaloneCatalog.class); } catch (Exception e) { Assert.fail(e.toString()); } } }
public static <T extends ValidatingConfig<T>> T getObjectFromStream(final URI uri, final InputStream stream, final Class<T> clazz) throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException, ValidationException { if (stream == null) { return null; } final Object o = unmarshaller(clazz).unmarshal(stream); if (clazz.isInstance(o)) { @SuppressWarnings("unchecked") final T castObject = (T) o; try { validate(uri, castObject); } catch (ValidationException e) { e.getErrors().log(log); System.err.println(e.getErrors().toString()); throw e; } return castObject; } else { return null; } }
@Test(groups = "fast") public void testTotalUnpaidInvoiceBalanceEqualsOrExceeds() throws Exception { final String xml = "<condition>" + " <totalUnpaidInvoiceBalanceEqualsOrExceeds>100.00</totalUnpaidInvoiceBalanceEqualsOrExceeds>" + "</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID(); final BillingState state0 = new BillingState(new UUID(0L, 1L), 0, BigDecimal.ZERO, new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state1 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("100.00"), new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state2 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("200.00"), new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); Assert.assertTrue(!c.evaluate(state0, new LocalDate())); Assert.assertTrue(c.evaluate(state1, new LocalDate())); Assert.assertTrue(c.evaluate(state2, new LocalDate())); }
public static <T> T getObjectFromStreamNoValidation(final InputStream stream, final Class<T> clazz) throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException { final Object o = unmarshaller(clazz).unmarshal(stream); if (clazz.isInstance(o)) { @SuppressWarnings("unchecked") final T castObject = (T) o; return castObject; } else { return null; } }
@Test(groups = "fast") public void testNumberOfUnpaidInvoicesEqualsOrExceeds() throws Exception { final String xml = "<condition>" + " <numberOfUnpaidInvoicesEqualsOrExceeds>1</numberOfUnpaidInvoicesEqualsOrExceeds>" + "</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID(); final BillingState state0 = new BillingState(new UUID(0L, 1L), 0, BigDecimal.ZERO, new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state1 = new BillingState(new UUID(0L, 1L), 1, BigDecimal.ZERO, new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state2 = new BillingState(new UUID(0L, 1L), 2, BigDecimal.ZERO, new LocalDate(), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); Assert.assertTrue(!c.evaluate(state0, new LocalDate())); Assert.assertTrue(c.evaluate(state1, new LocalDate())); Assert.assertTrue(c.evaluate(state2, new LocalDate())); }
final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(u, StandaloneCatalog.class); result.add(catalog);
public static <T extends ValidatingConfig<T>> T getObjectFromUri(final URI uri, final Class<T> objectType) throws Exception { if (uri == null) { return null; } log.info("Initializing an object of class " + objectType.getName() + " from xml file at: " + uri); return getObjectFromStream(uri, UriAccessor.accessUri(uri), objectType); }
@Test(groups = "fast") public void testTimeSinceEarliestUnpaidInvoiceEqualsOrExceeds() throws Exception { final String xml = "<condition>" + " <timeSinceEarliestUnpaidInvoiceEqualsOrExceeds><unit>DAYS</unit><number>10</number></timeSinceEarliestUnpaidInvoiceEqualsOrExceeds>" + "</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID(); final LocalDate now = new LocalDate(); final BillingState state0 = new BillingState(new UUID(0L, 1L), 0, BigDecimal.ZERO, null, DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state1 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("100.00"), now.minusDays(10), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state2 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("200.00"), now.minusDays(20), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); Assert.assertTrue(!c.evaluate(state0, now)); Assert.assertTrue(c.evaluate(state1, now)); Assert.assertTrue(c.evaluate(state2, now)); }
@Test(groups = "fast") public void testConfigLoad() throws Exception { XMLLoader.getObjectFromString(Resources.getResource("OverdueConfig.xml").toExternalForm(), OverdueConfig.class); } }
@Test(groups = "fast") public void test() throws Exception { final URI uri = new URI(Resources.getResource("WeaponsHireSmall.xml").toExternalForm()); final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(uri, StandaloneCatalog.class); Assert.assertNotNull(catalog); final PlanRules rules = catalog.getPlanRules(); final PlanSpecifier specifier = new PlanSpecifier("Laser-Scope", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, "DEFAULT"); final PlanAlignmentCreate alignment = rules.getPlanCreateAlignment(specifier, catalog); Assert.assertEquals(alignment, PlanAlignmentCreate.START_OF_SUBSCRIPTION); final PlanSpecifier specifier2 = new PlanSpecifier("Extra-Ammo", ProductCategory.ADD_ON, BillingPeriod.MONTHLY, "DEFAULT"); final PlanAlignmentCreate alignment2 = rules.getPlanCreateAlignment(specifier2, catalog); Assert.assertEquals(alignment2, PlanAlignmentCreate.START_OF_BUNDLE); } }
@Test(groups = "fast") public void test() throws Exception { final InputStream is = new ByteArrayInputStream(TEST_XML.getBytes()); final XmlTestClass test = XMLLoader.getObjectFromStream(new URI("internal:/"), is, XmlTestClass.class); assertEquals(test.getFoo(), "foo"); assertEquals(test.getBar(), 1.0); assertEquals(test.getLala(), 42); final String output = XMLWriter.writeXML(test, XmlTestClass.class); //System.out.println(output); assertEquals(output.replaceAll("\\s", ""), TEST_XML.replaceAll("\\s", "")); } }
@Test(groups = "fast") public void testResponseForLastFailedPaymentIn() throws Exception { final String xml = "<condition>" + " <responseForLastFailedPaymentIn><response>INSUFFICIENT_FUNDS</response><response>DO_NOT_HONOR</response></responseForLastFailedPaymentIn>" + "</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID(); final LocalDate now = new LocalDate(); final BillingState state0 = new BillingState(new UUID(0L, 1L), 0, BigDecimal.ZERO, null, DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.LOST_OR_STOLEN_CARD, new Tag[]{}); final BillingState state1 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("100.00"), now.minusDays(10), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.INSUFFICIENT_FUNDS, new Tag[]{}); final BillingState state2 = new BillingState(new UUID(0L, 1L), 1, new BigDecimal("200.00"), now.minusDays(20), DateTimeZone.UTC, unpaidInvoiceId, PaymentResponse.DO_NOT_HONOR, new Tag[]{}); Assert.assertTrue(!c.evaluate(state0, now)); Assert.assertTrue(c.evaluate(state1, now)); Assert.assertTrue(c.evaluate(state2, now)); }
@LifecycleHandlerType(LifecycleLevel.LOAD_CATALOG) public synchronized void loadConfig() throws ServiceException { if (!isConfigLoaded) { try { final URI u = new URI(properties.getConfigURI()); overdueConfig = XMLLoader.getObjectFromUri(u, OverdueConfig.class); // File not found? if (overdueConfig == null) { log.warn("Unable to load the overdue config from " + properties.getConfigURI()); overdueConfig = new OverdueConfig(); } isConfigLoaded = true; } catch (final URISyntaxException e) { overdueConfig = new OverdueConfig(); } catch (final IllegalArgumentException e) { overdueConfig = new OverdueConfig(); } catch (final Exception e) { throw new ServiceException(e); } factory.setOverdueConfig(overdueConfig); ((DefaultOverdueUserApi) userApi).setOverdueConfig(overdueConfig); } }
@Test(groups = "fast") public void test() throws SAXException, InvalidConfigException, JAXBException, IOException, TransformerException, URISyntaxException, ValidationException { final InputStream is = new ByteArrayInputStream(TEST_XML.getBytes()); final XmlTestClass test = XMLLoader.getObjectFromStream(new URI("internal:/"), is, XmlTestClass.class); assertEquals(test.getFoo(), "foo"); assertEquals(test.getBar(), 1.0); assertEquals(test.getLala(), 42); } }
@Test(groups = "slow") public void testWrapperNoConfig() throws Exception { overdueWrapperFactory.setOverdueConfig(null); final Account account; final OverdueWrapper wrapper; final OverdueState state; final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes()); final OverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); state = config.getStateSet().findState(DefaultBlockingState.CLEAR_STATE_NAME); account = testOverdueHelper.createAccount(clock.getUTCToday().minusDays(31)); wrapper = overdueWrapperFactory.createOverdueWrapperFor(account); final OverdueState result = wrapper.refresh(internalCallContext); Assert.assertEquals(result.getName(), state.getName()); Assert.assertEquals(result.blockChanges(), state.blockChanges()); Assert.assertEquals(result.disableEntitlementAndChangesBlocked(), state.disableEntitlementAndChangesBlocked()); } }
"</condition>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final MockCondition c = XMLLoader.getObjectFromStreamNoValidation(is, MockCondition.class); final UUID unpaidInvoiceId = UUID.randomUUID();
"</overdueConfig>"; final InputStream is = new ByteArrayInputStream(xml.getBytes()); final OverdueConfig c = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); Assert.assertEquals(c.getStateSet().size(), 2);
@Test(groups = "slow") public void testApplicator() throws Exception { final InputStream is = new ByteArrayInputStream(testOverdueHelper.getConfigXml().getBytes()); final OverdueConfig config = XMLLoader.getObjectFromStreamNoValidation(is, OverdueConfig.class); overdueWrapperFactory.setOverdueConfig(config); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(UUID.randomUUID()); final OverdueStateSet overdueStateSet = config.getStateSet(); final OverdueState clearState = config.getStateSet().findState(DefaultBlockingState.CLEAR_STATE_NAME); OverdueState state; state = config.getStateSet().findState("OD1"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD1"); state = config.getStateSet().findState("OD2"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD2"); state = config.getStateSet().findState("OD3"); applicator.apply(overdueStateSet, null, account, clearState, state, internalCallContext); testOverdueHelper.checkStateApplied(state); checkBussEvent("OD3"); }