ApplicationResource implements IApplicationResource { @Override public List<TargetAssociation> findTargetAssociations( String applicationName ) { this.logger.fine( "Request: find target associations in " + applicationName + "." ); List<TargetAssociation> result = new ArrayList<> (); Application app = this.manager.applicationMngr().findApplicationByName( applicationName ); if( app != null ) { String defaultTargetId = this.manager.targetsMngr().findTargetId( app, null ); TargetWrapperDescriptor twd = null; if( defaultTargetId != null ) twd = this.manager.targetsMngr().findTargetById( defaultTargetId ); result.add( new TargetAssociation( "", null, twd )); for( Instance inst : InstanceHelpers.findAllScopedInstances( app )) { String instancePath = InstanceHelpers.computeInstancePath( inst ); String targetId = this.manager.targetsMngr().findTargetId( app, instancePath, true ); twd = null; if( targetId != null ) twd = this.manager.targetsMngr().findTargetById( targetId ); result.add( new TargetAssociation( instancePath, inst.getComponent().getName(), twd )); } for( Component comp : ComponentHelpers.findAllComponents( app )) { if( ! ComponentHelpers.isTarget( comp )) continue; String key = "@" + comp.getName(); String targetId = this.manager.targetsMngr().findTargetId( app, key, true ); twd = null; if( targetId != null ) twd = this.manager.targetsMngr().findTargetById( targetId ); result.add( new TargetAssociation( key, null, twd )); } } return result; } ApplicationResource( Manager manager ); @Override Response changeInstanceState( String applicationName, String newState, String instancePath ); @Override Response setDescription( String applicationName, String desc ); @Override Response deployAndStartAll( String applicationName, String instancePath ); @Override Response stopAll( String applicationName, String instancePath ); @Override Response undeployAll( String applicationName, String instancePath ); @Override List<Instance> listChildrenInstances( String applicationName, String instancePath, boolean allChildren ); @Override Response bindApplication( String applicationName, String externalExportPrefix, String boundApp ); @Override Response unbindApplication( String applicationName, String externalExportPrefix, String boundApp ); @Override Response replaceApplicationBindings( String applicationName, String externalExportPrefix, List<String> boundApps ); @Override Response getApplicationBindings( String applicationName ); @Override Response addInstance( String applicationName, String parentInstancePath, Instance instance ); @Override Response removeInstance( String applicationName, String instancePath ); @Override List<TargetAssociation> findTargetAssociations( String applicationName ); @Override Response resynchronize( String applicationName ); @Override List<Component> listComponents( String applicationName ); @Override List<Component> findComponentChildren( String applicationName, String componentName ); @Override List<Component> findComponentAncestors( String applicationName, String componentName ); @Override List<String> listCommands( String app ); @Override Response executeCommand( String app, String commandName ); @Override Response getCommandInstructions( String appName, String commandName ); @Override Response replaceTags( String name, String version, List<String> tags );  }
@Test public void testFindTargetAssociations_noTarget_noAssociation() throws Exception { List<Instance> scopedInstances = InstanceHelpers.findAllScopedInstances( this.app ); Assert.assertEquals( 2, scopedInstances.size()); List<Component> scopedComponents = new ArrayList<> (); for( Instance inst : scopedInstances ) { if( ! scopedComponents.contains( inst.getComponent())) scopedComponents.add( inst.getComponent()); } List<TargetAssociation> associations = this.resource.findTargetAssociations( this.app.getName()); Assert.assertEquals( scopedInstances.size() + scopedComponents.size() + 1, associations.size()); for( int i=0; i<scopedInstances.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 ); String path = InstanceHelpers.computeInstancePath( scopedInstances.get( i )); Assert.assertEquals( scopedInstances.get( i ).getComponent().getName(), ta.getInstanceComponent()); Assert.assertEquals( path, ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } for( int i=0; i<scopedComponents.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 + scopedInstances.size()); Assert.assertEquals( "@" + scopedComponents.get( i ).getName(), ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } TargetAssociation ta = associations.get( 0 ); Assert.assertEquals( "", ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); }
@Test public void testFindTargetAssociations_withTarget_noAssociation() throws Exception { List<Instance> scopedInstances = InstanceHelpers.findAllScopedInstances( this.app ); Assert.assertEquals( 2, scopedInstances.size()); List<Component> scopedComponents = new ArrayList<> (); for( Instance inst : scopedInstances ) { if( ! scopedComponents.contains( inst.getComponent())) scopedComponents.add( inst.getComponent()); } this.manager.targetsMngr().createTarget( "id: tid\nhandler: h" ); List<TargetAssociation> associations = this.resource.findTargetAssociations( this.app.getName()); Assert.assertEquals( scopedInstances.size() + scopedComponents.size() + 1, associations.size()); for( int i=0; i<scopedInstances.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 ); String path = InstanceHelpers.computeInstancePath( scopedInstances.get( i )); Assert.assertEquals( scopedInstances.get( i ).getComponent().getName(), ta.getInstanceComponent()); Assert.assertEquals( path, ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } for( int i=0; i<scopedComponents.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 + scopedInstances.size()); Assert.assertEquals( "@" + scopedComponents.get( i ).getName(), ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } TargetAssociation ta = associations.get( 0 ); Assert.assertEquals( "", ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); }
@Test public void testFindTargetAssociations_withTarget_defaultAssociation() throws Exception { List<Instance> scopedInstances = InstanceHelpers.findAllScopedInstances( this.app ); Assert.assertEquals( 2, scopedInstances.size()); List<Component> scopedComponents = new ArrayList<> (); for( Instance inst : scopedInstances ) { if( ! scopedComponents.contains( inst.getComponent())) scopedComponents.add( inst.getComponent()); } String targetId = this.manager.targetsMngr().createTarget( "id: tid\nhandler: h" ); this.manager.targetsMngr().associateTargetWith( targetId, this.app, null ); List<TargetAssociation> associations = this.resource.findTargetAssociations( this.app.getName()); Assert.assertEquals( scopedInstances.size() + scopedComponents.size() + 1, associations.size()); for( int i=0; i<scopedInstances.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 ); String path = InstanceHelpers.computeInstancePath( scopedInstances.get( i )); Assert.assertEquals( scopedInstances.get( i ).getComponent().getName(), ta.getInstanceComponent()); Assert.assertEquals( path, ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } for( int i=0; i<scopedComponents.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 + scopedInstances.size()); Assert.assertEquals( "@" + scopedComponents.get( i ).getName(), ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } TargetAssociation ta = associations.get( 0 ); Assert.assertEquals( "", ta.getInstancePathOrComponentName()); Assert.assertEquals( targetId, ta.getTargetDescriptor().getId()); }
@Test public void testFindTargetAssociations_withTarget_withAssociations() throws Exception { List<Instance> scopedInstances = InstanceHelpers.findAllScopedInstances( this.app ); Assert.assertEquals( 2, scopedInstances.size()); List<Component> scopedComponents = new ArrayList<> (); for( Instance inst : scopedInstances ) { if( ! scopedComponents.contains( inst.getComponent())) scopedComponents.add( inst.getComponent()); } String targetId = this.manager.targetsMngr().createTarget( "id: tid\nhandler: h" ); this.manager.targetsMngr().associateTargetWith( targetId, this.app, null ); String newTargetId = this.manager.targetsMngr().createTarget( "id: new-tid\nhandler: h" ); String instancePath = InstanceHelpers.computeInstancePath( this.app.getTomcatVm()); this.manager.targetsMngr().associateTargetWith( newTargetId, this.app, instancePath ); List<TargetAssociation> associations = this.resource.findTargetAssociations( this.app.getName()); Assert.assertEquals( scopedInstances.size() + scopedComponents.size() + 1, associations.size()); boolean foundCustomInstance = false; for( int i=0; i<scopedInstances.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 ); String path = InstanceHelpers.computeInstancePath( scopedInstances.get( i )); Assert.assertEquals( path, ta.getInstancePathOrComponentName()); Assert.assertEquals( scopedInstances.get( i ).getComponent().getName(), ta.getInstanceComponent()); if( instancePath.equals( path )) { Assert.assertEquals( newTargetId, ta.getTargetDescriptor().getId()); foundCustomInstance = true; } else { Assert.assertNull( ta.getTargetDescriptor()); } } for( int i=0; i<scopedComponents.size(); i++ ) { TargetAssociation ta = associations.get( i + 1 + scopedInstances.size()); Assert.assertEquals( "@" + scopedComponents.get( i ).getName(), ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } Assert.assertTrue( foundCustomInstance ); TargetAssociation ta = associations.get( 0 ); Assert.assertEquals( "", ta.getInstancePathOrComponentName()); Assert.assertEquals( targetId, ta.getTargetDescriptor().getId()); this.manager.targetsMngr().associateTargetWith( newTargetId, this.app, null ); associations = this.resource.findTargetAssociations( this.app.getName()); foundCustomInstance = false; for( int i=0; i<scopedInstances.size(); i++ ) { ta = associations.get( i + 1 ); String path = InstanceHelpers.computeInstancePath( scopedInstances.get( i )); Assert.assertEquals( path, ta.getInstancePathOrComponentName()); if( instancePath.equals( path )) { Assert.assertEquals( newTargetId, ta.getTargetDescriptor().getId()); foundCustomInstance = true; } else { Assert.assertNull( ta.getTargetDescriptor()); } } for( int i=0; i<scopedComponents.size(); i++ ) { ta = associations.get( i + 1 + scopedInstances.size()); Assert.assertEquals( "@" + scopedComponents.get( i ).getName(), ta.getInstancePathOrComponentName()); Assert.assertNull( ta.getTargetDescriptor()); } Assert.assertTrue( foundCustomInstance ); ta = associations.get( 0 ); Assert.assertEquals( "", ta.getInstancePathOrComponentName()); Assert.assertEquals( newTargetId, ta.getTargetDescriptor().getId()); }