ResetCacheServlet extends BaseCartePlugin { @Override public void handleRequest( CarteRequest request ) throws IOException { Collection<String> names = request.getParameters().get( NAME_PARAMETER ); if ( names == null || names.isEmpty() ) { request.respond( 400 ).withMessage( NAME_PARAMETER + " not specified" ); return; } final Set<Cache> cacheSet = FluentIterable.from( names ) .transform( new Function<String, Cache>() { @Override public Cache apply( String name ) { return factory.getCache( name ).orNull(); } } ) .filter( Predicates.notNull() ) .toSet(); for ( Cache cache : cacheSet ) { cache.clear(); } request .respond( 200 ) .with( MediaType.PLAIN_TEXT_UTF_8.toString(), new WriterResponse() { @Override public void write( PrintWriter writer ) throws IOException { if ( cacheSet.isEmpty() ) { writer.println( "No matching caches to flush." ); } for ( Cache cache : cacheSet ) { writer.println( "Cleared cache: " + cache.getName() ); } writer.println( "Done" ); } } ); } ResetCacheServlet( ServiceCacheFactory factory ); String getContextPath(); @Override void handleRequest( CarteRequest request );  }
@Test public void testClearCache() throws Exception { parameterMap.putAll( "name", ImmutableList.of( "cache1", "noneSuch", "cache1" ) ); servlet.handleRequest( request ); verify( cache ).clear(); verify( request ).respond( 200 ); }
@Test public void testBadRequest() throws Exception { servlet.handleRequest( request ); verify( request ).respond( 400 ); }