mil.dod.th.ose.core/test/mil/dod/th/ose/core/factory/api/TestFactoryRegistry.java
@Test
    public void testPendingObjectHandleEventMultipleDeps() throws Exception
    {
        String someProp = "hgjdksla";
        String someProp2 = "hgjdksla";
        List<RegistryDependency> deps = new ArrayList<>();
        deps.add(new RegistryDependency(someProp, true)
        {
            //first time return null will be checked during creation,
            //second time return value, this will be first handle event check
            private int m_InvocCount = 0;
            @Override
            public Object findDependency(String objectName)
            {
                m_InvocCount ++;
                switch (m_InvocCount)
                {
                    case 1:
                        return null;
                    default:
                        return "jklsdflksjdflkj";
                }
            }
        });
        deps.add(new RegistryDependency(someProp, true)
        {
            //first time return null too, so that we can verify the above dep will
            //not trigger the object to be created - not this will not be checked the first time because the
            //dep above will be checked first
            //second time return value verifying that object will be created only once ALL
            //deps are satisfied
            private int m_InvocCount = 0;
            @Override
            public Object findDependency(String objectName)
            {
                m_InvocCount ++;
                switch (m_InvocCount)
                {
                    case 1:
                        return null;
                    default:
                        return "jklsdflksjdflkj";
                }
            }
        });
        when(m_Callback.retrieveRegistryDependencies()).thenReturn(deps);
        m_SUT.initialize(m_FactoryService, m_ServiceProxy, m_Callback);

        String pid = "pid";
        String name = "name";
        ComponentInstance instance = mockComponentInstance(UUID.randomUUID(), name, pid);
        FactoryObjectProxy proxy = mock(FactoryObjectProxy.class);

        when(m_FactoryDescriptor.create()).thenReturn(proxy);
        when(m_FactoryDescriptor.getProductType()).thenReturn("product type");
        when(m_FactoryObjectDataManager.getPersistentUuid("name")).thenReturn(null);
        when(m_FactoryObjectDataManager.getPid(Mockito.any(UUID.class))).thenReturn(pid);
        when(m_FactoryObjectDataManager.getName(Mockito.any(UUID.class))).
            thenReturn(FactoryObjectProxy.class.getSimpleName());
        when(m_ServiceProxy.createFactoryObjectInternal(m_FactoryDescriptor)).thenReturn(instance);
        Configuration config = mock(Configuration.class);
        doReturn(config).when(m_ConfigAdmin).createFactoryConfiguration(anyString(), anyString());
        when(m_ConfigAdmin.listConfigurations(anyString())).thenReturn(new Configuration[] { config });
        when(config.getPid()).thenReturn(pid);

        Map<String, Object> propDeps = new HashMap<>();
        propDeps.put(someProp, "peanut");
        propDeps.put(someProp2, "butter");
        when(config.getProperties()).thenReturn(new Hashtable<>(propDeps));

        try
        {
            m_SUT.createNewObject(m_FactoryDescriptor, name, propDeps);
            fail("Expected as the deps are not satisfied.");
        }
        catch (FactoryException e)
        {
            //expected exception
        }
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(someProp, "somethingelse");

        m_EventHandler.handleEvent(new Event(FactoryDescriptor.TOPIC_FACTORY_OBJ_CREATED, props));

        //verify still not satisfied
        assertThat(m_SUT.isObjectCreated(name), is(false));

        props.put(someProp2, "somethingelse");
        m_EventHandler.handleEvent(new Event(FactoryDescriptor.TOPIC_FACTORY_OBJ_CREATED, props));

        //verify
        assertThat(m_SUT.isObjectCreated(name), is(true));
    }
/**
     * Verify an unsatisfied object is added to pending objects.
     */
Verify an unsatisfied object is added to pending objects.
Verify an unsatisfied object is added to pending objects .
@Test
    public void testPendingObjectHandleEventMultipleDeps() throws Exception
    {
        String someProp = "hgjdksla";
        String someProp2 = "hgjdksla";
        List<RegistryDependency> deps = new ArrayList<>();
        deps.add(new RegistryDependency(someProp, true)
        {
            private int m_InvocCount = 0;
            @Override
            public Object findDependency(String objectName)
            {
                m_InvocCount ++;
                switch (m_InvocCount)
                {
                    case 1:
                        return null;
                    default:
                        return "jklsdflksjdflkj";
                }
            }
        });
        deps.add(new RegistryDependency(someProp, true)
        {
            private int m_InvocCount = 0;
            @Override
            public Object findDependency(String objectName)
            {
                m_InvocCount ++;
                switch (m_InvocCount)
                {
                    case 1:
                        return null;
                    default:
                        return "jklsdflksjdflkj";
                }
            }
        });
        when(m_Callback.retrieveRegistryDependencies()).thenReturn(deps);
        m_SUT.initialize(m_FactoryService, m_ServiceProxy, m_Callback);
        String pid = "pid";
        String name = "name";
        ComponentInstance instance = mockComponentInstance(UUID.randomUUID(), name, pid);
        FactoryObjectProxy proxy = mock(FactoryObjectProxy.class);
        when(m_FactoryDescriptor.create()).thenReturn(proxy);
        when(m_FactoryDescriptor.getProductType()).thenReturn("product type");
        when(m_FactoryObjectDataManager.getPersistentUuid("name")).thenReturn(null);
        when(m_FactoryObjectDataManager.getPid(Mockito.any(UUID.class))).thenReturn(pid);
        when(m_FactoryObjectDataManager.getName(Mockito.any(UUID.class))).
            thenReturn(FactoryObjectProxy.class.getSimpleName());
        when(m_ServiceProxy.createFactoryObjectInternal(m_FactoryDescriptor)).thenReturn(instance);
        Configuration config = mock(Configuration.class);
        doReturn(config).when(m_ConfigAdmin).createFactoryConfiguration(anyString(), anyString());
        when(m_ConfigAdmin.listConfigurations(anyString())).thenReturn(new Configuration[] { config });
        when(config.getPid()).thenReturn(pid);
        Map<String, Object> propDeps = new HashMap<>();
        propDeps.put(someProp, "peanut");
        propDeps.put(someProp2, "butter");
        when(config.getProperties()).thenReturn(new Hashtable<>(propDeps));
        try
        {
            m_SUT.createNewObject(m_FactoryDescriptor, name, propDeps);
            fail("Expected as the deps are not satisfied.");
        }
        catch (FactoryException e)
        {
        }
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(someProp, "somethingelse");
        m_EventHandler.handleEvent(new Event(FactoryDescriptor.TOPIC_FACTORY_OBJ_CREATED, props));
        assertThat(m_SUT.isObjectCreated(name), is(false));
        props.put(someProp2, "somethingelse");
        m_EventHandler.handleEvent(new Event(FactoryDescriptor.TOPIC_FACTORY_OBJ_CREATED, props));
        assertThat(m_SUT.isObjectCreated(name), is(true));
    }
@ Test public void testPendingObjectHandleEventMultipleDeps ( ) throws Exception { String someProp = "hgjdksla" ; String someProp2 = "hgjdksla" ; List < RegistryDependency > deps = new ArrayList < > ( ) ; deps . add ( new RegistryDependency ( someProp , true ) { private int m_InvocCount = 0 ; @ Override public Object findDependency ( String objectName ) { m_InvocCount ++ ; switch ( m_InvocCount ) { case 1 : return null ; default : return "jklsdflksjdflkj" ; } } } ) ; deps . add ( new RegistryDependency ( someProp , true ) { private int m_InvocCount = 0 ; @ Override public Object findDependency ( String objectName ) { m_InvocCount ++ ; switch ( m_InvocCount ) { case 1 : return null ; default : return "jklsdflksjdflkj" ; } } } ) ; when ( m_Callback . retrieveRegistryDependencies ( ) ) . thenReturn ( deps ) ; m_SUT . initialize ( m_FactoryService , m_ServiceProxy , m_Callback ) ; String pid = "pid" ; String name = "name" ; ComponentInstance instance = mockComponentInstance ( UUID . randomUUID ( ) , name , pid ) ; FactoryObjectProxy proxy = mock ( FactoryObjectProxy . class ) ; when ( m_FactoryDescriptor . create ( ) ) . thenReturn ( proxy ) ; when ( m_FactoryDescriptor . getProductType ( ) ) . thenReturn ( "product type" ) ; when ( m_FactoryObjectDataManager . getPersistentUuid ( "name" ) ) . thenReturn ( null ) ; when ( m_FactoryObjectDataManager . getPid ( Mockito . any ( UUID . class ) ) ) . thenReturn ( pid ) ; when ( m_FactoryObjectDataManager . getName ( Mockito . any ( UUID . class ) ) ) . thenReturn ( FactoryObjectProxy . class . getSimpleName ( ) ) ; when ( m_ServiceProxy . createFactoryObjectInternal ( m_FactoryDescriptor ) ) . thenReturn ( instance ) ; Configuration config = mock ( Configuration . class ) ; doReturn ( config ) . when ( m_ConfigAdmin ) . createFactoryConfiguration ( anyString ( ) , anyString ( ) ) ; when ( m_ConfigAdmin . listConfigurations ( anyString ( ) ) ) . thenReturn ( new Configuration [ ] { config } ) ; when ( config . getPid ( ) ) . thenReturn ( pid ) ; Map < String , Object > propDeps = new HashMap < > ( ) ; propDeps . put ( someProp , "peanut" ) ; propDeps . put ( someProp2 , "butter" ) ; when ( config . getProperties ( ) ) . thenReturn ( new Hashtable < > ( propDeps ) ) ; try { m_SUT . createNewObject ( m_FactoryDescriptor , name , propDeps ) ; fail ( "Expected as the deps are not satisfied." ) ; } catch ( FactoryException e ) { } final Dictionary < String , Object > props = new Hashtable < String , Object > ( ) ; props . put ( someProp , "somethingelse" ) ; m_EventHandler . handleEvent ( new Event ( FactoryDescriptor . TOPIC_FACTORY_OBJ_CREATED , props ) ) ; assertThat ( m_SUT . isObjectCreated ( name ) , is ( false ) ) ; props . put ( someProp2 , "somethingelse" ) ; m_EventHandler . handleEvent ( new Event ( FactoryDescriptor . TOPIC_FACTORY_OBJ_CREATED , props ) ) ; assertThat ( m_SUT . isObjectCreated ( name ) , is ( true ) ) ; }
Verify an unsatisfied object is added to pending objects.
Verify an unsatisfied object is added to pending objects .
//first time return null will be checked during creation, //second time return value, this will be first handle event check //first time return null too, so that we can verify the above dep will //not trigger the object to be created - not this will not be checked the first time because the //dep above will be checked first //second time return value verifying that object will be created only once ALL //deps are satisfied //expected exception //verify still not satisfied //verify