diff --git a/module/dal/test/src/test/java/org/openbase/bco/dal/test/layer/unit/location/LocationCrudTest.kt b/module/dal/test/src/test/java/org/openbase/bco/dal/test/layer/unit/location/LocationCrudTest.kt new file mode 100644 index 0000000000..85d97f96d0 --- /dev/null +++ b/module/dal/test/src/test/java/org/openbase/bco/dal/test/layer/unit/location/LocationCrudTest.kt @@ -0,0 +1,61 @@ +package org.openbase.bco.dal.test.layer.unit.location + +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.equals.shouldBeEqual +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Timeout +import org.openbase.bco.registry.remote.Registries +import org.openbase.jps.core.JPService +import org.openbase.jps.preset.JPDebugMode +import org.openbase.jps.preset.JPVerbose +import org.openbase.jul.extension.type.processing.LabelProcessor +import org.openbase.type.domotic.unit.UnitConfigType.UnitConfig +import org.openbase.type.domotic.unit.UnitTemplateType.UnitTemplate +import org.openbase.type.domotic.unit.location.LocationConfigType +import java.util.concurrent.TimeUnit + +/** + * @author [Tamino Huxohl](mailto:pleminoq@openbase.org) + */ +class LocationCrudTest : AbstractBCOLocationManagerTest() { + + @Test + @Timeout(10) + @Throws(Exception::class) + fun createTileTest() { + println("createTileTest") + + val wonderTile = UnitConfig.newBuilder() + .apply { locationConfigBuilder.setLocationType(LocationConfigType.LocationConfig.LocationType.TILE) } + .setUnitType(UnitTemplate.UnitType.LOCATION) + .setLabel(LabelProcessor.buildLabel("WonderTile")) + .build() + + + val savedWonderTile = Registries.getUnitRegistry(true).registerUnitConfig(wonderTile).get(5, TimeUnit.SECONDS) + + savedWonderTile.hasId().shouldBeTrue() + savedWonderTile.locationConfig.locationType.shouldBeEqual(wonderTile.locationConfig.locationType) + } + + @Test + @Timeout(10) + @Throws(Exception::class) + fun createZoneTest() { + println("createZoneTest") + + JPService.overwriteDefaultValue(JPDebugMode::class.java, true) + JPService.overwriteDefaultValue(JPVerbose::class.java, true) + + val wonderZone = UnitConfig.newBuilder() + .apply { locationConfigBuilder.setLocationType(LocationConfigType.LocationConfig.LocationType.ZONE) } + .setUnitType(UnitTemplate.UnitType.LOCATION) + .setLabel(LabelProcessor.buildLabel("WonderZone")) + .build() + + val savedWonderZone = Registries.getUnitRegistry(true).registerUnitConfig(wonderZone).get(5, TimeUnit.SECONDS) + + savedWonderZone.hasId().shouldBeTrue() + savedWonderZone.locationConfig.locationType.shouldBeEqual(wonderZone.locationConfig.locationType) + } +} diff --git a/module/registry/lib/src/main/java/org/openbase/bco/registry/lib/util/LocationUtils.kt b/module/registry/lib/src/main/java/org/openbase/bco/registry/lib/util/LocationUtils.kt index d65878c1aa..a9a21ddd0a 100644 --- a/module/registry/lib/src/main/java/org/openbase/bco/registry/lib/util/LocationUtils.kt +++ b/module/registry/lib/src/main/java/org/openbase/bco/registry/lib/util/LocationUtils.kt @@ -174,7 +174,7 @@ object LocationUtils { fun detectLocationType( locationUnit: UnitConfigType.UnitConfig, locationRegistry: ProtoBufRegistry, - ): LocationType { + ): LocationType? { try { if (!locationUnit.hasPlacementConfig()) { throw NotAvailableException("placementConfig") @@ -217,35 +217,29 @@ object LocationUtils { private fun detectLocationType( parentLocationType: LocationType, childLocationTypes: List, - ): LocationType { + ): LocationType? = when (parentLocationType) { - when (parentLocationType) { - // if the parent is a region or tile then the location has to be a region - LocationType.REGION, LocationType.TILE -> { - return LocationType.REGION - } - - LocationType.ZONE -> { + // if the parent is a region or tile then the location has to be a region + LocationType.REGION, LocationType.TILE -> { + LocationType.REGION + } - // if the parent is a zone and has no children then it has to be - // a tile since each branch could contain exactly one tile - if (childLocationTypes.isEmpty()) { - return LocationType.TILE - } + LocationType.ZONE -> { - // if one child is a zone or a tile the location has to be a zone - if (childLocationTypes.contains(LocationType.ZONE) || childLocationTypes.contains(LocationType.TILE)) { - return LocationType.ZONE - } + // if one child is a zone or a tile the location has to be a zone + if (childLocationTypes.contains(LocationType.ZONE) || childLocationTypes.contains(LocationType.TILE)) { + LocationType.ZONE + } - // if the parent is a zone and a child is a region than the location has to be a tile - if (childLocationTypes.contains(LocationType.REGION)) { - return LocationType.TILE - } + // if the parent is a zone and a child is a region than the location has to be a tile + if (childLocationTypes.contains(LocationType.REGION)) { + LocationType.TILE } - LocationType.UNKNOWN -> {} // skip detection + // fallback + null } - throw CouldNotPerformException("Could not detect locationType from parentType[${parentLocationType.name}] and childTypes ${childLocationTypes.map { it.name }}") + + LocationType.UNKNOWN -> { null } } } diff --git a/module/registry/unit-registry/core/src/main/java/org/openbase/bco/registry/unit/core/consistency/locationconfig/LocationTypeConsistencyHandler.kt b/module/registry/unit-registry/core/src/main/java/org/openbase/bco/registry/unit/core/consistency/locationconfig/LocationTypeConsistencyHandler.kt index 8313e6ddd9..28d07b4dc6 100644 --- a/module/registry/unit-registry/core/src/main/java/org/openbase/bco/registry/unit/core/consistency/locationconfig/LocationTypeConsistencyHandler.kt +++ b/module/registry/unit-registry/core/src/main/java/org/openbase/bco/registry/unit/core/consistency/locationconfig/LocationTypeConsistencyHandler.kt @@ -28,31 +28,31 @@ class LocationTypeConsistencyHandler : val locationUnit = entry.message.toBuilder() val locationConfig = locationUnit.locationConfigBuilder - val detectedType: LocationType = LocationUtils.detectLocationType(entry.message, registry) - - if (!locationConfig.hasLocationType()) { - try { - locationConfig.setLocationType(detectedType) - throw EntryModification(entry.setMessage(locationUnit, this), this) - } catch (ex: CouldNotPerformException) { - throw CouldNotPerformException( - "The locationType of location[" + locationUnit.label + "] has to be defined manually", - ex - ) - } - } else { - try { - if (detectedType != locationConfig.locationType) { - locationConfig.setLocationType(detectedType) + LocationUtils.detectLocationType(entry.message, registry).let { detectedType -> + if (locationConfig.hasLocationType() && locationConfig.locationType != LocationType.UNKNOWN) { + try { + if (detectedType != null && detectedType != locationConfig.locationType) { + locationConfig.locationType = detectedType + throw EntryModification(entry.setMessage(locationUnit, this), this) + } + } catch (ex: CouldNotPerformException) { + ExceptionPrinter.printHistory( + "Could not detect locationType for location[" + locationUnit.label + "] with current type [" + locationConfig.locationType.name + "]", + ex, + logger, + LogLevel.DEBUG + ) + } + } else { + try { + locationConfig.locationType = detectedType ?: LocationType.TILE throw EntryModification(entry.setMessage(locationUnit, this), this) + } catch (ex: CouldNotPerformException) { + throw CouldNotPerformException( + "The locationType of location[" + locationUnit.label + "] has to be defined manually", + ex + ) } - } catch (ex: CouldNotPerformException) { - ExceptionPrinter.printHistory( - "Could not detect locationType for location[" + locationUnit.label + "] with current type [" + locationConfig.locationType.name + "]", - ex, - logger, - LogLevel.DEBUG - ) } } }