Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ object LocationUtils {
fun detectLocationType(
locationUnit: UnitConfigType.UnitConfig,
locationRegistry: ProtoBufRegistry<String, UnitConfigType.UnitConfig, UnitConfigType.UnitConfig.Builder>,
): LocationType {
): LocationType? {
try {
if (!locationUnit.hasPlacementConfig()) {
throw NotAvailableException("placementConfig")
Expand Down Expand Up @@ -217,35 +217,29 @@ object LocationUtils {
private fun detectLocationType(
parentLocationType: LocationType,
childLocationTypes: List<LocationType>,
): 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 }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
Expand Down