|
5 | 5 |
|
6 | 6 | package org.opensearch.indexmanagement.rollup.interceptor |
7 | 7 |
|
| 8 | +import org.junit.Before |
| 9 | +import org.mockito.Mockito.mock |
| 10 | +import org.mockito.Mockito.`when` |
| 11 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver |
| 12 | +import org.opensearch.cluster.service.ClusterService |
| 13 | +import org.opensearch.common.settings.ClusterSettings |
| 14 | +import org.opensearch.common.settings.Settings |
8 | 15 | import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor.Companion.BYPASS_ROLLUP_SEARCH |
9 | 16 | import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor.Companion.BYPASS_SIZE_CHECK |
10 | | -import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor.Companion.clearBypass |
11 | | -import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor.Companion.getBypassLevel |
12 | | -import org.opensearch.indexmanagement.rollup.interceptor.RollupInterceptor.Companion.setBypass |
| 17 | +import org.opensearch.indexmanagement.rollup.settings.RollupSettings |
| 18 | +import org.opensearch.search.builder.SearchSourceBuilder |
| 19 | +import org.opensearch.search.fetch.subphase.FetchSourceContext |
| 20 | +import org.opensearch.search.internal.ShardSearchRequest |
13 | 21 | import org.opensearch.test.OpenSearchTestCase |
14 | 22 |
|
15 | 23 | class RollupInterceptorTests : OpenSearchTestCase() { |
16 | 24 |
|
17 | | - fun `test setBypass and getBypassLevel for BYPASS_ROLLUP_SEARCH`() { |
18 | | - setBypass(BYPASS_ROLLUP_SEARCH) |
19 | | - assertEquals(BYPASS_ROLLUP_SEARCH, getBypassLevel()) |
20 | | - clearBypass() |
| 25 | + private lateinit var interceptor: RollupInterceptor |
| 26 | + |
| 27 | + @Before |
| 28 | + fun setup() { |
| 29 | + interceptor = createInterceptor() |
| 30 | + } |
| 31 | + |
| 32 | + fun `test getBypassFromFetchSource returns null when no source`() { |
| 33 | + val request = mock(ShardSearchRequest::class.java) |
| 34 | + `when`(request.source()).thenReturn(null) |
| 35 | + |
| 36 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 37 | + |
| 38 | + assertNull(bypassLevel) |
21 | 39 | } |
22 | 40 |
|
23 | | - fun `test setBypass and getBypassLevel for BYPASS_SIZE_CHECK`() { |
24 | | - setBypass(BYPASS_SIZE_CHECK) |
25 | | - assertEquals(BYPASS_SIZE_CHECK, getBypassLevel()) |
26 | | - clearBypass() |
| 41 | + fun `test getBypassFromFetchSource returns null when no FetchSourceContext`() { |
| 42 | + val request = mock(ShardSearchRequest::class.java) |
| 43 | + val source = SearchSourceBuilder() |
| 44 | + |
| 45 | + `when`(request.source()).thenReturn(source) |
| 46 | + |
| 47 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 48 | + |
| 49 | + assertNull(bypassLevel) |
27 | 50 | } |
28 | 51 |
|
29 | | - fun `test getBypassLevel returns 0 when not set`() { |
30 | | - clearBypass() |
31 | | - assertEquals(0, getBypassLevel()) |
| 52 | + fun `test getBypassFromFetchSource returns null when no includes array`() { |
| 53 | + val request = mock(ShardSearchRequest::class.java) |
| 54 | + val source = SearchSourceBuilder() |
| 55 | + source.fetchSource(FetchSourceContext(true)) |
| 56 | + |
| 57 | + `when`(request.source()).thenReturn(source) |
| 58 | + |
| 59 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 60 | + |
| 61 | + assertNull(bypassLevel) |
32 | 62 | } |
33 | 63 |
|
34 | | - fun `test clearBypass resets bypass level`() { |
35 | | - setBypass(BYPASS_ROLLUP_SEARCH) |
36 | | - assertEquals(BYPASS_ROLLUP_SEARCH, getBypassLevel()) |
37 | | - clearBypass() |
38 | | - assertEquals(0, getBypassLevel()) |
| 64 | + fun `test getBypassFromFetchSource returns null when no bypass marker present`() { |
| 65 | + val request = mock(ShardSearchRequest::class.java) |
| 66 | + val source = SearchSourceBuilder() |
| 67 | + source.fetchSource(FetchSourceContext(false, arrayOf("field1", "field2"), emptyArray())) |
| 68 | + |
| 69 | + `when`(request.source()).thenReturn(source) |
| 70 | + |
| 71 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 72 | + |
| 73 | + assertNull(bypassLevel) |
39 | 74 | } |
40 | 75 |
|
41 | | - fun `test bypass is thread local`() { |
42 | | - setBypass(BYPASS_ROLLUP_SEARCH) |
43 | | - assertEquals(BYPASS_ROLLUP_SEARCH, getBypassLevel()) |
| 76 | + fun `test getBypassFromFetchSource extracts BYPASS_ROLLUP_SEARCH correctly`() { |
| 77 | + val request = mock(ShardSearchRequest::class.java) |
| 78 | + val source = SearchSourceBuilder() |
| 79 | + source.fetchSource(FetchSourceContext(false, arrayOf("_rollup_internal_bypass_$BYPASS_ROLLUP_SEARCH"), emptyArray())) |
44 | 80 |
|
45 | | - val thread = Thread { |
46 | | - assertEquals(0, getBypassLevel()) |
47 | | - setBypass(BYPASS_SIZE_CHECK) |
48 | | - assertEquals(BYPASS_SIZE_CHECK, getBypassLevel()) |
49 | | - } |
50 | | - thread.start() |
51 | | - thread.join() |
| 81 | + `when`(request.source()).thenReturn(source) |
52 | 82 |
|
53 | | - assertEquals(BYPASS_ROLLUP_SEARCH, getBypassLevel()) |
54 | | - clearBypass() |
| 83 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 84 | + |
| 85 | + assertEquals(BYPASS_ROLLUP_SEARCH, bypassLevel) |
55 | 86 | } |
56 | 87 |
|
57 | | - fun `test multiple setBypass calls overwrite previous value`() { |
58 | | - setBypass(BYPASS_ROLLUP_SEARCH) |
59 | | - assertEquals(BYPASS_ROLLUP_SEARCH, getBypassLevel()) |
60 | | - setBypass(BYPASS_SIZE_CHECK) |
61 | | - assertEquals(BYPASS_SIZE_CHECK, getBypassLevel()) |
62 | | - clearBypass() |
| 88 | + fun `test getBypassFromFetchSource extracts BYPASS_SIZE_CHECK correctly`() { |
| 89 | + val request = mock(ShardSearchRequest::class.java) |
| 90 | + val source = SearchSourceBuilder() |
| 91 | + source.fetchSource(FetchSourceContext(false, arrayOf("_rollup_internal_bypass_$BYPASS_SIZE_CHECK"), emptyArray())) |
| 92 | + |
| 93 | + `when`(request.source()).thenReturn(source) |
| 94 | + |
| 95 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 96 | + |
| 97 | + assertEquals(BYPASS_SIZE_CHECK, bypassLevel) |
| 98 | + } |
| 99 | + |
| 100 | + fun `test getBypassFromFetchSource finds marker among multiple includes`() { |
| 101 | + val request = mock(ShardSearchRequest::class.java) |
| 102 | + val source = SearchSourceBuilder() |
| 103 | + source.fetchSource( |
| 104 | + FetchSourceContext(false, arrayOf("field1", "_rollup_internal_bypass_$BYPASS_ROLLUP_SEARCH", "field2"), emptyArray()), |
| 105 | + ) |
| 106 | + |
| 107 | + `when`(request.source()).thenReturn(source) |
| 108 | + |
| 109 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 110 | + |
| 111 | + assertEquals(BYPASS_ROLLUP_SEARCH, bypassLevel) |
| 112 | + } |
| 113 | + |
| 114 | + fun `test getBypassFromFetchSource returns null for invalid bypass marker`() { |
| 115 | + val request = mock(ShardSearchRequest::class.java) |
| 116 | + val source = SearchSourceBuilder() |
| 117 | + source.fetchSource(FetchSourceContext(false, arrayOf("_rollup_internal_bypass_invalid"), emptyArray())) |
| 118 | + |
| 119 | + `when`(request.source()).thenReturn(source) |
| 120 | + |
| 121 | + val bypassLevel = interceptor.getBypassFromFetchSource(request) |
| 122 | + |
| 123 | + assertNull(bypassLevel) |
| 124 | + } |
| 125 | + |
| 126 | + // Helper method to create interceptor instance |
| 127 | + private fun createInterceptor(): RollupInterceptor { |
| 128 | + val clusterService = mock(ClusterService::class.java) |
| 129 | + val clusterSettings = ClusterSettings( |
| 130 | + Settings.EMPTY, |
| 131 | + setOf( |
| 132 | + RollupSettings.ROLLUP_SEARCH_ENABLED, |
| 133 | + RollupSettings.ROLLUP_SEARCH_ALL_JOBS, |
| 134 | + RollupSettings.ROLLUP_SEARCH_SOURCE_INDICES, |
| 135 | + ), |
| 136 | + ) |
| 137 | + `when`(clusterService.clusterSettings).thenReturn(clusterSettings) |
| 138 | + |
| 139 | + val settings = Settings.EMPTY |
| 140 | + val resolver = mock(IndexNameExpressionResolver::class.java) |
| 141 | + return RollupInterceptor(clusterService, settings, resolver) |
63 | 142 | } |
64 | 143 | } |
0 commit comments