@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 testInsertChild() { Component component_1 = new Component( "comp 1" ); Component component_1_1 = new Component( "comp 11" ); Assert.assertEquals( 0, component_1.getAncestors().size()); Assert.assertEquals( 0, component_1.getChildren().size()); Assert.assertEquals( 0, component_1_1.getAncestors().size()); Assert.assertEquals( 0, component_1_1.getChildren().size()); component_1.addChild( component_1_1 ); Assert.assertEquals( 0, component_1.getAncestors().size()); Assert.assertEquals( 1, component_1.getChildren().size()); Assert.assertEquals( 1, component_1_1.getAncestors().size()); Assert.assertEquals( 0, component_1_1.getChildren().size()); Assert.assertEquals( component_1_1, component_1.getChildren().iterator().next()); Assert.assertEquals( component_1, component_1_1.getAncestors().iterator().next()); Assert.assertNotSame( component_1, component_1_1 ); }
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; }
@Test public void testOverriddenExports() 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-overridden-exports.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 rootInstance = rootInstances.iterator().next(); Collection<Instance> instances = InstanceHelpers.buildHierarchicalList( rootInstance ); Assert.assertEquals( 2, instances.size()); Collection<ModelError> errors = RuntimeModelValidator.validate( instances ); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.RM_MAGIC_INSTANCE_VARIABLE, errors.iterator().next().getErrorCode()); }
@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 testExtraData() throws Exception { // Parse 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" )); // Write compareInstances( graphs, Arrays.asList( vmInstance ), true, true ); }
@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 test_N_InstantiationsWithConflict() throws Exception { // The graph 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 ); // The file to read File f = TestUtils.findTestFile( "/configurations/invalid/instanceof-name-conflict-with-count.instances" ); FromInstanceDefinition fromDef = new FromInstanceDefinition( f.getParentFile()); Collection<Instance> rootInstances = fromDef.buildInstances( graphs, f ); Assert.assertEquals( 1, fromDef.getErrors().size()); Assert.assertEquals( ErrorCode.CO_CONFLICTING_INFERRED_INSTANCE, fromDef.getErrors().iterator().next().getErrorCode()); // The assertions Application app = new Application( new ApplicationTemplate()); app.getRootInstances().addAll( rootInstances ); Assert.assertEquals( 5, rootInstances.size()); // Should be 6, but there two have the same path. // So, one is overriding the other. }
@Test public void testFindAllComponents_extended() { ApplicationTemplate app = ComplexApplicationFactory1.newApplication(); app.getGraphs().getRootComponents().clear(); Component root1 = new Component( "root1" ).installerName( Constants.TARGET_INSTALLER ); Component root2 = new Component( "root2" ); Component root3 = new Component( "root3" ); Assert.assertEquals( 0, ComponentHelpers.findAllComponents( app ).size()); app.getGraphs().getRootComponents().add( root1 ); Assert.assertEquals( 1, ComponentHelpers.findAllComponents( app ).size()); root2.extendComponent( root1 ); Assert.assertEquals( 2, ComponentHelpers.findAllComponents( app ).size()); root3.extendComponent( root2 ); Assert.assertEquals( 3, ComponentHelpers.findAllComponents( app ).size()); app.getGraphs().getRootComponents().add( root2 ); app.getGraphs().getRootComponents().add( root3 ); Assert.assertEquals( 3, ComponentHelpers.findAllComponents( app ).size()); Component childComponent = new Component( "child" ).installerName( "script" ); root3.addChild( childComponent ); Assert.assertEquals( 4, ComponentHelpers.findAllComponents( app ).size()); }
@Test public void testSearchForLoop() { Component c1 = new Component( "c1" ); Assert.assertNull( ComponentHelpers.searchForLoop( c1 )); Component c11 = new Component( "c11" ); c1.addChild( c11 ); Assert.assertNull( ComponentHelpers.searchForLoop( c1 )); Component c12 = new Component( "c1" ); c1.addChild( c12 ); Assert.assertEquals( "c1 -> c1", ComponentHelpers.searchForLoop( c1 )); Assert.assertNull( ComponentHelpers.searchForLoop( c11 )); Assert.assertNull( ComponentHelpers.searchForLoop( c12 )); c12.setName( "c12" ); Component c121 = new Component( "c1" ); c12.addChild( c121 ); Assert.assertEquals( "c1 -> c12 -> c1", ComponentHelpers.searchForLoop( c1 )); Assert.assertNull( ComponentHelpers.searchForLoop( c11 )); Assert.assertNull( ComponentHelpers.searchForLoop( c12 )); Assert.assertNull( ComponentHelpers.searchForLoop( c121 )); c121.setName( "c121" ); c121.addChild( c1 ); Assert.assertEquals( "c1 -> c12 -> c121 -> c1", ComponentHelpers.searchForLoop( c1 )); Assert.assertEquals( "c12 -> c121 -> c1 -> c12", ComponentHelpers.searchForLoop( c12 )); Assert.assertEquals( "c121 -> c1 -> c12 -> c121", ComponentHelpers.searchForLoop( c121 )); Assert.assertNull( ComponentHelpers.searchForLoop( c11 )); }
@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" )); }