ElasticSearchImageService extends DefaultService implements ImageStorageService { @Override public byte[] getImage(String imageId) { requireNotDisposed(); requireArgument(imageId != null, "imageId cannot be null"); ImageRecord record=null; try{ String queryJson = constructSimpleIdSearchQuery(imageId).toString(); final long start = System.currentTimeMillis(); Request request = new Request(ElasticSearchUtils.HttpMethod.POST.getName(), imageSearchRequestURL); request.setEntity(new StringEntity(queryJson, ContentType.APPLICATION_JSON)); Response response = esRestClient.performRequest(request); final long time = System.currentTimeMillis() - start; logger.info("ES get request completed in {} ms", time); String str = extractResponse(response); record = ElasticSearchUtils.toEntity(str, new TypeReference<ImageRecord>() {},imageMapper); } catch(IOException ex) { logger.error("Exception while retrieving the image",ex); throw new SystemException("Exception while retrieving the image",ex); } return record.getImageBytes(); } @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 testGetImage() throws IOException { ElasticSearchImageService spyService = _initializeSpyService(elasticSearchImageService, getReply, getReply); byte[] expectedBytes = "Test string".getBytes(); byte[] actualBytes = spyService.getImage("0FD3DBEC9730101BFF92ACC820BEFC34"); assertArrayEquals(expectedBytes, actualBytes); ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class); verify(restClient, times(1)).performRequest(requestCaptor.capture()); Request capturedRequest = requestCaptor.getValue(); String queryJson = convertToPrettyJson(EntityUtils.toString(capturedRequest.getEntity())); assertEquals(imageIdQuery, queryJson); String expectedURL = "/argus-image-*/_search"; assertEquals(expectedURL, capturedRequest.getEndpoint()); }