feat: FilterなどにtoStringとequalsを実装

This commit is contained in:
usbharu 2024-09-20 14:51:24 +09:00
parent b52839192c
commit d2b69b162a
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
6 changed files with 150 additions and 2 deletions

View File

@ -82,6 +82,16 @@ class Filter(
} }
override fun hashCode(): Int = id.hashCode() override fun hashCode(): Int = id.hashCode()
override fun toString(): String {
return "Filter(" +
"id=$id, " +
"userDetailId=$userDetailId, " +
"name=$name, " +
"filterContext=$filterContext, " +
"filterAction=$filterAction, " +
"filterKeywords=$filterKeywords" +
")"
}
companion object { companion object {
fun isAllow(user: UserDetail, action: Action, resource: Filter): Boolean { fun isAllow(user: UserDetail, action: Action, resource: Filter): Boolean {

View File

@ -33,4 +33,14 @@ class FilterKeyword(
override fun hashCode(): Int { override fun hashCode(): Int {
return id.hashCode() return id.hashCode()
} }
override fun toString(): String {
return "FilterKeyword(" +
"id=$id, " +
"keyword=$keyword, " +
"mode=$mode" +
")"
}
} }

View File

@ -33,6 +33,13 @@ class FilterName(name: String) {
return name.hashCode() return name.hashCode()
} }
override fun toString(): String {
return "FilterName(" +
"name='$name'" +
")"
}
companion object { companion object {
const val LENGTH = 300 const val LENGTH = 300
} }

View File

@ -16,4 +16,31 @@
package dev.usbharu.hideout.core.domain.model.filter package dev.usbharu.hideout.core.domain.model.filter
class FilterResult(val filter: Filter, val matchedKeyword: String) class FilterResult(val filter: Filter, val matchedKeyword: String) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FilterResult
if (filter != other.filter) return false
if (matchedKeyword != other.matchedKeyword) return false
return true
}
override fun hashCode(): Int {
var result = filter.hashCode()
result = 31 * result + matchedKeyword.hashCode()
return result
}
override fun toString(): String {
return "FilterResult(" +
"filter=$filter, " +
"matchedKeyword='$matchedKeyword'" +
")"
}
}

View File

@ -18,4 +18,31 @@ package dev.usbharu.hideout.core.domain.model.filter
import dev.usbharu.hideout.core.domain.model.post.Post import dev.usbharu.hideout.core.domain.model.post.Post
class FilteredPost(val post: Post, val filterResults: List<FilterResult>) class FilteredPost(val post: Post, val filterResults: List<FilterResult>) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as FilteredPost
if (post != other.post) return false
if (filterResults != other.filterResults) return false
return true
}
override fun hashCode(): Int {
var result = post.hashCode()
result = 31 * result + filterResults.hashCode()
return result
}
override fun toString(): String {
return "FilteredPost(" +
"post=$post, " +
"filterResults=$filterResults" +
")"
}
}

View File

@ -0,0 +1,67 @@
package dev.usbharu.hideout.core.domain.service.filter
import dev.usbharu.hideout.core.domain.model.filter.*
import dev.usbharu.hideout.core.domain.model.post.TestPostFactory
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailId
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
class FilterDomainServiceTest {
@Test
fun apply_filterContextの適用範囲にフィルターが適用される() {
val post = TestPostFactory.create()
val domainService = FilterDomainService()
val filter = Filter(
FilterId(1),
userDetailId = UserDetailId(1),
FilterName("filter"),
setOf(FilterContext.HOME),
filterAction = FilterAction.HIDE,
setOf(FilterKeyword(FilterKeywordId(1), FilterKeywordKeyword("test"), FilterMode.NONE))
)
val apply = domainService.apply(post, FilterContext.HOME, listOf(filter))
assertEquals(1, apply.filterResults.size)
assertEquals("test", apply.filterResults.first().matchedKeyword)
}
@Test
fun apply_filterContextに当てはまらないならfilterResultsが空になる() {
val post = TestPostFactory.create()
val domainService = FilterDomainService()
val filter = Filter(
FilterId(1),
userDetailId = UserDetailId(1),
FilterName("filter"),
setOf(FilterContext.PUBLIC),
filterAction = FilterAction.HIDE,
setOf(FilterKeyword(FilterKeywordId(1), FilterKeywordKeyword("test"), FilterMode.NONE))
)
val apply = domainService.apply(post, FilterContext.HOME, listOf(filter))
assertEquals(0, apply.filterResults.size)
}
@Test
fun overviewにも適用される() {
val post = TestPostFactory.create(overview = "test")
val domainService = FilterDomainService()
val filter = Filter(
FilterId(1),
userDetailId = UserDetailId(1),
FilterName("filter"),
setOf(FilterContext.HOME),
filterAction = FilterAction.HIDE,
setOf(FilterKeyword(FilterKeywordId(1), FilterKeywordKeyword("test"), FilterMode.NONE))
)
val apply = domainService.apply(post, FilterContext.HOME, listOf(filter))
assertEquals(2, apply.filterResults.size)
assertEquals("test", apply.filterResults.first().matchedKeyword)
}
@Test
fun applyAll_filterContextの適用範囲にフィルターが適用される() {
}
}