@Test public void createIndexReturnsFalse_whenCreationFails() throws Exception { final String requestBody = ConnectorUtils.readConfig(ConnectorUtils.getStreamForMappingsFile(MappingTypeElasticsearchConnector.MAPPINGS_FILE).get()); mockServerClient.when(request().withPath("/" + INDEX_NAME).withMethod(HttpPut.METHOD_NAME)) .respond(response().withBody("{\"error\":" + "{\"root_cause\":" + "[{\"type\":\"resource_already_exists_exception\"," + "\"reason\":\"index [rre/gLXTrMZMQ5S4mav9gdTXtA] already exists\"," + "\"index_uuid\":\"gLXTrMZMQ5S4mav9gdTXtA\"," + "\"index\":\"rre\"}]," + "\"type\":\"resource_already_exists_exception\"," + "\"reason\":\"index [rre/gLXTrMZMQ5S4mav9gdTXtA] already exists\"," + "\"index_uuid\":\"gLXTrMZMQ5S4mav9gdTXtA\"," + "\"index\":\"rre\"}," + "\"status\":400}")); assertThat(connector.createIndex(INDEX_NAME)).isFalse(); mockServerClient.verify( request() .withMethod(HttpPut.METHOD_NAME) .withPath("/" + INDEX_NAME) .withBody(requestBody)); }
@Override public boolean createIndex(String index) throws IOException { final boolean ret; final String configJson = ConnectorUtils.readConfig( ConnectorUtils.getStreamForMappingsFile(MAPPINGS_FILE) .orElseThrow(() -> new IOException("Configuration file " + MAPPINGS_FILE + " not available!"))); try { final Request createRequest = new Request(HttpPut.METHOD_NAME, "/" + index); createRequest.setJsonEntity(configJson); Response response = client.getLowLevelClient().performRequest(createRequest); final Map jsonMap = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Map.class); if (jsonMap.containsKey("acknowledged")) { ret = Boolean.valueOf(jsonMap.get("acknowledged").toString()); } else { ret = false; } } catch (ElasticsearchException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getDetailedMessage()); throw new IOException(e); } catch (ResponseException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getResponse().getStatusLine()); throw new IOException(e); } return ret; }
MappingTypeElasticsearchConnector implements ElasticsearchConnector { @Override public boolean createIndex(String index) throws IOException { final boolean ret; final String configJson = ConnectorUtils.readConfig( ConnectorUtils.getStreamForMappingsFile(MAPPINGS_FILE) .orElseThrow(() -> new IOException("Configuration file " + MAPPINGS_FILE + " not available!"))); try { final Request createRequest = new Request(HttpPut.METHOD_NAME, "/" + index); createRequest.setJsonEntity(configJson); Response response = client.getLowLevelClient().performRequest(createRequest); final Map jsonMap = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Map.class); if (jsonMap.containsKey("acknowledged")) { ret = Boolean.valueOf(jsonMap.get("acknowledged").toString()); } else { ret = false; } } catch (ElasticsearchException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getDetailedMessage()); throw new IOException(e); } catch (ResponseException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getResponse().getStatusLine()); throw new IOException(e); } return ret; } }
MappingTypeElasticsearchConnector implements ElasticsearchConnector { @Override public boolean createIndex(String index) throws IOException { final boolean ret; final String configJson = ConnectorUtils.readConfig( ConnectorUtils.getStreamForMappingsFile(MAPPINGS_FILE) .orElseThrow(() -> new IOException("Configuration file " + MAPPINGS_FILE + " not available!"))); try { final Request createRequest = new Request(HttpPut.METHOD_NAME, "/" + index); createRequest.setJsonEntity(configJson); Response response = client.getLowLevelClient().performRequest(createRequest); final Map jsonMap = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Map.class); if (jsonMap.containsKey("acknowledged")) { ret = Boolean.valueOf(jsonMap.get("acknowledged").toString()); } else { ret = false; } } catch (ElasticsearchException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getDetailedMessage()); throw new IOException(e); } catch (ResponseException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getResponse().getStatusLine()); throw new IOException(e); } return ret; } MappingTypeElasticsearchConnector(RestHighLevelClient client); }
MappingTypeElasticsearchConnector implements ElasticsearchConnector { @Override public boolean createIndex(String index) throws IOException { final boolean ret; final String configJson = ConnectorUtils.readConfig( ConnectorUtils.getStreamForMappingsFile(MAPPINGS_FILE) .orElseThrow(() -> new IOException("Configuration file " + MAPPINGS_FILE + " not available!"))); try { final Request createRequest = new Request(HttpPut.METHOD_NAME, "/" + index); createRequest.setJsonEntity(configJson); Response response = client.getLowLevelClient().performRequest(createRequest); final Map jsonMap = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Map.class); if (jsonMap.containsKey("acknowledged")) { ret = Boolean.valueOf(jsonMap.get("acknowledged").toString()); } else { ret = false; } } catch (ElasticsearchException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getDetailedMessage()); throw new IOException(e); } catch (ResponseException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getResponse().getStatusLine()); throw new IOException(e); } return ret; } MappingTypeElasticsearchConnector(RestHighLevelClient client); @Override boolean isAvailable(); @Override boolean indexExists(String index); @Override boolean createIndex(String index); @Override void storeItems(String index, Collection<QueryVersionReport> reports); @Override void close(); }
MappingTypeElasticsearchConnector implements ElasticsearchConnector { @Override public boolean createIndex(String index) throws IOException { final boolean ret; final String configJson = ConnectorUtils.readConfig( ConnectorUtils.getStreamForMappingsFile(MAPPINGS_FILE) .orElseThrow(() -> new IOException("Configuration file " + MAPPINGS_FILE + " not available!"))); try { final Request createRequest = new Request(HttpPut.METHOD_NAME, "/" + index); createRequest.setJsonEntity(configJson); Response response = client.getLowLevelClient().performRequest(createRequest); final Map jsonMap = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Map.class); if (jsonMap.containsKey("acknowledged")) { ret = Boolean.valueOf(jsonMap.get("acknowledged").toString()); } else { ret = false; } } catch (ElasticsearchException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getDetailedMessage()); throw new IOException(e); } catch (ResponseException e) { LOGGER.warn("Caught ResponseException creating index {}: {}", index, e.getResponse().getStatusLine()); throw new IOException(e); } return ret; } MappingTypeElasticsearchConnector(RestHighLevelClient client); @Override boolean isAvailable(); @Override boolean indexExists(String index); @Override boolean createIndex(String index); @Override void storeItems(String index, Collection<QueryVersionReport> reports); @Override void close();  }