100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Add support for null AssetQuery in AssetResource public asset queries and added tests
Rich Turner8d78c2dceaa8
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex 1874852b3..cb5314466 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -299,7 +299,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerif (authContext == null) {// Check attribute has public write flagif (!oldAttribute.hasMeta(MetaItemType.ACCESS_PUBLIC_WRITE)) {- throw new AssetProcessingException(NO_AUTH_CONTEXT);+ throw new AssetProcessingException(INSUFFICIENT_ACCESS);}// Check read-onlyif (oldAttribute.hasMeta(MetaItemType.READ_ONLY)) {diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex aa0fd96f0..1dc40d004 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -546,10 +546,14 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourString requestRealm = getRequestRealm();- if (query == null || TextUtil.isNullOrEmpty(requestRealm)) {+ if (TextUtil.isNullOrEmpty(requestRealm)) {return EMPTY_ASSETS;}+ if (query == null) {+ query = new AssetQuery();+ }+// Force realm to be request realmif (query.tenant == null) {query.tenant(new TenantPredicate(requestRealm));@@ -578,7 +582,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour@Overridepublic Asset<?>[] getPublicAssets(RequestParams requestParams, String q) {- AssetQuery assetQuery = Values.parse(q, AssetQuery.class)+ AssetQuery assetQuery = TextUtil.isNullOrEmpty(q) ? null : Values.parse(q, AssetQuery.class).orElseThrow(() -> new WebApplicationException("Error parsing query parameter 'q' as JSON object", BAD_REQUEST));Asset<?>[] result = queryPublicAssets(requestParams, assetQuery);@@ -609,6 +613,8 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourfailure = processingException.getReason();}+ } catch (AssetProcessingException e) {+ failure = e.getReason();} catch (IllegalStateException ex) {failure = AttributeWriteFailure.UNKNOWN;}diff --git a/model/src/main/java/org/openremote/model/asset/AssetResource.java b/model/src/main/java/org/openremote/model/asset/AssetResource.javaindex e42138be6..b94014427 100644--- a/model/src/main/java/org/openremote/model/asset/AssetResource.java+++ b/model/src/main/java/org/openremote/model/asset/AssetResource.java@@ -206,6 +206,7 @@ public interface AssetResource {@PUT@Path("{assetId}/attribute/{attributeName}")@Consumes(APPLICATION_JSON)+ @Produces(APPLICATION_JSON)@Operation(description = "Write to a single attribute", responses = {@ApiResponse(content = @Content(mediaType = "application/json",@@ -214,6 +215,7 @@ public interface AssetResource {@PUT@Consumes(APPLICATION_JSON)+ @Produces(APPLICATION_JSON)@Path("attributes")AttributeWriteResult[] writeAttributeValues(@BeanParam RequestParams requestParams, AttributeState[] attributeStates);diff --git a/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java b/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.javaindex 7258bbc6e..219d11c8d 100644--- a/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java+++ b/test/src/main/java/org/openremote/test/setup/ManagerTestSetup.java@@ -34,6 +34,7 @@ import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.MetaItem;import org.openremote.model.geo.GeoJSONPoint;import org.openremote.model.security.Tenant;+import org.openremote.model.value.MetaItemType;import org.openremote.model.value.ValueConstraint;import org.openremote.model.value.ValueType;import org.openremote.model.value.Values;@@ -282,6 +283,12 @@ public class ManagerTestSetup extends ManagerSetup {// The "Apartment 1" is the demo apartment with complex scenesBuildingAsset apartment1 = createDemoApartment(smartBuilding, "Apartment 1", new GeoJSONPoint(5.454233, 51.446800));apartment1.setParent(smartBuilding);+ apartment1.setAccessPublicRead(true);+ apartment1.getAttribute(Asset.LOCATION).ifPresent(locationAttr ->+ locationAttr.getMeta().addOrReplace(+ new MetaItem<>(ACCESS_PUBLIC_READ),+ new MetaItem<>(ACCESS_PUBLIC_WRITE)+ ));apartment1 = assetStorageService.merge(apartment1);apartment1Id = apartment1.getId();@@ -433,13 +440,16 @@ public class ManagerTestSetup extends ManagerSetup {apartment2Id = apartment2.getId();RoomAsset apartment2Livingroom = new RoomAsset("Living Room 2");+ apartment2Livingroom.setAccessPublicRead(true);apartment2Livingroom.setParent(apartment2);ObjectNode objectMap = Values.createJsonObject();objectMap.put("cactus", 0.8);apartment2Livingroom.getAttributes().addOrReplace(- new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454109, 51.446631)),+ new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454109, 51.446631)).addMeta(+ new MetaItem<>(ACCESS_PUBLIC_READ)+ ),new Attribute<>("motionSensor", BOOLEAN, false).addMeta(new MetaItem<>(LABEL, "Motion Sensor"),diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovyindex f4651e440..688bf0383 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy@@ -1,6 +1,9 @@package org.openremote.test.assetsimport org.openremote.manager.setup.SetupService+import org.openremote.model.attribute.AttributeState+import org.openremote.model.attribute.AttributeWriteFailure+import org.openremote.model.value.Valuesimport org.openremote.test.setup.KeycloakTestSetupimport org.openremote.test.setup.ManagerTestSetupimport org.openremote.model.asset.Asset@@ -825,4 +828,51 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTraithen: "the result should match"assert testAsset.getAttribute("myCustomAttribute").get().getMetaItem(LABEL).get().getValue().get() == "My label update"}++ def "Access assets as anonymous user"() {+ given: "the server container is started"+ def container = startContainer(defaultConfig(), defaultServices())+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)++ and: "the asset resource"+ def assetResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.tenantBuilding.realm).proxy(AssetResource.class)++ when: "the public assets are retrieved using the query parameter endpoint"+ def assets = assetResource.getPublicAssets(null, Values.asJSON(+ new AssetQuery().tenant(new TenantPredicate(keycloakTestSetup.tenantBuilding.realm))+ ).get())++ then: "the public assets should be retrieved"+ assert assets.size() == 2+ assert assets.find {it.id == managerTestSetup.apartment1Id} != null+ assert assets.find {it.id == managerTestSetup.apartment2LivingroomId} != null++ when: "the public assets are retrieved using the query parameter endpoint without a query"+ assets = assetResource.getPublicAssets(null, null)++ then: "the public assets should be retrieved"+ assert assets.size() == 2+ assert assets.find {it.id == managerTestSetup.apartment1Id} != null+ assert assets.find {it.id == managerTestSetup.apartment2LivingroomId} != null++ when: "the public assets are retrieved using the post endpoint"+ assets = assetResource.queryPublicAssets(null, new AssetQuery().tenant(new TenantPredicate(keycloakTestSetup.tenantBuilding.realm)))++ then: "the public assets should be retrieved"+ assert assets.size() == 2+ assert assets.find {it.id == managerTestSetup.apartment1Id} != null+ assert assets.find {it.id == managerTestSetup.apartment2LivingroomId} != null++ when: "an attribute with public write is written to and another with only public read"+ def writeResults = assetResource.writeAttributeValues(null, [+ new AttributeState(managerTestSetup.apartment1Id, Asset.LOCATION.name, null),+ new AttributeState(managerTestSetup.apartment2LivingroomId, Asset.LOCATION.name, null)+ ] as AttributeState[])++ then: "the request should have succeeded but only the apartment1 location event should have been successful"+ assert writeResults.size() == 2+ assert writeResults.find {it.ref.id == managerTestSetup.apartment1Id && it.ref.name == Asset.LOCATION.name}.failure == null+ assert writeResults.find {it.ref.id == managerTestSetup.apartment2LivingroomId && it.ref.name == Asset.LOCATION.name}.failure == AttributeWriteFailure.INSUFFICIENT_ACCESS+ }}