File tree Expand file tree Collapse file tree
kotlin/io/github/costaalex/workoutrelay/infrastructure/platform/trainingpeaks/user
test/kotlin/io/github/costaalex/workoutrelay/infrastructure/platform/trainingpeaks/user Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package io.github.costaalex.workoutrelay.infrastructure.platform.trainingpeaks.user
22
3- import com.fasterxml.jackson.annotation.JsonProperty
3+ import com.fasterxml.jackson.annotation.JsonIgnoreProperties
44
5- class TrainingPeaksUserDTO (
6- var userId : String? ,
7- val accountStatus : TPUserAccountStatusDTO ,
5+ @JsonIgnoreProperties(ignoreUnknown = true )
6+ data class TrainingPeaksUserDTO (
7+ val user : TPUserDTO ,
88) {
99
10- @JsonProperty(" user" )
11- private fun mapUserId (map : Map <* , * >) {
12- userId = (map[" userId" ] as Number ).toLong().toString()
13- }
10+ @JsonIgnoreProperties(ignoreUnknown = true )
11+ data class TPUserDTO (
12+ val userId : Long ,
13+ val settings : TPUserSettingsDTO ,
14+ )
15+
16+ @JsonIgnoreProperties(ignoreUnknown = true )
17+ data class TPUserSettingsDTO (
18+ val account : TPUserAccountDTO ,
19+ )
1420
15- class TPUserAccountStatusDTO (
21+ @JsonIgnoreProperties(ignoreUnknown = true )
22+ data class TPUserAccountDTO (
1623 val isAthlete : Boolean ,
17- val isPremium : Boolean = false ,
24+ val isPremium : Boolean ,
25+ val premiumTrial : Boolean = false ,
1826 )
1927}
Original file line number Diff line number Diff line change @@ -11,7 +11,16 @@ class TrainingPeaksUserRepository(
1111) {
1212 @Cacheable(key = " 'singleton'" )
1313 fun getUser (): TrainingPeaksUser {
14- val dto = trainingPeaksUserApiClient.getUser()
15- return TrainingPeaksUser (dto.userId!! , dto.accountStatus.isAthlete, dto.accountStatus.isPremium)
14+ val dto =
15+ trainingPeaksUserApiClient.getUser()
16+
17+ val account =
18+ dto.user.settings.account
19+
20+ return TrainingPeaksUser (
21+ userId = dto.user.userId.toString(),
22+ isAthlete = account.isAthlete,
23+ isPremium = account.isPremium,
24+ )
1625 }
1726}
Original file line number Diff line number Diff line change @@ -26,7 +26,6 @@ spring:
2626 serialization :
2727 fail-on-empty-beans : false
2828 datasource :
29- # url: jdbc:sqlite:/data/tp2intervals.sqlite
3029 url : ${SPRING_DATASOURCE_URL:jdbc:sqlite:/data/workout-relay.sqlite}
3130 driver-class-name : org.sqlite.JDBC
3231 jpa :
@@ -48,13 +47,10 @@ management:
4847 web :
4948 exposure :
5049 include : health,info
51- cors :
52- allowed-origins : ' *'
5350 endpoint :
5451 loggers :
5552 enabled : false
5653
5754logging :
5855 file :
59- # name: /data/tp2intervals.log
6056 name : ${LOGGING_FILE_NAME:/data/workout-relay.log}
Original file line number Diff line number Diff line change @@ -13,49 +13,70 @@ class TrainingPeaksUserDTOTest {
1313 jacksonObjectMapper()
1414
1515 @Test
16- fun `should default isPremium to false when missing ` () {
16+ fun `should deserialize premium account information ` () {
1717 val json = """
1818 {
1919 "user": {
20- "userId": 12345
21- },
22- "accountStatus": {
23- "isAthlete": true
20+ "userId": 3037421,
21+ "settings": {
22+ "account": {
23+ "userType": 4,
24+ "isAthlete": true,
25+ "isPremium": true,
26+ "isCoached": false,
27+ "premiumTrial": false
28+ }
29+ }
2430 }
2531 }
2632 """ .trimIndent()
2733
28- val user =
34+ val dto =
2935 objectMapper.readValue<TrainingPeaksUserDTO >(
3036 json
3137 )
3238
33- assertEquals(" 12345" , user.userId)
34- assertTrue(user.accountStatus.isAthlete)
35- assertFalse(user.accountStatus.isPremium)
39+ val account =
40+ dto.user.settings.account
41+
42+ assertEquals(
43+ 3037421L ,
44+ dto.user.userId,
45+ )
46+
47+ assertTrue(account.isAthlete)
48+ assertTrue(account.isPremium)
49+ assertFalse(account.premiumTrial)
3650 }
3751
3852 @Test
39- fun `should deserialize isPremium when present ` () {
53+ fun `should deserialize basic account information ` () {
4054 val json = """
4155 {
4256 "user": {
43- "userId": 12345
44- },
45- "accountStatus": {
46- "isAthlete": true,
47- "isPremium": true
57+ "userId": 3037421,
58+ "settings": {
59+ "account": {
60+ "isAthlete": true,
61+ "isPremium": false,
62+ "premiumTrial": false
63+ }
64+ }
4865 }
4966 }
5067 """ .trimIndent()
5168
52- val user =
69+ val dto =
5370 objectMapper.readValue<TrainingPeaksUserDTO >(
5471 json
5572 )
5673
57- assertEquals(" 12345" , user.userId)
58- assertTrue(user.accountStatus.isAthlete)
59- assertTrue(user.accountStatus.isPremium)
74+ assertTrue(
75+ dto.user.settings.account.isAthlete
76+ )
77+
78+ assertFalse(
79+ dto.user.settings.account.isPremium
80+ )
6081 }
6182}
You can’t perform that action at this time.
0 commit comments