InstanceHelpers { 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; } private  InstanceHelpers(); static String computeInstancePath( Instance inst ); static boolean haveSamePath( Instance i1, Instance i2 ); static String findInstanceName( String instancePath ); static List<Instance> buildHierarchicalList( Instance inst ); static void insertChild( Instance parent, Instance child ); static Map<String,String> findAllExportedVariables( Instance instance ); static Instance findInstanceByPath( AbstractApplication application, String instancePath ); static Instance findInstanceByPath( Instance rootInstance, String instancePath ); static List<Instance> findInstancesByComponentName( AbstractApplication application, String componentName ); static Instance findRootInstance( Instance instance ); static Instance findScopedInstance( Instance instance ); static List<Instance> findAllScopedInstances( AbstractApplication app ); static List<Instance> getAllInstances( AbstractApplication application ); static boolean tryToInsertChildInstance( AbstractApplication application, Instance parentInstance, Instance childInstance ); static boolean hasChildWithThisName( AbstractApplication application, Instance parentInstance, String nameToSearch ); static File findInstanceDirectoryOnAgent( Instance instance ); static int countInstances( String instancePath ); static Instance replicateInstance( Instance instance ); static boolean isTarget( Instance instance ); static void removeOffScopeInstances( Instance scopedInstance ); static String findRootInstancePath( String scopedInstancePath ); static void fixOverriddenExports( Instance instance );  }
@Test public void testFindInstancesByComponentName() { ApplicationTemplate app = new ApplicationTemplate(); Component tomcat = new Component( "tomcat" ).installerName( "puppet" ); Component other = new Component( "other" ).installerName( "chef" ); Instance i1 = new Instance( "i1" ).component( tomcat ); Instance i2 = new Instance( "i2" ).component( tomcat ); Instance i3 = new Instance( "i3" ).component( other ); Instance i4 = new Instance( "i4" ).component( other ); Graphs graphs = new Graphs(); graphs.getRootComponents().add( other ); graphs.getRootComponents().add( tomcat ); app.setGraphs( graphs ); InstanceHelpers.insertChild( i3, i1 ); app.getRootInstances().add( i2 ); app.getRootInstances().add( i3 ); app.getRootInstances().add( i4 ); List<Instance> tomcatInstances = InstanceHelpers.findInstancesByComponentName( app, tomcat.getName()); Assert.assertEquals( 2, tomcatInstances.size()); Assert.assertTrue( tomcatInstances.contains( i1 )); Assert.assertTrue( tomcatInstances.contains( i2 )); List<Instance> otherInstances = InstanceHelpers.findInstancesByComponentName( app, other.getName()); Assert.assertEquals( 2, otherInstances.size()); Assert.assertTrue( otherInstances.contains( i3 )); Assert.assertTrue( otherInstances.contains( i4 )); Assert.assertEquals( 0, InstanceHelpers.findInstancesByComponentName( app, "whatever" ).size()); }