Search in sources :

Example 1 with ChannelId

use of org.cometd.bayeux.ChannelId 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)

Aggregations

CatalogFramework (ddf.catalog.CatalogFramework)1 FilterAdapter (ddf.catalog.filter.FilterAdapter)1 FilterBuilder (ddf.catalog.filter.FilterBuilder)1 ServletContext (javax.servlet.ServletContext)1 PersistentStore (org.codice.ddf.persistence.PersistentStore)1 ActionRegistryImpl (org.codice.ddf.ui.searchui.query.actions.ActionRegistryImpl)1 ChannelId (org.cometd.bayeux.ChannelId)1 MarkedReference (org.cometd.bayeux.MarkedReference)1 LocalSession (org.cometd.bayeux.server.LocalSession)1 SecurityPolicy (org.cometd.bayeux.server.SecurityPolicy)1 ServerChannel (org.cometd.bayeux.server.ServerChannel)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 BundleContext (org.osgi.framework.BundleContext)1 EventAdmin (org.osgi.service.event.EventAdmin)1