KibanaService { public void importObjects() { var watch = new StopWatch(); try { logger.info("import kibana objects, banner={}", banner); String objects = ClasspathResources.text("kibana.json").replace("${NOTIFICATION_BANNER}", banner); var request = new HTTPRequest(HTTPMethod.POST, kibanaURL + "/api/saved_objects/_bulk_create?overwrite=true"); request.headers.put("kbn-xsrf", "true"); request.body(objects, ContentType.APPLICATION_JSON); HTTPResponse response = client.execute(request); if (response.statusCode == 200) { logger.info("kibana objects are imported, elapsed={}", watch.elapsed()); } else { logger.warn("failed to import kibana objects, status={}, responseText={}", response.statusCode, response.text()); } } catch (Throwable e) { logger.error("failed to connect to kibana", e); } } KibanaService(String kibanaURL, String banner, HTTPClient client); void importObjects();  }
@Test void importObjects() { when(httpClient.execute(argThat(request -> { assertThat(request.headers).containsEntry("kbn-xsrf", "true"); assertThat(new String(request.body, StandardCharsets.UTF_8)).doesNotContain("${NOTIFICATION_BANNER}"); return true; }))).thenReturn(new HTTPResponse(200, Map.of(), Strings.bytes("acknowledged"))); service.importObjects(); }
@Test void failedToConnectToKibana() { when(httpClient.execute(any(HTTPRequest.class))).thenThrow(new HTTPClientException("failed to connect", "HTTP_REQUEST_FAILED", null)); service.importObjects(); }
@Test void failedToImportObjects() { when(httpClient.execute(any(HTTPRequest.class))) .thenReturn(new HTTPResponse(400, Map.of(), new byte[0])); service.importObjects(); }