mil.dod.th.ose.datastream/test/mil/dod/th/ose/datastream/TestDataStreamServiceImpl.java
@SuppressWarnings("unchecked")
    @Test
    public void testConfigurationListenerInvalidStreamPort() throws InvalidSyntaxException, IllegalArgumentException, 
        FactoryException, FactoryObjectInformationException, IOException, InterruptedException
    {
        String factoryPid = "factory.pid";
        String host = "225.1.2.3";
        int port = 5004;
        String spiName = "stream_profile_1";
        StreamProfileInternal spi = mock(StreamProfileInternal.class);
        
        when(m_ServiceContext.getFactories()).thenReturn(m_StreamProfileFactories);
        when(m_StreamProfileFactory.getPid()).thenReturn(factoryPid);
        when(m_Registry.createNewObjectForConfig(m_StreamProfileFactory, null, "config.pid")).thenReturn(spi);
        when(spi.getName()).thenReturn(spiName);
        doThrow(URISyntaxException.class).when(spi).setStreamPort(Mockito.any(URI.class));
        
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(DataStreamServiceConfig.CONFIG_PROP_MULTICAST_HOST, host);
        props.put(DataStreamServiceConfig.CONFIG_PROP_START_PORT, port);
        
        m_SUT.activate(Collections.unmodifiableMap(props), m_BundleContext);
        
        ArgumentCaptor<EventHandler> handlerCaptor = ArgumentCaptor.forClass(EventHandler.class);
        verify(m_BundleContext, times(2)).registerService(eq(EventHandler.class), handlerCaptor.capture(), 
                Mockito.any(Dictionary.class));
        
        ConfigurationListener configListener = (ConfigurationListener)handlerCaptor.getAllValues().get(0);
        
        Map<String, Object> topicProps = new HashMap<>();
        topicProps.put(ConfigurationEventConstants.EVENT_PROP_FACTORY_PID, factoryPid);
        topicProps.put(ConfigurationEventConstants.EVENT_PROP_PID, "config.pid");
        Event configAddedEvent = new Event(StreamProfileConfigListener.TOPIC_STREAM_PROFILE_CONFIG_UPDATED, topicProps);
        
        configListener.handleEvent(configAddedEvent);
        
        //Sleep needed to allow thread time create the object.
        Thread.sleep(500);
        
        verify(m_Registry).createNewObjectForConfig(m_StreamProfileFactory, null, "config.pid");
        verify(spi).setStreamPort(Mockito.any(URI.class));
        verify(m_Logging).error(Mockito.any(URISyntaxException.class), eq("Unable to set stream port for the stream "
                + "profile: %s"), eq(spiName));
        verify(m_WakeLock).activate();
        verify(m_WakeLock).cancel();
    }
/**
     * Verify that an exception is logged if an error occurs while attempting to set the stream port for the newly
     * create stream profile object.
     */
Verify that an exception is logged if an error occurs while attempting to set the stream port for the newly
create stream profile object.
Verify that an exception is logged if an error occurs while attempting to set the stream port for the newly create stream profile object .
@SuppressWarnings("unchecked")
    @Test
    public void testConfigurationListenerInvalidStreamPort() throws InvalidSyntaxException, IllegalArgumentException, 
        FactoryException, FactoryObjectInformationException, IOException, InterruptedException
    {
        String factoryPid = "factory.pid";
        String host = "225.1.2.3";
        int port = 5004;
        String spiName = "stream_profile_1";
        StreamProfileInternal spi = mock(StreamProfileInternal.class);
        when(m_ServiceContext.getFactories()).thenReturn(m_StreamProfileFactories);
        when(m_StreamProfileFactory.getPid()).thenReturn(factoryPid);
        when(m_Registry.createNewObjectForConfig(m_StreamProfileFactory, null, "config.pid")).thenReturn(spi);
        when(spi.getName()).thenReturn(spiName);
        doThrow(URISyntaxException.class).when(spi).setStreamPort(Mockito.any(URI.class));
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(DataStreamServiceConfig.CONFIG_PROP_MULTICAST_HOST, host);
        props.put(DataStreamServiceConfig.CONFIG_PROP_START_PORT, port);
        m_SUT.activate(Collections.unmodifiableMap(props), m_BundleContext);
        ArgumentCaptor<EventHandler> handlerCaptor = ArgumentCaptor.forClass(EventHandler.class);
        verify(m_BundleContext, times(2)).registerService(eq(EventHandler.class), handlerCaptor.capture(), 
                Mockito.any(Dictionary.class));
        ConfigurationListener configListener = (ConfigurationListener)handlerCaptor.getAllValues().get(0);
        Map<String, Object> topicProps = new HashMap<>();
        topicProps.put(ConfigurationEventConstants.EVENT_PROP_FACTORY_PID, factoryPid);
        topicProps.put(ConfigurationEventConstants.EVENT_PROP_PID, "config.pid");
        Event configAddedEvent = new Event(StreamProfileConfigListener.TOPIC_STREAM_PROFILE_CONFIG_UPDATED, topicProps);
        configListener.handleEvent(configAddedEvent);
        Thread.sleep(500);
        verify(m_Registry).createNewObjectForConfig(m_StreamProfileFactory, null, "config.pid");
        verify(spi).setStreamPort(Mockito.any(URI.class));
        verify(m_Logging).error(Mockito.any(URISyntaxException.class), eq("Unable to set stream port for the stream "
                + "profile: %s"), eq(spiName));
        verify(m_WakeLock).activate();
        verify(m_WakeLock).cancel();
    }
@ SuppressWarnings ( "unchecked" ) @ Test public void testConfigurationListenerInvalidStreamPort ( ) throws InvalidSyntaxException , IllegalArgumentException , FactoryException , FactoryObjectInformationException , IOException , InterruptedException { String factoryPid = "factory.pid" ; String host = "225.1.2.3" ; int port = 5004 ; String spiName = "stream_profile_1" ; StreamProfileInternal spi = mock ( StreamProfileInternal . class ) ; when ( m_ServiceContext . getFactories ( ) ) . thenReturn ( m_StreamProfileFactories ) ; when ( m_StreamProfileFactory . getPid ( ) ) . thenReturn ( factoryPid ) ; when ( m_Registry . createNewObjectForConfig ( m_StreamProfileFactory , null , "config.pid" ) ) . thenReturn ( spi ) ; when ( spi . getName ( ) ) . thenReturn ( spiName ) ; doThrow ( URISyntaxException . class ) . when ( spi ) . setStreamPort ( Mockito . any ( URI . class ) ) ; Map < String , Object > props = new HashMap < String , Object > ( ) ; props . put ( DataStreamServiceConfig . CONFIG_PROP_MULTICAST_HOST , host ) ; props . put ( DataStreamServiceConfig . CONFIG_PROP_START_PORT , port ) ; m_SUT . activate ( Collections . unmodifiableMap ( props ) , m_BundleContext ) ; ArgumentCaptor < EventHandler > handlerCaptor = ArgumentCaptor . forClass ( EventHandler . class ) ; verify ( m_BundleContext , times ( 2 ) ) . registerService ( eq ( EventHandler . class ) , handlerCaptor . capture ( ) , Mockito . any ( Dictionary . class ) ) ; ConfigurationListener configListener = ( ConfigurationListener ) handlerCaptor . getAllValues ( ) . get ( 0 ) ; Map < String , Object > topicProps = new HashMap < > ( ) ; topicProps . put ( ConfigurationEventConstants . EVENT_PROP_FACTORY_PID , factoryPid ) ; topicProps . put ( ConfigurationEventConstants . EVENT_PROP_PID , "config.pid" ) ; Event configAddedEvent = new Event ( StreamProfileConfigListener . TOPIC_STREAM_PROFILE_CONFIG_UPDATED , topicProps ) ; configListener . handleEvent ( configAddedEvent ) ; Thread . sleep ( 500 ) ; verify ( m_Registry ) . createNewObjectForConfig ( m_StreamProfileFactory , null , "config.pid" ) ; verify ( spi ) . setStreamPort ( Mockito . any ( URI . class ) ) ; verify ( m_Logging ) . error ( Mockito . any ( URISyntaxException . class ) , eq ( "Unable to set stream port for the stream " + "profile: %s" ) , eq ( spiName ) ) ; verify ( m_WakeLock ) . activate ( ) ; verify ( m_WakeLock ) . cancel ( ) ; }
Verify that an exception is logged if an error occurs while attempting to set the stream port for the newly
 create stream profile object.
Verify that an exception is logged if an error occurs while attempting to set the stream port for the newly create stream profile object .
//Sleep needed to allow thread time create the object.