openworld-maker commented on code in PR #4369: URL: https://github.com/apache/solr/pull/4369#discussion_r3214480043
########## solr/core/src/test/org/apache/solr/handler/component/QueryComponentFieldExistsQueryErrorHandlingTest.java: ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.handler.component; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.search.QueryCommand; +import org.apache.solr.search.SolrIndexSearcher; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +public class QueryComponentFieldExistsQueryErrorHandlingTest extends SolrTestCaseJ4 { + + @BeforeClass + public static void beforeClass() { + assumeWorkingMockito(); + } + + @Test + public void mapsFieldExistsQueryIllegalStateExceptionToBadRequest() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException fieldExistsError = + new IllegalStateException("FieldExistsQuery requires norms, docValues, or vectors"); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(fieldExistsError); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertTrue(thrown.getCause() instanceof SolrException); + SolrException solrException = (SolrException) thrown.getCause(); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrException.code()); + assertTrue(solrException.getMessage().startsWith("FieldExistsQuery requires")); + } + + @Test + public void mapsNestedFieldExistsQueryIllegalStateExceptionToBadRequest() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException rootCause = + new IllegalStateException("FieldExistsQuery requires norms, docValues, or vectors"); + IllegalStateException wrapped = new IllegalStateException("outer", rootCause); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(wrapped); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertTrue(thrown.getCause() instanceof SolrException); + SolrException solrException = (SolrException) thrown.getCause(); + assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, solrException.code()); + assertTrue(solrException.getMessage().startsWith("FieldExistsQuery requires")); + } + + @Test + public void leavesOtherIllegalStateExceptionsUnchanged() throws Exception { + SolrIndexSearcher searcher = Mockito.mock(SolrIndexSearcher.class); + IllegalStateException nonFieldExistsError = + new IllegalStateException("not a field exists query"); + Mockito.when(searcher.search(Mockito.any(QueryCommand.class))).thenThrow(nonFieldExistsError); + + InvocationTargetException thrown = + expectThrows( + InvocationTargetException.class, + () -> invokeDoProcessUngroupedSearch(newResponseBuilder(searcher), new QueryCommand())); + + assertSame(nonFieldExistsError, thrown.getCause()); + } + + private static void invokeDoProcessUngroupedSearch(ResponseBuilder rb, QueryCommand cmd) + throws Exception { + Method method = + QueryComponent.class.getDeclaredMethod( + "doProcessUngroupedSearch", ResponseBuilder.class, QueryCommand.class); + method.setAccessible(true); + method.invoke(new QueryComponent(), rb, cmd); Review Comment: Addressed by replacing the reflection-heavy test entirely with a real request-driven regression test in `solr/core/src/test/org/apache/solr/search/FieldExistsQueryBadRequestTest.java`; no private-method reflection remains. ########## solr/core/src/test/org/apache/solr/handler/component/QueryComponentFieldExistsQueryErrorHandlingTest.java: ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.handler.component; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.search.QueryCommand; +import org.apache.solr.search.SolrIndexSearcher; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +public class QueryComponentFieldExistsQueryErrorHandlingTest extends SolrTestCaseJ4 { Review Comment: Addressed by replacing the mock-based approach with a realistic regression test in `solr/core/src/test/org/apache/solr/search/FieldExistsQueryBadRequestTest.java`. The fix is now validated through normal request handling rather than private-method mocking. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
