ElasticSearchImageService extends DefaultService implements ImageStorageService { @VisibleForTesting static ObjectMapper getImageObjectMapper(JsonSerializer<ImageRecord> serializer) { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); SimpleModule module = new SimpleModule(); module.addSerializer(ImageRecord.class, serializer); module.addDeserializer(ImageRecord.class, new ImageRecord.Deserializer()); mapper.registerModule(module); return mapper; } @Inject protected  ElasticSearchImageService(SystemConfiguration config,MonitorService monitorService, ElasticSearchUtils esUtils); @Override void putImage(String imageId,byte[] imageBytes, boolean sync); @Override byte[] getImage(String imageId); String extractResponse(Response response); @Override void dispose(); static String imageIndexTemplatePatternStart; static String imageIndexType; }
@Test public void testImageRecordMapper() throws IOException { ImageRecord.IndexSerializer imageIndexSerializer= new ImageRecord.IndexSerializer(); mapper = ElasticSearchImageService.getImageObjectMapper(imageIndexSerializer); String byteString = "Test string"; String imageId = ImageUtils.convertBytesToMd5Hash(byteString.getBytes()); ImageRecord imageRecord = new ImageRecord(imageId,byteString.getBytes()); String serialized = mapper.writeValueAsString(imageRecord); String[] lines = serialized.split("\\r?\\n"); String expectedIndexName=imageIndexSerializer.getImageIndex(System.currentTimeMillis()); String expectedDocumentId = ImageUtils.convertBytesToMd5Hash(imageRecord.getImageBytes()); String expectedImageBlob = ImageUtils.encodeBytesToBase64(imageRecord.getImageBytes()); JsonNode line1 = mapper.readTree(lines[0]); String actualIndexName = line1.get("index").get("_index").asText(); String actualDocumentId = line1.get("index").get("_id").asText(); assertEquals(expectedIndexName, actualIndexName); assertEquals(expectedDocumentId, actualDocumentId); JsonNode line2 = mapper.readTree(lines[1]); String actualImageBlob = line2.get("imageblob").asText(); assertEquals(expectedImageBlob, actualImageBlob); }