@Test public void testElasticSearchSearchNotCalledEmptyQuery() throws Exception { ArgumentCaptor<FullHttpResponse> argument = ArgumentCaptor.forClass(FullHttpResponse.class); handler.handle(context, createGetRequest("/v2.0/" + TENANT + "/events/")); verify(channel).write(argument.capture()); verify(searchIO, never()).search(TENANT, new HashMap<String, List<String>>()); 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", "Error: Query should contain at least one query parameter", errorResponse.getErrors().get(0).getMessage()); assertEquals("Invalid tenant", TENANT, errorResponse.getErrors().get(0).getTenantId()); assertEquals("Invalid status", HttpResponseStatus.BAD_REQUEST, argument.getValue().getStatus()); }
@Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); ObjectMapper objectMapper = new ObjectMapper(); String responseBody = null; final Timer.Context httpEventsFetchTimerContext = httpEventsFetchTimer.time(); try { HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; Map<String, List<String>> params = requestWithParams.getQueryParams(); if (params == null || params.size() == 0) { throw new InvalidDataException("Query should contain at least one query parameter"); } parseDateFieldInQuery(params, "from"); parseDateFieldInQuery(params, "until"); List<Map<String, Object>> searchResult = searchIO.search(tenantId, params); responseBody = objectMapper.writeValueAsString(searchResult); DefaultHandler.sendResponse(ctx, request, responseBody, HttpResponseStatus.OK, null); } catch (InvalidDataException e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsFetchTimerContext.stop(); } }
HttpEventsQueryHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); ObjectMapper objectMapper = new ObjectMapper(); String responseBody = null; final Timer.Context httpEventsFetchTimerContext = httpEventsFetchTimer.time(); try { HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; Map<String, List<String>> params = requestWithParams.getQueryParams(); if (params == null || params.size() == 0) { throw new InvalidDataException("Query should contain at least one query parameter"); } parseDateFieldInQuery(params, "from"); parseDateFieldInQuery(params, "until"); List<Map<String, Object>> searchResult = searchIO.search(tenantId, params); responseBody = objectMapper.writeValueAsString(searchResult); DefaultHandler.sendResponse(ctx, request, responseBody, HttpResponseStatus.OK, null); } catch (InvalidDataException e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsFetchTimerContext.stop(); } } }
HttpEventsQueryHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); ObjectMapper objectMapper = new ObjectMapper(); String responseBody = null; final Timer.Context httpEventsFetchTimerContext = httpEventsFetchTimer.time(); try { HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; Map<String, List<String>> params = requestWithParams.getQueryParams(); if (params == null || params.size() == 0) { throw new InvalidDataException("Query should contain at least one query parameter"); } parseDateFieldInQuery(params, "from"); parseDateFieldInQuery(params, "until"); List<Map<String, Object>> searchResult = searchIO.search(tenantId, params); responseBody = objectMapper.writeValueAsString(searchResult); DefaultHandler.sendResponse(ctx, request, responseBody, HttpResponseStatus.OK, null); } catch (InvalidDataException e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsFetchTimerContext.stop(); } } HttpEventsQueryHandler(EventsIO searchIO); }
HttpEventsQueryHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); ObjectMapper objectMapper = new ObjectMapper(); String responseBody = null; final Timer.Context httpEventsFetchTimerContext = httpEventsFetchTimer.time(); try { HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; Map<String, List<String>> params = requestWithParams.getQueryParams(); if (params == null || params.size() == 0) { throw new InvalidDataException("Query should contain at least one query parameter"); } parseDateFieldInQuery(params, "from"); parseDateFieldInQuery(params, "until"); List<Map<String, Object>> searchResult = searchIO.search(tenantId, params); responseBody = objectMapper.writeValueAsString(searchResult); DefaultHandler.sendResponse(ctx, request, responseBody, HttpResponseStatus.OK, null); } catch (InvalidDataException e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsFetchTimerContext.stop(); } } HttpEventsQueryHandler(EventsIO searchIO); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request); }
HttpEventsQueryHandler implements HttpRequestHandler { @Override public void handle(ChannelHandlerContext ctx, FullHttpRequest request) { Tracker.getInstance().track(request); final String tenantId = request.headers().get("tenantId"); ObjectMapper objectMapper = new ObjectMapper(); String responseBody = null; final Timer.Context httpEventsFetchTimerContext = httpEventsFetchTimer.time(); try { HttpRequestWithDecodedQueryParams requestWithParams = (HttpRequestWithDecodedQueryParams) request; Map<String, List<String>> params = requestWithParams.getQueryParams(); if (params == null || params.size() == 0) { throw new InvalidDataException("Query should contain at least one query parameter"); } parseDateFieldInQuery(params, "from"); parseDateFieldInQuery(params, "until"); List<Map<String, Object>> searchResult = searchIO.search(tenantId, params); responseBody = objectMapper.writeValueAsString(searchResult); DefaultHandler.sendResponse(ctx, request, responseBody, HttpResponseStatus.OK, null); } catch (InvalidDataException e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.BAD_REQUEST); } catch (Exception e) { log.error(String.format("Exception %s", e.toString())); responseBody = String.format("Error: %s", e.getMessage()); DefaultHandler.sendErrorResponse(ctx, request, responseBody, HttpResponseStatus.INTERNAL_SERVER_ERROR); } finally { httpEventsFetchTimerContext.stop(); } } HttpEventsQueryHandler(EventsIO searchIO); @Override void handle(ChannelHandlerContext ctx, FullHttpRequest request);  }