From db47f15ac5a54531450069eda8d84b8969908d0c Mon Sep 17 00:00:00 2001 From: Andreas Ernst Date: Sat, 23 Mar 2024 22:26:10 +0100 Subject: [PATCH] fixes --- .../portal/PortalAdministrationService.kt | 8 - .../impl/PortalAdministrationServiceImpl.kt | 348 ++++-------------- .../java/com/serious/portal/mapper/Mapper.kt | 43 ++- .../portal/mapper/RelationSynchronizer.kt | 3 +- .../serious/portal/mapper/CollectionTest.kt | 27 +- .../serious/portal/mapper/ConversionTest.kt | 2 +- .../serious/portal/mapper/InheritanceTest.kt | 2 +- .../com/serious/portal/mapper/MapperTest.kt | 41 +++ .../serious/portal/mapper/SynchronizerTest.kt | 2 +- 9 files changed, 168 insertions(+), 308 deletions(-) diff --git a/portal/core/src/main/java/com/serious/portal/PortalAdministrationService.kt b/portal/core/src/main/java/com/serious/portal/PortalAdministrationService.kt index c5e19b6a..f99050a0 100644 --- a/portal/core/src/main/java/com/serious/portal/PortalAdministrationService.kt +++ b/portal/core/src/main/java/com/serious/portal/PortalAdministrationService.kt @@ -94,14 +94,6 @@ interface PortalAdministrationService : Service { @DeleteMapping("delete-application-version/{application}/{version}") fun deleteApplicationVersion(@PathVariable application: String, @PathVariable version: String) - // TEST - - @GetMapping("compute-application-version-configuration/{application}") - fun computeApplicationVersionConfiguration(@PathVariable application: Long) - - - // TEST - // microfrontend @GetMapping("read-microfrontends") diff --git a/portal/core/src/main/java/com/serious/portal/impl/PortalAdministrationServiceImpl.kt b/portal/core/src/main/java/com/serious/portal/impl/PortalAdministrationServiceImpl.kt index e796d28b..34d080af 100644 --- a/portal/core/src/main/java/com/serious/portal/impl/PortalAdministrationServiceImpl.kt +++ b/portal/core/src/main/java/com/serious/portal/impl/PortalAdministrationServiceImpl.kt @@ -6,15 +6,14 @@ package com.serious.service.administration.portal.impl */ import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.serious.portal.* -import com.serious.portal.configuration.ConfigurationMerger +import com.serious.portal.mapper.* +import com.serious.portal.mapper.Mapping import com.serious.portal.model.* import com.serious.portal.persistence.* import com.serious.portal.persistence.entity.ApplicationEntity import com.serious.portal.persistence.entity.ApplicationVersionEntity import com.serious.portal.persistence.entity.StageEntity -import com.serious.portal.version.VersionRange import jakarta.persistence.* import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Component @@ -25,72 +24,6 @@ import java.net.URL import java.util.* import kotlin.collections.ArrayList -typealias PKGetter = (any: T) -> PK -abstract class RelationSynchronizer protected constructor(private val toPK: PKGetter, private val entityPK: PKGetter) { - // protected - - protected open fun missingPK(pk: PK) : Boolean { - return false - } - protected abstract fun provideEntity(referencedTransportObject: TO): ENTITY - protected open fun deleteEntity(entity: ENTITY) {} - protected open fun updateEntity(entity: ENTITY, transportObject: TO) {} - - protected fun addEntityToRelation(relation: MutableCollection, referencedEntity: ENTITY) { - relation.add(referencedEntity) - } - - protected fun removeEntityFromRelation(relation: MutableCollection, referencedEntity: ENTITY) { - relation.remove(referencedEntity) - } - - // main function - fun synchronize(toRelation: Collection, entityRelation: MutableCollection) { - val entityMap: MutableMap = HashMap() - - // collect all entities in a map - - for (entity in entityRelation) - entityMap[this.entityPK(entity)] = entity - - // iterate over transport objects - - for (to in toRelation) { - val key = this.toPK(to) - - if (!missingPK(key)) { - val entity = entityMap[key] - - if (entity == null) - addEntityToRelation(entityRelation, provideEntity(to)) - - else { - // possibly update entity - - updateEntity(entity, to) - - entityMap.remove(key) - } // else - } // if - else addEntityToRelation(entityRelation, provideEntity(to)) - } // for - - // deleted entities - - for (deletedPersistent in entityMap.values) - if (isEntityToRemove(deletedPersistent)) { - removeEntityFromRelation(entityRelation, deletedPersistent) - - deleteEntity(deletedPersistent) - } // if - } - - protected fun isEntityToRemove(entity: ENTITY): Boolean { - return true - } -} - - @Component @RestController class PortalAdministrationServiceImpl : PortalAdministrationService { @@ -213,8 +146,6 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { throw NullPointerException("ouch") } - // NEW - // stage @Transactional @@ -231,56 +162,52 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { } // application + @Transactional override fun createApplication(application: Application) : Application { this.applicationRepository.save(ApplicationEntity(application.name, application.configuration, ArrayList())) return application } + + val readApplicationMapper = Mapper( + Mapping.build(ApplicationEntity::class, Application::class) { + map { properties("name", "configuration") } + map { "versions" to "versions" deep true } + }, + + Mapping.build(AssignedMicrofrontendEntity::class, AssignedMicrofrontend::class) { + map { properties("id", "version") } + map { path("microfrontend", "name") to "microfrontend" } + }, + + Mapping.build(ApplicationVersionEntity::class, ApplicationVersion::class) { + map { properties("id", "version", "configuration") } + map { "assignedMicrofrontends" to "assignedMicrofrontends" deep true} + }) + @Transactional override fun readApplication(application: String) : Optional { - fun mapAssignedMicrofrontend(entity: AssignedMicrofrontendEntity): AssignedMicrofrontend { - return AssignedMicrofrontend( - entity.id, - entity.microfrontend.name, - entity.version - ) - } - - fun mapVersion(entity: ApplicationVersionEntity): ApplicationVersion { - return ApplicationVersion( - entity.id, - entity.version, - entity.configuration, - entity.assignedMicrofrontends.map { entity -> mapAssignedMicrofrontend(entity)} - ) - } + val application : Optional = this.applicationRepository.findById(application).map { entity -> readApplicationMapper.map(entity) } - return this.applicationRepository.findById(application).map { entity -> - Application( - entity.name, - entity.configuration, - entity.versions.map { versionEntity -> mapVersion(versionEntity) } - ) - } + println(readApplicationMapper.describe()) + return application } + @Transactional override fun updateApplication(application: Application) :Application { - var entity : ApplicationEntity = applicationRepository.findById(application.name).get() - - entity.configuration = application.configuration + val entity : ApplicationEntity = applicationRepository.findById(application.name).get() // local class - val synchronizer = object : RelationSynchronizer( - fun (to: ApplicationVersion) : Long? { return to.id }, - fun (entity: ApplicationVersionEntity) : Long { return entity.id }) { + val synch = object : RelationSynchronizer({ to: ApplicationVersion -> to.id }, { entity: ApplicationVersionEntity -> entity.id }) { + // override override fun missingPK(pk: Long?) : Boolean { return pk == null || pk == 0L } - override fun provideEntity(referencedTransportObject: ApplicationVersion): ApplicationVersionEntity { + override fun provide(referencedTransportObject: ApplicationVersion, context: Mapping.Context): ApplicationVersionEntity { val entity = ApplicationVersionEntity( 0, // pk entity, // applicationVersion @@ -296,20 +223,23 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { return entity } - override fun deleteEntity(entity: ApplicationVersionEntity) { + override fun delete(entity: ApplicationVersionEntity) { applicationVersionRepository.deleteById(entity.id) } - override fun updateEntity(entity: ApplicationVersionEntity, transportObject: ApplicationVersion) { + override fun update(entity: ApplicationVersionEntity, transportObject: ApplicationVersion, context: Mapping.Context) { entity.version = transportObject.version entity.configuration = transportObject.configuration - //entity.microfrontend = transportObject.microfrontend } } - // synchronize + val mapper = Mapper( + Mapping.build(Application::class, ApplicationEntity::class) { + map { properties("name", "configuration") } + map { "versions" to "versions" synchronize synch } + }) - synchronizer.synchronize(application.versions, entity.versions) + mapper.map(application, entity) // done @@ -322,30 +252,9 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { } @Transactional override fun readApplications() : List { - fun mapAssignedMicrofrontend(entity: AssignedMicrofrontendEntity): AssignedMicrofrontend { - return AssignedMicrofrontend( - entity.id, - entity.microfrontend.name, - entity.version - ) - } - - fun mapVersion(entity: ApplicationVersionEntity): ApplicationVersion { - return ApplicationVersion( - entity.id, - entity.version, - entity.configuration, - entity.assignedMicrofrontends.map { entity -> mapAssignedMicrofrontend(entity)} - ) - } + val applications = this.applicationRepository.findAll().map { entity -> readApplicationMapper.map(entity)!! } - return this.applicationRepository.findAll().map { entity -> - Application( - entity.name, - entity.configuration, - entity.versions.map { versionEntity -> mapVersion(versionEntity) } - ) - } + return applications } // application version @@ -365,6 +274,7 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { return applicationVersion } + @Transactional override fun updateApplicationVersion(application: ApplicationVersion) : ApplicationVersion { val entity = this.applicationVersionRepository.findById(application.id!!).get() @@ -382,7 +292,7 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { return pk == null || pk == 0L } - override fun provideEntity(referencedTransportObject: AssignedMicrofrontend): AssignedMicrofrontendEntity { + override fun provide(referencedTransportObject: AssignedMicrofrontend, context: Mapping.Context): AssignedMicrofrontendEntity { val entity = AssignedMicrofrontendEntity( 0, // pk entity, // applicationVersion @@ -397,24 +307,29 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { return entity } - override fun deleteEntity(entity: AssignedMicrofrontendEntity) { + override fun delete(entity: AssignedMicrofrontendEntity) { assignedMicrofrontendRepository.deleteById(entity.id) } - override fun updateEntity(entity: AssignedMicrofrontendEntity, transportObject: AssignedMicrofrontend) { + override fun update(entity: AssignedMicrofrontendEntity, transportObject: AssignedMicrofrontend, context: Mapping.Context) { entity.version = transportObject.version //entity.microfrontend = transportObject.microfrontend } } - // synchronize + val mapper = Mapper( + Mapping.build(ApplicationVersion::class, ApplicationVersionEntity::class) { + //map { properties("version", "configuration") } + map { "versions" to "versions" synchronize synchronizer } + }) - synchronizer.synchronize(application.assignedMicrofrontends, entity.assignedMicrofrontends) + mapper.map(application, entity) // done return application } + @Transactional override fun deleteApplicationVersion(application: String, version: String) { val applicationEntity = this.applicationRepository.findById(application).get() @@ -428,42 +343,30 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { // microfrontend + val readMicrofrontendMapper = Mapper( + Mapping.build(MicrofrontendEntity::class, Microfrontend::class) { + map { properties("name", "enabled", "configuration") } + map { "versions" to "versions" deep true } + }, + + Mapping.build(MicrofrontendVersionEntity::class, MicrofrontendVersion::class) { + map { properties("id", "version", "configuration", "enabled") } + map { path("microfrontend", "name") to "microfrontend" } + map { "manifest" to "manifest" convert {manifest: String -> objectMapper.readValue(manifest, Manifest::class.java)}} + map { "instances" to "instances" deep true } + map { path("applicationVersion", "id") to "applicationVersion" } + }, + + Mapping.build(MicrofrontendInstanceEntity::class, MicrofrontendInstance::class) { + map { path("microfrontendVersion", "microfrontend", "name") to "microfrontend" } + map { path("microfrontendVersion", "version") to "version" } + map { properties("uri", "enabled", "health", "configuration", "stage") } + map { "manifest" to "manifest" convert {manifest: String -> objectMapper.readValue(manifest, Manifest::class.java)}} + }) + @Transactional override fun readMicrofrontends() : List { - fun mapInstance(entity: MicrofrontendInstanceEntity): MicrofrontendInstance { - return MicrofrontendInstance( - entity.microfrontendVersion.microfrontend.name, - entity.microfrontendVersion.version, - entity.uri, - entity.enabled, - entity.health, - entity.configuration, - objectMapper.readValue(entity.manifest, Manifest::class.java), - entity.stage, - ) - } - - fun mapVersion(entity: MicrofrontendVersionEntity): MicrofrontendVersion { - return MicrofrontendVersion( - entity.id, - entity.microfrontend.name, - entity.version, - objectMapper.readValue(entity.manifest, Manifest::class.java), - entity.configuration, - entity.enabled, - entity.instances.map { entity -> mapInstance(entity) }, - entity.applicationVersion?.id - ) - } - - return this.microfrontendRepository.findAll().map { entity -> - Microfrontend( - entity.name, - entity.enabled, - entity.configuration, - entity.versions.map { entity -> mapVersion(entity) } - ) - } + return this.microfrontendRepository.findAll().map { entity -> readMicrofrontendMapper.map(entity)!! } } @Transactional @@ -481,31 +384,7 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { @Transactional override fun readMicrofrontendVersions() : List { - fun mapInstance(entity: MicrofrontendInstanceEntity): MicrofrontendInstance { - return MicrofrontendInstance( - entity.microfrontendVersion.microfrontend.name, - entity.microfrontendVersion.version, - entity.uri, - entity.enabled, - entity.health, - entity.configuration, - objectMapper.readValue(entity.manifest, Manifest::class.java), - entity.stage, - ) - } - - return this.microfrontendVersionRepository.findAll().map { entity -> - MicrofrontendVersion( - entity.id, - entity.microfrontend.name, - entity.version, - objectMapper.readValue(entity.manifest, Manifest::class.java), - entity.configuration, - entity.enabled, - entity.instances.map { instanceEntity -> mapInstance(instanceEntity) }, - entity.applicationVersion?.id, - ) - } + return this.microfrontendVersionRepository.findAll().map { entity -> readMicrofrontendMapper.map(entity)!! } } @Transactional @@ -541,31 +420,7 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { @Transactional override fun registerMicrofrontendInstance(manifest: Manifest) : MicrofrontendRegistryResult { - // local functions - - fun mapMicrofrontend(entity: MicrofrontendEntity): Microfrontend { - return Microfrontend( - entity.name, - entity.enabled, - entity.configuration, - ArrayList() // leave it empty - ) - } - - fun mapVersion(entity: MicrofrontendVersionEntity): MicrofrontendVersion { - return MicrofrontendVersion( - entity.id, - entity.microfrontend.name, - entity.version, - objectMapper.readValue(entity.manifest, Manifest::class.java), - entity.configuration, - entity.enabled, - ArrayList(), // leave it empty - entity.applicationVersion?.id - ) - } - - //go + // go val url = manifest.remoteEntry @@ -584,65 +439,14 @@ class PortalAdministrationServiceImpl : PortalAdministrationService { val microfrontendEntity = this.microfrontendRepository.findById(manifest.name).get() val microfrontendVersion = microfrontendEntity.versions.find { version -> version.version == manifest.version } + println(readMicrofrontendMapper.describe()) return MicrofrontendRegistryResult( null, - mapMicrofrontend(microfrontendEntity), - mapVersion(microfrontendVersion!!), + readMicrofrontendMapper.map(microfrontendEntity), + readMicrofrontendMapper.map(microfrontendVersion!!), instance, "registered" ) } } - - // TEST - - @Autowired - lateinit var merger : ConfigurationMerger - - @Transactional - override fun computeApplicationVersionConfiguration(application: Long) { - // TODO: cache Long -> ... ( Deployment ) - // read version - - val applicationVersion : ApplicationVersionEntity = this.applicationVersionRepository.findById(application).get() - - val configurations = ArrayList() - - configurations.add(applicationVersion.application.configuration) - configurations.add(applicationVersion.configuration) - - // local function - - fun matchingVersion(microfrontend: MicrofrontendEntity, range: VersionRange) :MicrofrontendVersionEntity? { - val versions = ArrayList(microfrontend.versions) - versions.sortByDescending { version -> version.version } - - for ( version in versions) - if ( range.matches(com.serious.portal.version.Version(version.version))) - return version - - return null - } - - val versions = ArrayList() - - for ( assigned in applicationVersion.assignedMicrofrontends) { - assigned.microfrontend.configuration - - val match = matchingVersion(assigned.microfrontend, VersionRange(assigned.version)) - if ( match != null) { - versions.add(match) - configurations.add(match.configuration) - - println(match.microfrontend.name + "." + match.version) - } - else { - println("no match for " + assigned.version) - } - } - - val configuration = merger.mergeConfigurationValues(configurations) - - println("configuration: " + configuration) - } } diff --git a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt index 08867d33..8d501654 100644 --- a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt +++ b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt @@ -13,13 +13,10 @@ import kotlin.reflect.jvm.jvmErasure import kotlin.reflect.jvm.reflect typealias Conversion = (I) -> O -typealias Finalizer = (S,T) -> Unit - -typealias PKGetter = (any: T) -> PK +typealias Finalizer = (S,T) -> Unit -// mapper - +// top level functions fun constant(value: Any) : MappingDefinition.Accessor { return MappingDefinition.ConstantAccessor(value) @@ -116,7 +113,7 @@ class OperationBuilder(private val matches: MutableCollection? @@ -559,7 +556,7 @@ class MappingDefinition(val sourceClass: KClass, val target var deep = false var conversion: Conversion? = null - var synchronizer: MapperRelationSynchronizer? = null + var synchronizer: RelationSynchronizer? = null var sourceAccessor : Array? = null var targetAccessor : Array? = null var properties : Properties? = null @@ -607,6 +604,20 @@ class MappingDefinition(val sourceClass: KClass, val target return This } + infix fun Accessor.to(target: String) :MapBuilder { + sourceAccessor = arrayOf(this) + targetAccessor = arrayOf(PropertyAccessor(target)) + + return This + } + + infix fun Accessor.to(target: Array) :MapBuilder { + sourceAccessor = arrayOf(this) + targetAccessor = target.map { PropertyAccessor(it) }.toTypedArray() + + return This + } + // property infix fun KProperty1.to(target: KProperty1):MapBuilder { @@ -624,8 +635,8 @@ class MappingDefinition(val sourceClass: KClass, val target // synchronize - infix fun synchronize(synchronizer: MapperRelationSynchronizer) : MapBuilder { - this.synchronizer = synchronizer as MapperRelationSynchronizer + infix fun synchronize(synchronizer: RelationSynchronizer) : MapBuilder { + this.synchronizer = synchronizer as RelationSynchronizer this.deep = true return this @@ -1143,7 +1154,7 @@ class MappingDefinition(val sourceClass: KClass, val target return this } - fun synchronize(from: String, to: String, synchronizer: MapperRelationSynchronizer): MappingDefinition { + fun synchronize(from: String, to: String, synchronizer: RelationSynchronizer): MappingDefinition { operations.add(MapAccessor( arrayOf(PropertyAccessor(from)), arrayOf(Mapping.RelationshipAccessor(to, synchronizer)), @@ -1404,7 +1415,7 @@ class Mapping( return if (value != null) property.get(value, context) else - UNDEFINED + null//UNDEFINED // TODO!!!!!!!!! } override fun set(instance: Any, value: Any?, context: Context) { @@ -1472,7 +1483,7 @@ class Mapping( } } - class SynchronizeMultiValuedRelationship(val property: KProperty1, val synchronizer: MapperRelationSynchronizer) + class SynchronizeMultiValuedRelationship(val property: KProperty1, val synchronizer: RelationSynchronizer) :Property { // override @@ -1493,7 +1504,7 @@ class Mapping( } } - class RelationshipAccessor(accessor: String, val synchronizer: MapperRelationSynchronizer) : MappingDefinition.PropertyAccessor(accessor) { + class RelationshipAccessor(accessor: String, val synchronizer: RelationSynchronizer) : MappingDefinition.PropertyAccessor(accessor) { override fun makeTransformerProperty(write: Boolean): Property { return if (!write) throw MapperDefinitionException("error") @@ -1667,7 +1678,7 @@ class Mapping( val result = writer.create(reader.size()) while ( reader.hasMore()) - writer.set(reader.get()) + writer.set(context.mapper.map(reader.get(), context)!!) property.set(instance, result, context) } // if diff --git a/portal/core/src/main/java/com/serious/portal/mapper/RelationSynchronizer.kt b/portal/core/src/main/java/com/serious/portal/mapper/RelationSynchronizer.kt index e22344f2..8bec962d 100644 --- a/portal/core/src/main/java/com/serious/portal/mapper/RelationSynchronizer.kt +++ b/portal/core/src/main/java/com/serious/portal/mapper/RelationSynchronizer.kt @@ -7,8 +7,9 @@ package com.serious.portal.mapper import java.util.HashMap +typealias PKGetter = (any: T) -> PK -abstract class MapperRelationSynchronizer protected constructor(private val toPK: PKGetter, private val entityPK: PKGetter) { +abstract class RelationSynchronizer protected constructor(private val toPK: PKGetter, private val entityPK: PKGetter) { // protected protected open fun missingPK(pk: PK) : Boolean { diff --git a/portal/core/src/test/java/com/serious/portal/mapper/CollectionTest.kt b/portal/core/src/test/java/com/serious/portal/mapper/CollectionTest.kt index d34330f6..5190349c 100644 --- a/portal/core/src/test/java/com/serious/portal/mapper/CollectionTest.kt +++ b/portal/core/src/test/java/com/serious/portal/mapper/CollectionTest.kt @@ -17,14 +17,20 @@ class CollectionTest { } class Bar { - var name: String = "line" + var name: String = "bar" + + var baz : Array = arrayOf(Baz()) + } + + class Baz { + var name: String = "baz" } // test @Test - fun testVersion() { + fun testCollection() { val mapper = Mapper( Mapping.build(Foo::class, Foo::class) { map { "barArray" to "barList" deep true} @@ -33,18 +39,23 @@ class CollectionTest { Mapping.build(Bar::class, Bar::class) { - map { properties() } + map { "name" to "name" } + map { "baz" to "baz" deep true} + }, + + Mapping.build(Baz::class, Baz::class) { + map { "name" to "name" } } ) val foo = Foo() - val result = mapper.map(foo) - - - // eq + val result = mapper.map(foo)!! - //assertEquals(true, Version("1.0").eq(Version("1.0")), "expected eq") + assertEquals(1, result.barArray.size) + assertEquals(1, result.barList.size) + assertEquals(1, result.barArray[0].baz.size) + assertEquals("baz", result.barArray[0].baz[0].name) //assertEquals(false, Version("1.0").eq(Version("1.1.1.1")), "expected !eq") } } \ No newline at end of file diff --git a/portal/core/src/test/java/com/serious/portal/mapper/ConversionTest.kt b/portal/core/src/test/java/com/serious/portal/mapper/ConversionTest.kt index 74d90908..cb272019 100644 --- a/portal/core/src/test/java/com/serious/portal/mapper/ConversionTest.kt +++ b/portal/core/src/test/java/com/serious/portal/mapper/ConversionTest.kt @@ -22,7 +22,7 @@ class ConversionTest { // test @Test - fun testVersion() { + fun testConversion() { val mapper = Mapper( Mapping.build(Foo::class, Foo::class) { map { "short" to "short" } diff --git a/portal/core/src/test/java/com/serious/portal/mapper/InheritanceTest.kt b/portal/core/src/test/java/com/serious/portal/mapper/InheritanceTest.kt index e65593cd..620d1249 100644 --- a/portal/core/src/test/java/com/serious/portal/mapper/InheritanceTest.kt +++ b/portal/core/src/test/java/com/serious/portal/mapper/InheritanceTest.kt @@ -23,7 +23,7 @@ class InheritanceTest { // test @Test - fun testVersion() { + fun testInheritance() { val baseMapping = Mapping.build(Base::class,Base::class) { map { properties() } } diff --git a/portal/core/src/test/java/com/serious/portal/mapper/MapperTest.kt b/portal/core/src/test/java/com/serious/portal/mapper/MapperTest.kt index 352bc2f6..95069aba 100644 --- a/portal/core/src/test/java/com/serious/portal/mapper/MapperTest.kt +++ b/portal/core/src/test/java/com/serious/portal/mapper/MapperTest.kt @@ -50,6 +50,35 @@ class MapperTest { class ReadOnly(val name: String) + @Test() + fun testPath() { + // classes + + data class Price(val currency: String, val value: Long) + + class Product (var name: String = "", val price1: Price?, val price2: Price) + + data class Target(var name: String, val price1: Long?, val price2: Long) + + + // mapper + + val mapper = Mapper( + Mapping.build(Product::class, Target::class) { + map { properties("name")} + map { path("price1", "value") to "price1"} + map { path("price2", "value") to "price2"} + }) + + println(mapper.describe()) + + val source = Product("product", null/*Price("EU", 1)*/, Price("EU", 2)) + val target = mapper.map(source)!! + + assertEquals(1, target.price1) + assertEquals(2, target.price2) + } + @Test() fun testDeepCollectionLevel1Data() { // classes @@ -172,6 +201,18 @@ class MapperTest { } } + @Test() + fun testConstant() { + val mapper = Mapper( + Mapping.build(From::class, To::class) { + map { constant("name") to "name"} + }) + + val result = mapper.map(From(""))!! + + assertEquals("name", result.name) + } + @Test() fun testExceptionMissingSpec() { try { diff --git a/portal/core/src/test/java/com/serious/portal/mapper/SynchronizerTest.kt b/portal/core/src/test/java/com/serious/portal/mapper/SynchronizerTest.kt index 748e8e4d..e86d17a9 100644 --- a/portal/core/src/test/java/com/serious/portal/mapper/SynchronizerTest.kt +++ b/portal/core/src/test/java/com/serious/portal/mapper/SynchronizerTest.kt @@ -32,7 +32,7 @@ class SynchronizerTest { } - class BarSynchronizer : MapperRelationSynchronizer({bar: Bar -> bar.id}, {bar: BarEntity -> bar.id}) { + class BarSynchronizer : RelationSynchronizer({ bar: Bar -> bar.id}, { bar: BarEntity -> bar.id}) { override fun provide(bar: Bar, context: Mapping.Context): BarEntity { return context.mapper.map(bar, context)!! }