Search in sources :

Example 6 with PostFederatedQueryPlugin

use of ddf.catalog.plugin.PostFederatedQueryPlugin in project ddf by codice.

the class FederationStrategyTest method testFederateTwoSourcesOffsetTwoPageSizeThree.

/**
     * Verify that a modified version of the mockQuery passed is used by the sources.
     * <p>
     * Special results handling done by OffsetResultsHandler.
     */
@Test
public void testFederateTwoSourcesOffsetTwoPageSizeThree() throws Exception {
    LOGGER.debug("testFederate_TwoSources_OffsetTwo_PageSizeThree()");
    // Test Setup
    Query mockQuery = mock(QueryImpl.class);
    // Offset of 2
    when(mockQuery.getStartIndex()).thenReturn(2);
    // Page size of 3
    when(mockQuery.getPageSize()).thenReturn(3);
    QueryRequest queryRequest = mock(QueryRequest.class);
    when(queryRequest.getQuery()).thenReturn(mockQuery);
    ArgumentCaptor<QueryRequest> argument1 = ArgumentCaptor.forClass(QueryRequest.class);
    ArgumentCaptor<QueryRequest> argument2 = ArgumentCaptor.forClass(QueryRequest.class);
    /**
         * When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
         * NOT used. So, the results returned by each source start at index 1 and end at (offset +
         * pageSize - 1).
         *
         * Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
         */
    Result mockSource1Result1 = mock(Result.class);
    Result mockSource1Result2 = mock(Result.class);
    Result mockSource1Result3 = mock(Result.class);
    Result mockSource1Result4 = mock(Result.class);
    SourceResponse mockSource1Response = mock(SourceResponse.class);
    List<Result> mockSource1Results = Arrays.asList(mockSource1Result1, mockSource1Result2, mockSource1Result3, mockSource1Result4);
    when(mockSource1Response.getResults()).thenReturn(mockSource1Results);
    Source mockSource1 = mock(Source.class);
    when(mockSource1.query(any(QueryRequest.class))).thenReturn(mockSource1Response);
    when(mockSource1.getId()).thenReturn("####### MOCK SOURCE 1.3 #######");
    /**
         * When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
         * NOT used. So, the results returned by each source start at index 1 and end at (offset +
         * pageSize - 1).
         *
         * Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
         */
    Result mockSource2Result1 = mock(Result.class);
    Result mockSource2Result2 = mock(Result.class);
    Result mockSource2Result3 = mock(Result.class);
    Result mockSource2Result4 = mock(Result.class);
    SourceResponse mockSource2Response = mock(SourceResponse.class);
    List<Result> mockSource2Results = Arrays.asList(mockSource2Result1, mockSource2Result2, mockSource2Result3, mockSource2Result4);
    when(mockSource2Response.getResults()).thenReturn(mockSource2Results);
    Source mockSource2 = mock(Source.class);
    when(mockSource2.query(any(QueryRequest.class))).thenReturn(mockSource2Response);
    when(mockSource2.getId()).thenReturn("####### MOCK SOURCE 2.3 #######");
    // Two sources
    List<Source> sources = new ArrayList<Source>(2);
    sources.add(mockSource1);
    sources.add(mockSource2);
    Result mockSortedResult1 = mock(Result.class);
    Result mockSortedResult2 = mock(Result.class);
    Result mockSortedResult3 = mock(Result.class);
    Result mockSortedResult4 = mock(Result.class);
    Result mockSortedResult5 = mock(Result.class);
    Result mockSortedResult6 = mock(Result.class);
    Result mockSortedResult7 = mock(Result.class);
    Result mockSortedResult8 = mock(Result.class);
    List<Result> mockSortedResults = Arrays.asList(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
    QueryResponseImpl mockOriginalResults = Mockito.mock(QueryResponseImpl.class);
    // Return true for the number of mockSortedResults
    Mockito.when(mockOriginalResults.hasMoreResults()).thenReturn(true, true, true, true, true, true, true, true, false);
    Mockito.when(mockOriginalResults.getResults()).thenReturn(mockSortedResults);
    // Returns the sorted results from both sources (4 + 4 = 8)
    Mockito.when(mockOriginalResults.take()).thenReturn(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
    QueryResponseImpl offsetResultQueue = new QueryResponseImpl(queryRequest, null);
    PowerMockito.whenNew(QueryResponseImpl.class).withArguments(queryRequest, (Map<String, Serializable>) null).thenReturn(mockOriginalResults, offsetResultQueue);
    SortedFederationStrategy strategy = new SortedFederationStrategy(executor, new ArrayList<PreFederatedQueryPlugin>(), new ArrayList<PostFederatedQueryPlugin>());
    // Run Test
    QueryResponse federatedResponse = strategy.federate(sources, queryRequest);
    // Make sure we've finished running the test
    killAndWaitForExecutor();
    // Verification
    assertNotNull(federatedResponse);
    verify(mockSource1).query(argument1.capture());
    // The modified query should have a start index of 1 and an end index of offset + pageSize -
    // 1
    assertEquals(1, argument1.getValue().getQuery().getStartIndex());
    assertEquals(4, argument1.getValue().getQuery().getPageSize());
    verify(mockSource2).query(argument2.capture());
    assertThat(mockQuery, not(argument2.getValue().getQuery()));
    // The modified query should have a start index of 1 and an end index of offset + pageSize -
    // 1
    assertEquals(1, argument2.getValue().getQuery().getStartIndex());
    assertEquals(4, argument2.getValue().getQuery().getPageSize());
    /**
         * Verify three results (page size) are returned. The sorted results returned by the sources
         * do NOT have the offset and page size taken into account, so the offset and page size are
         * applied to the sorted results in the OffsetResultHandler.
         *
         * Offset of 2 (start at result 2) and page size of 3 (end at result 4).
         */
    LOGGER.debug("mockSortedResult1: {}", mockSortedResult1);
    LOGGER.debug("mockSortedResult2: {}", mockSortedResult2);
    LOGGER.debug("mockSortedResult3: {}", mockSortedResult3);
    LOGGER.debug("mockSortedResult4: {}", mockSortedResult4);
    assertEquals(3, federatedResponse.getResults().size());
    assertEquals(mockSortedResult2, federatedResponse.getResults().get(0));
    assertEquals(mockSortedResult3, federatedResponse.getResults().get(1));
    assertEquals(mockSortedResult4, federatedResponse.getResults().get(2));
    for (Result result : federatedResponse.getResults()) {
        LOGGER.debug("federated response result: {}", result);
    }
}
Also used : Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) PreFederatedQueryPlugin(ddf.catalog.plugin.PreFederatedQueryPlugin) ArrayList(java.util.ArrayList) Source(ddf.catalog.source.Source) PostFederatedQueryPlugin(ddf.catalog.plugin.PostFederatedQueryPlugin) Result(ddf.catalog.data.Result) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryResponse(ddf.catalog.operation.QueryResponse) Map(java.util.Map) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

QueryRequest (ddf.catalog.operation.QueryRequest)6 QueryResponse (ddf.catalog.operation.QueryResponse)6 SourceResponse (ddf.catalog.operation.SourceResponse)6 PostFederatedQueryPlugin (ddf.catalog.plugin.PostFederatedQueryPlugin)6 PreFederatedQueryPlugin (ddf.catalog.plugin.PreFederatedQueryPlugin)6 Source (ddf.catalog.source.Source)6 Result (ddf.catalog.data.Result)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Map (java.util.Map)3 Query (ddf.catalog.operation.Query)2 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)2 Serializable (java.io.Serializable)2 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 PluginExecutionException (ddf.catalog.plugin.PluginExecutionException)1 StopProcessingException (ddf.catalog.plugin.StopProcessingException)1 CatalogProvider (ddf.catalog.source.CatalogProvider)1 HashMap (java.util.HashMap)1 List (java.util.List)1