TargetResource implements ITargetResource { @Override public List<TargetUsageItem> findUsageStatistics( String targetId ) { this.logger.fine( "Request: list usage statistics for target " + targetId + "." ); return this.manager.targetsMngr().findUsageStatistics( targetId ); } TargetResource( Manager manager ); @Override List<TargetWrapperDescriptor> listTargets( String name, String qualifier ); @Override Response createOrUpdateTarget( String rawProperties, String targetId ); @Override Response loadTargetArchive( InputStream uploadedInputStream, FormDataContentDisposition fileDetail ); @Override Response deleteTarget( String targetId ); @Override Response getTargetProperties( String targetId ); @Override Response findTargetById( String targetId ); @Override Response associateTarget(
			String name,
			String qualifier,
			String instancePathOrComponentName,
			String targetId,
			boolean bind ); @Override Response updateHint( String name, String qualifier, String targetId, boolean bind ); @Override List<TargetUsageItem> findUsageStatistics( String targetId );  }
@Test public void testFindUsageStatistics_inexistingTarget() throws Exception { List<TargetUsageItem> items = this.resource.findUsageStatistics( "4" ); Assert.assertEquals( 0, items.size()); }
@Test public void testFindUsageStatistics() throws Exception { String t1 = (String) this.resource.createOrUpdateTarget( "id: t1\nhandler: h\nprop: ok", null ).getEntity(); Assert.assertNotNull( t1 ); String t2 = (String) this.resource.createOrUpdateTarget( "id: t2\nhandler: h\nprop: ok", null ).getEntity(); Assert.assertNotNull( t2 ); String t3 = (String) this.resource.createOrUpdateTarget( "id: t3\nhandler: h\nprop: ok", null ).getEntity(); Assert.assertNotNull( t3 ); TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); this.managerWrapper.addManagedApplication( new ManagedApplication( app )); this.resource.associateTarget( app.getName(), null, InstanceHelpers.computeInstancePath( app.getMySqlVm()), t1, true ); this.resource.associateTarget( app.getName(), null, null, t2, true ); List<TargetUsageItem> items = this.resource.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.resource.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.resource.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); this.manager.targetsMngr().lockAndGetTarget( app, app.getTomcatVm()); items = this.resource.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.resource.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); items = this.resource.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.manager.targetsMngr().unlockTarget( app, app.getTomcatVm()); items = this.resource.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.resource.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.resource.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); this.resource.associateTarget( app.getName(), null, InstanceHelpers.computeInstancePath( app.getMySqlVm()), null, false ); items = this.resource.findUsageStatistics( t1 ); Assert.assertEquals( 0, items.size()); items = this.resource.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.resource.findUsageStatistics( t3 ); Assert.assertEquals( 0, items.size()); }