ElasticSearchImageService extends DefaultService implements ImageStorageService { @Override public void putImage(String imageId,byte[] imageBytes, boolean sync) { try { ImageRecord indexImageRecord = new ImageRecord(imageId,imageBytes); String requestBody = imageMapper.writeValueAsString(indexImageRecord); Request request = new Request(ElasticSearchUtils.HttpMethod.POST.getName(), PUT_REQUEST_URL); request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON)); if (sync) { performESRequest(request); } else{ performAsyncESRequest(request); } } catch (IOException e) { logger.error("Failed to index image record to ES.",e); throw new SystemException("Failed to index image record to ES.", e); } } @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 testPutImageIndex() throws IOException { ElasticSearchImageService spyService = _initializeSpyService(elasticSearchImageService, createSucessReply, createSucessReply); String byteString = "Test string"; String imageId = ImageUtils.convertBytesToMd5Hash(byteString.getBytes()); spyService.putImage(imageId,byteString.getBytes(),true); ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); verify(restClient, times(1)).performRequest(requestCaptor.capture()); Request capturedRequest = requestCaptor.getValue(); String indexJson = EntityUtils.toString(capturedRequest.getEntity()); String expectedURL = "_bulk"; assertEquals(expectedURL, capturedRequest.getEndpoint()); String[] lines = indexJson.split("\\r?\\n"); ImageRecord.IndexSerializer imageIndexSerializer= new ImageRecord.IndexSerializer(); String id = ImageUtils.convertBytesToMd5Hash(byteString.getBytes()); ImageRecord imageRecord = new ImageRecord(id,byteString.getBytes()); 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); }