From d2b69b162ae006a19e324e92ba6ca7f3bcc53407 Mon Sep 17 00:00:00 2001 From: usbharu Date: Fri, 20 Sep 2024 14:51:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Filter=E3=81=AA=E3=81=A9=E3=81=ABtoStri?= =?UTF-8?q?ng=E3=81=A8equals=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/model/filter/Filter.kt | 10 +++ .../core/domain/model/filter/FilterKeyword.kt | 10 +++ .../core/domain/model/filter/FilterName.kt | 7 ++ .../core/domain/model/filter/FilterResult.kt | 29 +++++++- .../core/domain/model/filter/FilteredPost.kt | 29 +++++++- .../service/filter/FilterDomainServiceTest.kt | 67 +++++++++++++++++++ 6 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainServiceTest.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt index 43d18485..30878db0 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/Filter.kt @@ -82,6 +82,16 @@ class Filter( } 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 { fun isAllow(user: UserDetail, action: Action, resource: Filter): Boolean { diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt index fb404f20..f0f2b2d7 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterKeyword.kt @@ -33,4 +33,14 @@ class FilterKeyword( override fun hashCode(): Int { return id.hashCode() } + + override fun toString(): String { + return "FilterKeyword(" + + "id=$id, " + + "keyword=$keyword, " + + "mode=$mode" + + ")" + } + + } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt index b88dbf71..bf31e0f9 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterName.kt @@ -33,6 +33,13 @@ class FilterName(name: String) { return name.hashCode() } + override fun toString(): String { + return "FilterName(" + + "name='$name'" + + ")" + } + + companion object { const val LENGTH = 300 } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt index 7bd074c6..b3256ed6 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilterResult.kt @@ -16,4 +16,31 @@ 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'" + + ")" + } + + +} diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt index 5d75f380..c2153101 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/filter/FilteredPost.kt @@ -18,4 +18,31 @@ package dev.usbharu.hideout.core.domain.model.filter import dev.usbharu.hideout.core.domain.model.post.Post -class FilteredPost(val post: Post, val filterResults: List) +class FilteredPost(val post: Post, val filterResults: List) { + 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" + + ")" + } + + +} diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainServiceTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainServiceTest.kt new file mode 100644 index 00000000..93505d16 --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/filter/FilterDomainServiceTest.kt @@ -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の適用範囲にフィルターが適用される() { + + } +} \ No newline at end of file