### Question:
IndexOnlyElasticsearchConnector 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!"))); CreateIndexRequest request = new CreateIndexRequest(index) .source(configJson, XContentType.JSON); try { ret = client.indices().create(request, RequestOptions.DEFAULT).isAcknowledged(); } catch (ElasticsearchException e) { LOGGER.error("Caught ElasticsearchException creating index {} :: {}", index, e.getDetailedMessage()); throw new IOException(e); } return ret; } IndexOnlyElasticsearchConnector(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(expected = IOException.class) public void createIndexThrowsException_whenCreationReturnsError() throws Exception { final String requestBody = ConnectorUtils.readConfig(ConnectorUtils.getStreamForMappingsFile(IndexOnlyElasticsearchConnector.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}", MediaType.JSON_UTF_8)); assertThat(connector.createIndex(INDEX_NAME)).isFalse(); mockServerClient.verify( request() .withMethod(HttpPut.METHOD_NAME) .withPath("/" + INDEX_NAME) .withBody(requestBody)); }
@Test public void createIndexReturnsFalse_whenCreationFails() throws Exception { mockServerClient.when(request().withPath("/" + INDEX_NAME).withMethod(HttpPut.METHOD_NAME)) .respond(response().withBody("{\"acknowledged\":false,\"shards_acknowledged\":false,\"index\":\"rre\"}", MediaType.JSON_UTF_8)); assertThat(connector.createIndex(INDEX_NAME)).isFalse(); mockServerClient.verify( request() .withMethod(HttpPut.METHOD_NAME) .withPath("/" + INDEX_NAME)); }
@Test public void createIndexReturnsTrue_whenCreationSucceeds() throws Exception { mockServerClient.when(request().withPath("/" + INDEX_NAME).withMethod(HttpPut.METHOD_NAME)) .respond(response().withBody("{\"acknowledged\":true,\"shards_acknowledged\":true,\"index\":\"rre\"}", MediaType.JSON_UTF_8)); assertThat(connector.createIndex(INDEX_NAME)).isTrue(); mockServerClient.verify( request() .withMethod(HttpPut.METHOD_NAME) .withPath("/" + INDEX_NAME)); }