### Question:
ApiVersionFilter implements HttpRequestFilter { @Override public HttpRequest filter(HttpRequest request) throws HttpException { checkArgument(request instanceof GeneratedHttpRequest, "This filter can only be applied to GeneratedHttpRequest objects"); GeneratedHttpRequest generatedRequest = (GeneratedHttpRequest) request; String commandName = config.getCommandName(generatedRequest.getInvocation()); String customApiVersion = versions.get(commandName); if (customApiVersion == null) { Invokable<?, ?> invoked = generatedRequest.getInvocation().getInvokable(); String className = invoked.getOwnerType().getRawType().getSimpleName(); customApiVersion = versions.get(className); } if (customApiVersion != null) { return request.toBuilder().replaceQueryParam("api-version", customApiVersion).build(); } return request; } @Inject  ApiVersionFilter(InvocationConfig config, Function<Predicate<String>, Map<String, String>> filterStringsBoundByName); @Override HttpRequest filter(HttpRequest request);  }### Answer:
@Test(expectedExceptions = IllegalArgumentException.class) public void testFailIfNoGeneratedHttpRequest() { ApiVersionFilter filter = new ApiVersionFilter(config, filterStringsBoundToInjectorByName(new Properties())); filter.filter(HttpRequest.builder().method("GET").endpoint("http: }
@Test public void testOverrideMethodVersion() { Properties props = new Properties(); props.setProperty(API_VERSION_PREFIX + "named:get", "namedversion"); props.setProperty(API_VERSION_PREFIX + "VersionedApi.noName", "noNameversion"); ApiVersionFilter filter = new ApiVersionFilter(config, filterStringsBoundToInjectorByName(props)); HttpRequest request = GeneratedHttpRequest.builder().method("GET").endpoint("http: .invocation(noName).addQueryParam("api-version", "original", "original2").build(); HttpRequest filtered = filter.filter(request); assertEquals(filtered.getEndpoint().getQuery(), "api-version=noNameversion"); request = GeneratedHttpRequest.builder().method("GET").endpoint("http: .addQueryParam("api-version", "original", "original2").build(); filtered = filter.filter(request); assertEquals(filtered.getEndpoint().getQuery(), "api-version=namedversion"); }
@Test public void testFallbackToClassName() { Properties props = new Properties(); props.setProperty(API_VERSION_PREFIX + "VersionedApi", "classversion"); ApiVersionFilter filter = new ApiVersionFilter(config, filterStringsBoundToInjectorByName(props)); HttpRequest request = GeneratedHttpRequest.builder().method("GET").endpoint("http: .invocation(noName).addQueryParam("api-version", "original", "original2").build(); HttpRequest filtered = filter.filter(request); assertEquals(filtered.getEndpoint().getQuery(), "api-version=classversion"); request = GeneratedHttpRequest.builder().method("GET").endpoint("http: .addQueryParam("api-version", "original", "original2").build(); filtered = filter.filter(request); assertEquals(filtered.getEndpoint().getQuery(), "api-version=classversion"); }
@Test public void testNothingChangesIfNoCustomVersion() { ApiVersionFilter filter = new ApiVersionFilter(config, filterStringsBoundToInjectorByName(new Properties())); HttpRequest request = GeneratedHttpRequest.builder().method("GET").endpoint("http: .addQueryParam("api-version", "foo").build(); HttpRequest filtered = filter.filter(request); assertEquals(filtered.getEndpoint().getQuery(), "api-version=foo"); }