mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/osgi/TestEventAdminMessageService.java
@Test
    public void testActivateDatastorePullIncreaseRegId() throws Exception
    {
        //persistent reg messages.
        List<PersistentData> datas = new ArrayList<PersistentData>();
        PersistentEventRegistrationMessage message1 = createPersistedRegMessage();
        PersistentEventRegistrationMessage message2 = createPersistedRegMessage();
        
        //mock persistent data
        PersistentData data1 = mock(PersistentData.class);
        when(data1.getEntity()).thenReturn(message1.toByteArray());
        when(data1.getDescription()).thenReturn("5");
        PersistentData data2 = mock(PersistentData.class);
        when(data2.getEntity()).thenReturn(message2.toByteArray());
        when(data2.getDescription()).thenReturn("6");
        
        //add the mocks to the collection
        datas.add(data1);
        datas.add(data2);
        Collection<PersistentData> perData = datas;
        when(m_DataStore.query(EventAdminMessageService.class)).thenReturn(perData);
        
        //when the message are pulled the events will be re registered for
        ServiceRegistration<EventHandler> handler1 = mock(ServiceRegistration.class);
        ServiceRegistration<EventHandler> handler2 = mock(ServiceRegistration.class);
        
        //service registration mocks
        when(m_Context.registerService(eq(EventHandler.class), Mockito.any(EventHandler.class), 
                Mockito.any(Dictionary.class))).thenReturn(handler1, handler2);
        
        //activate
        m_SUT.activate(m_Context);
        
        verify(m_DataStore, times(2)).query(EventAdminMessageService.class);
        
        //verify the increase in the registration ids
      //construct message
        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(eventAdminMessage);
        TerraHarvestPayload payload = createPayload(eventAdminMessage);

        // mock the channel the message came from
        RemoteChannel channel = mock(RemoteChannel.class);
        
        // replay
        m_SUT.handleMessage(message, payload, channel);
        
        // verify the event contains the data message
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        Event event = eventCaptor.getValue();
        assertThat((EventRegistrationRequestData)event.getProperty(RemoteConstants.EVENT_PROP_DATA_MESSAGE),
                is(requestMessage.build()));

        ArgumentCaptor<EventRegistrationResponseData> messageCaptor = 
                ArgumentCaptor.forClass(EventRegistrationResponseData.class);
        ArgumentCaptor<Dictionary> properties = ArgumentCaptor.forClass(Dictionary.class);

        // verify that the event is being registered with the proper properties
        verify(m_Context, times(3)).registerService(eq(EventHandler.class), 
                Mockito.any(EventHandler.class), properties.capture());
        // verify response message is sent back to source requester (id=0)
        verify(m_MessageFactory).createEventAdminResponseMessage(eq(message), 
                eq(EventAdminMessageType.EventRegistrationResponse), messageCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);
        EventRegistrationResponseData receivedMessage = messageCaptor.getValue();
        //ensure that registered event has unique ID
        assertThat(receivedMessage.getId(), is(7));
    }
/**
     * Verify persisted event registrations are pulled out of the datastore.
     * Verify that the registration ids are increased and that when the largest id is found while pulling the data
     * that that id is then made the 'last reg ID' so that the next ID is one higher. 
     */
Verify persisted event registrations are pulled out of the datastore.
Verify that the registration ids are increased and that when the largest id is found while pulling the data
that that id is then made the 'last reg ID' so that the next ID is one higher.
Verify persisted event registrations are pulled out of the datastore . Verify that the registration ids are increased and that when the largest id is found while pulling the data that that id is then made the ' last reg ID ' so that the next ID is one higher .
@Test
    public void testActivateDatastorePullIncreaseRegId() throws Exception
    {
        List<PersistentData> datas = new ArrayList<PersistentData>();
        PersistentEventRegistrationMessage message1 = createPersistedRegMessage();
        PersistentEventRegistrationMessage message2 = createPersistedRegMessage();
        PersistentData data1 = mock(PersistentData.class);
        when(data1.getEntity()).thenReturn(message1.toByteArray());
        when(data1.getDescription()).thenReturn("5");
        PersistentData data2 = mock(PersistentData.class);
        when(data2.getEntity()).thenReturn(message2.toByteArray());
        when(data2.getDescription()).thenReturn("6");
        datas.add(data1);
        datas.add(data2);
        Collection<PersistentData> perData = datas;
        when(m_DataStore.query(EventAdminMessageService.class)).thenReturn(perData);
        ServiceRegistration<EventHandler> handler1 = mock(ServiceRegistration.class);
        ServiceRegistration<EventHandler> handler2 = mock(ServiceRegistration.class);
        when(m_Context.registerService(eq(EventHandler.class), Mockito.any(EventHandler.class), 
                Mockito.any(Dictionary.class))).thenReturn(handler1, handler2);
        m_SUT.activate(m_Context);
        verify(m_DataStore, times(2)).query(EventAdminMessageService.class);
        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(eventAdminMessage);
        TerraHarvestPayload payload = createPayload(eventAdminMessage);
        RemoteChannel channel = mock(RemoteChannel.class);
        m_SUT.handleMessage(message, payload, channel);
        ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
        verify(m_EventAdmin).postEvent(eventCaptor.capture());
        Event event = eventCaptor.getValue();
        assertThat((EventRegistrationRequestData)event.getProperty(RemoteConstants.EVENT_PROP_DATA_MESSAGE),
                is(requestMessage.build()));
        ArgumentCaptor<EventRegistrationResponseData> messageCaptor = 
                ArgumentCaptor.forClass(EventRegistrationResponseData.class);
        ArgumentCaptor<Dictionary> properties = ArgumentCaptor.forClass(Dictionary.class);
        verify(m_Context, times(3)).registerService(eq(EventHandler.class), 
                Mockito.any(EventHandler.class), properties.capture());
        verify(m_MessageFactory).createEventAdminResponseMessage(eq(message), 
                eq(EventAdminMessageType.EventRegistrationResponse), messageCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);
        EventRegistrationResponseData receivedMessage = messageCaptor.getValue();
        assertThat(receivedMessage.getId(), is(7));
    }
@ Test public void testActivateDatastorePullIncreaseRegId ( ) throws Exception { List < PersistentData > datas = new ArrayList < PersistentData > ( ) ; PersistentEventRegistrationMessage message1 = createPersistedRegMessage ( ) ; PersistentEventRegistrationMessage message2 = createPersistedRegMessage ( ) ; PersistentData data1 = mock ( PersistentData . class ) ; when ( data1 . getEntity ( ) ) . thenReturn ( message1 . toByteArray ( ) ) ; when ( data1 . getDescription ( ) ) . thenReturn ( "5" ) ; PersistentData data2 = mock ( PersistentData . class ) ; when ( data2 . getEntity ( ) ) . thenReturn ( message2 . toByteArray ( ) ) ; when ( data2 . getDescription ( ) ) . thenReturn ( "6" ) ; datas . add ( data1 ) ; datas . add ( data2 ) ; Collection < PersistentData > perData = datas ; when ( m_DataStore . query ( EventAdminMessageService . class ) ) . thenReturn ( perData ) ; ServiceRegistration < EventHandler > handler1 = mock ( ServiceRegistration . class ) ; ServiceRegistration < EventHandler > handler2 = mock ( ServiceRegistration . class ) ; when ( m_Context . registerService ( eq ( EventHandler . class ) , Mockito . any ( EventHandler . class ) , Mockito . any ( Dictionary . class ) ) ) . thenReturn ( handler1 , handler2 ) ; m_SUT . activate ( m_Context ) ; verify ( m_DataStore , times ( 2 ) ) . query ( EventAdminMessageService . class ) ; 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 ( eventAdminMessage ) ; TerraHarvestPayload payload = createPayload ( eventAdminMessage ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; m_SUT . handleMessage ( message , payload , channel ) ; ArgumentCaptor < Event > eventCaptor = ArgumentCaptor . forClass ( Event . class ) ; verify ( m_EventAdmin ) . postEvent ( eventCaptor . capture ( ) ) ; Event event = eventCaptor . getValue ( ) ; assertThat ( ( EventRegistrationRequestData ) event . getProperty ( RemoteConstants . EVENT_PROP_DATA_MESSAGE ) , is ( requestMessage . build ( ) ) ) ; ArgumentCaptor < EventRegistrationResponseData > messageCaptor = ArgumentCaptor . forClass ( EventRegistrationResponseData . class ) ; ArgumentCaptor < Dictionary > properties = ArgumentCaptor . forClass ( Dictionary . class ) ; verify ( m_Context , times ( 3 ) ) . registerService ( eq ( EventHandler . class ) , Mockito . any ( EventHandler . class ) , properties . capture ( ) ) ; verify ( m_MessageFactory ) . createEventAdminResponseMessage ( eq ( message ) , eq ( EventAdminMessageType . EventRegistrationResponse ) , messageCaptor . capture ( ) ) ; verify ( m_ResponseWrapper ) . queue ( channel ) ; EventRegistrationResponseData receivedMessage = messageCaptor . getValue ( ) ; assertThat ( receivedMessage . getId ( ) , is ( 7 ) ) ; }
Verify persisted event registrations are pulled out of the datastore.
Verify persisted event registrations are pulled out of the datastore .
//persistent reg messages. //mock persistent data //add the mocks to the collection //when the message are pulled the events will be re registered for //service registration mocks //activate //verify the increase in the registration ids //construct message // add required fields // mock the channel the message came from // replay // verify the event contains the data message // verify that the event is being registered with the proper properties // verify response message is sent back to source requester (id=0) //ensure that registered event has unique ID