Commit 2c12b296 authored by Julien Topçu's avatar Julien Topçu
Browse files

Recommendation entity

parent 1110aee3
......@@ -3,4 +3,28 @@ package org.craftsrecords.talkadvisor.recommendation
import org.craftsrecords.talkadvisor.recommendation.talk.Talk
import java.util.*
class Recommendation(val id: UUID = UUID.randomUUID(), val talks: Set<Talk>)
\ No newline at end of file
class Recommendation(val id: UUID = UUID.randomUUID(), talks: Set<Talk>) {
val talks = talks.toSet()
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Recommendation
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
override fun toString(): String {
return "Recommendation(id=$id, talks=$talks)"
}
}
\ No newline at end of file
......@@ -3,5 +3,5 @@ package org.craftsrecords.talkadvisor.recommendation.criteria
import org.craftsrecords.talkadvisor.recommendation.talk.TalkFormat
class Criteria(val topic: Topic, talksFormats: Set<TalkFormat>) {
val talksFormats: Set<TalkFormat> = talksFormats.toHashSet()
val talksFormats: Set<TalkFormat> = talksFormats.toSet()
}
\ No newline at end of file
......@@ -14,11 +14,30 @@ class Talk private constructor(id: String,
val duration = notNegative(duration)
val format = TalkFormat.ofDuration(duration)
private fun notNegative(duration: Duration): Duration {
Validate.isTrue(duration.isPositive(), "Talk duration must be strictly positive")
return duration
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Talk
if (id != other.id) return false
return true
}
override fun hashCode(): Int {
return id.hashCode()
}
override fun toString(): String {
return "Talk(id='$id', title='$title', duration=$duration, format=$format)"
}
companion object {
inline fun with(content: Builder.() -> Unit) = Builder().apply(content)
}
......
package org.craftsrecords.talkadvisor.recommendation
import org.assertj.core.api.Assertions.assertThat
import org.craftsrecords.talkadvisor.recommendation.talk.createTalk
import org.craftsrecords.talkadvisor.recommendation.talk.createTalks
import org.junit.jupiter.api.Test
import java.util.*
internal class RecommendationTest {
@Test
fun `todo`() {
TODO("write tests like TalkTest, DomainFactory reusing talk() in the same test?")
fun `should create a recommendation`() {
val talks = createTalks()
val recommendation = Recommendation(talks = talks)
assertThat(recommendation.id).isNotNull()
assertThat(recommendation.talks).isEqualTo(talks)
}
@Test
fun `should store a copy of the talks`() {
val talks = createTalks().toMutableSet()
val recommendation = Recommendation(talks = talks)
val newTalk = createTalk()
talks.add(newTalk)
assertThat(recommendation.talks).doesNotContain(newTalk)
}
@Test
fun `should satisfy entity equality`() {
val id = UUID.randomUUID()
val recommendation1 = Recommendation(id, talks = createTalks())
val recommendation2 = Recommendation(id, talks = createTalks())
assertThat(recommendation1).isEqualTo(recommendation2)
assertThat(recommendation1.hashCode()).isEqualTo(recommendation2.hashCode())
}
@Test
fun `should satisfy entity inequality`() {
val talks = createTalks()
val recommendation1 = Recommendation(talks = talks)
val recommendation2 = Recommendation(talks = talks)
assertThat(recommendation1).isNotEqualTo(recommendation2)
assertThat(recommendation1.hashCode()).isNotEqualTo(recommendation2.hashCode())
}
}
package org.craftsrecords.talkadvisor.recommendation.talk
import java.time.Duration.ofMinutes
import java.util.*
import kotlin.random.Random
fun createTalks() = setOf(createTalk(), createTalk())
fun createTalk(): Talk {
return Talk.with {
id = UUID.randomUUID().toString()
title = "title ${Random.nextInt()}"
duration = ofMinutes(Random.nextLong(2, 120))
}.build()
}
......@@ -43,6 +43,24 @@ internal class TalkTest {
.hasMessage("Talk duration must be strictly positive")
}
@Test
fun `should satisfy entity equality`() {
val talk1 = talk().build()
val talk2 = talk().apply { title = "new title" }.build()
assertThat(talk1).isEqualTo(talk2)
assertThat(talk1.hashCode()).isEqualTo(talk2.hashCode())
}
@Test
fun `should satisfy entity inequality`() {
val talk1 = talk().build()
val talk2 = talk().apply { id = "new id" }.build()
assertThat(talk1).isNotEqualTo(talk2)
assertThat(talk1.hashCode()).isNotEqualTo(talk2.hashCode())
}
fun talk() =
Talk.with {
id = "id"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment