mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
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:
@@ -27,4 +27,9 @@ public class HttpClientException extends AlfrescoRuntimeException
|
|||||||
{
|
{
|
||||||
super(msgId);
|
super(msgId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpClientException(String msgId, Throwable cause)
|
||||||
|
{
|
||||||
|
super(msgId, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,6 +33,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.alfresco.httpclient.HttpClientException;
|
||||||
import org.alfresco.repo.search.QueryParserException;
|
import org.alfresco.repo.search.QueryParserException;
|
||||||
import org.apache.commons.httpclient.Header;
|
import org.apache.commons.httpclient.Header;
|
||||||
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
@@ -139,8 +140,11 @@ public abstract class AbstractSolrQueryHTTPClient
|
|||||||
|
|
||||||
Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
|
Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
|
||||||
// TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
|
// TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
|
||||||
JSONObject json = new JSONObject(new JSONTokener(reader));
|
return new JSONObject(new JSONTokener(reader));
|
||||||
return json;
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
throw new HttpClientException("[%s] %s".formatted(this.getClass().getSimpleName(), e.getMessage()), e);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
@@ -26,7 +26,10 @@
|
|||||||
package org.alfresco.repo.search.impl.solr;
|
package org.alfresco.repo.search.impl.solr;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
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.junit.Assert.fail;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@@ -35,7 +38,9 @@ import static org.mockito.MockitoAnnotations.openMocks;
|
|||||||
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.alfresco.httpclient.HttpClientException;
|
||||||
import org.alfresco.repo.search.QueryParserException;
|
import org.alfresco.repo.search.QueryParserException;
|
||||||
import org.apache.commons.httpclient.Header;
|
import org.apache.commons.httpclient.Header;
|
||||||
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
@@ -53,7 +58,7 @@ public class AbstractSolrQueryHTTPClientTest
|
|||||||
private static final String URL = "http://this/is/a/url";
|
private static final String URL = "http://this/is/a/url";
|
||||||
|
|
||||||
/** The abstract class under test. */
|
/** The abstract class under test. */
|
||||||
private AbstractSolrQueryHTTPClient abstractSolrQueryHTTPClient = spy(AbstractSolrQueryHTTPClient.class);
|
private final AbstractSolrQueryHTTPClient abstractSolrQueryHTTPClient = spy(AbstractSolrQueryHTTPClient.class);
|
||||||
@Mock
|
@Mock
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
@Mock
|
@Mock
|
||||||
@@ -147,6 +152,19 @@ public class AbstractSolrQueryHTTPClientTest
|
|||||||
assertEquals("Unexpected JSON response received.", "{}", response.toString());
|
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. */
|
/** Create an input stream containing the given string. */
|
||||||
private ByteArrayInputStream convertStringToInputStream(String message)
|
private ByteArrayInputStream convertStringToInputStream(String message)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user