Commit 54553f0e authored by Rustam Lotsmanenko (EPAM)'s avatar Rustam Lotsmanenko (EPAM)
Browse files

add unit tests, include all error messages

parent a95ee19b
Pipeline #97236 failed with stages
in 27 minutes and 58 seconds
......@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringJoiner;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -16,7 +17,7 @@ import org.springframework.stereotype.Component;
@Slf4j
@Component
@RequiredArgsConstructor
public class DetailedBadRequestMessageUtil implements IDetailedBadRequestMessageUtil{
public class DetailedBadRequestMessageUtil implements IDetailedBadRequestMessageUtil {
private final ObjectMapper objectMapper;
......@@ -24,22 +25,10 @@ public class DetailedBadRequestMessageUtil implements IDetailedBadRequestMessage
public String getDetailedBadRequestMessage(SearchRequest searchRequest, Exception e) {
String defaultErrorMessage = "Invalid parameters were given on search request";
Throwable[] suppressed = e.getSuppressed();
if (suppressed != null && suppressed.length > 0 && suppressed[0] instanceof ResponseException) {
ResponseException responseException = (ResponseException) suppressed[0];
Response response = responseException.getResponse();
HttpEntity entity = response.getEntity();
try {
InputStream content = entity.getContent();
JsonNode errorNode = objectMapper.readValue(content, JsonNode.class);
JsonNode reasonNode = errorNode.findValue("reason");
String reasonMessage = reasonNode.textValue();
if (StringUtils.isNotEmpty(reasonMessage)) {
return reasonMessage;
} else {
log.error(String.format("Unable to find fail reason in elasticsearch response:%s", errorNode.textValue()));
}
} catch (IOException ex) {
log.error("Unable to parse fail reason from elasticsearch response");
if (suppressed != null && suppressed.length > 0) {
String fullReasonMessage = extractAndCombineReasonMessages(suppressed);
if (StringUtils.isNotEmpty(fullReasonMessage)) {
return fullReasonMessage;
}
}
if (e.getCause() == null) {
......@@ -52,6 +41,31 @@ public class DetailedBadRequestMessageUtil implements IDetailedBadRequestMessage
return defaultErrorMessage;
}
private String extractAndCombineReasonMessages(Throwable[] suppressed) {
StringJoiner stringJoiner = new StringJoiner(".");
for (Throwable throwable : suppressed) {
if (throwable instanceof ResponseException) {
ResponseException responseException = (ResponseException) throwable;
Response response = responseException.getResponse();
HttpEntity entity = response.getEntity();
try {
InputStream content = entity.getContent();
JsonNode errorNode = objectMapper.readValue(content, JsonNode.class);
JsonNode reasonNode = errorNode.findValue("reason");
String reasonMessage = reasonNode.textValue();
if (StringUtils.isNotEmpty(reasonMessage)) {
stringJoiner.add(reasonMessage);
} else {
log.error(String.format("Unable to find fail reason in elasticsearch response:%s", errorNode.textValue()));
}
} catch (IOException ex) {
log.error("Unable to parse fail reason from elasticsearch response");
}
}
}
return stringJoiner.toString();
}
private String getKeywordFieldErrorMessage(SearchRequest searchRequest, String msg) {
if (msg == null) {
return null;
......
package org.opengroup.osdu.search.util;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class DetailedBadRequestMessageUtilTest {
private static final String NESTED_FAIL_REASON = "failed to create query: [nested] failed to find nested object under path [data.NameAliases]";
private static final String GEO_FIELD_FAIL_REASON = "failed to find geo field [officeAddress]";
private final ObjectMapper objectMapper = new ObjectMapper();
private DetailedBadRequestMessageUtil badRequestMessageUtil;
private Throwable[] throwable;
@Mock
private SearchRequest searchRequest;
@Before
public void setUp() {
badRequestMessageUtil = new DetailedBadRequestMessageUtil(objectMapper);
}
@Test
public void testSingleResponse() throws IOException {
ResponseException responseExceptionMock = Mockito.mock(ResponseException.class);
Response responseMock = Mockito.mock(Response.class);
HttpEntity httpEntityMock = Mockito.mock(HttpEntity.class);
ElasticsearchStatusException elasticsearchStatusExceptionMock = Mockito.mock(ElasticsearchStatusException.class);
throwable = new Throwable[]{responseExceptionMock};
when(responseExceptionMock.getResponse()).thenReturn(responseMock);
when(responseMock.getEntity()).thenReturn(httpEntityMock);
when(httpEntityMock.getContent()).thenReturn(getResponseContent("nestedfail.json"));
when(elasticsearchStatusExceptionMock.getSuppressed()).thenReturn(throwable);
String detailedBadRequestMessage = badRequestMessageUtil.getDetailedBadRequestMessage(searchRequest, elasticsearchStatusExceptionMock);
assertEquals(NESTED_FAIL_REASON, detailedBadRequestMessage);
}
@Test
public void testMultipleResponse() throws IOException {
ResponseException responseExceptionMock = Mockito.mock(ResponseException.class);
Response responseMock = Mockito.mock(Response.class);
HttpEntity httpEntityMock = Mockito.mock(HttpEntity.class);
when(responseExceptionMock.getResponse()).thenReturn(responseMock);
when(responseMock.getEntity()).thenReturn(httpEntityMock);
when(httpEntityMock.getContent()).thenReturn(getResponseContent("nestedfail.json"));
ResponseException secondResponseExceptionMock = Mockito.mock(ResponseException.class);
Response secondResponseMock = Mockito.mock(Response.class);
HttpEntity secondHttpEntityMock = Mockito.mock(HttpEntity.class);
when(secondResponseExceptionMock.getResponse()).thenReturn(secondResponseMock);
when(secondResponseMock.getEntity()).thenReturn(secondHttpEntityMock);
when(secondHttpEntityMock.getContent()).thenReturn(getResponseContent("geofieldfail.json"));
ElasticsearchStatusException elasticsearchStatusExceptionMock = Mockito.mock(ElasticsearchStatusException.class);
throwable = new Throwable[]{responseExceptionMock, secondResponseExceptionMock};
when(elasticsearchStatusExceptionMock.getSuppressed()).thenReturn(throwable);
String detailedBadRequestMessage = badRequestMessageUtil.getDetailedBadRequestMessage(searchRequest, elasticsearchStatusExceptionMock);
assertEquals(NESTED_FAIL_REASON + "." + GEO_FIELD_FAIL_REASON, detailedBadRequestMessage);
}
private InputStream getResponseContent(String fileName) {
return this.getClass().getResourceAsStream("/errorresponses/" + fileName);
}
}
\ No newline at end of file
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to find geo field [officeAddress]",
"index_uuid": "pOkjuO_2TgOzsBBE3FBmjw",
"index": "osdu-testquery1646402410474-well-1.0.0"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "osdu-testquery1646402410474-well-1.0.0",
"node": "I9579IL-TX2IXJVJatOu6Q",
"reason": {
"type": "query_shard_exception",
"reason": "failed to find geo field [officeAddress]",
"index_uuid": "pOkjuO_2TgOzsBBE3FBmjw",
"index": "osdu-testquery1646402410474-well-1.0.0"
}
}
]
},
"status": 400
}
\ No newline at end of file
{
"error": {
"root_cause": [{
"type": "query_shard_exception",
"reason": "failed to create query: [nested] failed to find nested object under path [data.NameAliases]",
"index_uuid": "yTHQkD08S_Whctq_6QkXPA",
"index": "osdu-wks-master-data--well-1.0.0"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [{
"shard": 0,
"index": "osdu-wks-master-data--well-1.0.0",
"node": "I9579IL-TX2IXJVJatOu6Q",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: [nested] failed to find nested object under path [data.NameAliases]",
"index_uuid": "yTHQkD08S_Whctq_6QkXPA",
"index": "osdu-wks-master-data--well-1.0.0",
"caused_by": {
"type": "illegal_state_exception",
"reason": "[nested] failed to find nested object under path [data.NameAliases]"
}
}
}
]
},
"status": 400
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment