Skip to content

Commit

Permalink
Fix TP null title (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
freekode authored Jan 1, 2025
1 parent 29104c9 commit 26b8bb6
Show file tree
Hide file tree
Showing 10 changed files with 838 additions and 1,237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TPWorkoutLibraryRepository(
try {
tpToWorkoutConverter.toWorkout(it)
} catch (e: Exception) {
log.warn("Can't convert workout - ${it.title}, error - ${e.message}'", e)
log.warn("Can't convert workout, ${it.id} - ${it.title}, error - ${e.message}'", e)
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.workout.s
abstract class TPBaseWorkoutResponseDTO(
val id: String,
val workoutTypeValueId: Int?,
val title: String,
val title: String?,
val totalTimePlanned: Double?,
val tssPlanned: Int?,
val description: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TPToWorkoutConverter {
return Workout(
WorkoutDetails(
tpWorkout.getWorkoutType()!!,
tpWorkout.title.ifBlank { "Workout" },
if (tpWorkout.title.isNullOrBlank()) "Workout" else tpWorkout.title,
description,
tpWorkout.totalTimePlanned?.let { Duration.ofMinutes((it * 60).toLong()) },
tpWorkout.tssPlanned,
Expand All @@ -58,7 +58,7 @@ class TPToWorkoutConverter {
}
FromTPStructureConverter.toWorkoutStructure(tpWorkout.structure)
} catch (e: IllegalArgumentException) {
log.warn("Can't convert workout - ${tpWorkout.title}, error - ${e.message}'")
log.warn("Error during TP Workout conversion, skipping, id: ${tpWorkout.id}, name: ${tpWorkout.title}, error - ${e.message}'")
null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TPWorkoutCalendarResponseDTO(
val workoutDay: LocalDateTime,
workoutId: String,
workoutTypeValueId: Int?,
title: String,
title: String?,
totalTimePlanned: Double?,
tssPlanned: Int?,
description: String?,
Expand Down
10 changes: 6 additions & 4 deletions boot/src/test/kotlin/config/mock/TrainingPeaksApiClientMock.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config.mock

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import org.freekode.tp2intervals.infrastructure.platform.intervalsicu.workout.IntervalsEventDTO
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.TrainingPeaksApiClient
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.workout.CreateTPWorkoutRequestDTO
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.workout.TPNoteResponseDTO
Expand All @@ -10,11 +12,11 @@ import java.io.InputStream

class TrainingPeaksApiClientMock(
objectMapper: ObjectMapper,
workoutResponses: List<InputStream>,
workoutsResponse: InputStream,
) : TrainingPeaksApiClient {
private val workouts: List<TPWorkoutCalendarResponseDTO> = workoutResponses.map {
objectMapper.readValue(it, TPWorkoutCalendarResponseDTO::class.java)
}
private val workouts: List<TPWorkoutCalendarResponseDTO> = objectMapper.readValue(
workoutsResponse,
object : TypeReference<List<TPWorkoutCalendarResponseDTO>>() {}) as List<TPWorkoutCalendarResponseDTO>;

override fun getWorkouts(userId: String, startDate: String, endDate: String) = workouts

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.freekode.tp2intervals.domain.workout.structure.StepLength
import org.freekode.tp2intervals.domain.workout.structure.MultiStep
import org.freekode.tp2intervals.domain.workout.structure.SingleStep
import org.freekode.tp2intervals.domain.workout.structure.WorkoutStructure
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.TrainingPeaksApiClient
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.configuration.TrainingPeaksConfigurationRepository
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.library.TPWorkoutLibraryRepository
import org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.plan.TrainingPeaksPlanCoachApiClient
Expand All @@ -26,30 +27,16 @@ import java.time.LocalDate
class TrainingPeaksWorkoutRepositoryTest {
private val objectMapper = ObjectMapperFactory.objectMapper()

private val trainingPeaksApiClient = TrainingPeaksApiClientMock(
objectMapper,
listOf(
ResourceUtils.getFile("classpath:tp-calendar-workout-swim.json").inputStream(),
ResourceUtils.getFile("classpath:tp-calendar-workout-bike.json").inputStream(),
)
)

private val trainingPeaksUserRepository = getTrainingPeaksUserRepository()

private val trainingPeaksWorkoutRepository = TrainingPeaksWorkoutRepository(
trainingPeaksApiClient,
mock(TrainingPeaksPlanCoachApiClient::class.java),
TPToWorkoutConverter(),
mock(TrainingPeaksPlanRepository::class.java),
trainingPeaksUserRepository,
mock(TPWorkoutLibraryRepository::class.java),
mock(TrainingPeaksConfigurationRepository::class.java),
objectMapper
)
private val trainingPeaksUserRepository = trainingPeaksUserRepository()

@Test
fun `should parse swim workout with distance based steps`() {
// when
val trainingPeaksApiClient = TrainingPeaksApiClientMock(
objectMapper,
ResourceUtils.getFile("classpath:tp-calendar-workout-distant-steps.json").inputStream()
)
val trainingPeaksWorkoutRepository = trainingPeaksWorkoutRepository(trainingPeaksApiClient)
val workouts =
trainingPeaksWorkoutRepository.getWorkoutsFromCalendar(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 1, 1))

Expand All @@ -76,9 +63,43 @@ class TrainingPeaksWorkoutRepositoryTest {
TestUtils.assertStep(multiStep2.steps[1], 10, StepLength.LengthUnit.SECONDS, 0, 0)
}

private fun getTrainingPeaksUserRepository(): TrainingPeaksUserRepository {
@Test
fun `should parse workout with null title`() {
// when
val trainingPeaksApiClient = TrainingPeaksApiClientMock(
objectMapper,
ResourceUtils.getFile("classpath:tp-calendar-workout-null-title.json").inputStream()
)
val trainingPeaksWorkoutRepository = trainingPeaksWorkoutRepository(trainingPeaksApiClient)
val workouts =
trainingPeaksWorkoutRepository.getWorkoutsFromCalendar(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 1, 1))

// then
assertTrue(workouts.isNotEmpty())

val workout = workouts[0]
val structure = workout.structure!!

assertEquals(TrainingType.BIKE, workout.details.type)
assertEquals(WorkoutStructure.TargetUnit.FTP_PERCENTAGE, structure.target)
assertEquals(10, structure.steps.size)
}

private fun trainingPeaksUserRepository(): TrainingPeaksUserRepository {
val mock = mock(TrainingPeaksUserRepository::class.java)
`when`(mock.getUser()).thenReturn(TrainingPeaksUser("123", true))
return mock
}

private fun trainingPeaksWorkoutRepository(trainingPeaksApiClient: TrainingPeaksApiClient) =
TrainingPeaksWorkoutRepository(
trainingPeaksApiClient,
mock(TrainingPeaksPlanCoachApiClient::class.java),
TPToWorkoutConverter(),
mock(TrainingPeaksPlanRepository::class.java),
trainingPeaksUserRepository,
mock(TPWorkoutLibraryRepository::class.java),
mock(TrainingPeaksConfigurationRepository::class.java),
objectMapper
)
}
Loading

0 comments on commit 26b8bb6

Please sign in to comment.