MNT-24346: Fix "0" return status code in Search API when Solr is unavailable. (#2939)

* MNT-24346: Fix 0 status code in Search API when Solr is unavailable.

* MNT-24346: Fixing PMD issues.

* MNT-24346: Fixing formatting.

* MNT-24346: Reverting accidental method name change.
This commit is contained in:
Maciej Pichura
2024-09-25 12:47:06 +02:00
committed by GitHub
parent 0b511e0b55
commit 837fb0cccd
3 changed files with 30 additions and 3 deletions

View File

@@ -27,4 +27,9 @@ public class HttpClientException extends AlfrescoRuntimeException
{
super(msgId);
}
public HttpClientException(String msgId, Throwable cause)
{
super(msgId, cause);
}
}

View File

@@ -33,6 +33,7 @@ import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.alfresco.httpclient.HttpClientException;
import org.alfresco.repo.search.QueryParserException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
@@ -139,8 +140,11 @@ public abstract class AbstractSolrQueryHTTPClient
Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
// TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
JSONObject json = new JSONObject(new JSONTokener(reader));
return json;
return new JSONObject(new JSONTokener(reader));
}
catch (IOException e)
{
throw new HttpClientException("[%s] %s".formatted(this.getClass().getSimpleName(), e.getMessage()), e);
}
finally
{

View File

@@ -26,7 +26,10 @@
package org.alfresco.repo.search.impl.solr;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -35,7 +38,9 @@ import static org.mockito.MockitoAnnotations.openMocks;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.alfresco.httpclient.HttpClientException;
import org.alfresco.repo.search.QueryParserException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
@@ -53,7 +58,7 @@ public class AbstractSolrQueryHTTPClientTest
private static final String URL = "http://this/is/a/url";
/** The abstract class under test. */
private AbstractSolrQueryHTTPClient abstractSolrQueryHTTPClient = spy(AbstractSolrQueryHTTPClient.class);
private final AbstractSolrQueryHTTPClient abstractSolrQueryHTTPClient = spy(AbstractSolrQueryHTTPClient.class);
@Mock
private HttpClient httpClient;
@Mock
@@ -147,6 +152,19 @@ public class AbstractSolrQueryHTTPClientTest
assertEquals("Unexpected JSON response received.", "{}", response.toString());
}
@Test
public void testPostQuery_handlesIOException() throws Exception
{
String messageFromHttp = "Some IO Exception";
when(httpClient.executeMethod(any())).thenThrow(new IOException(messageFromHttp));
HttpClientException expectedException =
assertThrows(HttpClientException.class, () -> abstractSolrQueryHTTPClient.postQuery(httpClient, URL, body));
String exceptionMessage = expectedException.getMessage();
assertTrue(exceptionMessage.endsWith("[%s] %s".formatted(abstractSolrQueryHTTPClient.getClass().getSimpleName(), messageFromHttp)));
}
/** Create an input stream containing the given string. */
private ByteArrayInputStream convertStringToInputStream(String message)
{