ExportedVariable var = new ExportedVariable(); String variableName = entry.getKey(); var.setRandom( true ); var.setRawKind( m.group( 1 )); variableName = m.group( 2 ).trim(); var.setName( variableName ); var.setValue( entry.getValue()); result.put( var.getName(), var );
@Test public void testParseExportedVariables_listWithMixedValuesAndRandom() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = \"value1 is here\" , random[port] key2= value2, key3 = \"key33\", key4 = oops " ); Assert.assertEquals( 4, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1 is here", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertTrue( var.isRandom()); Assert.assertEquals( RandomKind.PORT, var.getRandomKind()); var = variables.get( "key3" ); Assert.assertEquals( "key33", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key4" ); Assert.assertEquals( "oops", var.getValue()); Assert.assertFalse( var.isRandom()); } }
@Test public void testEqualsAndHashCode() { ExportedVariable var1 = new ExportedVariable( "test", "value 1" ); ExportedVariable var2 = new ExportedVariable( "test", "value 2" ); HashSet<ExportedVariable> set = new HashSet<>( 2 ); set.add( var1 ); set.add( var2 ); Assert.assertEquals( 1, set.size()); Assert.assertEquals( var1, var2 ); Assert.assertFalse( var1.equals( new Object())); Assert.assertFalse( var1.equals( new ExportedVariable( "test2", "value 1" ))); Assert.assertTrue( new ExportedVariable().hashCode() > 0 ); }
/** * A trick to validate instances with random variables. * @param instance a non-null instance * @param set true to setup the trick, false to tear it down */ private static void randomVariablesTrickForValidation( Instance instance, boolean set ) { final String trickValue = "@# --- #@"; Map<String,ExportedVariable> exportedVariables = instance.getComponent() != null ? instance.getComponent().exportedVariables : new HashMap<String,ExportedVariable>( 0 ); for( ExportedVariable var : exportedVariables.values()) { if( ! var.isRandom()) continue; String overriddenExport = instance.overriddenExports.get( var.getName()); // Set and no export? => Set it. if( set && overriddenExport == null ) instance.overriddenExports.put( var.getName(), trickValue ); // Unset and trick value? => Unset it. else if( ! set && Objects.equals( trickValue, overriddenExport )) instance.overriddenExports.remove( var.getName()); } } }
@Override public synchronized void generateRandomValues( Application application, Instance instance ) { // Exported variables that are random will be set a value for( ExportedVariable var : instance.getComponent().exportedVariables.values()) { // Not random? if( ! var.isRandom()) continue; // Port if( var.getRandomKind() == RandomKind.PORT ) { // Acknowledge: verify a random value was not already set on it. // Otherwise, generate a random port and associate it. if( ! acknowledgePort( application, instance, var.getName())) generateRandomPort( application, instance, var.getName()); } } // Save the updated model ConfigurationUtils.saveInstances( application ); }
@Test public void testFromGraphs_withInheritance() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); cB.extendComponent( cA ); graphs.getRootComponents().add( cB ); cB.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "A.ip", null )); compareGraphs( graphs, false ); }
String exportedVarName = exportedVariable.getName(); if( Utils.isEmptyOrWhitespaces( exportedVarName )) errors.add( new ModelError( RM_EMPTY_VARIABLE_NAME, component, variable( exportedVarName ))); errors.add( new ModelError( RM_INVALID_VARIABLE_NAME, component, variable( exportedVarName ))); if( exportedVariable.isRandom()) { if( exportedVariable.getRandomKind() == null ) errors.add( new ModelError( RM_INVALID_RANDOM_KIND, component, unrecognized( exportedVariable.getRawKind()))); if( exportedVariable.getValue() != null ) errors.add( new ModelError( RM_NO_VALUE_FOR_RANDOM, component, variable( exportedVarName )));
@Test public void testParseExportedVariables_simpleListOfVariables() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = value1, key2=value2 , key3 = value3 " ); Assert.assertEquals( 3, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key3" ); Assert.assertEquals( "value3", var.getValue()); Assert.assertFalse( var.isRandom()); }
comp.addExportedVariable( new ExportedVariable( "comp.export1", "c1" )); comp.addExportedVariable( new ExportedVariable( "comp.export2", "c2" )); comp.addExportedVariable( new ExportedVariable( "comp.export3", "c3" )); Assert.assertEquals( "c1", comp.exportedVariables.get("comp.export1").getValue()); Assert.assertEquals( "c2", comp.exportedVariables.get("comp.export2").getValue()); Assert.assertEquals( "c3", comp.exportedVariables.get("comp.export3").getValue());
@Test public void testToString() { ExportedVariable var = new ExportedVariable( "test", null ); Assert.assertEquals( "test", var.toString()); }
private String writeExportedVariables( AbstractType type ) { StringBuilder sb = new StringBuilder(); for( Iterator<Map.Entry<String,ExportedVariable>> it=type.exportedVariables.entrySet().iterator(); it.hasNext(); ) { Map.Entry<String,ExportedVariable> entry = it.next(); sb.append( entry.getKey()); String variableValue = entry.getValue().getValue(); if( ! Utils.isEmptyOrWhitespaces( variableValue )) { sb.append( "=\"" ); sb.append( variableValue ); sb.append( "\"" ); } if( it.hasNext()) sb.append( ", " ); } return sb.toString(); }
/** * A shortcut method to add a new exported variable. * @param var a non-null variable to add */ public void addExportedVariable( ExportedVariable var ) { this.exportedVariables.put( var.getName(), var ); } }
@Test public void testFindAllExportedVariables_simple() { Component comp = new Component( "comp" ); Map<String,String> exports = ComponentHelpers.findAllExportedVariables( comp ); Assert.assertEquals( 0, exports.size()); comp.addExportedVariable( new ExportedVariable( "test", "ok" )); exports = ComponentHelpers.findAllExportedVariables( comp ); Assert.assertEquals( 1, exports.size()); Assert.assertEquals( "ok", exports.get( "comp.test" )); }
ExportedVariable originalVar = originalComponent.exportedVariables.get( entry.getKey()); Assert.assertEquals( readComponent.getName(), entry.getValue().getName(), originalVar.getName()); Assert.assertEquals( readComponent.getName(), entry.getValue().getValue(), originalVar.getValue()); Assert.assertEquals( readComponent.getName(), entry.getValue().getRandomKind(), originalVar.getRandomKind()); Assert.assertEquals( readComponent.getName(), entry.getValue().isRandom(), originalVar.isRandom());
@Override public synchronized void releaseRandomValues( Application application, Instance instance ) { // Only used for random ports. // We only remove the ports used by this instance. List<Integer> portsToRelease = new ArrayList<> (); for( ExportedVariable var : instance.getComponent().exportedVariables.values()) { if( ! var.isRandom() || var.getRandomKind() != RandomKind.PORT ) continue; String value = instance.overriddenExports.get( var.getName()); if( value != null ) portsToRelease.add( Integer.parseInt( value )); } // Update the cache InstanceContext ctx = findAgentContext( application, instance ); List<Integer> ports = this.agentToRandomPorts.get( ctx ); if( ports != null ) { ports.removeAll( portsToRelease ); if( ports.isEmpty()) this.agentToRandomPorts.remove( ctx ); } }
@Test public void testParseExportedVariables_listWithMixedValues() { Map<String,ExportedVariable> variables = VariableHelpers.parseExportedVariables( "key1 = \"value1 is here\" , key2= value2, key3 = \"key33\", key4 = oops " ); Assert.assertEquals( 4, variables.size()); ExportedVariable var = variables.get( "key1" ); Assert.assertEquals( "value1 is here", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key2" ); Assert.assertEquals( "value2", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key3" ); Assert.assertEquals( "key33", var.getValue()); Assert.assertFalse( var.isRandom()); var = variables.get( "key4" ); Assert.assertEquals( "oops", var.getValue()); Assert.assertFalse( var.isRandom()); }
String value = var.getValue().getValue(); if( localExportedVariables.containsKey( var.getKey())) localExportedVariables.put( var.getKey(), value );
for( ExportedVariable var : ModelUtils.findExportedVariables( value, block.getFile(), line ).values()) { String exportKey = var.getName(); if( Utils.isEmptyOrWhitespaces( exportKey )) result.add( new ParsingError( ErrorCode.PM_EMPTY_VARIABLE_NAME, block.getFile(), line ));
@Test public void testComponentsWithInvalidRandomPorts() throws Exception { File f = TestUtils.findTestFile( "/configurations/invalid/component-with-invalid-random-port.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs g = fromDef.buildGraphs( f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Component comp2 = ComponentHelpers.findComponent( g, "comp2" ); Assert.assertNotNull( comp2 ); Assert.assertEquals( 3, comp2.exportedVariables.size()); Assert.assertNotNull( comp2.exportedVariables.get( "ip" )); Assert.assertNotNull( comp2.exportedVariables.get( "httpPort" )); Assert.assertNull( comp2.exportedVariables.get( "httpPort" ).getValue()); Assert.assertTrue( comp2.exportedVariables.get( "httpPort" ).isRandom()); Assert.assertEquals( RandomKind.PORT, comp2.exportedVariables.get( "httpPort" ).getRandomKind()); Assert.assertNotNull( comp2.exportedVariables.get( "ajpPort" )); Assert.assertNull( comp2.exportedVariables.get( "ajpPort" ).getValue()); Assert.assertTrue( comp2.exportedVariables.get( "ajpPort" ).isRandom()); Assert.assertNull( comp2.exportedVariables.get( "ajpPort" ).getRandomKind()); }
@Test public void testFindPrefixesForExportedVariables_withComponentVariables() { Component component = new Component( "comp" ); component.addExportedVariable( new ExportedVariable( "comp.ip", "" )); component.addExportedVariable( new ExportedVariable( "comp.split.property", "" )); component.addExportedVariable( new ExportedVariable( "comp.port", "8000" )); Instance instance = new Instance( "inst" ).component( component ); Set<String> prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 1, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Facet facet = new Facet( "facet" ); facet.addExportedVariable( new ExportedVariable( "something", "value" )); component.associateFacet( facet ); prefixes = VariableHelpers.findPrefixesForExportedVariables( instance ); Assert.assertEquals( 2, prefixes.size()); Assert.assertTrue( prefixes.contains( "comp" )); Assert.assertTrue( prefixes.contains( "facet" )); }