Permalink
Browse files

AssetQuery deserialisation improvement (Fixes #95)

  • Loading branch information...
richturner committed Nov 14, 2018
1 parent 8bddf0e commit a8bb65e4cbcafa8952ffc741d1c52afec667a75d
@@ -19,6 +19,7 @@
*/
package org.openremote.model.query;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.asset.AssetType;
import org.openremote.model.asset.CalendarEventConfiguration;
import org.openremote.model.query.filter.*;
@@ -336,6 +337,7 @@ public CHILD select(Select select) {
return (CHILD) this;
}
@JsonProperty("id")
public CHILD id(String id) {
this.ids = Collections.singletonList(id);
return (CHILD) this;
@@ -3,27 +3,26 @@ package org.openremote.test.assets
import org.openremote.container.Container
import org.openremote.container.persistence.PersistenceService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
import org.openremote.model.asset.Asset
import org.openremote.model.asset.AssetResource
import org.openremote.model.asset.AssetAttribute
import org.openremote.model.query.AssetQuery
import org.openremote.model.asset.AssetResource
import org.openremote.model.attribute.MetaItem
import org.openremote.model.query.filter.TenantPredicate
import org.openremote.model.query.AssetQuery
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import spock.lang.Shared
import spock.lang.Specification
import static org.openremote.model.Constants.MASTER_REALM
import static org.openremote.model.query.BaseAssetQuery.*
import static org.openremote.model.asset.AssetMeta.ACCESS_PUBLIC_READ
import static org.openremote.model.asset.AssetMeta.LABEL
import static org.openremote.model.asset.AssetType.THING
import static org.openremote.model.attribute.AttributeValueType.NUMBER
import static org.openremote.model.query.BaseAssetQuery.Include
import static org.openremote.model.query.BaseAssetQuery.Select
class AssetPublicQueryTest extends Specification implements ManagerContainerTrait {
@@ -39,9 +38,8 @@ class AssetPublicQueryTest extends Specification implements ManagerContainerTrai
static PersistenceService persistenceService
@Shared
static AssetResource assetResource
@Shared
static String somePublicAssetId
static List<Asset> returnedAssets
def setupSpec() {
given: "the server container is started"
@@ -52,22 +50,24 @@ class AssetPublicQueryTest extends Specification implements ManagerContainerTrai
assetStorageService = container.getService(AssetStorageService.class)
persistenceService = container.getService(PersistenceService.class)
assetResource = getClientTarget(serverUri(serverPort), MASTER_REALM).proxy(AssetResource.class)
returnedAssets = new ArrayList<>()
Asset somePublicAsset = new Asset("Some Public Asset", THING)
somePublicAsset.setParentId(managerDemoSetup.smartOfficeId)
somePublicAsset.setAccessPublicRead(true)
somePublicAsset.setAttributes(
new AssetAttribute("somePrivateAttribute", NUMBER, Values.create(123)).addMeta(
new MetaItem(LABEL, Values.create("Some Private Attribute"))
),
new AssetAttribute("somePublicAttribute", NUMBER, Values.create(456)).addMeta(
new MetaItem(LABEL, Values.create("Some Public Attribute")),
new MetaItem(ACCESS_PUBLIC_READ, Values.create(true))
),
)
for (int i = 0; i < 10; i++) {
Asset somePublicAsset = new Asset("Some Public Asset", THING)
somePublicAsset.setParentId(managerDemoSetup.smartOfficeId)
somePublicAsset.setAccessPublicRead(true)
somePublicAsset.setAttributes(
new AssetAttribute("somePrivateAttribute", NUMBER, Values.create(123)).addMeta(
new MetaItem(LABEL, Values.create("Some Private Attribute"))
),
new AssetAttribute("somePublicAttribute", NUMBER, Values.create(456)).addMeta(
new MetaItem(LABEL, Values.create("Some Public Attribute")),
new MetaItem(ACCESS_PUBLIC_READ, Values.create(true))
),
)
somePublicAsset = assetStorageService.merge(somePublicAsset)
somePublicAssetId = somePublicAsset.getId()
returnedAssets.add(assetStorageService.merge(somePublicAsset))
}
}
def cleanupSpec() {
@@ -77,16 +77,38 @@ class AssetPublicQueryTest extends Specification implements ManagerContainerTrai
def "Query public assets"() {
when: "a query for all public assets is executed"
def assets = assetResource.queryPublicAssets(null,
new AssetQuery()
.select(new Select(Include.ALL, Access.PUBLIC_READ))
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.id))
)
expect: "10 public assets to have been created"
assert returnedAssets.size() == 10
assert returnedAssets.count { it.id != null } == 10
when: "a query for a specific public asset is executed"
def query = new AssetQuery()
.select(new Select(Include.ALL))
.id(returnedAssets.get(0).id)
def assets = assetResource.queryPublicAssets(null, query)
then: "the result should match"
assets.size() == 1
assets[0].id == returnedAssets.get(0).id
assets[0].name == "Some Public Asset"
assets[0].wellKnownType == THING
assets[0].parentId == managerDemoSetup.smartOfficeId
assets[0].realmId == keycloakDemoSetup.masterTenant.id
assets[0].tenantRealm == keycloakDemoSetup.masterTenant.realm
assets[0].tenantDisplayName == keycloakDemoSetup.masterTenant.displayName
!assets[0].getAttribute("somePrivateAttribute").isPresent()
assets[0].getAttribute("somePublicAttribute").get().getValue().get() == Values.create(456)
assets[0].getAttribute("somePublicAttribute").get().getMeta().size() == 1
assets[0].getAttribute("somePublicAttribute").get().getMetaItem(LABEL).get().getValueAsString().get() == "Some Public Attribute"
when: "a GET query for a specific public asset is executed"
def queryJson = "{\"select\":{\"include\":\"ALL\"},\"id\":\"${returnedAssets.get(0).id}\"}"
queryJson = container.JSON.writeValueAsString(query)
assets = assetResource.getPublicAssets(null, queryJson)
then: "the result should match"
assets.size() == 1
assets[0].id == somePublicAssetId
assets[0].id == returnedAssets.get(0).id
assets[0].name == "Some Public Asset"
assets[0].wellKnownType == THING
assets[0].parentId == managerDemoSetup.smartOfficeId
@@ -97,5 +119,5 @@ class AssetPublicQueryTest extends Specification implements ManagerContainerTrai
assets[0].getAttribute("somePublicAttribute").get().getValue().get() == Values.create(456)
assets[0].getAttribute("somePublicAttribute").get().getMeta().size() == 1
assets[0].getAttribute("somePublicAttribute").get().getMetaItem(LABEL).get().getValueAsString().get() == "Some Public Attribute"
}
}
}

0 comments on commit a8bb65e

Please sign in to comment.