Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Add custom map upload and tile server URL configuration tests (#1835)

Ekhornb29c213f0664
diff --git a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
index 5a9ff57a9..834b973a2 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
@@ -46,7 +46,7 @@ public class MapResourceImpl extends WebResource implements MapResource {
}
@Override
- public Object saveSettings(RequestParams requestParams, MapConfig mapConfig) {
+ public ObjectNode saveSettings(RequestParams requestParams, MapConfig mapConfig) {
return mapService.saveMapConfig(mapConfig);
}
diff --git a/manager/src/main/java/org/openremote/manager/map/MapService.java b/manager/src/main/java/org/openremote/manager/map/MapService.java
index 823446242..0ad4af035 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapService.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapService.java
@@ -620,7 +620,7 @@ public class MapService implements ContainerService {
boolean boundsValid = this.bounds.size() == 4
&& StreamSupport.stream(Spliterators.spliterator(this.bounds.elements(), 0, 4), false).allMatch(v -> v.numberType() != null);
- boolean centerValid = this.center.size() == 3
+ boolean centerValid = this.center.size() >= 2
&& StreamSupport.stream(Spliterators.spliterator(this.bounds.elements(), 0, 3), false).allMatch(v -> v.numberType() != null);
if (!boundsValid) {
LOG.log(Level.WARNING, "Map bounds are invalid.");
diff --git a/model/src/main/java/org/openremote/model/map/MapResource.java b/model/src/main/java/org/openremote/model/map/MapResource.java
index e2d1c6c72..e95b2464c 100644
--- a/model/src/main/java/org/openremote/model/map/MapResource.java
+++ b/model/src/main/java/org/openremote/model/map/MapResource.java
@@ -43,7 +43,7 @@ public interface MapResource {
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({Constants.WRITE_ADMIN_ROLE})
@Operation(operationId = "saveSettings", summary = "Update map settings")
- Object saveSettings(@BeanParam RequestParams requestParams, MapConfig mapConfig);
+ ObjectNode saveSettings(@BeanParam RequestParams requestParams, MapConfig mapConfig);
/**
* Returns style used to initialise Mapbox GL
diff --git a/test/src/test/groovy/org/openremote/test/map/MapResourceTest.groovy b/test/src/test/groovy/org/openremote/test/map/MapResourceTest.groovy
index e711ae76c..aa4df5175 100644
--- a/test/src/test/groovy/org/openremote/test/map/MapResourceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/map/MapResourceTest.groovy
@@ -1,25 +1,39 @@
package org.openremote.test.map
+import com.fasterxml.jackson.databind.node.ObjectNode
import groovy.json.JsonSlurper
+import jakarta.ws.rs.client.Entity
+import jakarta.ws.rs.core.MediaType
+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget
+import org.openremote.manager.map.MapService
+import org.openremote.model.manager.MapConfig
import org.openremote.model.map.MapResource
import org.openremote.model.util.ValueUtil
import org.openremote.test.ManagerContainerTrait
-import spock.lang.Ignore
+import spock.lang.Shared
import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import java.nio.file.Files
+import java.nio.file.Path
import static org.openremote.container.util.MapAccess.getString
import static org.openremote.manager.security.ManagerIdentityProvider.*
import static org.openremote.model.Constants.*
// TODO: Remove this once map service is removed and we fallback to standalone tile server
-@Ignore
class MapResourceTest extends Specification implements ManagerContainerTrait {
+ @Shared
+ static ResteasyWebTarget clientTarget
- def "Retrieve map settings"() {
+ @Shared
+ static MapResource mapResource
+
+ def setupSpec() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())
- and: "an authenticated user"
+ and: "an access_token is retrieved"
def realm = MASTER_REALM
def accessToken = authenticate(
container,
@@ -29,12 +43,14 @@ class MapResourceTest extends Specification implements ManagerContainerTrait {
getString(container.getConfig(), OR_ADMIN_PASSWORD, OR_ADMIN_PASSWORD_DEFAULT)
).token
- and: "a test client target"
- def clientTarget = getClientApiTarget(serverUri(serverPort), realm, accessToken)
+ and: "a test client target set"
+ clientTarget = getClientApiTarget(serverUri(serverPort), realm, accessToken)
- and: "the map resource"
- def mapResource = clientTarget.proxy(MapResource.class)
+ and: "the map resource is configured"
+ mapResource = clientTarget.proxy(MapResource.class)
+ }
+ def "Retrieve map settings"() {
when: "a request has been made"
def mapSettings = mapResource.getSettings(null)
@@ -45,9 +61,102 @@ class MapResourceTest extends Specification implements ManagerContainerTrait {
def json = new JsonSlurper().parseText(ValueUtil.asJSON(mapSettings).orElse("null"))
json.options != null
json.options.default != null
- json.options.default.center.size() == 2
+ json.options.default.center.size() >= 2
json.options.default.bounds.size() == 4
json.sources != null
json.layers.size() > 0
}
+
+ def "Upload custom map tiles"() {
+ when: "valid tiles are uploaded"
+ def response = clientTarget
+ .path("map/upload")
+ .queryParam("filename", "eindhoven.mbtiles")
+ .request()
+ .post(Entity.entity(Files.readAllBytes(Path.of("manager/src/map/mapdata.mbtiles")), MediaType.APPLICATION_OCTET_STREAM_TYPE))
+
+ then: "the custom tiles should be saved"
+ new PollingConditions(timeout: 20, delay: 1).eventually {
+ assert Files.exists(Path.of("tmp/map/eindhoven.mbtiles"))
+ }
+
+ and: "and the mapsettings match with the custom mbtiles metadata"
+ def json = ValueUtil.JSON.readValue(response.getEntity(), MapConfig.class)
+ json.options != null
+ json.options.default != null
+ json.options.default.center.size() == 3
+ json.options.default.bounds.size() == 4
+ json.sources != null
+
+ when: "invalid tiles are uploaded"
+ mapResource.deleteMap(null)
+
+ then: "an internal server error should occur"
+ !Files.exists(Path.of("tmp/map/eindhoven.mbtiles"))
+
+ when: "invalid tiles are uploaded"
+ response = clientTarget
+ .path("map/upload")
+ .queryParam("filename", "eindhoven.mbtiles")
+ .request()
+ .post(Entity.entity(Files.readAllBytes(Path.of("manager/src/map/mapsettings.json")), MediaType.APPLICATION_OCTET_STREAM_TYPE))
+
+ then: "an internal server error should occur"
+ response.status == 500
+
+ when: "illegal filename was specified"
+ response = clientTarget
+ .path("map/upload")
+ .queryParam("filename", "../mapdata.mbtiles")
+ .request()
+ .post(Entity.entity(Files.readAllBytes(Path.of("manager/src/map/mapdata.mbtiles")), MediaType.APPLICATION_OCTET_STREAM_TYPE))
+
+ then: "bad request error should occur"
+ response.status == 400
+
+ when: "the tiles are too large"
+ container.getService(MapService.class).customMapLimit = 10
+ response = clientTarget
+ .path("map/upload")
+ .queryParam("filename", "mapdata.mbtiles")
+ .request()
+ .post(Entity.entity(Files.readAllBytes(Path.of("manager/src/map/mapdata.mbtiles")), MediaType.APPLICATION_OCTET_STREAM_TYPE))
+
+ then: "request entity too large error should occur"
+ response.status == 413
+ }
+
+ def "Configure custom tile server URL"() {
+ when: "mapsettings with a valid tile server URL is configured"
+ def mapSettings = ValueUtil.parse("""{
+ "options" : { "default": {} },
+ "sources" : {
+ "vector_tiles" : {
+ "type" : "vector" ,
+ "tiles" : [ "https://example.com/tileset/{z}/{x}/{y}.mvt" ],
+ "custom" : true
+ }
+ }
+ }""", MapConfig.class)
+ def savedSettings = (ObjectNode)mapResource.saveSettings(null, mapSettings.get())
+
+ then: "the custom tile server URL should be returned"
+ savedSettings.get("sources").get("vector_tiles").get("url").textValue() == "https://example.com/tileset/{z}/{x}/{y}.mvt"
+
+ when: "an invalid tile server URL is configured"
+ mapSettings = ValueUtil.parse("""{
+ "options" : { "default": {} },
+ "sources" : {
+ "vector_tiles" : {
+ "type" : "vector" ,
+ "tiles" : [ "https://example.com/tileset.mvt" ],
+ "custom" : true
+ }
+ }
+ }""", MapConfig.class)
+ savedSettings = (ObjectNode)mapResource.saveSettings(null, mapSettings.get())
+
+ then: "the URL should be reset back to normal"
+ savedSettings.get("sources").get("vector_tiles").get("url").textValue() == MapService.DEFAULT_VECTOR_TILES_URL
+ }
}