mil.dod.th.ose.remote/test/mil/dod/th/ose/remote/mp/TestMissionProgrammingMessageService.java
@SuppressWarnings("unchecked") //mock exception for the proto converter
    @Test
    public void testInvalidGetTemplates() throws IllegalArgumentException, IOException, ObjectConverterException
    {
        //activate the mission programming message service
        m_SUT.activate();
        
        //construct the request for templates
        MissionProgrammingNamespace missionMessage = MissionProgrammingNamespace.newBuilder().
            setType(MissionProgrammingMessageType.GetTemplatesRequest).build();
        TerraHarvestPayload payload = createPayload(missionMessage);
        TerraHarvestMessage message = createMissionProgrammingMessage(missionMessage);
        
        // mock the channel the message came from
        RemoteChannel channel = mock(RemoteChannel.class);
        //mock the template manager response to get templates
        Set<MissionProgramTemplate> templates = getTemplatesSet();
        
        //add mock template to throw error
        MissionProgramTemplate templateBad = mock(MissionProgramTemplate.class);
        templates.add(templateBad);

        when(templateBad.getName()).thenReturn("NameBad");
        when(m_TemplateManager.getTemplates()).thenReturn(templates);
        //mock converter behavior
        MissionProgramTemplateGen.MissionProgramTemplate protoTemplate = MissionProgramTemplateGen.
            MissionProgramTemplate.newBuilder().setName("Name").setSource("Source").build();
        when(m_Converter.convertToProto(Mockito.any(MissionProgramTemplate.class))).
            thenReturn(protoTemplate);
        when(m_Converter.convertToProto(templateBad)).thenThrow(ObjectConverterException.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();
        //verify data message is null as there is not a data message for this request
        assertThat(event.getProperty(RemoteConstants.EVENT_PROP_DATA_MESSAGE), is(nullValue()));

        //verify the interaction with the template manager
        verify(m_TemplateManager).getTemplates();
        //verfiy that the three templates in the mocked set are passed to the converter
        verify(m_Converter, times(3)).convertToProto(Mockito.any(MissionProgramTemplate.class));
           
        // verify get templates response is sent back
        ArgumentCaptor<GetTemplatesResponseData> messageCaptor = 
            ArgumentCaptor.forClass(GetTemplatesResponseData.class);
        verify(m_MessageFactory).createMissionProgrammingResponseMessage(eq(message), 
                eq(MissionProgrammingMessageType.GetTemplatesResponse), messageCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);
        GetTemplatesResponseData response = messageCaptor.getValue();
         //verify that the two valid templates were made. Behavior is mocked..
        assertThat(response.getTemplateCount(), is(2));
        assertThat(response.getTemplateList(), hasItem(protoTemplate));
        assertThat(response.getTemplate(0).getName(), is("Name"));
        assertThat(response.getTemplate(0).getSource(), is("Source"));       
    }
/**
     * Test request to get templates with object converter error.
     * 
     * Verify that three templates are asked to be converted.
     * 
     * Verify that only two are in the response message.
     */
Test request to get templates with object converter error.

Verify that three templates are asked to be converted.

Verify that only two are in the response message.
Test request to get templates with object converter error . Verify that three templates are asked to be converted . Verify that only two are in the response message .
@SuppressWarnings("unchecked") 
    @Test
    public void testInvalidGetTemplates() throws IllegalArgumentException, IOException, ObjectConverterException
    {
        m_SUT.activate();
        MissionProgrammingNamespace missionMessage = MissionProgrammingNamespace.newBuilder().
            setType(MissionProgrammingMessageType.GetTemplatesRequest).build();
        TerraHarvestPayload payload = createPayload(missionMessage);
        TerraHarvestMessage message = createMissionProgrammingMessage(missionMessage);
        RemoteChannel channel = mock(RemoteChannel.class);
        Set<MissionProgramTemplate> templates = getTemplatesSet();
        MissionProgramTemplate templateBad = mock(MissionProgramTemplate.class);
        templates.add(templateBad);
        when(templateBad.getName()).thenReturn("NameBad");
        when(m_TemplateManager.getTemplates()).thenReturn(templates);
        MissionProgramTemplateGen.MissionProgramTemplate protoTemplate = MissionProgramTemplateGen.
            MissionProgramTemplate.newBuilder().setName("Name").setSource("Source").build();
        when(m_Converter.convertToProto(Mockito.any(MissionProgramTemplate.class))).
            thenReturn(protoTemplate);
        when(m_Converter.convertToProto(templateBad)).thenThrow(ObjectConverterException.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(event.getProperty(RemoteConstants.EVENT_PROP_DATA_MESSAGE), is(nullValue()));
        verify(m_TemplateManager).getTemplates();
        verify(m_Converter, times(3)).convertToProto(Mockito.any(MissionProgramTemplate.class));
        ArgumentCaptor<GetTemplatesResponseData> messageCaptor = 
            ArgumentCaptor.forClass(GetTemplatesResponseData.class);
        verify(m_MessageFactory).createMissionProgrammingResponseMessage(eq(message), 
                eq(MissionProgrammingMessageType.GetTemplatesResponse), messageCaptor.capture());
        verify(m_ResponseWrapper).queue(channel);
        GetTemplatesResponseData response = messageCaptor.getValue();
        assertThat(response.getTemplateCount(), is(2));
        assertThat(response.getTemplateList(), hasItem(protoTemplate));
        assertThat(response.getTemplate(0).getName(), is("Name"));
        assertThat(response.getTemplate(0).getSource(), is("Source"));       
    }
@ SuppressWarnings ( "unchecked" ) @ Test public void testInvalidGetTemplates ( ) throws IllegalArgumentException , IOException , ObjectConverterException { m_SUT . activate ( ) ; MissionProgrammingNamespace missionMessage = MissionProgrammingNamespace . newBuilder ( ) . setType ( MissionProgrammingMessageType . GetTemplatesRequest ) . build ( ) ; TerraHarvestPayload payload = createPayload ( missionMessage ) ; TerraHarvestMessage message = createMissionProgrammingMessage ( missionMessage ) ; RemoteChannel channel = mock ( RemoteChannel . class ) ; Set < MissionProgramTemplate > templates = getTemplatesSet ( ) ; MissionProgramTemplate templateBad = mock ( MissionProgramTemplate . class ) ; templates . add ( templateBad ) ; when ( templateBad . getName ( ) ) . thenReturn ( "NameBad" ) ; when ( m_TemplateManager . getTemplates ( ) ) . thenReturn ( templates ) ; MissionProgramTemplateGen . MissionProgramTemplate protoTemplate = MissionProgramTemplateGen . MissionProgramTemplate . newBuilder ( ) . setName ( "Name" ) . setSource ( "Source" ) . build ( ) ; when ( m_Converter . convertToProto ( Mockito . any ( MissionProgramTemplate . class ) ) ) . thenReturn ( protoTemplate ) ; when ( m_Converter . convertToProto ( templateBad ) ) . thenThrow ( ObjectConverterException . 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 ( event . getProperty ( RemoteConstants . EVENT_PROP_DATA_MESSAGE ) , is ( nullValue ( ) ) ) ; verify ( m_TemplateManager ) . getTemplates ( ) ; verify ( m_Converter , times ( 3 ) ) . convertToProto ( Mockito . any ( MissionProgramTemplate . class ) ) ; ArgumentCaptor < GetTemplatesResponseData > messageCaptor = ArgumentCaptor . forClass ( GetTemplatesResponseData . class ) ; verify ( m_MessageFactory ) . createMissionProgrammingResponseMessage ( eq ( message ) , eq ( MissionProgrammingMessageType . GetTemplatesResponse ) , messageCaptor . capture ( ) ) ; verify ( m_ResponseWrapper ) . queue ( channel ) ; GetTemplatesResponseData response = messageCaptor . getValue ( ) ; assertThat ( response . getTemplateCount ( ) , is ( 2 ) ) ; assertThat ( response . getTemplateList ( ) , hasItem ( protoTemplate ) ) ; assertThat ( response . getTemplate ( 0 ) . getName ( ) , is ( "Name" ) ) ; assertThat ( response . getTemplate ( 0 ) . getSource ( ) , is ( "Source" ) ) ; }
Test request to get templates with object converter error.
Test request to get templates with object converter error .
//mock exception for the proto converter //activate the mission programming message service //construct the request for templates // mock the channel the message came from //mock the template manager response to get templates //add mock template to throw error //mock converter behavior // replay // verify the event contains the data message //verify data message is null as there is not a data message for this request //verify the interaction with the template manager //verfiy that the three templates in the mocked set are passed to the converter // verify get templates response is sent back //verify that the two valid templates were made. Behavior is mocked..