HealthRestServiceImpl implements HealthRestService, HealthService { @Override public State getState() { boolean failed = false; boolean ready = true; BundleContext ctx = getBundleContext(); BundleStateService bundleStateService = getBundleStateService(); Bundle[] bundles = ctx.getBundles(); if ( bundles.length < BUNDLE_COUNT ) { LOG.warn( "Bundle count is {}", bundles.length ); return State.LOADING; } for ( Bundle bundle : bundles ) { BundleState bundleState = bundleStateService.getState( bundle ); if ( bundleState == BundleState.Failure ) { failed = true; break; } int bundleIntState = bundle.getState(); String bundleDiag = bundleStateService.getDiag( bundle ); if ( bundleState == BundleState.Waiting && bundleIntState == Bundle.ACTIVE && bundleDiag != null && bundleDiag.contains( "Missing" ) ) { failed = true; break; } if ( !( ( bundleIntState == Bundle.ACTIVE ) || ( bundleIntState == Bundle.RESOLVED ) ) ) { ready = false; break; } } return failed ? State.FAILED : ready ? State.READY : State.LOADING; }  @Override Response isReady(); @Override State getState();  }
@Test public void testGetState() { List<Bundle> bundles = Lists.newArrayList( bundle ); doReturn( bundles.toArray( new Bundle[0] ) ).when( bundleContext ).getBundles(); assertEquals( HealthService.State.LOADING, healthRestService.getState() ); doReturn( BundleState.Failure ).when( bundleStateService ).getState( bundle ); for ( int i = 0; i <= HealthService.BUNDLE_COUNT; i++ ) { bundles.add( bundle ); } doReturn( bundles.toArray( new Bundle[0] ) ).when( bundleContext ).getBundles(); assertEquals( HealthService.State.FAILED, healthRestService.getState() ); doReturn( BundleState.Active ).when( bundleStateService ).getState( bundle ); assertEquals( HealthService.State.READY, healthRestService.getState() ); }