@Override public void serialize( Component component, JsonGenerator generator, SerializerProvider provider ) throws IOException { generator.writeStartObject(); if( component.getName() != null ) generator.writeStringField( NAME, component.getName()); String installerName = ComponentHelpers.findComponentInstaller( component ); if( installerName != null ) generator.writeStringField( COMP_INSTALLER, installerName ); generator.writeEndObject(); } }
/** * Details for a component. * @param component the component * @return an object with error details */ public static ErrorDetails component( Component component ) { return new ErrorDetails( component.getName(), ErrorDetailsKind.COMPONENT ); }
/** * Finds a component by name. * @param graphs the graph(s) (can be null) * @param name the component name (not null) * @return a component (can be null) */ public static Component findComponent( Graphs graphs, String name ) { Component result = null; for( Iterator<Component> it = findAllComponents( graphs ).iterator(); it.hasNext() && result == null; ) { Component c = it.next(); if( name.equals( c.getName())) result = c; } return result; }
/** * Finds instances by component name. * @param application an application (not null) * @param componentName a component name (not null) * @return a non-null list of instances */ public static List<Instance> findInstancesByComponentName( AbstractApplication application, String componentName ) { List<Instance> result = new ArrayList<> (); for( Instance inst : getAllInstances( application )) { if( componentName.equals( inst.getComponent().getName())) result.add( inst ); } return result; }
/** * Builds a map with the variables defined by this class. * @param instance a non-null instance * @return a non-null map where all the properties here are mapped to their values for this instance */ public static Map<String,String> buildReferenceMap( Instance instance ) { Map<String,String> result = new HashMap<> (); String instancePath = InstanceHelpers.computeInstancePath( instance ); result.put( ROBOCONF_INSTANCE_NAME, instance.getName()); result.put( ROBOCONF_INSTANCE_PATH, instancePath ); result.put( ROBOCONF_COMPONENT_NAME, instance.getComponent().getName()); result.put( ROBOCONF_CLEAN_INSTANCE_PATH, cleanInstancePath( instancePath )); result.put( ROBOCONF_CLEAN_REVERSED_INSTANCE_PATH, cleanReversedInstancePath( instancePath )); return result; }
/** * Constructor. * @param app * @param commandFile */ public Context( AbstractApplication app, File commandFile ) { this.app = app; this.commandFile = commandFile; for( Instance instance : InstanceHelpers.getAllInstances( app )) this.instancePathToComponentName.put( InstanceHelpers.computeInstancePath( instance ), instance.getComponent().getName()); }
/** * Constructor. * @param instance */ public Import( Instance instance ) { this( InstanceHelpers.computeInstancePath( instance ), instance.getComponent() == null ? null : instance.getComponent().getName(), InstanceHelpers.findAllExportedVariables( instance )); }
@Test public void testComponentsSorting() { List<Component> components = new ArrayList<Component> (); components.add( new Component( "z" )); components.add( new Component( "m" )); components.add( new Component( "a" )); components.add( new Component( "b" )); Assert.assertEquals( "z", components.get( 0 ).getName()); Collections.sort( components, new AbstractTypeComparator()); Assert.assertEquals( "a", components.get( 0 ).getName()); Assert.assertEquals( "b", components.get( 1 ).getName()); Assert.assertEquals( "m", components.get( 2 ).getName()); Assert.assertEquals( "z", components.get( 3 ).getName()); }
/** * Constructor. * @param instance */ public MsgCmdAddInstance( Instance instance ) { this.parentInstancePath = instance.getParent() == null ? null : InstanceHelpers.computeInstancePath( instance.getParent()); this.instanceName = instance.getName(); this.componentName = instance.getComponent() != null ? instance.getComponent().getName() : null; this.channels = instance.channels; this.data = instance.data; this.overridenExports = instance.overriddenExports; }
/** * Constructor. * @param instance */ public MsgCmdAddInstance( Instance instance ) { this.parentInstancePath = instance.getParent() == null ? null : InstanceHelpers.computeInstancePath( instance.getParent()); this.instanceName = instance.getName(); this.componentName = instance.getComponent() != null ? instance.getComponent().getName() : null; this.channels = instance.channels; this.data = instance.data; this.overridenExports = instance.overriddenExports; }
@Test public void testConstructor() { List<Instance> instances = InstanceHelpers.getAllInstances( this.app ); Assert.assertEquals( instances.size(), this.context.instancePathToComponentName.size()); for( Instance instance : instances ) { String path = InstanceHelpers.computeInstancePath( instance ); Assert.assertEquals( instance.getComponent().getName(), this.context.instancePathToComponentName.get( path )); } }
@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_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 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 testFindScopedInstancesDirectories() throws Exception { final File appDir = this.folder.newFolder(); TestApplication app = new TestApplication(); app.setDirectory( appDir ); Assert.assertEquals( 0, ResourceUtils.findScopedInstancesDirectories( app ).size()); File vmDir = new File( appDir, Constants.PROJECT_DIR_GRAPH + "/" + app.getMySqlVm().getComponent().getName()); Assert.assertTrue( vmDir.mkdirs()); Map<Component,File> map = ResourceUtils.findScopedInstancesDirectories( app ); Assert.assertEquals( 1, map.size()); Assert.assertEquals( vmDir, map.get( app.getMySqlVm().getComponent())); } }
@Test public void testResolveInstance() { Assert.assertEquals( "vm", this.context.resolveInstance( "/tomcat-vm" ).getComponent().getName()); Assert.assertEquals( "tomcat", this.context.resolveInstance( "/tomcat-vm/tomcat-server" ).getComponent().getName()); Assert.assertNull( this.context.resolveInstance( "/tomcat-vm/invalid" )); this.context.instancePathToComponentName.put( "not a path", "whatever" ); Assert.assertNull( this.context.resolveInstance( "not a path" )); Assert.assertNull( this.context.resolveInstance( "" )); this.context.instancePathToComponentName.put( "", "vm" ); Assert.assertNull( this.context.resolveInstance( "" )); }
@Test public void testBuildReferenceMap() { TestApplication app = new TestApplication(); Map<String,String> map = DockerAndScriptUtils.buildReferenceMap( app.getMySqlVm()); Assert.assertEquals( 5, map.size()); Assert.assertEquals( app.getMySqlVm().getName(), map.get( DockerAndScriptUtils.ROBOCONF_INSTANCE_NAME )); Assert.assertEquals( "/" + app.getMySqlVm().getName(), map.get( DockerAndScriptUtils.ROBOCONF_INSTANCE_PATH )); Assert.assertEquals( app.getMySqlVm().getComponent().getName(), map.get( DockerAndScriptUtils.ROBOCONF_COMPONENT_NAME )); Assert.assertEquals( "mysql_vm", map.get( DockerAndScriptUtils.ROBOCONF_CLEAN_INSTANCE_PATH )); Assert.assertEquals( "mysql_vm", map.get( DockerAndScriptUtils.ROBOCONF_CLEAN_REVERSED_INSTANCE_PATH )); map = DockerAndScriptUtils.buildReferenceMap( app.getWar()); Assert.assertEquals( 5, map.size()); Assert.assertEquals( app.getWar().getName(), map.get( DockerAndScriptUtils.ROBOCONF_INSTANCE_NAME )); Assert.assertEquals( "/tomcat-vm/tomcat-server/hello-world", map.get( DockerAndScriptUtils.ROBOCONF_INSTANCE_PATH )); Assert.assertEquals( app.getWar().getComponent().getName(), map.get( DockerAndScriptUtils.ROBOCONF_COMPONENT_NAME )); Assert.assertEquals( "tomcat_vm_tomcat_server_hello_world", map.get( DockerAndScriptUtils.ROBOCONF_CLEAN_INSTANCE_PATH )); Assert.assertEquals( "hello_world_tomcat_server_tomcat_vm", map.get( DockerAndScriptUtils.ROBOCONF_CLEAN_REVERSED_INSTANCE_PATH )); } }
@Test public void testFindComponentFrom() { TestApplicationTemplate app = new TestApplicationTemplate(); Component comp = app.getTomcatVm().getComponent(); Assert.assertNull( ComponentHelpers.findComponentFrom( comp, "inexisting" )); Assert.assertNull( ComponentHelpers.findComponentFrom( null, "inexisting" )); Assert.assertEquals( comp, ComponentHelpers.findComponentFrom( comp, comp.getName())); Component targetComp = app.getTomcat().getComponent(); Assert.assertEquals( targetComp, ComponentHelpers.findComponentFrom( comp, targetComp.getName())); targetComp = app.getWar().getComponent(); Assert.assertEquals( targetComp, ComponentHelpers.findComponentFrom( comp, targetComp.getName())); }
@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 testChain() { Component c = new Component( "c" ); Component comp = new Component().name( "ins" ).installerName( "my-installer" ); comp.extendComponent( c ); Assert.assertEquals( "ins", comp.getName()); Assert.assertEquals( "my-installer", comp.getInstallerName()); Assert.assertEquals( c, comp.getExtendedComponent()); }