### Question:
HttpMetricNamesHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final Timer.Context httpMetricNamesHandlerTimerContext = HttpMetricNamesHandlerTimer.time(); final String tenantId = request.headers().get("tenantId"); HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; List<String> query = requestWithParams.getQueryParams().get("query"); if (query == null || query.size() != 1) { DefaultHandler.sendErrorResponse(ctx, request, "Invalid Query String", HttpResponseStatus.BAD_REQUEST); return; } if (discoveryHandle == null) { sendResponse(ctx, request, null, HttpResponseStatus.NOT_FOUND); return; } try { List<MetricName> metricNames = discoveryHandle.getMetricNames(tenantId, query.get(0)); sendResponse(ctx, request, getSerializedJSON(metricNames), HttpResponseStatus.OK); } catch (Exception e) { log.error(String.format("Exception occurred while trying to get metrics index for %s", tenantId), e); DefaultHandler.sendErrorResponse(ctx, request, "Error getting metrics index", HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpMetricNamesHandlerTimerContext.stop(); } } HttpMetricNamesHandler();  HttpMetricNamesHandler(DiscoveryIO discoveryHandle); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request); String getSerializedJSON(final List<MetricName> metricNames); static boolean EXP_TOKEN_SEARCH_IMPROVEMENTS; }### Answer:
@Test public void emptyPrefix() throws Exception { ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); handler.handle(context, createGetRequest("/v2.0/" + TENANT + "/metric_name/search")); verify(channel).write(argument.capture()); verify(mockDiscoveryHandle, never()).getMetricNames(anyString(), anyString()); String errorResponseBody = argument.getValue().content().toString(Charset.defaultCharset()); ErrorResponse errorResponse = getErrorResponse(errorResponseBody); assertEquals("Number of errors invalid", 1, errorResponse.getErrors().size()); assertEquals("Invalid error message", "Invalid Query String", errorResponse.getErrors().get(0).getMessage()); assertEquals("Invalid tenant", TENANT, errorResponse.getErrors().get(0).getTenantId()); assertEquals("Invalid status", HttpResponseStatus.BAD_REQUEST, argument.getValue().getStatus()); }
@Test public void invalidQuerySize() throws Exception { ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); handler.handle(context, createGetRequest("/v2.0/" + TENANT + "/metric_name/search?query=foo&query=bar")); verify(channel).write(argument.capture()); verify(mockDiscoveryHandle, never()).getMetricNames(anyString(), anyString()); String errorResponseBody = argument.getValue().content().toString(Charset.defaultCharset()); ErrorResponse errorResponse = getErrorResponse(errorResponseBody); assertEquals("Number of errors invalid", 1, errorResponse.getErrors().size()); assertEquals("Invalid error message", "Invalid Query String", errorResponse.getErrors().get(0).getMessage()); assertEquals("Invalid tenant", TENANT, errorResponse.getErrors().get(0).getTenantId()); assertEquals("Invalid status", HttpResponseStatus.BAD_REQUEST, argument.getValue().getStatus()); }
@Test public void validQuery() throws Exception { handler.handle(context, createGetRequest("/v2.0/" + TENANT + "/metric_name/search?query=foo")); verify(mockDiscoveryHandle, times(1)).getMetricNames(anyString(), anyString()); }