From 8eb7aa656a4522339ccac9340e90bdb2746026c3 Mon Sep 17 00:00:00 2001 From: Andrew Parmet Date: Fri, 6 Oct 2023 16:21:52 -0400 Subject: [PATCH] fix tests Signed-off-by: Andrew Parmet --- .../integTest/AbstractClusterClientIT.java | 17 ++++-- .../opensearch/integTest/AbstractCrudIT.java | 34 ++++++++--- .../integTest/AbstractIndicesClientIT.java | 60 ++++++++++++++----- .../integTest/AbstractRequestIT.java | 22 ++++--- 4 files changed, 96 insertions(+), 37 deletions(-) diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java index e695f7cf87..c229d3d2b5 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractClusterClientIT.java @@ -93,10 +93,17 @@ public void testClusterUpdateSettingNonExistent() throws IOException { try { openSearchClient.cluster().putSettings(request); fail(); - } catch (OpenSearchException e) { - assertNotNull(e); - assertEquals(e.response().status(), 400); - assertTrue(e.getMessage().contains("transient setting [no_idea_what_you_are_talking_about], not recognized")); + } catch (Exception e) { + OpenSearchException openSearchException; + if (e instanceof OpenSearchException) { + openSearchException = (OpenSearchException) e; + } else { + assertTrue(e.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) e.getCause(); + } + + assertEquals(openSearchException.response().status(), 400); + assertTrue(openSearchException.getMessage().contains("transient setting [no_idea_what_you_are_talking_about], not recognized")); } } @@ -300,4 +307,4 @@ public void testClusterHealthNotFoundIndex() throws IOException { assertNotNull(e); } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java index dbf45cd4e6..f221472ab7 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractCrudIT.java @@ -142,13 +142,21 @@ public void testSourceExists() throws IOException { public void testGet() throws IOException { { - OpenSearchException exception = expectThrows( - OpenSearchException.class, + Exception exception = expectThrows( + Exception.class, () -> javaClient().get(new GetRequest.Builder().index("index").id("id").build(), String.class) ); - assertEquals(404, exception.status()); + OpenSearchException openSearchException; + if (exception instanceof OpenSearchException) { + openSearchException = (OpenSearchException) exception; + } else { + assertTrue(exception.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) exception.getCause(); + } + + assertEquals(404, openSearchException.status()); assertEquals("Request failed: [index_not_found_exception] no such index [index]", - exception.getMessage()); + openSearchException.getMessage()); } AppData appData = new AppData(); @@ -260,12 +268,20 @@ public void testUpdate() throws Exception { .build(); try { javaClient().update(updateRequest, AppData.class); - } catch (OpenSearchException e) { + } catch (Exception e) { + OpenSearchException openSearchException; + if (e instanceof OpenSearchException) { + openSearchException = (OpenSearchException) e; + } else { + assertTrue(e.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) e.getCause(); + } + // 1.x: [document_missing_exception] [_doc][does_not_exist]: document missing // 2.x: [document_missing_exception] [does_not_exist]: document missing - assertTrue(e.getMessage().contains("[document_missing_exception]")); - assertTrue(e.getMessage().contains("[does_not_exist]: document missing")); - assertEquals(404, e.status()); + assertTrue(openSearchException.getMessage().contains("[document_missing_exception]")); + assertTrue(openSearchException.getMessage().contains("[does_not_exist]: document missing")); + assertEquals(404, openSearchException.status()); } } { @@ -605,4 +621,4 @@ public void setMsg(String msg) { this.msg = msg; } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java index 869e63cc1c..0fc09a9fc3 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractIndicesClientIT.java @@ -53,10 +53,17 @@ public void testIndicesExists() throws IOException { try { javaClient().indices().get(request); fail(); // should never execute - } catch (OpenSearchException ex) { - assertNotNull(ex); - assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), + } catch (Exception ex) { + OpenSearchException openSearchException; + if (ex instanceof OpenSearchException) { + openSearchException = (OpenSearchException) ex; + } else { + assertTrue(ex.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) ex.getCause(); + } + + assertEquals(openSearchException.status(), 404); + assertEquals(openSearchException.getMessage(), "Request failed: [index_not_found_exception] " + "no such index [non_existent_index]"); } @@ -106,10 +113,17 @@ public void testGetSettingsNonExistentIndex() throws IOException { try { javaClient().indices().getSettings(getIndicesSettingsRequest); fail(); - } catch (OpenSearchException ex) { - assertNotNull(ex); - assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), + } catch (Exception ex) { + OpenSearchException openSearchException; + if (ex instanceof OpenSearchException) { + openSearchException = (OpenSearchException) ex; + } else { + assertTrue(ex.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) ex.getCause(); + } + + assertEquals(openSearchException.status(), 404); + assertEquals(openSearchException.getMessage(), "Request failed: [index_not_found_exception] " + "no such index [index_that_doesnt_exist]"); } @@ -185,9 +199,16 @@ public void testDataStream() throws IOException { try { javaClient().indices().getDataStream(b -> b.name(dataStreamName)); fail(); - } catch (OpenSearchException ex) { - assertNotNull(ex); - assertEquals(ex.status(), 404); + } catch (Exception ex) { + OpenSearchException openSearchException; + if (ex instanceof OpenSearchException) { + openSearchException = (OpenSearchException) ex; + } else { + assertTrue(ex.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) ex.getCause(); + } + + assertEquals(openSearchException.status(), 404); } } @@ -197,12 +218,19 @@ public void testGetNotExistingIndexAlias() throws Exception { try { GetAliasResponse response = javaClient().indices().getAlias(aliasRequest); fail(); - } catch (OpenSearchException ex) { - assertNotNull(ex); - assertEquals(ex.status(), 404); - assertEquals(ex.getMessage(), + } catch (Exception ex) { + OpenSearchException openSearchException; + if (ex instanceof OpenSearchException) { + openSearchException = (OpenSearchException) ex; + } else { + assertTrue(ex.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) ex.getCause(); + } + + assertEquals(openSearchException.status(), 404); + assertEquals(openSearchException.getMessage(), "Request failed: [string_error] " + "alias [alias_not_exists] missing"); } } -} \ No newline at end of file +} diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java index c52908bbcb..db0a3fc34c 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/AbstractRequestIT.java @@ -394,15 +394,23 @@ public void errorResponse() throws Exception { BooleanResponse exists = javaClient().exists(_0 -> _0.index("doesnotexist").id("reallynot")); assertFalse(exists.value()); - OpenSearchException ex = assertThrows(OpenSearchException.class, () -> { + Exception ex = assertThrows(Exception.class, () -> { GetResponse response = javaClient().get( _0 -> _0.index("doesnotexist").id("reallynot"), String.class ); }); - assertEquals(404, ex.status()); - assertEquals("index_not_found_exception", ex.error().type()); - assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class)); + OpenSearchException openSearchException; + if (ex instanceof OpenSearchException) { + openSearchException = (OpenSearchException) ex; + } else { + assertTrue(ex.getCause() instanceof OpenSearchException); + openSearchException = (OpenSearchException) ex.getCause(); + } + + assertEquals(404, openSearchException.status()); + assertEquals("index_not_found_exception", openSearchException.error().type()); + assertEquals("doesnotexist", openSearchException.error().metadata().get("index").to(String.class)); ExecutionException ee = assertThrows(ExecutionException.class, () -> { @@ -412,9 +420,9 @@ public void errorResponse() throws Exception { ).get(); }); - ex = ((OpenSearchException) ee.getCause()); - assertEquals(404, ex.status()); - assertEquals("index_not_found_exception", ex.error().type()); + openSearchException = ((OpenSearchException) ee.getCause()); + assertEquals(404, openSearchException.status()); + assertEquals("index_not_found_exception", openSearchException.error().type()); } @Test