Permalink
Browse files

NEXUS-5250 fixed empty GUI keyword search results

This was a regression introduced during maven-indexer 5.0.0
rework. SearchNGIndexPlexusResource was not correctly falling
back to uncollapsed response when query returned small number
of records.

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
  • Loading branch information...
1 parent b9df334 commit 43b215d78695cdc413bac5a9801a522dc4f668dc @ifedorenko ifedorenko committed with cstamas Nov 14, 2012
@@ -67,11 +67,13 @@
import org.sonatype.plexus.rest.resource.PlexusResource;
import org.sonatype.plexus.rest.resource.PlexusResourceException;
-@Component( role = PlexusResource.class, hint = "SearchNGIndexPlexusResource" )
+@Component( role = PlexusResource.class, hint = SearchNGIndexPlexusResource.ROLE_HINT )
@Path( SearchNGIndexPlexusResource.RESOURCE_URI )
public class SearchNGIndexPlexusResource
extends AbstractIndexerNexusPlexusResource
{
+ public static final String ROLE_HINT = "SearchNGIndexPlexusResource";
+
/**
* Capping the number of Lucene Documents to process, to avoid potention problems and DOS-like attacks. If someone
* needs more results, download the index instead and process it in-situ.
@@ -187,7 +189,7 @@ public Object getPayloadInstance()
@ResourceMethodSignature( queryParams = { @QueryParam( "q" ), @QueryParam( "g" ), @QueryParam( "a" ),
@QueryParam( "v" ), @QueryParam( "p" ), @QueryParam( "c" ), @QueryParam( "cn" ), @QueryParam( "sha1" ),
@QueryParam( "from" ), @QueryParam( "count" ), @QueryParam( "repositoryId" ) }, output = SearchResponse.class )
- public Object get( Context context, Request request, Response response, Variant variant )
+ public SearchNGResponse get( Context context, Request request, Response response, Variant variant )
throws ResourceException
{
Form form = request.getResourceRef().getQueryAsForm();
@@ -289,7 +291,17 @@ public Object get( Context context, Request request, Response response, Variant
{
IteratorSearchResponse searchResult =
searchByTerms( terms, repositoryId, from, count, exact, expandVersion, collapseResults, filters,
- searchers );
+ searchers );
+
+ if ( collapseResults && searchResult == null )
+ {
+ collapseResults = false;
+
+ searchResult =
+ searchByTerms( terms, repositoryId, from, count, exact, expandVersion, collapseResults,
+ filters, searchers );
+ }
+
if ( searchResult != null )
{
try
@@ -13,8 +13,13 @@
package org.sonatype.nexus.rest.indexng;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.*;
-import static org.mockito.Matchers.*;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -27,11 +32,17 @@
import org.apache.lucene.queryParser.ParseException;
import org.apache.maven.index.ArtifactInfoFilter;
import org.apache.maven.index.SearchType;
-import org.junit.Before;
+import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
-import org.sonatype.nexus.NexusAppTestSupport;
+import org.restlet.Context;
+import org.restlet.data.Reference;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.sonatype.nexus.AbstractMavenRepoContentTests;
import org.sonatype.nexus.index.Searcher;
+import org.sonatype.nexus.rest.model.SearchNGResponse;
+import org.sonatype.plexus.rest.resource.PlexusResource;
import org.sonatype.plexus.rest.resource.PlexusResourceException;
import org.sonatype.plexus.rest.resource.error.ErrorMessage;
import org.sonatype.plexus.rest.resource.error.ErrorResponse;
@@ -40,17 +51,11 @@
* Test for SearchNGIndexPlexusResource
*/
public class SearchNGIndexPlexusResourceTest
- extends NexusAppTestSupport
+ extends AbstractMavenRepoContentTests
{
- @Before
- public void setUp()
- throws Exception
- {
- }
-
@Test
- public void testGet()
+ public void testPlexusResourceException()
throws Exception
{
SearchNGIndexPlexusResource resource = new SearchNGIndexPlexusResource();
@@ -59,15 +64,18 @@ public void testGet()
Searcher searcher = mock( Searcher.class );
when( searcher.canHandle( Mockito.any( Map.class ) ) ).thenReturn( true );
- when( searcher.flatIteratorSearch( Mockito.any( Map.class ), anyString(), anyInt(), anyInt(), anyInt(), anyBoolean(),
- Mockito.any( SearchType.class ), Mockito.any( List.class ) ) )
- // emulate current indexer search behavior, illegal query results in IllegalArgEx with the ParseEx as cause
- .thenThrow( new IllegalArgumentException( new ParseException( "mock" ) ) );
+ when(
+ searcher.flatIteratorSearch( Mockito.any( Map.class ), anyString(), anyInt(), anyInt(), anyInt(),
+ anyBoolean(), Mockito.any( SearchType.class ), Mockito.any( List.class ) ) )
+ // emulate current indexer search behavior, illegal query results in IllegalArgEx with the ParseEx as cause
+ .thenThrow( new IllegalArgumentException( new ParseException( "mock" ) ) );
try
{
- resource.searchByTerms( terms, "rid", 1, 1, false, false, true, Collections.<ArtifactInfoFilter>emptyList(),
- Arrays.asList( searcher ) );
+ resource.searchByTerms( terms, "rid", 1, 1, false, false, true,
+ Collections.<ArtifactInfoFilter> emptyList(), Arrays.asList( searcher ) );
+
+ Assert.fail( "Expected PlexusResourceException" );
}
catch ( PlexusResourceException e )
{
@@ -78,8 +86,30 @@ public void testGet()
ErrorMessage errorMessage = errors.get( 0 );
// ID needs to be stable for UI handling
- assertThat( errorMessage.getId(), equalTo( "search" ));
+ assertThat( errorMessage.getId(), equalTo( "search" ) );
assertThat( errorMessage.getMsg(), containsString( "mock" ) );
}
}
+
+ @Test
+ public void testUncollapseResults()
+ throws Exception
+ {
+ fillInRepo();
+
+ SearchNGIndexPlexusResource subject =
+ (SearchNGIndexPlexusResource) lookup( PlexusResource.class, SearchNGIndexPlexusResource.ROLE_HINT );
+
+ Context context = new Context();
+ Request request = new Request();
+ Reference ref = new Reference( "http://localhost:12345/" );
+ request.setRootRef( ref );
+ request.setResourceRef( new Reference( ref, SearchNGIndexPlexusResource.RESOURCE_URI
+ + "?q=nexus&collapseresults=true" ) );
+
+ Response response = new Response( request );
+ SearchNGResponse result = subject.get( context, request, response, null );
+
+ Assert.assertEquals( 1, result.getTotalCount() );
+ }
}

0 comments on commit 43b215d

Please sign in to comment.