@Test
public void testGetApplicationBindings_success_withExternalImports() throws Exception {

	// Add external imports
	ImportedVariable newVar = new ImportedVariable( "prefix1.ip", false, true );
	this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar );
	newVar = new ImportedVariable( "prefix2.something", false, true );
	this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar );
	newVar = new ImportedVariable( "prefix3.else", false, true );
	this.app.getWar().getComponent().importedVariables.put( newVar.getName(), newVar );
	// Create some fake applications
	Map<String,String> nameToExternalExportPrefix = new LinkedHashMap<>( 4 );
	nameToExternalExportPrefix.put( "app1", "prefix1" );
	nameToExternalExportPrefix.put( "this_app", "prefix2" );
	nameToExternalExportPrefix.put( "app2", "prefix1" );
	nameToExternalExportPrefix.put( "app", null );
	for( Map.Entry<String,String> entry : nameToExternalExportPrefix.entrySet()) {
		// Register
		TestApplication ta = new TestApplication();
		ta.setDirectory( this.folder.newFolder());
		ta.setName( entry.getKey());
		ta.getTemplate().setExternalExportsPrefix( entry.getValue());
		this.managerWrapper.addManagedApplication( new ManagedApplication( ta ));
		// Bind
		if( entry.getValue() != null )
			this.app.bindWithApplication( entry.getValue(), ta.getName());
	}
	// Add an invalid binding.
	// It should not be listed farther.
	this.app.bindWithApplication( "invalid", "app" );
	// Verify the bindings
	Response resp = this.resource.getApplicationBindings( this.app.getName());
	Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus());
	ApplicationBindings bindings = (ApplicationBindings) resp.getEntity();
	Assert.assertEquals( 3, bindings.prefixToItems.size());
	List<ApplicationBindingItem> items = bindings.prefixToItems.get( "prefix1" );
	Assert.assertEquals( 2, items.size());
	Assert.assertTrue( items.get( 0 ).isBound());
	Assert.assertTrue( items.get( 1 ).isBound());
	List<String> boundAppNames = new ArrayList<> ();
	boundAppNames.add( items.get( 0 ).getApplicationName());
	boundAppNames.add( items.get( 1 ).getApplicationName());
	Collections.sort( boundAppNames );
	Assert.assertEquals( Arrays.asList( "app1", "app2" ), boundAppNames );
	items = bindings.prefixToItems.get( "prefix2" );
	Assert.assertEquals( 1, items.size());
	Assert.assertTrue( items.get( 0 ).isBound());
	Assert.assertEquals( "this_app", items.get( 0 ).getApplicationName());
	items = bindings.prefixToItems.get( "prefix3" );
	Assert.assertEquals( 0, items.size());
	// Unbind
	Assert.assertTrue( this.app.unbindFromApplication( "prefix1", "app2" ));
	resp = this.resource.getApplicationBindings( this.app.getName());
	Assert.assertEquals( Status.OK.getStatusCode(), resp.getStatus());
	bindings = (ApplicationBindings) resp.getEntity();
	Assert.assertEquals( 3, bindings.prefixToItems.size());
	items = bindings.prefixToItems.get( "prefix1" );
	Assert.assertEquals( 2, items.size());
	if( "app1".equals( items.get( 1 ).getApplicationName())) {
		ApplicationBindingItem itemToMove = items.remove( 1 );
		items.add( 0, itemToMove );
	}
	Assert.assertEquals( "app1", items.get( 0 ).getApplicationName());
	Assert.assertTrue( items.get( 0 ).isBound());
	Assert.assertEquals( "app2", items.get( 1 ).getApplicationName());
	Assert.assertFalse( items.get( 1 ).isBound());
	items = bindings.prefixToItems.get( "prefix2" );
	Assert.assertEquals( 1, items.size());
	Assert.assertTrue( items.get( 0 ).isBound());
	Assert.assertEquals( "this_app", items.get( 0 ).getApplicationName());
	items = bindings.prefixToItems.get( "prefix3" );
	Assert.assertEquals( 0, items.size());
}