if( graphs.getRootComponents().isEmpty()) errors.add( new ModelError( RM_NO_ROOT_COMPONENT, graphs )); for( Component rootComponent : graphs.getRootComponents()) { if( ! ComponentHelpers.findAllAncestors( rootComponent ).isEmpty()) errors.add( new ModelError( RM_NOT_A_ROOT_COMPONENT, rootComponent, component( rootComponent ))); for( Facet f : graphs.getFacetNameToFacet().values()) { facetVariables.addAll( f.exportedVariables.keySet()); facetVariables.add( f.getName() + "." + Constants.WILDCARD ); for( Facet f : graphs.getFacetNameToFacet().values()) { if( f.getAssociatedComponents().isEmpty()) { if( f.getChildren().isEmpty())
/** * Finds a component by name from another component. * @param component the component used to build a partial graph (should not be null) * @param componentName the component name (not null) * @return a component (can be null) */ public static Component findComponentFrom( Component component, String componentName ) { Graphs partialGraph = new Graphs(); if( component != null ) partialGraph.getRootComponents().add( component ); return findComponent( partialGraph, componentName ); }
private Graphs buildFinalGraphs() { Graphs result = new Graphs(); for( ComponentData cd : this.componentNameToComponentData.values()) { if( ComponentHelpers.findAllAncestors( cd.object ).isEmpty()) result.getRootComponents().add( cd.object ); } for( FacetData data : this.facetNameToFacetData.values()) result.getFacetNameToFacet().put( data.object.getName(), data.object ); return result; }
@Override protected String renderDocumentIndex() { // What keys should we inject in the index? List<String> keys = new ArrayList<> (); keys.add( "introduction" ); keys.add( "components" ); if( this.options.containsKey( DocConstants.OPTION_RECIPE )) { if( ! this.applicationTemplate.getGraphs().getFacetNameToFacet().isEmpty()) keys.add( "facets" ); } else { keys.add( "instances" ); } // Create the index StringBuilder sb = new StringBuilder(); sb.append( "<ul>\n" ); for( String key : keys ) { sb.append( "\t<li><a href=\"index.html#" ); sb.append( this.messages.get( key ).toLowerCase()); sb.append( "\">" ); sb.append( this.messages.get( key )); sb.append( "</a></li>\n" ); } sb.append( "</ul>\n" ); this.menu = sb.toString(); return ""; }
/** * Finds all the components of a graph. * <p> * Inheritance cycles are ignored. * </p> * * @param graphs a set of graphs * @return a non-null list of components */ public static List<Component> findAllComponents( Graphs graphs ) { Set<Component> result = new HashSet<> (); Set<Component> toProcess = new HashSet<> (); toProcess.addAll( graphs.getRootComponents()); while( ! toProcess.isEmpty()) { Component current = toProcess.iterator().next(); result.add( current ); toProcess.addAll( findAllExtendedComponents( current )); toProcess.addAll( findAllExtendingComponents( current )); toProcess.addAll( findAllChildren( current )); toProcess.addAll( findAllAncestors( current )); // Prevent loops toProcess.removeAll( result ); } return new ArrayList<>( result ); }
@Test public void testChain() { ApplicationTemplate app = new ApplicationTemplate().name( "ins" ).description( "desc" ).version( "snapshot" ).graphs( new Graphs()); Assert.assertEquals( "ins", app.getName()); Assert.assertEquals( "desc", app.getDescription()); Assert.assertEquals( "snapshot", app.getVersion()); Assert.assertNotNull( app.getGraphs()); }
Graphs graph = new Graphs(); app.setGraphs( graph ); graph.getFacetNameToFacet().put( facetDeployable.getName(), facetDeployable ); graph.getFacetNameToFacet().put( facetVm.getName(), facetVm ); facetVm.addChild( facetDeployable ); facetVm.addExportedVariable( new ExportedVariable( Constants.SPECIFIC_VARIABLE_IP, null )); graph.getRootComponents().add( root1 ); graph.getRootComponents().add( root2 ); graph.getFacetNameToFacet().put( facetJee.getName(), facetJee ); facetJee.addExportedVariable( new ExportedVariable( "server-suffix", "/path" )); facetJee.extendFacet( facetDeployable ); graph.getFacetNameToFacet().put( facetWeb.getName(), facetWeb ); facetWeb.addExportedVariable( new ExportedVariable( "server-suffix", "/path" )); facetWeb.extendFacet( facetDeployable ); graph.getFacetNameToFacet().put( facetDb.getName(), facetDb ); facetDb.addExportedVariable( new ExportedVariable( "port", "3306" )); facetDb.extendFacet( facetDeployable ); graph.getFacetNameToFacet().put( facetStorage.getName(), facetStorage ); facetStorage.extendFacet( facetDb );
keys.add( "components" ); if( this.options.containsKey( DocConstants.OPTION_RECIPE ) ) { if( ! this.applicationTemplate.getGraphs().getFacetNameToFacet().isEmpty() ) keys.add( "facets" );
@Test public void test_WithSpecialNames() throws Exception { File f = TestUtils.findTestFile( "/configurations/valid/special-names.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs graphs = fromDef.buildGraphs( f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( 4, graphs.getRootComponents().size()); List<String> componentNames = new ArrayList<> (); for( Component component : graphs.getRootComponents() ) { componentNames.add( component.getName()); } Assert.assertTrue( componentNames.contains( "ImportingComponent" )); Assert.assertTrue( componentNames.contains( "ExportingComponent" )); Assert.assertTrue( componentNames.contains( "FacetComponent" )); Assert.assertTrue( componentNames.contains( "InstanceOfComponent" )); }
@Test public void testInstancesWithWrongImport() throws Exception { Graphs graphs = new Graphs(); graphs.getRootComponents().add( new Component( "VM" ).installerName( "target" )); File f = TestUtils.findTestFile( "/configurations/invalid/instances-with-inexisting-import.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_UNREACHABLE_FILE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testIncompleteFilesResultInEmptyGraphByDefault() throws Exception { List<File> files = TestUtils.findTestFiles( "/configurations/flexibles" ); Assert.assertNotSame( 0, files.size()); List<String> almostValids = Arrays.asList( new String[] { "one-component-no-property.graph", "two-components-no-property.graph" }); File directory = files.iterator().next().getParentFile(); for( File f : files ) { // There are some exceptions if( almostValids.contains( f.getName())) continue; // Others contain syntactic errors and no component was found FromGraphDefinition fromDef = new FromGraphDefinition( directory ); Graphs graph = fromDef.buildGraphs( f ); Assert.assertNotSame( f.getName(), 0, fromDef.getErrors().size()); Assert.assertEquals( f.getName(), 0, graph.getRootComponents().size()); Assert.assertEquals( f.getName(), 0, graph.getFacetNameToFacet().size()); } } }
@Override protected String renderDocumentIndex() { // What keys should we inject in the index? List<String> keys = new ArrayList<> (); keys.add( "introduction" ); keys.add( "components" ); if( this.options.containsKey( DocConstants.OPTION_RECIPE )) { if( ! this.applicationTemplate.getGraphs().getFacetNameToFacet().isEmpty()) keys.add( "facets" ); } else { keys.add( "instances" ); } // Create the index StringBuilder sb = new StringBuilder(); sb.append( "<ul>\n" ); for( String key : keys ) { sb.append( "\t<li><a href=\"index.html#" ); sb.append( this.messages.get( key ).toLowerCase()); sb.append( "\">" ); sb.append( this.messages.get( key )); sb.append( "</a></li>\n" ); } sb.append( "</ul>\n" ); this.menu = sb.toString(); return ""; }
candidates = ComponentHelpers.findAllChildren( parentComponent ); else if( Utils.isEmptyOrWhitespaces( ctx.parentInstanceType )) candidates = alr.getApplicationTemplate().getGraphs().getRootComponents(); else this.errorMsg = "Component " + ctx.parentInstanceType + " does not exist.";
@Test public void testGraphs_notRoot() { Component comp1 = new Component( "comp1" ).installerName( Constants.TARGET_INSTALLER ); Component comp2 = new Component( "comp2" ).installerName( Constants.TARGET_INSTALLER ); comp1.addChild( comp2 ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( comp2 ); Iterator<ModelError> iterator = RuntimeModelValidator.validate( graphs ).iterator(); Assert.assertEquals( ErrorCode.RM_NOT_A_ROOT_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testIDsWithSpaces() throws Exception { File f = TestUtils.findTestFile( "/configurations/valid/real-lamp-all-in-one-flex.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs graphs = fromDef.buildGraphs( f ); Assert.assertEquals( 7, fromDef.getErrors().size()); for( ParsingError error : fromDef.getErrors()) { boolean a1 = ErrorCode.PM_INVALID_NAME == error.getErrorCode(); boolean a2 = ErrorCode.PM_INVALID_CHILD_NAME == error.getErrorCode(); Assert.assertTrue( error.getErrorCode().name(), a1 || a2 ); } Assert.assertEquals( 0, graphs.getRootComponents().size()); Assert.assertEquals( 0, graphs.getFacetNameToFacet().size()); }
keys.add( "components" ); if( this.options.containsKey( DocConstants.OPTION_RECIPE ) ) { if( ! this.applicationTemplate.getGraphs().getFacetNameToFacet().isEmpty() ) keys.add( "facets" );
@Test public void test_inexistingChildInComponent() throws Exception { File f = TestUtils.findTestFile( "/configurations/invalid/inexisting-child-in-component.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs graphs = fromDef.buildGraphs( f ); Iterator<ParsingError> it = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_INEXISTING_CHILD, it.next().getErrorCode()); Assert.assertFalse( it.hasNext()); Assert.assertEquals( "root", graphs.getRootComponents().iterator().next().getName()); }
@Test public void testFindAllComponents_simple() { ApplicationTemplate app = new ApplicationTemplate(); Assert.assertEquals( 0, ComponentHelpers.findAllComponents( app ).size()); Graphs graphs = new Graphs(); app.setGraphs( graphs ); Component comp1 = new Component( "comp1" ); graphs.getRootComponents().add( comp1 ); Assert.assertEquals( 1, ComponentHelpers.findAllComponents( app ).size()); comp1.addChild( new Component( "comp-2" )); Assert.assertEquals( 2, ComponentHelpers.findAllComponents( app ).size()); Component comp3 = new Component( "comp_3" ); graphs.getRootComponents().add( comp3 ); comp3.addChild( new Component( "comp-2" )); Assert.assertEquals( 3, ComponentHelpers.findAllComponents( app ).size()); }
Assert.assertEquals( 0, graphs.getRootComponents().size()); Assert.assertEquals( 0, graphs.getFacetNameToFacet().size()); Assert.assertEquals( 2, graphs.getFacetNameToFacet().size()); Facet f1 = graphs.getFacetNameToFacet().get( "f1" ); Assert.assertNotNull( f1 ); Assert.assertEquals( 2, f1.exportedVariables.size()); Assert.assertEquals( 0, f1.getChildren().size()); Facet f2 = graphs.getFacetNameToFacet().get( "f2" ); Assert.assertNotNull( f2 ); Assert.assertEquals( 0, f2.exportedVariables.size());
types.addAll( g.getFacetNameToFacet().values());