GetBackendStatusHandler extends ActionHandler { public void handleAction(ActionParameters params) throws ActionException { boolean alert = isRequestOnlyForLayersWithAlerts(params); List<BackendStatus> statuses; if (alert) { statuses = service.findAllWithAlert(); } else { statuses = modifyErrorsWithUnknownMessageToUnknown(service.findAll()); } statuses = modifyErrorsWithUnknownMessageToUnknown(statuses); LOG.debug("BackendStatus list size: " + statuses.size()); try { byte[] b = serialize(om, statuses); ResponseHelper.writeResponse(params, 200, ResponseHelper.CONTENT_TYPE_JSON_UTF8, b); } catch (JsonProcessingException e) { LOG.warn(e, "Failed to write JSON!"); throw new ActionException("Failed to serialize response to JSON!", e); } } GetBackendStatusHandler();  GetBackendStatusHandler(BackendStatusService service, ObjectMapper om); void handleAction(ActionParameters params);  }
@Test public void whenStatusIsErrorAndStatusMessageStartsWithUnknownShouldBeChangedToUnknownInResponse() throws JsonProcessingException, UnsupportedEncodingException, ActionException { ByteArrayOutputStream responseBody = new ByteArrayOutputStream(); Map<String, String> queryParams = new TreeMap<>(); queryParams.put(GetBackendStatusHandler.PARAM_SUBSET, GetBackendStatusHandler.SUBSET_ALL_KNOWN); ActionParameters params = new ActionParameters(); params.setRequest(mockHttpServletRequest("GET", queryParams)); params.setResponse(mockHttpServletResponse(responseBody)); handler.handleAction(params); String actual = responseBody.toString("UTF-8"); String expected = "{'backendstatus':[" + "{'maplayer_id':1,'status':'OK','statusjson':null,'infourl':'http: + "{'maplayer_id':2,'status':'UNKNOWN','statusjson':null,'infourl':null,'ts':null}," + "{'maplayer_id':3,'status':'UNKNOWN','statusjson':'stale','infourl':null,'ts':null}," + "{'maplayer_id':4,'status':'DOWN','statusjson':null,'infourl':'http: + "]}"; expected = expected.replace('\'', '"'); assertEquals(expected, actual); }
@Test public void whenNoParamsSpecifiedReturnsOnlyAlerts() throws JsonProcessingException, UnsupportedEncodingException, ActionException { ByteArrayOutputStream responseBody = new ByteArrayOutputStream(); ActionParameters params = new ActionParameters(); params.setRequest(mockHttpServletRequest()); params.setResponse(mockHttpServletResponse(responseBody)); handler.handleAction(params); String actual = responseBody.toString("UTF-8"); String expected = "{'backendstatus':[" + "{'maplayer_id':4,'status':'DOWN','statusjson':null,'infourl':'http: + "]}"; expected = expected.replace('\'', '"'); assertEquals(expected, actual); }