Skip to content

Commit e46eaa0

Browse files
committed
Add UnifiedQueryContextTest coverage for liveSettings bridge
Pins the four behaviors the previous commit introduced: * `testLiveSettingsBridgeRespectsClusterRexMaxMatchLimit` — operator's cluster-side override (5) wins over the static default (10). * `testLiveSettingsAbsentFallsBackToStaticDefault` — pre-bridge behavior preserved when the REST handler doesn't supply a live `Settings` instance. * `testLiveSettingsNullValueFallsBackToStaticDefault` — pins the `!= null` guard in `build()`; if the live settings instance is supplied but has no cluster override for the rex key, the static default still wins. * `testLiveSettingsDoNotBridgeOtherKeys` — pins the rex-only scoping decision: a cluster override for `QUERY_SIZE_LIMIT` is intentionally NOT picked up. Surfaces accidental scope creep (someone widening the snapshot to additional keys) as a hard test failure. Uses a small inline `Settings` stub — the same pattern `buildSettings()` uses in production code, no Mockito needed. Signed-off-by: Jialiang Liang <jiallian@amazon.com>
1 parent 62d617d commit e46eaa0

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

api/src/test/java/org/opensearch/sql/api/UnifiedQueryContextTest.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
import static org.junit.Assert.assertTrue;
1212
import static org.opensearch.sql.common.setting.Settings.Key.*;
1313

14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
1417
import org.junit.Test;
1518
import org.opensearch.sql.calcite.SysLimit;
19+
import org.opensearch.sql.common.setting.Settings;
1620
import org.opensearch.sql.executor.QueryType;
1721

1822
public class UnifiedQueryContextTest extends UnifiedQueryTestBase {
@@ -83,6 +87,99 @@ public void testInvalidDefaultNamespace() {
8387
.build();
8488
}
8589

90+
@Test
91+
public void testLiveSettingsBridgeRespectsClusterRexMaxMatchLimit() {
92+
// When the operator has set plugins.ppl.rex.max_match.limit on the cluster,
93+
// UnifiedQueryContext should return that value, not the hardcoded 10.
94+
Settings live = stubSettings(Map.of(PPL_REX_MAX_MATCH_LIMIT, 5));
95+
96+
UnifiedQueryContext context =
97+
UnifiedQueryContext.builder()
98+
.language(QueryType.PPL)
99+
.catalog("opensearch", testSchema)
100+
.liveSettings(live)
101+
.build();
102+
103+
assertEquals(
104+
"Cluster override should win over the static default",
105+
Integer.valueOf(5),
106+
context.getSettings().getSettingValue(PPL_REX_MAX_MATCH_LIMIT));
107+
}
108+
109+
@Test
110+
public void testLiveSettingsAbsentFallsBackToStaticDefault() {
111+
// No liveSettings injected — the static default (10) wins. Confirms the
112+
// pre-bridge behavior is preserved when the REST handler doesn't supply a
113+
// live Settings instance (e.g. in test paths that build the context directly).
114+
UnifiedQueryContext context =
115+
UnifiedQueryContext.builder()
116+
.language(QueryType.PPL)
117+
.catalog("opensearch", testSchema)
118+
.build();
119+
120+
assertEquals(
121+
"Static default of 10 should win when no liveSettings are injected",
122+
Integer.valueOf(10),
123+
context.getSettings().getSettingValue(PPL_REX_MAX_MATCH_LIMIT));
124+
}
125+
126+
@Test
127+
public void testLiveSettingsNullValueFallsBackToStaticDefault() {
128+
// liveSettings injected, but getSettingValue returns null for the rex key
129+
// (i.e. the operator has not set a cluster-side override). Static default (10)
130+
// should still win — pins the {@code != null} guard in build().
131+
Settings live = stubSettings(new HashMap<>());
132+
133+
UnifiedQueryContext context =
134+
UnifiedQueryContext.builder()
135+
.language(QueryType.PPL)
136+
.catalog("opensearch", testSchema)
137+
.liveSettings(live)
138+
.build();
139+
140+
assertEquals(
141+
"Static default of 10 should win when liveSettings has no override",
142+
Integer.valueOf(10),
143+
context.getSettings().getSettingValue(PPL_REX_MAX_MATCH_LIMIT));
144+
}
145+
146+
@Test
147+
public void testLiveSettingsDoNotBridgeOtherKeys() {
148+
// Pins the rex-only scoping decision: a cluster override for QUERY_SIZE_LIMIT
149+
// (or any other key in the static map) is intentionally NOT picked up by
150+
// liveSettings. Widening the snapshot is a future scope decision; this test
151+
// surfaces accidental scope creep as a hard failure.
152+
Settings live = stubSettings(Map.of(QUERY_SIZE_LIMIT, 999));
153+
154+
UnifiedQueryContext context =
155+
UnifiedQueryContext.builder()
156+
.language(QueryType.PPL)
157+
.catalog("opensearch", testSchema)
158+
.liveSettings(live)
159+
.build();
160+
161+
assertEquals(
162+
"Static default for QUERY_SIZE_LIMIT should win — bridge is rex-only",
163+
Integer.valueOf(SysLimit.DEFAULT.querySizeLimit()),
164+
context.getSettings().getSettingValue(QUERY_SIZE_LIMIT));
165+
}
166+
167+
/** Minimal Settings stub: returns values from the supplied map; null for any other key. */
168+
private static Settings stubSettings(Map<Settings.Key, Object> values) {
169+
return new Settings() {
170+
@Override
171+
@SuppressWarnings("unchecked")
172+
public <T> T getSettingValue(Key key) {
173+
return (T) values.get(key);
174+
}
175+
176+
@Override
177+
public List<?> getSettings() {
178+
return List.copyOf(values.entrySet());
179+
}
180+
};
181+
}
182+
86183
@Test
87184
public void testContextClose() throws Exception {
88185
// Create a separate context for this test to avoid affecting other tests

0 commit comments

Comments
 (0)