Search in sources :

Example 1 with ServerChannel

use of org.cometd.bayeux.server.ServerChannel in project ddf by codice.

the class SearchControllerTest method testMetacardTypeValuesCacheDisabled.

@Test
public void testMetacardTypeValuesCacheDisabled() throws Exception {
    final String ID = "id";
    Set<String> srcIds = new HashSet<String>();
    srcIds.add(ID);
    BayeuxServer bayeuxServer = mock(BayeuxServer.class);
    ServerChannel channel = mock(ServerChannel.class);
    ArgumentCaptor<ServerMessage.Mutable> reply = ArgumentCaptor.forClass(ServerMessage.Mutable.class);
    when(bayeuxServer.getChannel(any(String.class))).thenReturn(channel);
    SearchRequest request = new SearchRequest(srcIds, getQueryRequest("title LIKE 'Meta*'"), ID);
    searchController.setBayeuxServer(bayeuxServer);
    // Disable Cache
    searchController.setCacheDisabled(true);
    searchController.executeQuery(request, mockServerSession, null);
    verify(channel, timeout(1000).only()).publish(any(ServerSession.class), reply.capture());
    List<Mutable> replies = reply.getAllValues();
    assertReplies(replies);
}
Also used : Mutable(org.cometd.bayeux.server.ServerMessage.Mutable) SearchRequest(org.codice.ddf.ui.searchui.query.model.SearchRequest) ServerSession(org.cometd.bayeux.server.ServerSession) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) ServerMessage(org.cometd.bayeux.server.ServerMessage) Matchers.anyString(org.mockito.Matchers.anyString) ServerChannel(org.cometd.bayeux.server.ServerChannel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ServerChannel

use of org.cometd.bayeux.server.ServerChannel in project ddf by codice.

the class SearchControllerTest method testMetacardTypeValuesCacheEnabled.

@Test
public void testMetacardTypeValuesCacheEnabled() throws Exception {
    final String ID = "id";
    Set<String> srcIds = new HashSet<>();
    srcIds.add(ID);
    BayeuxServer bayeuxServer = mock(BayeuxServer.class);
    ServerChannel channel = mock(ServerChannel.class);
    ArgumentCaptor<ServerMessage.Mutable> reply = ArgumentCaptor.forClass(ServerMessage.Mutable.class);
    when(bayeuxServer.getChannel(any(String.class))).thenReturn(channel);
    SearchRequest request = new SearchRequest(srcIds, getQueryRequest("title LIKE 'Meta*'"), ID);
    searchController.setBayeuxServer(bayeuxServer);
    searchController.setCacheDisabled(false);
    searchController.executeQuery(request, mockServerSession, null);
    verify(channel, timeout(1000).times(2)).publish(any(ServerSession.class), reply.capture());
    List<Mutable> replies = reply.getAllValues();
    assertReplies(replies);
}
Also used : Mutable(org.cometd.bayeux.server.ServerMessage.Mutable) SearchRequest(org.codice.ddf.ui.searchui.query.model.SearchRequest) ServerSession(org.cometd.bayeux.server.ServerSession) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) ServerMessage(org.cometd.bayeux.server.ServerMessage) Matchers.anyString(org.mockito.Matchers.anyString) ServerChannel(org.cometd.bayeux.server.ServerChannel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ServerChannel

use of org.cometd.bayeux.server.ServerChannel in project ddf by codice.

the class CometdEndpointTest method setUp.

/**
     * @throws java.lang.Exception
     */
@Before
public void setUp() throws Exception {
    when(mockServerSession.getId()).thenReturn(MOCK_SESSION_ID);
    // Return a new mock of LocalSession each time newLocalSession is
    // called on the BayeuxServer
    when(bayeuxServer.newLocalSession(Mockito.anyString())).thenAnswer(new Answer<LocalSession>() {

        public LocalSession answer(InvocationOnMock invocation) {
            LocalSession localSession = mock(LocalSession.class);
            when(localSession.getServerSession()).thenReturn(mock(ServerSession.class));
            return localSession;
        }
    });
    // Mock enough of the behavior of the createChannelIfAbsent method to
    // ensure proper processing of CometD Service annotations
    when(bayeuxServer.createChannelIfAbsent(Mockito.anyString())).thenAnswer(new Answer<MarkedReference<ServerChannel>>() {

        public MarkedReference<ServerChannel> answer(InvocationOnMock invokation) {
            String channelName = invokation.getArguments()[0].toString();
            LOGGER.debug("Channel Name: {}", channelName);
            if (null == channelName) {
                return null;
            }
            ChannelId channelId = new ChannelId(channelName);
            ServerChannel serverChannel = mock(ServerChannel.class);
            when(serverChannel.getChannelId()).thenReturn(channelId);
            when(serverChannel.getId()).thenReturn(channelName);
            @SuppressWarnings("unchecked") MarkedReference<ServerChannel> markedReference = (MarkedReference<ServerChannel>) mock(MarkedReference.class);
            // Mark with value of true indicates that the serverChannel
            // did not previously exist. Implementation of test setup
            // needs to change if there is a later need to maintain
            // whether a channel has already been created
            when(markedReference.isMarked()).thenReturn(true);
            when(markedReference.getReference()).thenReturn(serverChannel);
            return markedReference;
        }
    });
    // Call the actual BayeuxServer methods, rather than the mock methods, 
    // when setting/getting the security policy.
    Mockito.doCallRealMethod().when(bayeuxServer).setSecurityPolicy(Mockito.any(SecurityPolicy.class));
    Mockito.doCallRealMethod().when(bayeuxServer).getSecurityPolicy();
    // Associate the BayeuxServer with a CometdServlet
    ServletContext servletContext = mock(ServletContext.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    when(servletContext.getAttribute(BayeuxServer.ATTRIBUTE)).thenReturn(bayeuxServer);
    // Create the CometdEndpoint, passing in the mocked CometdServlet
    cometdEndpoint = spy(new CometdEndpoint(mock(CatalogFramework.class), mock(FilterBuilder.class), mock(FilterAdapter.class), mock(PersistentStore.class), mock(BundleContext.class), mock(EventAdmin.class), new ActionRegistryImpl(Collections.EMPTY_LIST, Collections.EMPTY_LIST), Executors.newSingleThreadExecutor()));
    doNothing().when(cometdEndpoint).init();
}
Also used : ActionRegistryImpl(org.codice.ddf.ui.searchui.query.actions.ActionRegistryImpl) EventAdmin(org.osgi.service.event.EventAdmin) MarkedReference(org.cometd.bayeux.MarkedReference) LocalSession(org.cometd.bayeux.server.LocalSession) FilterAdapter(ddf.catalog.filter.FilterAdapter) PersistentStore(org.codice.ddf.persistence.PersistentStore) ChannelId(org.cometd.bayeux.ChannelId) ServerChannel(org.cometd.bayeux.server.ServerChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterBuilder(ddf.catalog.filter.FilterBuilder) SecurityPolicy(org.cometd.bayeux.server.SecurityPolicy) CatalogFramework(ddf.catalog.CatalogFramework) ServletContext(javax.servlet.ServletContext) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 4 with ServerChannel

use of org.cometd.bayeux.server.ServerChannel in project ddf by codice.

the class SearchControllerTest method testFailingQuery.

@Test
public void testFailingQuery() throws Exception {
    // Setup
    framework = mock(CatalogFramework.class);
    QueryResponse response = mock(QueryResponse.class);
    when(response.getResults()).thenThrow(new RuntimeException("Getting results failed"));
    when(framework.query(any(QueryRequest.class))).thenReturn(response);
    searchController = new SearchController(framework, new ActionRegistryImpl(Collections.emptyList(), Collections.emptyList()), new GeotoolsFilterAdapterImpl(), new SequentialExecutorService());
    final String ID = "id";
    Set<String> srcIds = new HashSet<>();
    srcIds.add(ID);
    SearchRequest request = new SearchRequest(srcIds, getQueryRequest("anyText LIKE '*'"), "queryId");
    BayeuxServer bayeuxServer = mock(BayeuxServer.class);
    ServerChannel channel = mock(ServerChannel.class);
    when(bayeuxServer.getChannel(any(String.class))).thenReturn(channel);
    searchController.setBayeuxServer(bayeuxServer);
    searchController.setCacheDisabled(true);
    // Perform Test
    searchController.executeQuery(request, mockServerSession, null);
    // Verify
    verify(channel, times(1)).publish(any(), any());
}
Also used : SearchRequest(org.codice.ddf.ui.searchui.query.model.SearchRequest) QueryRequest(ddf.catalog.operation.QueryRequest) ActionRegistryImpl(org.codice.ddf.ui.searchui.query.actions.ActionRegistryImpl) Matchers.anyString(org.mockito.Matchers.anyString) ServerChannel(org.cometd.bayeux.server.ServerChannel) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) GeotoolsFilterAdapterImpl(ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ServerChannel

use of org.cometd.bayeux.server.ServerChannel in project ddf by codice.

the class SearchControllerTest method cacheQuery.

private List<String> cacheQuery(Set<String> srcIds, int queryRequestCount) throws CQLException, UnsupportedQueryException, SourceUnavailableException, FederationException {
    SearchRequest request = new SearchRequest(srcIds, getQueryRequest("anyText LIKE '*'"), "queryId");
    BayeuxServer bayeuxServer = mock(BayeuxServer.class);
    ServerChannel channel = mock(ServerChannel.class);
    when(bayeuxServer.getChannel(any(String.class))).thenReturn(channel);
    ArgumentCaptor<QueryRequest> queryRequestCaptor = ArgumentCaptor.forClass(QueryRequest.class);
    searchController.setCacheDisabled(false);
    searchController.setNormalizationDisabled(false);
    searchController.setBayeuxServer(bayeuxServer);
    // Perform Test
    searchController.executeQuery(request, mockServerSession, null);
    // Verify
    verify(framework, times(queryRequestCount)).query(queryRequestCaptor.capture());
    List<QueryRequest> capturedQueryRequests = queryRequestCaptor.getAllValues();
    List<String> modes = new ArrayList<>(queryRequestCount);
    for (QueryRequest queryRequest : capturedQueryRequests) {
        for (String key : queryRequest.getProperties().keySet()) {
            modes.add((String) queryRequest.getPropertyValue(key));
        }
    }
    return modes;
}
Also used : SearchRequest(org.codice.ddf.ui.searchui.query.model.SearchRequest) QueryRequest(ddf.catalog.operation.QueryRequest) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) ServerChannel(org.cometd.bayeux.server.ServerChannel)

Aggregations

ServerChannel (org.cometd.bayeux.server.ServerChannel)7 BayeuxServer (org.cometd.bayeux.server.BayeuxServer)6 SearchRequest (org.codice.ddf.ui.searchui.query.model.SearchRequest)5 Matchers.anyString (org.mockito.Matchers.anyString)5 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 QueryRequest (ddf.catalog.operation.QueryRequest)3 ServerMessage (org.cometd.bayeux.server.ServerMessage)3 ServerSession (org.cometd.bayeux.server.ServerSession)3 CatalogFramework (ddf.catalog.CatalogFramework)2 ActionRegistryImpl (org.codice.ddf.ui.searchui.query.actions.ActionRegistryImpl)2 Mutable (org.cometd.bayeux.server.ServerMessage.Mutable)2 FilterAdapter (ddf.catalog.filter.FilterAdapter)1 FilterBuilder (ddf.catalog.filter.FilterBuilder)1 GeotoolsFilterAdapterImpl (ddf.catalog.filter.proxy.adapter.GeotoolsFilterAdapterImpl)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 PersistentStore (org.codice.ddf.persistence.PersistentStore)1 SearchService (org.codice.ddf.ui.searchui.query.service.SearchService)1