use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.
the class ExtractionDimensionSpecTest method testSerdeBackwardsCompatibility.
@Test
public void testSerdeBackwardsCompatibility() throws Exception {
final ObjectMapper objectMapper = new DefaultObjectMapper();
final String oldJson = "{\n" + " \"type\": \"extraction\",\n" + " \"outputName\": \"first3Letters\",\n" + " \"dimension\": \"myDim\"," + " \"dimExtractionFn\": {\n" + " \"type\": \"regex\",\n" + " \"expr\": \"(...).*\"\n" + " }\n" + "}";
final ExtractionDimensionSpec extractionDimensionSpec = (ExtractionDimensionSpec) objectMapper.readValue(oldJson, DimensionSpec.class);
Assert.assertEquals("first3Letters", extractionDimensionSpec.getOutputName());
Assert.assertEquals("myDim", extractionDimensionSpec.getDimension());
Assert.assertNotNull(extractionDimensionSpec.getExtractionFn());
Assert.assertTrue(extractionDimensionSpec.getExtractionFn() instanceof RegexDimExtractionFn);
Assert.assertEquals(extractionDimensionSpec, objectMapper.readValue(objectMapper.writeValueAsBytes(extractionDimensionSpec), DimensionSpec.class));
// new trumps old
final String oldAndNewJson = "{\n" + " \"type\": \"extraction\",\n" + " \"outputName\": \"first3Letters\",\n" + " \"dimension\": \"myDim\"," + " \"extractionFn\": {\n" + " \"type\": \"partial\",\n" + " \"expr\": \"(...).*\"\n" + " },\n" + " \"dimExtractionFn\": {\n" + " \"type\": \"regex\",\n" + " \"expr\": \"(...).*\"\n" + " }\n" + "}";
Assert.assertTrue(objectMapper.readValue(oldAndNewJson, DimensionSpec.class).getExtractionFn() instanceof MatchingDimExtractionFn);
}
use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.
the class FloatLastAggregationTest method testSerde.
@Test
public void testSerde() throws Exception {
DefaultObjectMapper mapper = new DefaultObjectMapper();
String floatSpecJson = "{\"type\":\"floatLast\",\"name\":\"billy\",\"fieldName\":\"nilly\"}";
AggregatorFactory deserialized = mapper.readValue(floatSpecJson, AggregatorFactory.class);
Assert.assertEquals(floatLastAggregatorFactory, deserialized);
Assert.assertArrayEquals(floatLastAggregatorFactory.getCacheKey(), deserialized.getCacheKey());
}
use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.
the class ResponseContextTest method serializeWithTruncateValueTest.
@Test
public void serializeWithTruncateValueTest() throws IOException {
final ResponseContext ctx = ResponseContext.createEmpty();
ctx.put(EXTN_COUNTER_KEY, 100L);
ctx.put(EXTN_STRING_KEY, "long-string-that-is-supposed-to-be-removed-from-result");
final DefaultObjectMapper objectMapper = new DefaultObjectMapper();
final ResponseContext.SerializationResult res1 = ctx.serializeWith(objectMapper, Integer.MAX_VALUE);
Assert.assertEquals(ctx.getDelegate(), deserializeContext(res1.getResult(), objectMapper));
final ResponseContext ctxCopy = ResponseContext.createEmpty();
ctxCopy.merge(ctx);
final int target = EXTN_COUNTER_KEY.getName().length() + 3 + Keys.TRUNCATED.getName().length() + 5 + // Fudge factor for quotes, separators, etc.
15;
final ResponseContext.SerializationResult res2 = ctx.serializeWith(objectMapper, target);
ctxCopy.remove(EXTN_STRING_KEY);
ctxCopy.put(Keys.TRUNCATED, true);
Assert.assertEquals(ctxCopy.getDelegate(), deserializeContext(res2.getResult(), objectMapper));
}
use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.
the class ResponseContextTest method serializeWithCorrectnessTest.
@Test
public void serializeWithCorrectnessTest() throws JsonProcessingException {
final ResponseContext ctx1 = ResponseContext.createEmpty();
ctx1.add(EXTN_STRING_KEY, "string-value");
final DefaultObjectMapper mapper = new DefaultObjectMapper();
Assert.assertEquals(mapper.writeValueAsString(ImmutableMap.of(EXTN_STRING_KEY.getName(), "string-value")), ctx1.serializeWith(mapper, Integer.MAX_VALUE).getResult());
final ResponseContext ctx2 = ResponseContext.createEmpty();
// Add two non-header fields, and one that will be in the header
ctx2.putEntityTag("not in header");
ctx2.addCpuNanos(100);
ctx2.add(EXTN_COUNTER_KEY, 100);
Assert.assertEquals(mapper.writeValueAsString(ImmutableMap.of(EXTN_COUNTER_KEY.getName(), 100)), ctx2.serializeWith(mapper, Integer.MAX_VALUE).getResult());
}
use of org.apache.druid.jackson.DefaultObjectMapper in project druid by druid-io.
the class ResponseContextTest method deserializeTest.
@Test
public void deserializeTest() throws IOException {
final DefaultObjectMapper mapper = new DefaultObjectMapper();
final ResponseContext ctx = ResponseContext.deserialize(mapper.writeValueAsString(ImmutableMap.of(Keys.ETAG.getName(), "string-value", Keys.NUM_SCANNED_ROWS.getName(), 100L, Keys.CPU_CONSUMED_NANOS.getName(), 100000L)), mapper);
Assert.assertEquals("string-value", ctx.getEntityTag());
Assert.assertEquals((Long) 100L, ctx.getRowScanCount());
Assert.assertEquals((Long) 100000L, ctx.getCpuNanos());
ctx.addRowScanCount(10L);
Assert.assertEquals((Long) 110L, ctx.getRowScanCount());
ctx.addCpuNanos(100L);
Assert.assertEquals((Long) 100100L, ctx.getCpuNanos());
}
Aggregations