refactor filter exception

feature/top10
Razmig Sarkissian 7 years ago
parent 4055ac56ef
commit 7e3644ddbd
  1. 15
      app/src/androidTest/java/net/pokeranalytics/android/unitTests/filter/ExceptionFilterInstrumentedTest.kt
  2. 3
      app/src/androidTest/java/net/pokeranalytics/android/unitTests/filter/RealmFilterInstrumentedUnitTest.kt
  3. 41
      app/src/main/java/net/pokeranalytics/android/exceptions/Exceptions.kt
  4. 18
      app/src/main/java/net/pokeranalytics/android/model/filter/QueryType.kt
  5. 9
      app/src/main/java/net/pokeranalytics/android/model/realm/Filter.kt
  6. 33
      app/src/main/java/net/pokeranalytics/android/model/realm/FilterElement.kt
  7. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/FilterElementRow.kt

@ -2,7 +2,7 @@ package net.pokeranalytics.android.unitTests.filter
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest
import net.pokeranalytics.android.exceptions.FilterValueMapException import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.model.filter.QueryType
import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.model.realm.FilterElement import net.pokeranalytics.android.model.realm.FilterElement
@ -13,7 +13,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class ExceptionFilterInstrumentedTest: BaseFilterInstrumentedUnitTest() { class ExceptionFilterInstrumentedTest: BaseFilterInstrumentedUnitTest() {
@Test(expected = FilterValueMapException::class) @Test(expected = PokerAnalyticsException.FilterElementExpectedValueMissing::class)
fun testValueKeyFilterException() { fun testValueKeyFilterException() {
val filter = QueryType.STARTED_FROM_DATE val filter = QueryType.STARTED_FROM_DATE
val filterElement = FilterElement() val filterElement = FilterElement()
@ -27,15 +27,8 @@ class ExceptionFilterInstrumentedTest: BaseFilterInstrumentedUnitTest() {
) )
} }
@Test(expected = FilterValueMapException::class) @Test(expected = PokerAnalyticsException.FilterElementUnknownName::class)
fun testFilterException() { fun testFilterException() {
val realm = this.mockRealm FilterElement().queryType
val filter = QueryType.BLINDS
filter.updateValueMap(FilterElement())
Filter.queryOn(
realm,
Session,
arrayListOf(filter)
)
} }
} }

@ -2,6 +2,7 @@ package net.pokeranalytics.android.unitTests.filter
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest import net.pokeranalytics.android.components.BaseFilterInstrumentedUnitTest
import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.model.filter.QueryType
import net.pokeranalytics.android.model.realm.Filter import net.pokeranalytics.android.model.realm.Filter
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
@ -37,7 +38,7 @@ class RealmFilterInstrumentedUnitTest : BaseFilterInstrumentedUnitTest() {
val filterComponent = filter.filterElements.first() val filterComponent = filter.filterElements.first()
filterComponent?.let { filterComponent?.let {
Assert.assertEquals(QueryType.CASH, QueryType.valueOf(it.filterName)) Assert.assertEquals(QueryType.CASH, QueryType.valueOf(it.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName))
} ?: run { } ?: run {
Assert.fail() Assert.fail()
} }

@ -1,25 +1,22 @@
package net.pokeranalytics.android.exceptions package net.pokeranalytics.android.exceptions
class ModelException(message: String) : Exception(message) { import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow
} class ModelException(message: String) : Exception(message)
class FormattingException(message: String) : Exception(message)
class FormattingException(message: String) : Exception(message) { class RowRepresentableEditDescriptorException(message: String) : Exception(message)
} class ConfigurationException(message: String) : Exception(message)
class RowRepresentableEditDescriptorException(message: String) : Exception(message) { sealed class PokerAnalyticsException(message: String) : Exception(message) {
object FilterElementUnknownName : PokerAnalyticsException(message = "No filterElement name was found to identify the queryType")
} object FilterElementUnknownSectionName: PokerAnalyticsException(message = "No filterElement section name was found to identify the queryType")
object FilterMissingEntity: PokerAnalyticsException(message = "This filter has no entity initialized")
class FilterValueMapException(message: String) : Exception(message) { object FilterUnhandledEntity : PokerAnalyticsException(message = "This entity is not filterable")
init { object QueryValueMapUnknown: PokerAnalyticsException(message = "fieldName is missing")
println("FilterValueMapException(): $message") object QueryTypeUnhandled: PokerAnalyticsException(message = "filter type not handled")
} object QueryValueMapUnexpectedValue: PokerAnalyticsException(message = "valueMap null not expected")
} object FilterElementExpectedValueMissing : PokerAnalyticsException(message = "filter is empty or null")
data class QueryValueMapMissingKeys(val missingKeys: List<String>) : PokerAnalyticsException(message = "valueMap does not contain $missingKeys")
class FilterMissingEntityException(message: String) : Exception(message) data class UnknownQueryTypeForRow(val filterElementRow: FilterElementRow) : PokerAnalyticsException(message = "no filter type for $filterElementRow")
class FilterUnhandledEntityException(message : String) : Exception(message)
class ConfigurationException(message: String) : Exception(message) {
} }

@ -3,7 +3,7 @@ package net.pokeranalytics.android.model.filter
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.RealmQuery import io.realm.RealmQuery
import net.pokeranalytics.android.exceptions.FilterValueMapException import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.realm.FilterElementBlind import net.pokeranalytics.android.model.realm.FilterElementBlind
import net.pokeranalytics.android.model.realm.FilterElement import net.pokeranalytics.android.model.realm.FilterElement
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
@ -104,9 +104,9 @@ enum class QueryType(private var subType:SubType? = null) {
val smallBlindFieldName = filterable.fieldNameForQueryType(SMALL_BLIND) val smallBlindFieldName = filterable.fieldNameForQueryType(SMALL_BLIND)
val bigBlindFieldName = filterable.fieldNameForQueryType(BIG_BLIND) val bigBlindFieldName = filterable.fieldNameForQueryType(BIG_BLIND)
val currencyCodeFieldName = filterable.fieldNameForQueryType(CURRENCY_CODE) val currencyCodeFieldName = filterable.fieldNameForQueryType(CURRENCY_CODE)
smallBlindFieldName ?: throw FilterValueMapException("fieldName is missing") smallBlindFieldName ?: throw PokerAnalyticsException.QueryValueMapUnknown
bigBlindFieldName ?: throw FilterValueMapException("fieldName is missing") bigBlindFieldName ?: throw PokerAnalyticsException.QueryValueMapUnknown
currencyCodeFieldName ?: throw FilterValueMapException("fieldName is missing") currencyCodeFieldName ?: throw PokerAnalyticsException.QueryValueMapUnknown
val blinds: RealmList<FilterElementBlind> by valueMap val blinds: RealmList<FilterElementBlind> by valueMap
blinds.forEachIndexed {index, blind -> blinds.forEachIndexed {index, blind ->
@ -142,7 +142,7 @@ enum class QueryType(private var subType:SubType? = null) {
this == WEEK_DAY -> return WEEK_END.filter(realmQuery.not(), filterable) this == WEEK_DAY -> return WEEK_END.filter(realmQuery.not(), filterable)
else -> { else -> {
val fieldName = filterable.fieldNameForQueryType(this) val fieldName = filterable.fieldNameForQueryType(this)
fieldName ?: throw FilterValueMapException("fieldName is missing") fieldName ?: throw PokerAnalyticsException.QueryValueMapUnknown
this.subType?.let { subType -> this.subType?.let { subType ->
return when (subType) { return when (subType) {
@ -216,7 +216,7 @@ enum class QueryType(private var subType:SubType? = null) {
realmQuery.`in`(fieldName, arrayOf(Calendar.SATURDAY, Calendar.SUNDAY)) realmQuery.`in`(fieldName, arrayOf(Calendar.SATURDAY, Calendar.SUNDAY))
} }
else -> { else -> {
throw FilterValueMapException("filter type not handled") throw PokerAnalyticsException.QueryTypeUnhandled
} }
} }
} }
@ -267,7 +267,7 @@ enum class QueryType(private var subType:SubType? = null) {
valueMap = mapOf("year" to filterElement.year) valueMap = mapOf("year" to filterElement.year)
} }
else -> { else -> {
throw FilterValueMapException("filter type not handled") throw PokerAnalyticsException.QueryValueMapUnexpectedValue
} }
} }
} }
@ -278,10 +278,10 @@ enum class QueryType(private var subType:SubType? = null) {
field?.let { map -> field?.let { map ->
val missingKeys = map.keys.filter { !valueMapExceptedKeys.contains(it) } val missingKeys = map.keys.filter { !valueMapExceptedKeys.contains(it) }
if (map.keys.size == valueMapExceptedKeys.size && missingKeys.isNotEmpty()) { if (map.keys.size == valueMapExceptedKeys.size && missingKeys.isNotEmpty()) {
throw FilterValueMapException("valueMap does not contain $missingKeys") throw PokerAnalyticsException.QueryValueMapMissingKeys(missingKeys)
} }
} ?: run { } ?: run {
throw FilterValueMapException("valueMap null not expected") throw PokerAnalyticsException.QueryValueMapUnexpectedValue
} }
} }
return field return field

@ -2,8 +2,7 @@ package net.pokeranalytics.android.model.realm
import io.realm.* import io.realm.*
import io.realm.annotations.PrimaryKey import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.exceptions.FilterMissingEntityException import net.pokeranalytics.android.exceptions.*
import net.pokeranalytics.android.exceptions.FilterUnhandledEntityException
import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.filter.Filterable
import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.model.filter.QueryType
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterCategoryRow
@ -38,7 +37,7 @@ open class Filter(entity:Filterable) : RealmObject() {
fun filterableClass(entity: Filterable): FilterableClass { fun filterableClass(entity: Filterable): FilterableClass {
return when (entity) { return when (entity) {
is Session.Companion -> SESSION is Session.Companion -> SESSION
else -> throw FilterUnhandledEntityException("this entity is not filterable") else -> throw PokerAnalyticsException.FilterUnhandledEntity
} }
} }
} }
@ -116,7 +115,7 @@ open class Filter(entity:Filterable) : RealmObject() {
fun countBy(filterCategoryRow: FilterCategoryRow) : Int { fun countBy(filterCategoryRow: FilterCategoryRow) : Int {
val sections = filterCategoryRow.filterSectionRows val sections = filterCategoryRow.filterSectionRows
return filterElements.count { return filterElements.count {
sections.contains(FilterSectionRow.valueOf(it.sectionName)) sections.contains(FilterSectionRow.valueOf(it.sectionName ?: throw PokerAnalyticsException.FilterElementUnknownSectionName))
} }
} }
@ -131,7 +130,7 @@ open class Filter(entity:Filterable) : RealmObject() {
} }
fun results(): RealmResults<*> { fun results(): RealmResults<*> {
val filterableClass : FilterableClass = this.filterableClass ?: throw FilterMissingEntityException("this filter has no entity initialized") val filterableClass : FilterableClass = this.filterableClass ?: throw PokerAnalyticsException.FilterMissingEntity
val realmEntity : Class < out RealmObject > = filterableClass.relatedEntity val realmEntity : Class < out RealmObject > = filterableClass.relatedEntity
var realmQuery : RealmQuery<out RealmObject> = realm.where(realmEntity) var realmQuery : RealmQuery<out RealmObject> = realm.where(realmEntity)
this.filterElements.map { this.filterElements.map {

@ -2,7 +2,7 @@ package net.pokeranalytics.android.model.realm
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import net.pokeranalytics.android.exceptions.FilterValueMapException import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.model.filter.QueryType
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow
import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow.* import net.pokeranalytics.android.ui.view.rowrepresentable.FilterElementRow.*
@ -17,7 +17,8 @@ open class FilterElement() : RealmObject() {
} }
constructor(filterElementRows: ArrayList<FilterElementRow>) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) { constructor(filterElementRows: ArrayList<FilterElementRow>) : this(filterElementRows.first().filterName, filterElementRows.first().filterSectionRow.name) {
this.stringValues = when (QueryType.valueOf(this.filterName)) { val filterName : String = this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName
this.stringValues = when (QueryType.valueOf(filterName)) {
QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> { QueryType.GAME, QueryType.BANKROLL, QueryType.TOURNAMENT_NAME, QueryType.ALL_TOURNAMENT_FEATURES, QueryType.ANY_TOURNAMENT_FEATURES, QueryType.LOCATION -> {
RealmList<String>().apply { RealmList<String>().apply {
this.addAll(filterElementRows.map { this.addAll(filterElementRows.map {
@ -86,11 +87,11 @@ open class FilterElement() : RealmObject() {
} }
} }
var filterName : String = "" var filterName : String? = null
var sectionName : String = "" var sectionName : String? = null
val queryType : QueryType val queryType : QueryType
get() = QueryType.valueOf(filterName) get() = QueryType.valueOf(this.filterName ?: throw PokerAnalyticsException.FilterElementUnknownName)
.apply { .apply {
this.updateValueMap(this@FilterElement) this.updateValueMap(this@FilterElement)
} }
@ -101,7 +102,7 @@ open class FilterElement() : RealmObject() {
private var blindValues : RealmList<FilterElementBlind>? = null private var blindValues : RealmList<FilterElementBlind>? = null
val ids : Array<String> val ids : Array<String>
get() = stringValues?.toTypedArray()?: throw FilterValueMapException("filter type not handled") get() = stringValues?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val blinds : RealmList<FilterElementBlind> val blinds : RealmList<FilterElementBlind>
get() { get() {
@ -109,44 +110,44 @@ open class FilterElement() : RealmObject() {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
return it return it
} else { } else {
throw FilterValueMapException("filter is empty or null") throw PokerAnalyticsException.FilterElementExpectedValueMissing
} }
} }
throw FilterValueMapException("filter is empty or null") throw PokerAnalyticsException.FilterElementExpectedValueMissing
} }
val date : Date val date : Date
get() = dateValue?: throw FilterValueMapException("filter type not handled") get() = dateValue?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val values : Array<Int> val values : Array<Int>
get() = numericValues?.map { get() = numericValues?.map {
it.toInt() it.toInt()
}?.toTypedArray()?: throw FilterValueMapException("filter type not handled") }?.toTypedArray()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val value : Double val value : Double
get() = numericValues?.first()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val leftValue : Double val leftValue : Double
get() = numericValues?.first()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.first()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val rightValue : Double val rightValue : Double
get() = numericValues?.last()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.last()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val dayOfWeek : Int val dayOfWeek : Int
get() = numericValues?.first()?.toInt()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val month : Int val month : Int
get() = numericValues?.first()?.toInt()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
val year : Int val year : Int
get() = numericValues?.first()?.toInt()?: throw FilterValueMapException("filter type not handled") get() = numericValues?.first()?.toInt()?: throw PokerAnalyticsException.FilterElementExpectedValueMissing
} }

@ -1,7 +1,7 @@
package net.pokeranalytics.android.ui.view.rowrepresentable package net.pokeranalytics.android.ui.view.rowrepresentable
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.FilterValueMapException import net.pokeranalytics.android.exceptions.PokerAnalyticsException
import net.pokeranalytics.android.model.filter.QueryType import net.pokeranalytics.android.model.filter.QueryType
import net.pokeranalytics.android.model.interfaces.Manageable import net.pokeranalytics.android.model.interfaces.Manageable
import net.pokeranalytics.android.model.realm.FilterElement import net.pokeranalytics.android.model.realm.FilterElement
@ -88,7 +88,7 @@ sealed class FilterElementRow : RowRepresentable {
is AllTournamentFeature -> QueryType.ALL_TOURNAMENT_FEATURES is AllTournamentFeature -> QueryType.ALL_TOURNAMENT_FEATURES
is ResultMoreThan -> QueryType.MORE_THAN_NET_RESULT is ResultMoreThan -> QueryType.MORE_THAN_NET_RESULT
is ResultLessThan -> QueryType.LESS_THAN_NET_RESULT is ResultLessThan -> QueryType.LESS_THAN_NET_RESULT
else -> throw FilterValueMapException("no filter type for $this") //TODO create exception else -> throw PokerAnalyticsException.UnknownQueryTypeForRow(this)
} }
} }

Loading…
Cancel
Save