Skip to content

Commit c7e7e1a

Browse files
author
Johan Vergeer
committed
Resolve dependencies
1 parent 62c7454 commit c7e7e1a

6 files changed

Lines changed: 108 additions & 0 deletions

File tree

mapstruct-kotlin-gradle/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ repositories {
1313

1414
dependencies {
1515
compile(kotlin("stdlib-jdk8"))
16+
compile("org.mapstruct:mapstruct-jdk8:1.2.0.Final")
17+
kapt("org.mapstruct:mapstruct-processor:1.2.0.Final")
18+
19+
testCompile("org.junit.jupiter:junit-jupiter-api:5.3.1")
20+
testCompile("org.junit.jupiter:junit-jupiter-engine:5.3.1")
21+
testCompile("org.assertj:assertj-core:3.11.1")
1622
}
1723

1824
tasks.withType<KotlinCompile> {
1925
kotlinOptions.jvmTarget = "1.8"
26+
}
27+
28+
val test by tasks.getting(Test::class) {
29+
useJUnitPlatform { }
2030
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mapstruct.example.kotlin
2+
3+
import org.mapstruct.example.kotlin.converter.PersonConverter
4+
import org.mapstruct.example.kotlin.model.Person
5+
import org.mapstruct.factory.Mappers
6+
import java.time.LocalDate
7+
8+
fun main(args: Array<String>) {
9+
10+
val converter = Mappers.getMapper(PersonConverter::class.java) // or PersonConverterImpl()
11+
12+
val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
13+
14+
val personDto = converter.convertToDto(person)
15+
println(personDto)
16+
17+
val personModel = converter.convertToModel(personDto)
18+
println(personModel)
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.mapstruct.example.kotlin.converter
2+
3+
import org.mapstruct.InheritInverseConfiguration
4+
import org.mapstruct.Mapper
5+
import org.mapstruct.Mapping
6+
import org.mapstruct.example.kotlin.dto.PersonDto
7+
import org.mapstruct.example.kotlin.model.Person
8+
9+
@Mapper
10+
interface PersonConverter {
11+
12+
@Mapping(source = "phoneNumber", target = "phone")
13+
fun convertToDto(person: Person): PersonDto
14+
15+
@InheritInverseConfiguration
16+
fun convertToModel(personDto: PersonDto): Person
17+
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.mapstruct.example.kotlin.dto
2+
3+
import java.time.LocalDate
4+
5+
data class PersonDto(var firstName: String?, var lastName: String?, var phone: String?, var birthdate: LocalDate?) {
6+
7+
// Necessary for MapStruct
8+
constructor() : this(null, null, null, null)
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.mapstruct.example.kotlin.model
2+
3+
import java.time.LocalDate
4+
5+
data class Person(var firstName: String?, var lastName: String?, var phoneNumber: String?, var birthdate: LocalDate?) {
6+
7+
// Necessary for MapStruct
8+
constructor() : this(null, null, null, null)
9+
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.mapstruct.example.kotlin.converter
2+
3+
import org.mapstruct.example.kotlin.dto.PersonDto
4+
import org.mapstruct.example.kotlin.model.Person
5+
6+
import org.assertj.core.api.Assertions.assertThat
7+
import org.junit.jupiter.api.Test
8+
import org.mapstruct.factory.Mappers
9+
import java.time.LocalDate
10+
11+
class PersonConverterTest {
12+
13+
@Test
14+
fun testConvertToDto() {
15+
val converter = Mappers.getMapper(PersonConverter::class.java)
16+
17+
val person = Person("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
18+
19+
val personDto = converter.convertToDto(person)
20+
assertThat(personDto).isNotNull
21+
assertThat(personDto.firstName).isEqualTo("Samuel")
22+
assertThat(personDto.lastName).isEqualTo("Jackson")
23+
assertThat(personDto.phone).isEqualTo("0123 334466")
24+
assertThat(personDto.birthdate).isEqualTo(LocalDate.of(1948, 12, 21))
25+
}
26+
27+
@Test
28+
fun testConvertToModel() {
29+
val converter = Mappers.getMapper(PersonConverter::class.java)
30+
31+
val personDto = PersonDto("Samuel", "Jackson", "0123 334466", LocalDate.of(1948, 12, 21))
32+
33+
val person = converter.convertToModel(personDto)
34+
assertThat(person).isNotNull()
35+
assertThat(person.firstName).isEqualTo("Samuel")
36+
assertThat(person.lastName).isEqualTo("Jackson")
37+
assertThat(person.phoneNumber).isEqualTo("0123 334466")
38+
assertThat(person.birthdate).isEqualTo(LocalDate.of(1948, 12, 21))
39+
}
40+
}

0 commit comments

Comments
 (0)