ElasticSearchAnnotationService extends DefaultService implements AnnotationStorageService { @VisibleForTesting static ObjectMapper getAnnotationObjectMapper(JsonSerializer<AnnotationRecordList> serializer) { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); SimpleModule module = new SimpleModule(); module.addSerializer(AnnotationRecordList.class, serializer); module.addDeserializer(AnnotationRecordList.class, new AnnotationRecordList.Deserializer()); mapper.registerModule(module); return mapper; } @Inject  ElasticSearchAnnotationService(SystemConfiguration config, MonitorService monitorService, ElasticSearchUtils esUtils); @Override void dispose(); @Override void putAnnotations(List<Annotation> annotations); @Override List<Annotation> getAnnotations(List<AnnotationQuery> queries); static final int ANNOTATION_INDEX_MAX_RESULT_WINDOW; static String ANNOTATION_INDEX_TEMPLATE_PATTERN_START; }
@Test public void testAnnotationRecordListMapper() throws IOException { mapper = ElasticSearchAnnotationService.getAnnotationObjectMapper(new AnnotationRecordList.IndexSerializer()); Annotation record1 = new Annotation("unittest", "id123", "unittest", "scope1", "metric1", 1557801635504L); AnnotationRecordList recordList = new AnnotationRecordList(Arrays.asList(record1), AnnotationRecordList.HashAlgorithm.fromString("MD5")); String serialized = mapper.writeValueAsString(recordList); String[] lines = serialized.split("\\r?\\n"); String expectedIndexName = "argus-annotation-2019-05"; JsonNode root = mapper.readTree(lines[0]); String actualIndexName = root.get("index").get("_index").asText(); assertEquals(expectedIndexName, actualIndexName); String expectedSerializedAnnotation = "{\"metric\":\"metric1\",\"scope\":\"scope1\",\"source\":\"unittest\",\"id\":\"id123\",\"type\":\"unittest\",\"fields\":\"{}\",\"tags\":\"{}\",\"sid\":\"f9c22bcbd813474ec99f7011ae50b080\",\"ts\":\"1557801635504\"}"; assertEquals(expectedSerializedAnnotation, lines[1]); }