ElasticSearchSchemaService extends AbstractSchemaService { @VisibleForTesting static String doExtractResponse(int statusCode, HttpEntity entity) { String message = null; if (entity != null) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { entity.writeTo(baos); message = baos.toString("UTF-8"); } catch (IOException ex) { throw new SystemException(ex); } } if (statusCode>= HttpStatus.SC_BAD_REQUEST && statusCode < HttpStatus.SC_INTERNAL_SERVER_ERROR) { throw new IllegalArgumentException("Status code: " + statusCode + " . Error occurred. " + message); } if ((statusCode < HttpStatus.SC_OK) || (statusCode >= HttpStatus.SC_MULTIPLE_CHOICES)) { throw new SystemException("Status code: " + statusCode + " . Error occurred. " + message); } else { return message; } } @Inject  ElasticSearchSchemaService(SystemConfiguration config, MonitorService monitorService, ElasticSearchUtils esUtils); @Override void dispose(); @Override Properties getServiceProperties(); @Override List<MetricSchemaRecord> get(MetricSchemaRecordQuery query); @Override List<String> browseUnique(MetricSchemaRecordQuery query, RecordType type, int indexLevel); @Override List<MetricSchemaRecord> getUnique(MetricSchemaRecordQuery query, RecordType type); @Override List<MetricSchemaRecord> keywordSearch(KeywordQuery kq);  }
@Test public void testDoExtractResponse() throws Exception { final String message = "this is a test"; BasicHttpEntity entity = new BasicHttpEntity(); try(ByteArrayInputStream bis = new ByteArrayInputStream(message.getBytes())) { entity.setContent(bis); } catch (IOException e) { throw e; } String responseMessage = ElasticSearchSchemaService.doExtractResponse(200, entity); assertEquals("expect the entity to be equal after extraction", message, responseMessage); }
@Test public void testDoExtractResponse400() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Status code: 400"); ElasticSearchSchemaService.doExtractResponse(400, null); }
@Test public void testDoExtractResponse500() { expectedException.expect(SystemException.class); expectedException.expectMessage("Status code: 500"); ElasticSearchSchemaService.doExtractResponse(500, null); }