/** * 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; }
@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 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 test_inexistingChildInFacet() throws Exception { File f = TestUtils.findTestFile( "/configurations/invalid/inexisting-child-in-facet.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( 0, graphs.getRootComponents().size()); }
@Test public void test_simpleInstaller() throws Exception { File f = TestUtils.findTestFile( "/configurations/valid/simple-installer.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs graphs = fromDef.buildGraphs( f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( "my-own-installer", graphs.getRootComponents().iterator().next().getInstallerName()); }
@Test public void test_conflictingNames() throws Exception { File f = TestUtils.findTestFile( "/configurations/invalid/conflicting-names.graph" ); FromGraphDefinition fromDef = new FromGraphDefinition( f.getParentFile()); Graphs graphs = fromDef.buildGraphs( f ); Iterator<ParsingError> it = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_CONFLICTING_NAME, it.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_CONFLICTING_NAME, it.next().getErrorCode()); Assert.assertFalse( it.hasNext()); Assert.assertEquals( "type", graphs.getRootComponents().iterator().next().getName()); }
@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 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 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()); }
@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()); }
@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 ); }
@Test public void testInstanceWithExtraData() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-with-extra-data.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 0, fromDef.getErrors().size()); Assert.assertEquals( 1, rootInstances.size()); Instance vmInstance = rootInstances.iterator().next(); Assert.assertEquals( 1, vmInstance.getChildren().size()); Assert.assertEquals( "VM1", vmInstance.getName()); Assert.assertEquals( 1, vmInstance.data.size()); Assert.assertEquals( "192.168.1.10", vmInstance.data.get( "ec2.elastic.ip" )); }
@Test public void testComponentResolutionWhenSurroundingSpaces() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component aComponent = new Component( "A" ).installerName( "whatever" ); vmComponent.addChild( aComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/valid/instance-with-space-after.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertFalse( iterator.hasNext()); Assert.assertEquals( 2, rootInstances.size()); for( Instance rootInstance : rootInstances ) { Assert.assertEquals( 1, rootInstance.getChildren().size()); Instance instance = rootInstance.getChildren().iterator().next(); Assert.assertEquals( "A", instance.getComponent().getName()); Assert.assertEquals( "A ", instance.getName()); } }
@Test public void testFindComponent() { Graphs g = new Graphs(); Assert.assertNull( ComponentHelpers.findComponent( g, "c" )); Component c1 = new Component( "c1" ); g.getRootComponents().add( c1 ); Assert.assertEquals( c1, ComponentHelpers.findComponent( g, "c1" )); Component c2 = new Component( "c2" ); g.getRootComponents().add( c2 ); Assert.assertEquals( c2, ComponentHelpers.findComponent( g, "c2" )); Component c21 = new Component( "c21" ); c2.addChild( c21 ); Assert.assertEquals( c21, ComponentHelpers.findComponent( g, "c21" )); Component duplicateC1 = new Component( "c1" ); g.getRootComponents().add( duplicateC1 ); Assert.assertNotNull( ComponentHelpers.findComponent( g, "c1" )); TestApplication app = new TestApplication(); Assert.assertNotNull( ComponentHelpers.findComponent( app, "tomcat" )); Assert.assertNull( ComponentHelpers.findComponent( app, "tommy" )); }
@Test public void testInexistingComponent() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/invalid/inexisting-component.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_INEXISTING_COMPONENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testDuplicateInstance() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); File f = TestUtils.findTestFile( "/configurations/invalid/duplicate-instance.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); fromDef.buildInstances( graphs, f ); Iterator<ParsingError> iterator = fromDef.getErrors().iterator(); Assert.assertEquals( ErrorCode.CO_ALREADY_DEFINED_INSTANCE, iterator.next().getErrorCode()); Assert.assertEquals( ErrorCode.CO_ALREADY_DEFINED_INSTANCE, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testInvalidChildInstance_2() throws Exception { Component vmComponent = new Component( "VM" ).installerName( "target" ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( vmComponent ); // We cannot instantiate a VM under a VM Instance vmInstance = new Instance("vm" ).component( vmComponent ); Instance tomcatInstance = new Instance("tomcat" ).component( tomcatComponent ); InstanceHelpers.insertChild( vmInstance, tomcatInstance ); ApplicationTemplate app = new ApplicationTemplate( "app" ).version( "2.4" ).graphs( graphs ); app.getRootInstances().add( vmInstance ); Iterator<ModelError> iterator = RuntimeModelValidator.validate( app ).iterator(); Assert.assertEquals( ErrorCode.RM_INVALID_INSTANCE_PARENT, iterator.next().getErrorCode()); Assert.assertFalse( iterator.hasNext()); }
@Test public void testFromGraphs_withExternal() 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 )); cA.addImportedVariable( new ImportedVariable( "B.port", false, true )); cA.addImportedVariable( new ImportedVariable( "B.ip", true, false )); Component cB = new Component( "B" ).installerName( Constants.TARGET_INSTALLER ); graphs.getRootComponents().add( cB ); cB.addExportedVariable( new ExportedVariable( "B.port", "9000" )); cB.addExportedVariable( new ExportedVariable( "B.ip", null )); compareGraphs( graphs, false ); }
private Graphs buildGraphs() { Graphs graphs = new Graphs(); Component vmComponent = new Component( "VM" ).installerName( "target" ); graphs.getRootComponents().add( vmComponent ); Component tomcatComponent = new Component( "Tomcat" ).installerName( "puppet" ); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.ip", null )); tomcatComponent.addExportedVariable( new ExportedVariable( "Tomcat.port", "8080" )); vmComponent.addChild( tomcatComponent ); Component warComponent = new Component( "WAR" ).installerName( "script" ); tomcatComponent.addChild( warComponent ); Assert.assertEquals( 0, RuntimeModelValidator.validate( graphs ).size()); return graphs; }