Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Parmet <[email protected]>
  • Loading branch information
andrewparmet committed Oct 6, 2023
1 parent 6383a3f commit 8eb7aa6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down Expand Up @@ -300,4 +307,4 @@ public void testClusterHealthNotFoundIndex() throws IOException {
assertNotNull(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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());
}
}
{
Expand Down Expand Up @@ -605,4 +621,4 @@ public void setMsg(String msg) {
this.msg = msg;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}
Expand Down Expand Up @@ -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]");
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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, () -> {
Expand All @@ -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
Expand Down

0 comments on commit 8eb7aa6

Please sign in to comment.