CoordinatesHandler extends ActionHandler { @Override public void handleAction(final ActionParameters params) throws ActionException { final Point point = new Point(params.getRequiredParamDouble(PARAM_LON), params.getRequiredParamDouble(PARAM_LAT)); final String srs = params.getRequiredParam(PARAM_SRS); final String target = params.getRequiredParam(TARGET_SRS); LOG.debug("Params - lon", point.getLon(), "lat", point.getLat(), "in", srs); try { PointTransformer transformer = getTransformer(); Point value = srs.equals(target) ? point : transformer.reproject(point, srs, target); LOG.debug("Reprojected - lon", value.getLon(), "lat", value.getLat(), "in", target); JSONObject response = new JSONObject(); JSONHelper.putValue(response, PARAM_LON, value.getLon()); JSONHelper.putValue(response, PARAM_LAT, value.getLat()); JSONHelper.putValue(response, PARAM_SRS, target); ResponseHelper.writeResponse(params, response); } catch (RuntimeException ex) { throw new ActionParamsException(ex.getMessage()); } }  @Override void init(); @Override void handleAction(final ActionParameters params);  }
@Test(expected = ActionParamsException.class) public void testHandleActionNoParams() throws Exception { handler.handleAction(createActionParams()); }
@Test(expected = ActionParamsException.class) public void testHandleActionNoCoordinates() throws Exception { Map<String, String> params = getValidParams(); params.remove(PARAM_LAT); handler.handleAction(createActionParams(params)); }
@Test(expected = ActionParamsException.class) public void testHandleActionNoSourceSRS() throws Exception { Map<String, String> params = getValidParams(); params.remove(PARAM_SRS); handler.handleAction(createActionParams(params)); }
@Test(expected = ActionParamsException.class) public void testHandleActionNoTargetSRS() throws Exception { Map<String, String> params = getValidParams(); params.remove(handler.TARGET_SRS); handler.handleAction(createActionParams(params)); }
@Test public void testHandleAction4326to3067() throws Exception { Map<String, String> params = getValidParams(); handler.handleAction(createActionParams(params)); assertEquals(PARAM_LAT, 6822546.781459001, getResponseJSON().getDouble(PARAM_LAT), 0.0); assertEquals(PARAM_LON, 327578.78108392254, getResponseJSON().getDouble(PARAM_LON), 1e-9); }
@Test public void testHandleAction3067to4326() throws Exception { Map<String, String> params = new HashMap<>(); params.put(PARAM_LON, "327578.7810839222"); params.put(PARAM_LAT, "6822546.781459001"); params.put(PARAM_SRS, "EPSG:3067"); params.put(handler.TARGET_SRS, "EPSG:4326"); handler.handleAction(createActionParams(params)); assertEquals(PARAM_LAT, 61.4980214, getResponseJSON().getDouble(PARAM_LAT), 0.00001); assertEquals(PARAM_LON, 23.7603118, getResponseJSON().getDouble(PARAM_LON), 0.00001); }