### Question:
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();  }### Answer:
@Test(expected = IOException.class) public void createIndexThrowsException_forServerFailure() throws Exception { mockServerClient.when(request().withPath("/" + INDEX_NAME)) .error(HttpError.error().withDropConnection(true)); connector.createIndex(INDEX_NAME); }
@Test(expected = IOException.class) public void createIndexThrowsException_whenBadResponse() throws Exception { mockServerClient.when(request().withPath("/" + INDEX_NAME)) .respond(response().withStatusCode(500)); connector.createIndex(INDEX_NAME); }
@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)); }
@Test public void createIndexReturnsTrue_whenCreationSucceeds() 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("{\"acknowledged\":true,\"shards_acknowledged\":true,\"index\":\"rre\"}")); assertThat(connector.createIndex(INDEX_NAME)).isTrue(); mockServerClient.verify( request() .withMethod(HttpPut.METHOD_NAME) .withPath("/" + INDEX_NAME) .withBody(requestBody)); }