InstanceHelpers { public static boolean tryToInsertChildInstance( AbstractApplication application, Instance parentInstance, Instance childInstance ) { boolean hasAlreadyAChildWithThisName = hasChildWithThisName( application, parentInstance, childInstance.getName()); boolean success = false; if( parentInstance == null ) { if( ! hasAlreadyAChildWithThisName && ComponentHelpers.findAllAncestors( childInstance.getComponent()).isEmpty()) { application.getRootInstances().add( childInstance ); success = true; } } else { if( ! hasAlreadyAChildWithThisName && ComponentHelpers.findAllChildren( parentInstance.getComponent()).contains( childInstance.getComponent())) { InstanceHelpers.insertChild( parentInstance, childInstance ); Collection<Instance> allInstances = InstanceHelpers.getAllInstances( application ); Collection<ModelError> errors = RuntimeModelValidator.validate( allInstances ); if( RoboconfErrorHelpers.containsCriticalErrors( errors )) { parentInstance.getChildren().remove( childInstance ); childInstance.setParent( null ); } else { success = true; } } } return success; } private  InstanceHelpers(); static String computeInstancePath( Instance inst ); static boolean haveSamePath( Instance i1, Instance i2 ); static String findInstanceName( String instancePath ); static List<Instance> buildHierarchicalList( Instance inst ); static void insertChild( Instance parent, Instance child ); static Map<String,String> findAllExportedVariables( Instance instance ); static Instance findInstanceByPath( AbstractApplication application, String instancePath ); static Instance findInstanceByPath( Instance rootInstance, String instancePath ); static List<Instance> findInstancesByComponentName( AbstractApplication application, String componentName ); static Instance findRootInstance( Instance instance ); static Instance findScopedInstance( Instance instance ); static List<Instance> findAllScopedInstances( AbstractApplication app ); static List<Instance> getAllInstances( AbstractApplication application ); static boolean tryToInsertChildInstance( AbstractApplication application, Instance parentInstance, Instance childInstance ); static boolean hasChildWithThisName( AbstractApplication application, Instance parentInstance, String nameToSearch ); static File findInstanceDirectoryOnAgent( Instance instance ); static int countInstances( String instancePath ); static Instance replicateInstance( Instance instance ); static boolean isTarget( Instance instance ); static void removeOffScopeInstances( Instance scopedInstance ); static String findRootInstancePath( String scopedInstancePath ); static void fixOverriddenExports( Instance instance );  }
@Test public void testTryToInsertChildInstance() throws Exception { File directory = TestUtils.findTestFile( "/applications/lamp-legacy-with-only-components" ); ApplicationLoadResult result = RuntimeModelIo.loadApplication( directory ); Assert.assertNotNull( result ); Assert.assertNotNull( result.getApplicationTemplate()); Assert.assertFalse( RoboconfErrorHelpers.containsCriticalErrors( result.getLoadErrors())); ApplicationTemplate app = result.getApplicationTemplate(); app.getRootInstances().clear(); Assert.assertEquals( 0, InstanceHelpers.getAllInstances( app ).size()); Instance vmInstance = new Instance( "vm-1" ).component( ComponentHelpers.findComponent( app.getGraphs(), "VM" )); Assert.assertTrue( InstanceHelpers.tryToInsertChildInstance( app, null, vmInstance )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, null, vmInstance )); Assert.assertEquals( 1, InstanceHelpers.getAllInstances( app ).size()); Instance tomcatInstance_1 = new Instance( "tomcat-1" ).component( ComponentHelpers.findComponent( app.getGraphs(), "Tomcat" )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, null, tomcatInstance_1 )); Assert.assertTrue( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, tomcatInstance_1 )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, tomcatInstance_1 )); Assert.assertEquals( 2, InstanceHelpers.getAllInstances( app ).size()); Instance mySqlInstance_1 = new Instance( "MySQL-1" ).component( ComponentHelpers.findComponent( app.getGraphs(), "MySQL" )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, tomcatInstance_1, mySqlInstance_1 )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, mySqlInstance_1, tomcatInstance_1 )); Assert.assertEquals( 2, InstanceHelpers.getAllInstances( app ).size()); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, mySqlInstance_1 )); mySqlInstance_1.overriddenExports.put( "port", "3307" ); Assert.assertTrue( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, mySqlInstance_1 )); Assert.assertEquals( 3, InstanceHelpers.getAllInstances( app ).size()); Instance instanceWithNoComponent = new Instance( "MySQL-2" ); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, instanceWithNoComponent )); Assert.assertEquals( 3, InstanceHelpers.getAllInstances( app ).size()); Instance instWithInvalidName = new Instance( "inst!!!" ).component( ComponentHelpers.findComponent( app.getGraphs(), "Apache" )); Assert.assertFalse( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, instWithInvalidName )); Assert.assertEquals( 3, InstanceHelpers.getAllInstances( app ).size()); instWithInvalidName.setName( "whatever" ); Assert.assertTrue( InstanceHelpers.tryToInsertChildInstance( app, vmInstance, instWithInvalidName )); Assert.assertEquals( 4, InstanceHelpers.getAllInstances( app ).size()); }