ImportHelpers { public static Import buildTailoredImport( Instance instanceThatUseTheImport, String exportingInstancePath, String exportingInstanceComponent, Map<String,String> exportedVariables ) { Import imp = new Import( exportingInstancePath, exportingInstanceComponent ); if( exportedVariables != null && ! exportedVariables.isEmpty()) { Component comp = instanceThatUseTheImport.getComponent(); for( ImportedVariable var : ComponentHelpers.findAllImportedVariables( comp ).values()) { String importedVariable = var.getName(); if( var.getName().endsWith( "." + Constants.WILDCARD )) { String prefix = VariableHelpers.parseVariableName( importedVariable ).getKey(); for( Map.Entry<String,String> entry : exportedVariables.entrySet()) { String exportedVariable = entry.getKey(); if( Utils.isEmptyOrWhitespaces( exportedVariable )) continue; String exportedVarPrefix = VariableHelpers.parseVariableName( exportedVariable ).getKey(); if( prefix.equals( exportedVarPrefix )) imp.getExportedVars().put( entry.getKey(), entry.getValue()); } } else if( exportedVariables.containsKey( importedVariable )) imp.getExportedVars().put( importedVariable, exportedVariables.get( importedVariable )); } } return imp; } private  ImportHelpers(); static boolean hasAllRequiredImports( Instance instance, Logger logger ); static Import buildTailoredImport(
			Instance instanceThatUseTheImport,
			String exportingInstancePath,
			String exportingInstanceComponent,
			Map<String,String> exportedVariables ); static void addImport( Instance instance, String componentOrFacetName, Import imp ); static void updateImports( Instance instance, Map<String,Collection<Import>> variablePrefixToImports ); static Import findImportByExportingInstance( Collection<Import> imports, String exportingInstancePath );  }
@Test public void testBuildTailoredImport() { String instancePath = "/whatever/this/is/a-test"; Component comp = new Component( "comp" ); comp.addImportedVariable( new ImportedVariable( "comp1.port", false, false )); comp.addImportedVariable( new ImportedVariable( "comp1.ip", false, false )); comp.addImportedVariable( new ImportedVariable( "comp2.option", true, false )); Instance inst = new Instance( "inst" ).component( comp ); Import imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", null ); Assert.assertEquals( instancePath, imp.getInstancePath()); Assert.assertEquals( 0, imp.getExportedVars().size()); imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", new HashMap<String,String> ()); Assert.assertEquals( 0, imp.getExportedVars().size()); Map<String,String> map = new HashMap<String,String> (); map.put( "comp1.ip", "127.0.0.1" ); map.put( "comp2.option", "ciao!" ); map.put( "not.a.valid.variable", "yeah" ); map.put( null, "null" ); map.put( "", "hop" ); imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", map ); Assert.assertEquals( instancePath, imp.getInstancePath()); Assert.assertEquals( 2, imp.getExportedVars().size()); Assert.assertEquals( "comp", imp.getComponentName()); Assert.assertEquals( "127.0.0.1", imp.getExportedVars().get( "comp1.ip" )); Assert.assertEquals( "ciao!", imp.getExportedVars().get( "comp2.option" )); }
@Test public void testBuildTailoredImport_withWildcard() { String instancePath = "/whatever/this/is/a-test"; Component comp = new Component( "comp" ); comp.addImportedVariable( new ImportedVariable( "comp1.*", false, false )); comp.addImportedVariable( new ImportedVariable( "comp2.option", true, false )); Instance inst = new Instance( "inst" ).component( comp ); Import imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", null ); Assert.assertEquals( instancePath, imp.getInstancePath()); Assert.assertEquals( 0, imp.getExportedVars().size()); imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", new HashMap<String,String> ()); Assert.assertEquals( 0, imp.getExportedVars().size()); Map<String,String> map = new HashMap<String,String> (); map.put( "comp1.ip", "127.0.0.1" ); map.put( "comp2.option", "ciao!" ); map.put( "not.a.valid.variable", "yeah" ); map.put( null, "null" ); map.put( "", "hop" ); imp = ImportHelpers.buildTailoredImport( inst, instancePath, "comp", map ); Assert.assertEquals( instancePath, imp.getInstancePath()); Assert.assertEquals( 2, imp.getExportedVars().size()); Assert.assertEquals( "comp", imp.getComponentName()); Assert.assertEquals( "127.0.0.1", imp.getExportedVars().get( "comp1.ip" )); Assert.assertEquals( "ciao!", imp.getExportedVars().get( "comp2.option" )); }