Skip to content

Commit

Permalink
Add KotlinAndJavaCompositeArbitraryIntrospector for supporting instan…
Browse files Browse the repository at this point in the history
…tiating kotlin class with java reference Type (#948)
  • Loading branch information
jinia91 authored Mar 18, 2024
1 parent f42c332 commit 37b1990
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fun sampleOrder() {
* @[sangy515](https://github.com/sangy515)
* @[yunseok-jeong0](https://github.com/yunseok-jeong0)
* @[wicksome](https://github.com/wicksome)
* @[Wonjin Choi](https://github.com/jinia91)
* @[jinia91](https://github.com/jinia91)
* @[songkg7](https://github.com/songkg7)
* @[this-is-spear](https://github.com/this-is-spear)
* @[donggyunhuh](https://github.com/donggyunhuh)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,74 @@ fun test() {
val product: Product = fixtureMonkey.giveMeOne()
}
```

## KotlinAndJavaCompositeArbitraryIntrospector

The `KotlinAndJavaCompositeArbitraryIntrospector` is an introspector designed to assist in the creation of Kotlin classes that reference Java classes.


**Example Kotlin Class :**
```kotlin
class KotlinClassWithJavaClass(val javaObject: JavaObject)
```

**Example Java Class :**
```java
public class JavaObject {
private String value;
private Map<String, String> map;

public JavaObject() {
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
this.map = map;
}
}
```

**Using PrimaryConstructorArbitraryIntrospector :**
```kotlin
fun kotlinClassWithJavaClass() {
// given
val sut: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector())
.build()

// when
val actual = sut.giveMeOne<KotlinClassWithJavaClass>()

then(actual).isNotNull
then(actual.javaObject).isNotNull
}
```

For Kotlin and Java classes respectively, it uses the PrimaryConstructorArbitraryIntrospector and the BeanArbitraryIntrospector by default.

If changes are desired, these can be injected as arguments.

```kotlin
// given
val sut: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(
KotlinAndJavaCompositeArbitraryIntrospector(
kotlinArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE,
javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE
)
)
.build()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.navercorp.fixturemonkey.kotlin

import com.navercorp.fixturemonkey.OrderSheet
import java.math.BigDecimal
import java.time.Instant
import javax.validation.constraints.Size

data class KotlinJavaCompositeOrderSheet(
val id: String,
val backUrl: String?,
val userNo: Long?,
@field:Size(min = 1, max = 1)
val products: List<OrderSheet.OrderSheetProduct>,
val merchantsByMerchantNo: Map.Entry<Long, OrderSheetMerchant>?,
val registeredDateTime: Instant?,
@field:Size(min = 1, max = 1)
val bundleDeliveryFeesByDeliveryGroupKey: Map<String, OrderSheetBundleDeliveryFee>?
) {
data class OrderSheetMerchant(
val talkInterlockAccountId: String?,
val logeyeRequestId: String?,
val logeyeInflowPathName: String?,
val logeyePayAccumulation: Boolean?
)

data class OrderSheetBundleDeliveryFee(
val deliveryFee: BigDecimal?,
val type: BundleType
)

enum class BundleType {
MANUALLY, IDENTICAL_PRODUCT
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.navercorp.fixturemonkey.OrderSheet
import com.navercorp.fixturemonkey.FixtureMonkey
import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector
import com.navercorp.fixturemonkey.api.type.TypeCache
import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
Expand Down Expand Up @@ -48,4 +49,16 @@ open class KotlinObjectGenerationBenchMark {

private fun generateKotlinOrderSheet(fixtureMonkey: FixtureMonkey): List<KotlinOrderSheet> =
List(COUNT) { fixtureMonkey.giveMeOne(KotlinOrderSheet::class.java) }

@Benchmark
fun beanGenerateKotlinJavaCompositeOrderSheetWithFixtureMonkey(blackhole: Blackhole) {
val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector())
.build()
blackhole.consume(generateKotlinJavaCompositeOrderSheet(fixtureMonkey))
}

private fun generateKotlinJavaCompositeOrderSheet(fixtureMonkey: FixtureMonkey): List<KotlinJavaCompositeOrderSheet> =
List(COUNT) { fixtureMonkey.giveMeOne(KotlinJavaCompositeOrderSheet::class.java) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.kotlin.introspector

import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext
import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospector
import com.navercorp.fixturemonkey.api.introspector.ArbitraryIntrospectorResult
import com.navercorp.fixturemonkey.api.introspector.BeanArbitraryIntrospector
import com.navercorp.fixturemonkey.kotlin.type.actualType
import com.navercorp.fixturemonkey.kotlin.type.isKotlinType
import org.slf4j.LoggerFactory

class KotlinAndJavaCompositeArbitraryIntrospector(
private val kotlinArbitraryIntrospector: ArbitraryIntrospector = PrimaryConstructorArbitraryIntrospector.INSTANCE,
private val javaArbitraryIntrospector: ArbitraryIntrospector = BeanArbitraryIntrospector.INSTANCE,
) : ArbitraryIntrospector {
override fun introspect(context: ArbitraryGeneratorContext): ArbitraryIntrospectorResult {
val type = context.resolvedType.actualType()
try {
return if (type.isKotlinType()) {
kotlinArbitraryIntrospector.introspect(context)
} else {
javaArbitraryIntrospector.introspect(context)
}
} catch (e: Exception) {
LOGGER.warn("Given type $type is failed to generated due to the exception.", e)
return ArbitraryIntrospectorResult.NOT_INTROSPECTED
}
}

companion object {
private val LOGGER = LoggerFactory.getLogger(KotlinAndJavaCompositeArbitraryIntrospector::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.kotlin.test

import com.navercorp.fixturemonkey.FixtureMonkey
import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector
import com.navercorp.fixturemonkey.kotlin.KotlinPlugin
import com.navercorp.fixturemonkey.kotlin.giveMeOne
import com.navercorp.fixturemonkey.kotlin.introspector.KotlinAndJavaCompositeArbitraryIntrospector
import net.jqwik.api.Property
import org.assertj.core.api.BDDAssertions.then

class KotlinAndJavaCompositeArbitraryIntrospectorTest {
@Property
fun kotlinClassWithJavaClass() {
// given
val sut: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector())
.build()

// when
val actual = sut.giveMeOne<KotlinClassWithJavaClass>()

then(actual).isNotNull
then(actual.javaObject).isNotNull
}

@Property
fun kotlinClassWithJavaClassUsingOtherIntrospector() {
// given
val sut: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(
KotlinAndJavaCompositeArbitraryIntrospector(
javaArbitraryIntrospector = ConstructorPropertiesArbitraryIntrospector.INSTANCE
)
)
.build()

// when
val actual = sut.giveMeOne<KotlinClassWithJavaClass>()

then(actual).isNotNull
then(actual.javaObject).isNotNull
}

@Property
fun sampleMapValue() {
// given
val sut: FixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.objectIntrospector(KotlinAndJavaCompositeArbitraryIntrospector())
.build()

// when
val actual = sut.giveMeOne<MapValue>()

then(actual).isNotNull
then(actual.map).isNotNull
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Fixture Monkey
*
* Copyright (c) 2021-present NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.fixturemonkey.kotlin.test

import com.navercorp.fixturemonkey.kotlin.spec.JavaObject

class KotlinClassWithJavaClass(val javaObject: JavaObject)

class MapValue(val map: Map<String, String>)

0 comments on commit 37b1990

Please sign in to comment.