From 7e48284f8f6a0636658226eec858ce28a93f36f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20M=C3=A4kinen?= Date: Wed, 6 Mar 2024 12:49:51 +0200 Subject: [PATCH] Use layer url as default with an option to override it --- .../control/statistics/db/RegionSet.java | 4 +++- .../statistics/RegionSetHelperTest.java | 23 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/service-statistics-common/src/main/java/fi/nls/oskari/control/statistics/db/RegionSet.java b/service-statistics-common/src/main/java/fi/nls/oskari/control/statistics/db/RegionSet.java index 6654587e7e..717dd30941 100755 --- a/service-statistics-common/src/main/java/fi/nls/oskari/control/statistics/db/RegionSet.java +++ b/service-statistics-common/src/main/java/fi/nls/oskari/control/statistics/db/RegionSet.java @@ -56,6 +56,8 @@ public String getAttributes() { public void setAttributes(String attributes) { this.attributes = attributes; + // force getStatsJSON to recalculate + this.stats = null; } public JSONObject asJSON() { @@ -71,7 +73,7 @@ public String getIdProperty() { } public String getFeaturesUrl() { - return getStatsJSON().optString("featuresUrl"); + return getStatsJSON().optString("featuresUrl", getUrl()); } private JSONObject getStatsJSON() { diff --git a/service-statistics-common/src/test/java/fi/nls/oskari/control/statistics/RegionSetHelperTest.java b/service-statistics-common/src/test/java/fi/nls/oskari/control/statistics/RegionSetHelperTest.java index f1d34f4dcb..4d1f17bd0d 100644 --- a/service-statistics-common/src/test/java/fi/nls/oskari/control/statistics/RegionSetHelperTest.java +++ b/service-statistics-common/src/test/java/fi/nls/oskari/control/statistics/RegionSetHelperTest.java @@ -73,12 +73,33 @@ public void testGeoJSONResourceFileWorks() throws MismatchedDimensionException, } } + @Test + public void testFeaturesUrl() throws MismatchedDimensionException, FactoryException, TransformException, ServiceException, IOException, JSONException { + String endPoint = "https://my.domain"; + String overridingEndPoint = endPoint + "/feat"; + RegionSet kunnatJSON = new RegionSet(); + kunnatJSON.setId(-1); + kunnatJSON.setUrl(endPoint); + kunnatJSON.setName("oskari:kunnat2013"); + kunnatJSON.setSrs_name("EPSG:3067"); + assertEquals("Should return url when attributes NOT defined", endPoint, kunnatJSON.getFeaturesUrl()); + + kunnatJSON.setAttributes(getAttributes("kuntakoodi", "kuntanimi", overridingEndPoint)); + assertEquals("Should return features url when attributes ARE defined", overridingEndPoint, kunnatJSON.getFeaturesUrl()); + + overridingEndPoint = null; + kunnatJSON.setAttributes(getAttributes("kuntakoodi", "kuntanimi", overridingEndPoint)); + assertEquals("Should return url when attributes ARE defined WITHOUT features url", endPoint, kunnatJSON.getFeaturesUrl()); + } + private String getAttributes(String regionIdTag, String nameIdTag, String featuresUrl) throws JSONException { JSONObject attributes = new JSONObject(); JSONObject statistics = new JSONObject(); statistics.put("regionIdTag", regionIdTag); statistics.put("nameIdTag", nameIdTag); - statistics.put("featuresUrl", featuresUrl); + if (featuresUrl != null) { + statistics.put("featuresUrl", featuresUrl); + } attributes.put("statistics", statistics); return attributes.toString(); }