private void processFacet( BlockFacet block ) { FacetData data = this.facetNameToFacetData.get( block.getName()); if( data != null ) { data.blocks.add( block ); } else { data = new FacetData(); data.object = new Facet( block.getName()); data.object.exportedVariables.putAll( ModelUtils.getExportedVariables( block )); data.childrenNames.addAll( ModelUtils.getPropertyValues( block, ParsingConstants.PROPERTY_GRAPH_CHILDREN )); data.extendedFacetNames.addAll( ModelUtils.getPropertyValues( block, ParsingConstants.PROPERTY_GRAPH_EXTENDS )); data.blocks.add( block ); this.facetNameToFacetData.put( block.getName(), data ); } }
/** * Details for a facet. * @param facet the facet * @return an object with error details */ public static ErrorDetails facet( Facet facet ) { return facet( facet.getName()); }
private void resolveFacets() { for( FacetData data : this.facetNameToFacetData.values()) { // Being here means we did not find conflicting names AbstractBlockHolder holder = data.blocks.get( 0 ); SourceReference sr = new SourceReference( data.object, holder.getFile(), holder.getLine()); this.objectToSource.put( data.object, sr ); // The extended facets for( String s : data.extendedFacetNames ) { FacetData facetData = this.facetNameToFacetData.get( s ); if( facetData != null ) data.object.extendFacet( facetData.object ); else this.errors.addAll( data.error( ErrorCode.CO_INEXISTING_FACET, facet( s ))); } // The children for( String s : data.childrenNames ) { ComponentData componentData = this.componentNameToComponentData.get( s ); FacetData facetData = this.facetNameToFacetData.get( s ); if( componentData != null ) data.object.addChild( componentData.object ); else if( facetData != null ) data.object.addChild( facetData.object ); else this.errors.addAll( data.error( ErrorCode.CO_INEXISTING_CHILD, name( s ))); } } }
@Test public void testFacetsSorting() { List<Facet> facets = new ArrayList<Facet> (); facets.add( new Facet( "z" )); facets.add( new Facet( "m" )); facets.add( new Facet( "a" )); facets.add( new Facet( "b" )); Assert.assertEquals( "z", facets.get( 0 ).getName()); Collections.sort( facets, new AbstractTypeComparator()); Assert.assertEquals( "a", facets.get( 0 ).getName()); Assert.assertEquals( "b", facets.get( 1 ).getName()); Assert.assertEquals( "m", facets.get( 2 ).getName()); Assert.assertEquals( "z", facets.get( 3 ).getName()); }
list.addAll( children ? c.getChildren() : c.getAncestors()); for( Facet facet : findAllFacets( c )) list.addAll( children ? facet.getChildren() : facet.getAncestors()); for( Component cc : facet.getAssociatedComponents()) { result.add( cc ); result.addAll( findAllExtendingComponents( cc ));
Facet facetDeployable = new Facet( FACET_DEPLOYABLE ); graph.getFacetNameToFacet().put( facetDeployable.getName(), facetDeployable ); Facet facetVm = new Facet( FACET_VM ); graph.getFacetNameToFacet().put( facetVm.getName(), facetVm ); facetVm.addChild( facetDeployable ); facetVm.addExportedVariable( new ExportedVariable( Constants.SPECIFIC_VARIABLE_IP, null )); Facet facetJee = new Facet( FACET_JEE ); graph.getFacetNameToFacet().put( facetJee.getName(), facetJee ); facetJee.addExportedVariable( new ExportedVariable( "server-suffix", "/path" )); facetJee.extendFacet( facetDeployable ); Facet facetWeb = new Facet( FACET_WEB ); graph.getFacetNameToFacet().put( facetWeb.getName(), facetWeb ); facetWeb.addExportedVariable( new ExportedVariable( "server-suffix", "/path" )); facetWeb.extendFacet( facetDeployable ); Facet facetDb = new Facet( FACET_DATABASE ); graph.getFacetNameToFacet().put( facetDb.getName(), facetDb ); facetDb.addExportedVariable( new ExportedVariable( "port", "3306" )); facetDb.extendFacet( facetDeployable ); Facet facetStorage = new Facet( FACET_STORAGE ); graph.getFacetNameToFacet().put( facetStorage.getName(), facetStorage ); facetStorage.extendFacet( facetDb );
@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" )); }
Assert.assertEquals( ComplexApplicationFactory1.FACET_VM, vm.getName()); Assert.assertEquals( 1, vm.getChildren().size()); Facet deployable = (Facet) vm.getChildren().iterator().next(); Assert.assertEquals( ComplexApplicationFactory1.FACET_DEPLOYABLE, deployable.getName()); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_DATABASE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_JEE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_STORAGE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_WEB ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_STORAGE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_DEPLOYABLE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_DEPLOYABLE ))); Assert.assertTrue( facets.contains( new Facet( ComplexApplicationFactory1.FACET_DATABASE )));
for( Facet f : graphs.getFacetNameToFacet().values()) { facetVariables.addAll( f.exportedVariables.keySet()); facetVariables.add( f.getName() + "." + Constants.WILDCARD ); if( f.getAssociatedComponents().isEmpty()) { if( f.getChildren().isEmpty()) errors.add( new ModelError( RM_ORPHAN_FACET, f, facet( f ))); else for( AbstractType t : f.getChildren()) { if( t instanceof Component ) errors.add( new ModelError( RM_UNREACHABLE_COMPONENT, t, component( t.getName())));
component.addExportedVariable( new ExportedVariable( "ip", null )); Facet f1 = new Facet( "f1" ); f1.addExportedVariable( new ExportedVariable( "param1", "value1" )); component.associateFacet( f1 ); Facet f2 = new Facet( "f2" ); f2.addExportedVariable( new ExportedVariable( "param2", "value2" )); component.associateFacet( f2 ); Facet f3 = new Facet( "f3" ); f3.addExportedVariable( new ExportedVariable( "param3", "value3" )); component.associateFacet( f3 ); component.addExportedVariable( new ExportedVariable( "f3.param3", "component overrides facet" )); Facet f4 = new Facet( "f4" ); f4.addExportedVariable( new ExportedVariable( "param4-1", "value4" )); f4.addExportedVariable( new ExportedVariable( "param4-2", "value4" )); f2.extendFacet( f4 ); f2.addExportedVariable( new ExportedVariable( "f4.param4-1", "facet overrides facet" ));
StringBuilder sb = new StringBuilder(); sb.append( "# Facet '" ); sb.append( facet.getName()); sb.append( "'" ); result.add( new BlockComment( file, sb.toString())); blockFacet.setName( facet.getName()); result.add( blockFacet ); result.add( new BlockBlank( file, "\n" )); String s = writeCollection( facet.getExtendedFacets()); if( ! Utils.isEmptyOrWhitespaces( s )) { p = new BlockProperty( file, ParsingConstants.PROPERTY_GRAPH_EXTENDS, s ); s = writeCollection( facet.getChildren()); if( ! Utils.isEmptyOrWhitespaces( s )) { p = new BlockProperty( file, ParsingConstants.PROPERTY_GRAPH_CHILDREN, s );
Facet facet = new Facet(); Iterator<ModelError> iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertEquals( ErrorCode.RM_EMPTY_FACET_NAME, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); facet.setName( "my # facet" ); iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_FACET_NAME, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); facet.setName( "my.facet" ); iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertEquals( ErrorCode.RM_DOT_IS_NOT_ALLOWED, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); facet.setName( "facet" ); iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertFalse( iterator.hasNext()); facet.addExportedVariable( new ExportedVariable( "", "value" )); iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertEquals( ErrorCode.RM_EMPTY_VARIABLE_NAME, iterator.next().getErrorCode()); facet.addExportedVariable( new ExportedVariable( "facet.inva!id", "value" )); iterator = RuntimeModelValidator.validate( facet ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_VARIABLE_NAME, iterator.next().getErrorCode()); facet.addExportedVariable( new ExportedVariable( "toto", "" )); iterator = RuntimeModelValidator.validate( facet ).iterator();
@Test public void testAssociateFacet() { Component component = new Component( "comp" ); Facet facet = new Facet( "facet" ); Assert.assertEquals( 0, component.getFacets().size()); Assert.assertEquals( 0, facet.getAssociatedComponents().size()); component.associateFacet( facet ); Assert.assertEquals( 1, component.getFacets().size()); Assert.assertEquals( 1, facet.getAssociatedComponents().size()); Assert.assertEquals( facet, component.getFacets().iterator().next()); Assert.assertEquals( component, facet.getAssociatedComponents().iterator().next()); } }
Assert.assertNotNull( f1 ); Assert.assertEquals( 2, f1.exportedVariables.size()); Assert.assertEquals( 0, f1.getChildren().size()); Assert.assertEquals( 0, f2.getChildren().size());
/** * Finds all the facets that this facet inherits from. * <p> * Inheritance cycles are ignored. * </p> * * @param facet a non-null facet * @return a non-null collection */ public static Collection<Facet> findAllExtendedFacets( Facet facet ) { Set<Facet> result = new HashSet<> (); Set<Facet> toProcess = new HashSet<>( facet.getExtendedFacets()); while( ! toProcess.isEmpty()) { Facet f = toProcess.iterator().next(); result.add( f ); toProcess.addAll( f.getExtendedFacets()); toProcess.removeAll( result ); } return result; }
serverWithAnotherApp.extendComponent( serverWithApp ); Facet serverFacet = new Facet( "server facet" ); Facet anotherServerFacet = new Facet( "another server facet" ); anotherServerFacet.extendFacet( serverFacet ); serverFacet.addExportedVariable( new ExportedVariable( "url-suffix", "some/path" )); serverWithAnotherApp.addExportedVariable( new ExportedVariable( serverFacet.getName() + ".url-suffix", "another/path" )); serverWithAnotherApp.addExportedVariable( new ExportedVariable( "whatever", "something" )); serverWithAnotherApp.addExportedVariable( new ExportedVariable( "env", "prod" )); Assert.assertEquals( "test", exports.get( serverWithApp.getName() + ".env" )); Assert.assertEquals( "some/path", exports.get( serverWithApp.getName() + ".url-suffix" )); Assert.assertEquals( "some/path", exports.get( serverFacet.getName() + ".url-suffix" )); Assert.assertEquals( "8080", exports.get( serverWithAnotherApp.getName() + ".port" )); Assert.assertEquals( "another/path", exports.get( serverFacet.getName() + ".url-suffix" )); Assert.assertEquals( "some/path", exports.get( serverWithApp.getName() + ".url-suffix" )); Assert.assertEquals( "some/path", exports.get( serverWithAnotherApp.getName() + ".url-suffix" ));
@Test public void testFromGraphs_threeFacets() throws Exception { Graphs graphs = new Graphs(); Component cA = new Component( "A" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cA ); Facet facet = new Facet( "my-facet-1" ); facet.addExportedVariable( new ExportedVariable( "data", "coucou" )); cA.associateFacet( facet ); cA.addExportedVariable( new ExportedVariable( "A.port", "9000" )); cA.addExportedVariable( new ExportedVariable( "A.ip", null )); cA.addImportedVariable( new ImportedVariable( "B.port", true, false )); cA.addImportedVariable( new ImportedVariable( "B.ip", true, false )); cA.addImportedVariable( new ImportedVariable( "facetF.props", false, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); facet = new Facet( "facetF" ); facet.addExportedVariable( new ExportedVariable( "facetF.props", "some value" )); cB.associateFacet( facet ); facet = new Facet( "my-facet-2" ); facet.addExportedVariable( new ExportedVariable( "my-facet-2.woo", "woo" )); cB.associateFacet( facet ); cB.addExportedVariable( new ExportedVariable( "B.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "B.ip", null )); compareGraphs( graphs, false ); }
Assert.assertNotNull( f1 ); Assert.assertEquals( 2, f1.exportedVariables.size()); Assert.assertEquals( 0, f1.getChildren().size()); Assert.assertEquals( 0, f2.getChildren().size());
/** * Finds all the facets of a component. * <p> * Inheritance cycles are ignored. * </p> * * @param component a non-null component * @return a non-null list of facets */ public static Collection<Facet> findAllFacets( Component component ) { Set<Facet> result = new HashSet<> (); List<Facet> toProcess = new ArrayList<> (); for( Component c : findAllExtendedComponents( component )) toProcess.addAll( c.getFacets()); while( ! toProcess.isEmpty()) { Facet f = toProcess.remove( 0 ); result.add( f ); toProcess.addAll( f.getExtendedFacets()); // Prevent loops toProcess.removeAll( result ); } return result; }
Assert.assertEquals( 0, errors.size()); Facet facet = new Facet( "facet" ); facet.addExportedVariable( new ExportedVariable( "ip", null )); tomcatComponent.associateFacet( facet );