|
| 1 | +package io.github.abaddon.kcqrs.eventstoredb.config |
| 2 | + |
| 3 | +import io.kurrent.dbclient.Position |
| 4 | +import org.junit.jupiter.api.Assertions.* |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.junit.jupiter.api.assertThrows |
| 7 | + |
| 8 | +internal class SubscriptionFilterConfigTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + fun `Given event type prefix filter when created then filter type and value are set correctly`() { |
| 12 | + // Given |
| 13 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX |
| 14 | + val filterValue = "Counter" |
| 15 | + |
| 16 | + // When |
| 17 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 18 | + |
| 19 | + // Then |
| 20 | + assertNotNull(config) |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `Given event type regex filter when subscribeToAllOptions called then returns valid options`() { |
| 25 | + // Given |
| 26 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_REGEX |
| 27 | + val filterValue = "^Counter.*Event$" |
| 28 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 29 | + val position = Position(0, 0) |
| 30 | + |
| 31 | + // When |
| 32 | + val options = config.subscribeToAllOptions(position) |
| 33 | + |
| 34 | + // Then |
| 35 | + assertNotNull(options) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `Given stream name prefix filter when subscribeToAllOptions called then returns valid options`() { |
| 40 | + // Given |
| 41 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_PREFIX |
| 42 | + val filterValue = "account" |
| 43 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 44 | + val position = Position(0, 0) |
| 45 | + |
| 46 | + // When |
| 47 | + val options = config.subscribeToAllOptions(position) |
| 48 | + |
| 49 | + // Then |
| 50 | + assertNotNull(options) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `Given stream name regex filter when subscribeToAllOptions called then returns valid options`() { |
| 55 | + // Given |
| 56 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX |
| 57 | + val filterValue = "^(account|transaction)" |
| 58 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 59 | + val position = Position(0, 0) |
| 60 | + |
| 61 | + // When |
| 62 | + val options = config.subscribeToAllOptions(position) |
| 63 | + |
| 64 | + // Then |
| 65 | + assertNotNull(options) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `Given different positions when subscribeToAllOptions called then position is set correctly`() { |
| 70 | + // Given |
| 71 | + val config = SubscriptionFilterConfig( |
| 72 | + SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX, |
| 73 | + "Test" |
| 74 | + ) |
| 75 | + val startPosition = Position(0, 0) |
| 76 | + val endPosition = Position(100, 50) |
| 77 | + |
| 78 | + // When |
| 79 | + val optionsFromStart = config.subscribeToAllOptions(startPosition) |
| 80 | + val optionsFromEnd = config.subscribeToAllOptions(endPosition) |
| 81 | + |
| 82 | + // Then |
| 83 | + assertNotNull(optionsFromStart) |
| 84 | + assertNotNull(optionsFromEnd) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `Given invalid filter type when subscribeToAllOptions called then throws IllegalStateException`() { |
| 89 | + // Given |
| 90 | + val filterType = "INVALID_FILTER_TYPE" |
| 91 | + val filterValue = "test" |
| 92 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 93 | + val position = Position(0, 0) |
| 94 | + |
| 95 | + // When/Then |
| 96 | + val exception = assertThrows<IllegalStateException> { |
| 97 | + config.subscribeToAllOptions(position) |
| 98 | + } |
| 99 | + |
| 100 | + assertTrue(exception.message?.contains("No filter type") == true) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `Given event type prefix constant when accessed then has correct value`() { |
| 105 | + // Then |
| 106 | + assertEquals("eventTypePrefix", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun `Given event type regex constant when accessed then has correct value`() { |
| 111 | + // Then |
| 112 | + assertEquals("eventTypeRegEx", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_REGEX) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `Given stream name prefix constant when accessed then has correct value`() { |
| 117 | + // Then |
| 118 | + assertEquals("streamNamePrefix", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_PREFIX) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun `Given stream name regex constant when accessed then has correct value`() { |
| 123 | + // Then |
| 124 | + assertEquals("streamNameRegEx", SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `Given complex regex pattern when created then pattern is preserved`() { |
| 129 | + // Given |
| 130 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX |
| 131 | + val complexPattern = "^(account|transaction|customer)\\.(create|update|delete)$" |
| 132 | + val config = SubscriptionFilterConfig(filterType, complexPattern) |
| 133 | + val position = Position(0, 0) |
| 134 | + |
| 135 | + // When |
| 136 | + val options = config.subscribeToAllOptions(position) |
| 137 | + |
| 138 | + // Then |
| 139 | + assertNotNull(options) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `Given multiple configs with different filter types when created then each is independent`() { |
| 144 | + // Given |
| 145 | + val config1 = SubscriptionFilterConfig( |
| 146 | + SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX, |
| 147 | + "Account" |
| 148 | + ) |
| 149 | + val config2 = SubscriptionFilterConfig( |
| 150 | + SubscriptionFilterConfig.SUBSCRIPTION_FILTER_STREAM_NAME_REGEX, |
| 151 | + "^account" |
| 152 | + ) |
| 153 | + val position = Position(0, 0) |
| 154 | + |
| 155 | + // When |
| 156 | + val options1 = config1.subscribeToAllOptions(position) |
| 157 | + val options2 = config2.subscribeToAllOptions(position) |
| 158 | + |
| 159 | + // Then |
| 160 | + assertNotNull(options1) |
| 161 | + assertNotNull(options2) |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + fun `Given empty filter value when created then config is created`() { |
| 166 | + // Given |
| 167 | + val filterType = SubscriptionFilterConfig.SUBSCRIPTION_FILTER_EVENT_TYPE_PREFIX |
| 168 | + val filterValue = "" |
| 169 | + val config = SubscriptionFilterConfig(filterType, filterValue) |
| 170 | + val position = Position(0, 0) |
| 171 | + |
| 172 | + // When |
| 173 | + val options = config.subscribeToAllOptions(position) |
| 174 | + |
| 175 | + // Then |
| 176 | + assertNotNull(options) |
| 177 | + } |
| 178 | +} |
0 commit comments