@Override public ParseSpec withTimestampSpec(TimestampSpec spec) { return new JavaScriptParseSpec(spec, getDimensionsSpec(), function, config); }
@Override public ParseSpec withDimensionsSpec(DimensionsSpec spec) { return new JavaScriptParseSpec(getTimestampSpec(), spec, function, config); }
@Test public void testSerde() throws IOException { jsonMapper.setInjectableValues( new InjectableValues.Std().addValue( JavaScriptConfig.class, JavaScriptConfig.getEnabledInstance() ) ); JavaScriptParseSpec spec = new JavaScriptParseSpec( new TimestampSpec("abc", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Collections.singletonList("abc")), null, null), "abc", JavaScriptConfig.getEnabledInstance() ); final JavaScriptParseSpec serde = (JavaScriptParseSpec) jsonMapper.readValue( jsonMapper.writeValueAsString(spec), ParseSpec.class ); Assert.assertEquals("abc", serde.getTimestampSpec().getTimestampColumn()); Assert.assertEquals("iso", serde.getTimestampSpec().getTimestampFormat()); Assert.assertEquals("abc", serde.getFunction()); Assert.assertEquals(Collections.singletonList("abc"), serde.getDimensionsSpec().getDimensionNames()); }
@Test public void testMakeParser() { final JavaScriptConfig config = JavaScriptConfig.getEnabledInstance(); JavaScriptParseSpec spec = new JavaScriptParseSpec( new TimestampSpec("abc", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Collections.singletonList("abc")), null, null), "function(str) { var parts = str.split(\"-\"); return { one: parts[0], two: parts[1] } }", config ); final Parser<String, Object> parser = spec.makeParser(); final Map<String, Object> obj = parser.parseToMap("x-y"); Assert.assertEquals(ImmutableMap.of("one", "x", "two", "y"), obj); }
@Test public void testDisableJavaScript() { final JavaScriptParseSpec parseSpec = new JavaScriptParseSpec( new TimestampSpec("timestamp", "auto", null), new DimensionsSpec( DimensionsSpec.getDefaultSchemas( ImmutableList.of( "dim1", "dim2" ) ), null, null ), "func", new JavaScriptConfig(false) ); final StringInputRowParser parser = new StringInputRowParser(parseSpec, "UTF-8"); expectedException.expect(CoreMatchers.instanceOf(IllegalStateException.class)); expectedException.expectMessage("JavaScript is disabled"); parser.startFileFromBeginning(); }
@Test public void testMakeParserNotAllowed() { final JavaScriptConfig config = new JavaScriptConfig(false); JavaScriptParseSpec spec = new JavaScriptParseSpec( new TimestampSpec("abc", "iso", null), new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Collections.singletonList("abc")), null, null), "abc", config ); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("JavaScript is disabled"); spec.makeParser(); } }
@Test public void testDisableJavaScript2() { final JavaScriptParseSpec parseSpec = new JavaScriptParseSpec( new TimestampSpec("timestamp", "auto", null), new DimensionsSpec( DimensionsSpec.getDefaultSchemas( ImmutableList.of( "dim1", "dim2" ) ), null, null ), "func", new JavaScriptConfig(false) ); final StringInputRowParser parser = new StringInputRowParser(parseSpec, "UTF-8"); expectedException.expect(CoreMatchers.instanceOf(IllegalStateException.class)); expectedException.expectMessage("JavaScript is disabled"); parser.parse(""); } }
@Override public ParseSpec withTimestampSpec(TimestampSpec spec) { return new JavaScriptParseSpec(spec, getDimensionsSpec(), function, config); }
@Override public ParseSpec withDimensionsSpec(DimensionsSpec spec) { return new JavaScriptParseSpec(getTimestampSpec(), spec, function, config); }