ServiceCacheController extends AbstractController { protected PushDownOptimizationMeta locateServiceCacheMeta( DataServiceModel model ) { List<PushDownOptimizationMeta> cacheOptimizations = model.getPushDownOptimizations( factory.getType() ); PushDownOptimizationMeta meta; if ( cacheOptimizations.isEmpty() ) { meta = new PushDownOptimizationMeta(); meta.setStepName( model.getServiceStep() ); meta.setType( factory.createPushDown() ); model.add( meta ); } else { meta = cacheOptimizations.get( 0 ); } if ( cacheOptimizations.size() > 1 ) { model.removeAll( cacheOptimizations.subList( 1, cacheOptimizations.size() ) ); } return meta; } ServiceCacheController( ServiceCacheFactory factory ); void initBindings( DataServiceModel model );  }
@Test public void testLocateServiceCacheMeta() throws Exception { controller = new ServiceCacheController( factory ); when( factory.getType() ).thenReturn( ServiceCache.class ); when( model.getPushDownOptimizations( ServiceCache.class ) ) .thenReturn( ImmutableList.<PushDownOptimizationMeta>of() ); when( factory.createPushDown() ).thenReturn( serviceCache = mock( ServiceCache.class ) ); meta = controller.locateServiceCacheMeta( model ); assertThat( meta.isEnabled(), is( true ) ); assertThat( meta.getType(), is( (PushDownType) serviceCache ) ); PushDownOptimizationMeta meta2 = mock( PushDownOptimizationMeta.class ); when( model.getPushDownOptimizations( ServiceCache.class ) ).thenReturn( ImmutableList.of( meta, meta2 ) ); assertThat( controller.locateServiceCacheMeta( model ), is( meta ) ); verify( model ).add( meta ); verify( model ).removeAll( ImmutableList.of( meta2 ) ); }