mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestEventAdminMessageService.java
@Test
    public final void testCleanupPostChannels() throws Exception
    {
        // mock service registrations for source 1 and 2
        ServiceRegistration reg1 = mock(ServiceRegistration.class);
        ServiceRegistration reg2 = mock(ServiceRegistration.class);

        when(m_Context.registerService(eq(EventHandler.class), 
                Mockito.any(EventHandler.class), Mockito.any(Dictionary.class))).thenReturn(reg1, reg2);

        // Register Event
        EventRegistrationRequestData.Builder requestMessage = EventRegistrationRequestData.newBuilder()
                .addTopic("test-topic");
        // add required fields
        requestMessage = RemoteUtils.appendRequiredFields(requestMessage, false);       
        EventAdminNamespace eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.EventRegistrationRequest)
                .setData(requestMessage.build().toByteString())
                .build();
        TerraHarvestMessage message = createEventAdminMessage(1, eventAdminMessage);
        TerraHarvestPayload payload = createPayload(eventAdminMessage);

        // mock the channel the message came from
        RemoteChannel channel = mock(RemoteChannel.class);
        
        // replay
        m_SUT.handleMessage(message, payload, channel);

        // Register another event
        requestMessage = EventRegistrationRequestData.newBuilder()
                .addTopic("test-topic");
        // add required fields
        requestMessage = RemoteUtils.appendRequiredFields(requestMessage, false);
                
        eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.EventRegistrationRequest)
                .setData(requestMessage.build().toByteString())
                .build();
        message = createEventAdminMessage(2, eventAdminMessage);
        m_SUT.handleMessage(message, payload, channel);

        // Create cleanup request from source 1
        eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.CleanupRequest)
                .build();
        payload = createPayload(eventAdminMessage);
        message = createEventAdminMessage(1, eventAdminMessage);
        m_SUT.handleMessage(message, payload, channel);

        //verify that the registration from source 1 is unregistered, but not the one from source 2
        verify(reg1, times(1)).unregister();
        verify(reg2, never()).unregister();
        
        verify(m_DataStore, times(1)).removeMatching(eq(EventAdminMessageService.class), anyString());
        
        verify(m_MessageFactory).createEventAdminResponseMessage(message, 
                EventAdminMessageType.CleanupResponse, null);
        //reused channel
        verify(m_ResponseWrapper, times(3)).queue(channel);
        
        // send message again, verify unregister not called again
        m_SUT.handleMessage(message, payload, channel);
        // should still be at the same number of calls
        verify(reg1, times(1)).unregister();
        verify(reg2, never()).unregister();
        
        verify(m_DataStore, times(1)).removeMatching(eq(EventAdminMessageService.class), anyString());
    }
/**
     * Test if registered remote events are properly unregistered via cleanup requests/responses.
     * 
     * Verify that only registrations from the request of the cleanup are unregistered.
     * 
     * Verify that unregister won't be called again on subsequent calls.
     */
Test if registered remote events are properly unregistered via cleanup requests/responses.

Verify that only registrations from the request of the cleanup are unregistered.

Verify that unregister won't be called again on subsequent calls.
Test if registered remote events are properly unregistered via cleanup requests / responses . Verify that only registrations from the request of the cleanup are unregistered . Verify that unregister won ' t be called again on subsequent calls .
@Test
    public final void testCleanupPostChannels() throws Exception
    {
        ServiceRegistration reg1 = mock(ServiceRegistration.class);
        ServiceRegistration reg2 = mock(ServiceRegistration.class);
        when(m_Context.registerService(eq(EventHandler.class), 
                Mockito.any(EventHandler.class), Mockito.any(Dictionary.class))).thenReturn(reg1, reg2);
        EventRegistrationRequestData.Builder requestMessage = EventRegistrationRequestData.newBuilder()
                .addTopic("test-topic");
        requestMessage = RemoteUtils.appendRequiredFields(requestMessage, false);       
        EventAdminNamespace eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.EventRegistrationRequest)
                .setData(requestMessage.build().toByteString())
                .build();
        TerraHarvestMessage message = createEventAdminMessage(1, eventAdminMessage);
        TerraHarvestPayload payload = createPayload(eventAdminMessage);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        requestMessage = EventRegistrationRequestData.newBuilder()
                .addTopic("test-topic");
        requestMessage = RemoteUtils.appendRequiredFields(requestMessage, false);
        eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.EventRegistrationRequest)
                .setData(requestMessage.build().toByteString())
                .build();
        message = createEventAdminMessage(2, eventAdminMessage);
        m_SUT.handleMessage(message, payload, channel);
        eventAdminMessage = EventAdminNamespace.newBuilder()
                .setType(EventAdminMessageType.CleanupRequest)
                .build();
        payload = createPayload(eventAdminMessage);
        message = createEventAdminMessage(1, eventAdminMessage);
        m_SUT.handleMessage(message, payload, channel);
        verify(reg1, times(1)).unregister();
        verify(reg2, never()).unregister();
        verify(m_DataStore, times(1)).removeMatching(eq(EventAdminMessageService.class), anyString());
        verify(m_MessageFactory).createEventAdminResponseMessage(message, 
                EventAdminMessageType.CleanupResponse, null);
        verify(m_ResponseWrapper, times(3)).queue(channel);
        m_SUT.handleMessage(message, payload, channel);
        verify(reg1, times(1)).unregister();
        verify(reg2, never()).unregister();
        verify(m_DataStore, times(1)).removeMatching(eq(EventAdminMessageService.class), anyString());
    }
@ Test public final void testCleanupPostChannels ( ) throws Exception { ServiceRegistration reg1 = mock ( ServiceRegistration . class ) ; ServiceRegistration reg2 = mock ( ServiceRegistration . class ) ; when ( m_Context . registerService ( eq ( EventHandler . class ) , Mockito . any ( EventHandler . class ) , Mockito . any ( Dictionary . class ) ) ) . thenReturn ( reg1 , reg2 ) ; EventRegistrationRequestData . Builder requestMessage = EventRegistrationRequestData . newBuilder ( ) . addTopic ( "test-topic" ) ; requestMessage = RemoteUtils . appendRequiredFields ( requestMessage , false ) ; EventAdminNamespace eventAdminMessage = EventAdminNamespace . newBuilder ( ) . setType ( EventAdminMessageType . EventRegistrationRequest ) . setData ( requestMessage . build ( ) . toByteString ( ) ) . build ( ) ; TerraHarvestMessage message = createEventAdminMessage ( 1 , eventAdminMessage ) ; TerraHarvestPayload payload = createPayload ( eventAdminMessage ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; requestMessage = EventRegistrationRequestData . newBuilder ( ) . addTopic ( "test-topic" ) ; requestMessage = RemoteUtils . appendRequiredFields ( requestMessage , false ) ; eventAdminMessage = EventAdminNamespace . newBuilder ( ) . setType ( EventAdminMessageType . EventRegistrationRequest ) . setData ( requestMessage . build ( ) . toByteString ( ) ) . build ( ) ; message = createEventAdminMessage ( 2 , eventAdminMessage ) ; m_SUT . handleMessage ( message , payload , channel ) ; eventAdminMessage = EventAdminNamespace . newBuilder ( ) . setType ( EventAdminMessageType . CleanupRequest ) . build ( ) ; payload = createPayload ( eventAdminMessage ) ; message = createEventAdminMessage ( 1 , eventAdminMessage ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( reg1 , times ( 1 ) ) . unregister ( ) ; verify ( reg2 , never ( ) ) . unregister ( ) ; verify ( m_DataStore , times ( 1 ) ) . removeMatching ( eq ( EventAdminMessageService . class ) , anyString ( ) ) ; verify ( m_MessageFactory ) . createEventAdminResponseMessage ( message , EventAdminMessageType . CleanupResponse , null ) ; verify ( m_ResponseWrapper , times ( 3 ) ) . queue ( channel ) ; m_SUT . handleMessage ( message , payload , channel ) ; verify ( reg1 , times ( 1 ) ) . unregister ( ) ; verify ( reg2 , never ( ) ) . unregister ( ) ; verify ( m_DataStore , times ( 1 ) ) . removeMatching ( eq ( EventAdminMessageService . class ) , anyString ( ) ) ; }
Test if registered remote events are properly unregistered via cleanup requests/responses.
Test if registered remote events are properly unregistered via cleanup requests / responses .
// mock service registrations for source 1 and 2 // Register Event // add required fields // mock the channel the message came from // replay // Register another event // add required fields // Create cleanup request from source 1 //verify that the registration from source 1 is unregistered, but not the one from source 2 //reused channel // send message again, verify unregister not called again // should still be at the same number of calls