@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());
	// Invalid application => no insertion
	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());
}