TargetsMngrImpl implements ITargetsMngr { @Override public List<TargetUsageItem> findUsageStatistics( String targetId ) { List<String> appNames; synchronized( LOCK ) { appNames = applicationsThatUse( targetId ); } Set<TargetUsageItem> result = new HashSet<> (); for( Map.Entry<InstanceContext,String> entry : this.instanceToCachedId.entrySet()) { if( ! entry.getValue().equals( targetId )) continue; String appName = entry.getKey().getName(); TargetUsageItem item = new TargetUsageItem(); item.setName( appName ); item.setVersion( entry.getKey().getQualifier()); item.setReferencing( true ); item.setUsing( appNames.contains( appName )); result.add( item ); } return new ArrayList<>( result ); } TargetsMngrImpl( IConfigurationMngr configurationMngr ); @Override String createTarget( String targetContent ); @Override String createTarget( File targetPropertiesFile, ApplicationTemplate creator ); @Override void updateTarget( String targetId, String newTargetContent ); @Override void deleteTarget( String targetId ); @Override void associateTargetWith( String targetId, AbstractApplication app, String instancePathOrComponentName ); @Override void dissociateTargetFrom( AbstractApplication app, String instancePathOrComponentName ); @Override void copyOriginalMapping( Application app ); @Override void applicationWasDeleted( AbstractApplication app ); @Override TargetProperties findTargetProperties( AbstractApplication app, String instancePath ); @Override TargetProperties findTargetProperties( String targetId ); @Override String findTargetId( AbstractApplication app, String instancePath, boolean strict ); @Override String findTargetId( AbstractApplication app, String instancePath ); @Override Map<String,byte[]> findScriptResourcesForAgent( String targetId ); @Override Map<String,byte[]> findScriptResourcesForAgent( AbstractApplication app, Instance scopedInstance ); @Override File findScriptForDm( AbstractApplication app, Instance scopedInstance ); @Override List<TargetWrapperDescriptor> listAllTargets(); @Override TargetWrapperDescriptor findTargetById( String targetId ); @Override List<TargetWrapperDescriptor> listPossibleTargets( AbstractApplication app ); @Override void addHint( String targetId, AbstractApplication app ); @Override void removeHint( String targetId, AbstractApplication app ); @Override TargetProperties lockAndGetTarget( Application app, Instance scopedInstance ); @Override void unlockTarget( Application app, Instance scopedInstance ); @Override List<TargetUsageItem> findUsageStatistics( String targetId );  }
@Test public void testFindUsageStatistics_inexistingTarget() throws Exception { List<TargetUsageItem> items = this.mngr.findUsageStatistics( "4" ); Assert.assertEquals( 0, items.size()); }
@Test public void testFindUsageStatistics() throws Exception { TestApplication app = new TestApplication(); Instance newRootInstance = new Instance( "newRoot" ).component( app.getMySqlVm().getComponent()); app.getRootInstances().add( newRootInstance ); String t1 = this.mngr.createTarget( "prop: ok\nid: t1\nhandler: h" ); String t2 = this.mngr.createTarget( "prop: ok\nid: t2\nhandler: h" ); String t3 = this.mngr.createTarget( "prop: ok\nid: t3\nhandler: h" ); this.mngr.associateTargetWith( t1, app, InstanceHelpers.computeInstancePath( app.getMySqlVm())); this.mngr.associateTargetWith( t1, app, InstanceHelpers.computeInstancePath( newRootInstance )); this.mngr.associateTargetWith( t2, app, null ); List<TargetUsageItem> items = this.mngr.findUsageStatistics( t1 ); Assert.assertEquals( 1, items.size()); TargetUsageItem item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t2 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); this.mngr.lockAndGetTarget( app, app.getTomcatVm()); items = this.mngr.findUsageStatistics( t1 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); items = this.mngr.findUsageStatistics( t2 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertTrue( item.isReferencing()); Assert.assertTrue( item.isUsing()); this.mngr.unlockTarget( app, app.getTomcatVm()); items = this.mngr.findUsageStatistics( t1 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t2 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); this.mngr.dissociateTargetFrom( app, InstanceHelpers.computeInstancePath( app.getMySqlVm())); items = this.mngr.findUsageStatistics( t1 ); Assert.assertEquals( 1, items.size()); this.mngr.dissociateTargetFrom( app, InstanceHelpers.computeInstancePath( newRootInstance )); items = this.mngr.findUsageStatistics( t1 ); Assert.assertEquals( 0, items.size()); items = this.mngr.findUsageStatistics( t2 ); Assert.assertEquals( 1, items.size()); item = items.get( 0 ); Assert.assertEquals( app.getName(), item.getName()); Assert.assertNull( item.getVersion()); Assert.assertFalse( item.isUsing()); Assert.assertTrue( item.isReferencing()); items = this.mngr.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); }