-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(streak): create streak on habit creation
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
services/streak/src/intTest/kotlin/de/codecentric/streak/HabitConsumerIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package de.codecentric.streak | ||
|
||
import de.codecentric.streak.domain.Habit.Frequency.WEEKLY | ||
import de.codecentric.streak.domain.StreakRepository | ||
import io.kotest.assertions.asClue | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.kafka.core.KafkaTemplate | ||
import org.springframework.kafka.test.context.EmbeddedKafka | ||
import org.springframework.test.annotation.DirtiesContext | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.testcontainers.shaded.org.awaitility.Awaitility | ||
import java.util.Optional | ||
import java.util.UUID | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
|
||
@ActiveProfiles("intTest") | ||
@SpringBootTest(classes = [HabitConsumer::class, StreakRepository::class, DataJdbcConfiguration::class]) | ||
@EnableAutoConfiguration | ||
@DirtiesContext | ||
@EmbeddedKafka(topics = ["habit-events", "habit-deleted"]) | ||
class HabitConsumerIntegrationTest { | ||
|
||
@Autowired | ||
private lateinit var habitCreatedKafkaTemplate: KafkaTemplate<String, HabitCreated> | ||
|
||
@Autowired | ||
private lateinit var habitDeletedKafkaTemplate: KafkaTemplate<String, HabitDeleted> | ||
|
||
@Autowired | ||
private lateinit var streakRepository: StreakRepository | ||
|
||
@Test | ||
fun createsAndSavesStreakWhenHabitWasCreated() { | ||
val id = UUID.randomUUID() | ||
habitCreatedKafkaTemplate.send("habit-events", "key", HabitCreated(id, WEEKLY, 3)) | ||
habitCreatedKafkaTemplate.flush() | ||
|
||
Awaitility.await().atMost(1, SECONDS).untilAsserted { | ||
val result = streakRepository.findByHabitId(id) | ||
result.asClue { | ||
result.isPresent shouldBe true | ||
result.get().habit.id shouldBe id | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun deletesStreakWhenHabitWasDeleted() { | ||
val id = UUID.randomUUID() | ||
habitDeletedKafkaTemplate.send("habit-deleted", "key", HabitDeleted(id)) | ||
|
||
Awaitility.await().atMost(1, SECONDS).untilAsserted { | ||
val result = streakRepository.findByHabitId(id) | ||
result shouldBe Optional.empty() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
services/streak/src/main/kotlin/de/codecentric/streak/HabitConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.codecentric.streak | ||
|
||
import de.codecentric.streak.domain.Habit | ||
import de.codecentric.streak.domain.Habit.Frequency | ||
import de.codecentric.streak.domain.Streak | ||
import de.codecentric.streak.domain.StreakRepository | ||
import org.springframework.kafka.annotation.KafkaHandler | ||
import org.springframework.kafka.annotation.KafkaListener | ||
import org.springframework.stereotype.Component | ||
import java.util.UUID | ||
|
||
@Component | ||
@KafkaListener(topics = ["habit-events"]) | ||
class HabitConsumer(private val repository: StreakRepository) { | ||
|
||
@KafkaHandler | ||
fun consumeHabitCreatedEvent(event: HabitCreated) { | ||
val streak = Streak.from(Habit.from(event.habitId, event.frequency, event.repetitions)) | ||
repository.save(streak) | ||
} | ||
} | ||
|
||
data class HabitCreated(val habitId: UUID, val frequency: Frequency, val repetitions: Int) | ||
|
||
data class HabitDeleted(val habitId: UUID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
services/streak/src/main/kotlin/de/codecentric/streak/domain/StreakRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
package de.codecentric.streak.domain | ||
|
||
import org.springframework.data.jdbc.repository.query.Query | ||
import org.springframework.data.repository.CrudRepository | ||
import java.util.Optional | ||
import java.util.UUID | ||
|
||
interface StreakRepository : CrudRepository<Streak, UUID> { | ||
|
||
@Query( | ||
""" | ||
SELECT s.id, h.id habit_id, h.frequency habit_frequency, h.repetitions habit_repetitions | ||
FROM hc_streak.streaks s | ||
INNER JOIN hc_streak.habits h | ||
ON s.id = h.streak WHERE h.id = :id | ||
""" | ||
) | ||
fun findByHabitId(id: UUID): Optional<Streak> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters