/** * Create a new schema loader with a given loading configuration * * @param cfg the configuration * @see LoadingConfiguration * @see LoadingConfigurationBuilder */ public SchemaLoader(final LoadingConfiguration cfg) { translator = new URITranslator(cfg.getTranslatorConfiguration()); dereferencing = cfg.getDereferencing(); manager = new URIManager(cfg); preloadedSchemas = ImmutableMap.copyOf(cfg.getPreloadedSchemas()); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (cfg.getCacheSize() != -1) { builder.maximumSize(cfg.getCacheSize()); } cache = builder.build(new CacheLoader<URI, JsonNode>() { @Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } }); }
/** * Create a new schema loader with a given loading configuration * * @param cfg the configuration * @see LoadingConfiguration * @see LoadingConfigurationBuilder */ public SchemaLoader(final LoadingConfiguration cfg) { translator = new URITranslator(cfg.getTranslatorConfiguration()); dereferencing = cfg.getDereferencing(); manager = new URIManager(cfg); preloadedSchemas = ImmutableMap.copyOf(cfg.getPreloadedSchemas()); CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder(); if (cfg.getCacheSize() != -1) { builder.maximumSize(cfg.getCacheSize()); } cache = builder.build(new CacheLoader<URI, JsonNode>() { @Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } }); }
/** * Create a new schema loader with a given loading configuration * * @param cfg the configuration * @see LoadingConfiguration * @see LoadingConfigurationBuilder */ public SchemaLoader(final LoadingConfiguration cfg) { translator = new URITranslator(cfg.getTranslatorConfiguration()); dereferencing = cfg.getDereferencing(); manager = new URIManager(cfg); preloadedSchemas = ImmutableMap.copyOf(cfg.getPreloadedSchemas()); final CacheBuilder<Object, Object> cacheBuilder = cfg.getEnableCache() ? CacheBuilder.newBuilder() : CacheBuilder.from(CacheBuilderSpec.disableCaching()); cache = cacheBuilder.build(new CacheLoader<URI, JsonNode>() { @Nonnull @Override public JsonNode load(@Nonnull final URI key) throws ProcessingException { return manager.getContent(key); } }); }
@Test public void nonJSONInputShouldBeReportedAsSuch() throws IOException { final URI uri = URI.create("foo://bar"); final InputStream sampleStream = new ByteArrayInputStream("}".getBytes()); when(mock.fetch(any(URI.class))).thenReturn(sampleStream); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", mock).freeze(); final URIManager manager = new URIManager(cfg); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("uriManager.uriNotJson", uri)) .hasTextField("parsingMessage").hasLevel(LogLevel.FATAL) .hasField("uri", uri); } }
@Test public void downloaderProblemsShouldBeReportedAsSuch() throws IOException { final URI uri = URI.create("foo://bar"); final Exception foo = new IOException("foo"); when(mock.fetch(any(URI.class))).thenThrow(foo); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", mock).freeze(); final URIManager manager = new URIManager(cfg); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("uriManager.uriIOError", uri)) .hasField("uri", uri).hasLevel(LogLevel.FATAL) .hasField("exceptionMessage", "foo"); } }
@Test public void unhandledSchemeShouldBeReportedAsSuch() { final URI uri = URI.create("bar://baz"); final URIManager manager = new URIManager(); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.unhandledScheme", "bar", uri)) .hasField("scheme", "bar").hasField("uri", uri) .hasLevel(LogLevel.FATAL); } }
@Test void managerParsesNonstandardJSON() throws IOException, ProcessingException { // get resource URIs for standard and nonstandard sources final String wellFormed = "resource:/load/standard-source.json"; final URI uri1 = JsonRef.fromString(wellFormed).getLocator(); final String illFormed = "resource:/load/nonstandard-source.json"; final URI uri2 = JsonRef.fromString(illFormed).getLocator(); // get URIManager configured to parse nonstandard sources final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addParserFeature(JsonParser.Feature.ALLOW_COMMENTS) .addParserFeature(JsonParser.Feature.ALLOW_SINGLE_QUOTES) .addParserFeature(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES) .freeze(); final URIManager manager = new URIManager(cfg); // load JSON nodes from sources using nonstandard manager final JsonNode node1 = manager.getContent(uri1); final JsonNode node2 = manager.getContent(uri2); // validate correctness of loaded equivalent sources assertTrue(JsonNumEquals.getInstance().equivalent(node1, node2)); } }