Skip to content

Commit 2d84578

Browse files
committed
modify tests
Signed-off-by: Eric Wei <mengwei.eric@gmail.com>
1 parent 5f0a84c commit 2d84578

2 files changed

Lines changed: 149 additions & 42 deletions

File tree

plugin/src/test/java/org/opensearch/sql/plugin/rest/RestPPLGrammarActionTest.java

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,29 @@
1010
import static org.junit.Assert.assertTrue;
1111
import static org.mockito.Mockito.mock;
1212

13-
import java.io.ByteArrayOutputStream;
14-
import java.nio.charset.StandardCharsets;
13+
import java.io.IOException;
1514
import org.junit.Before;
1615
import org.junit.Test;
16+
import org.opensearch.common.io.stream.BytesStreamOutput;
17+
import org.opensearch.common.xcontent.XContentType;
1718
import org.opensearch.core.rest.RestStatus;
19+
import org.opensearch.core.xcontent.MediaType;
1820
import org.opensearch.core.xcontent.NamedXContentRegistry;
21+
import org.opensearch.core.xcontent.XContentBuilder;
1922
import org.opensearch.rest.RestChannel;
2023
import org.opensearch.rest.RestRequest;
2124
import org.opensearch.rest.RestResponse;
22-
import org.opensearch.test.OpenSearchTestCase;
2325
import org.opensearch.test.rest.FakeRestRequest;
2426
import org.opensearch.transport.client.node.NodeClient;
2527

2628
/** Unit tests for {@link RestPPLGrammarAction}. */
27-
public class RestPPLGrammarActionTest extends OpenSearchTestCase {
29+
public class RestPPLGrammarActionTest {
2830

2931
private RestPPLGrammarAction action;
3032
private NodeClient client;
3133

3234
@Before
33-
public void setUp() throws Exception {
34-
super.setUp();
35+
public void setUp() {
3536
action = new RestPPLGrammarAction();
3637
client = mock(NodeClient.class);
3738
}
@@ -57,19 +58,22 @@ public void testGetGrammar_ReturnsBundle() throws Exception {
5758
.build();
5859

5960
MockRestChannel channel = new MockRestChannel(request, true);
60-
action.prepareRequest(request, client).accept(channel);
61+
action.handleRequest(request, channel, client);
6162

6263
RestResponse response = channel.getResponse();
6364
assertNotNull("Response should not be null", response);
6465
assertEquals("Should return 200 OK", RestStatus.OK, response.status());
6566

66-
String content = new String(response.content().array(), StandardCharsets.UTF_8);
67-
assertTrue("Should contain bundleVersion", content.contains("\"bundleVersion\":"));
68-
assertTrue("Should contain grammarHash", content.contains("\"grammarHash\":"));
69-
assertTrue("Should contain lexerSerializedATN", content.contains("\"lexerSerializedATN\":"));
70-
assertTrue("Should contain parserSerializedATN", content.contains("\"parserSerializedATN\":"));
71-
assertTrue("Should contain literalNames", content.contains("\"literalNames\":"));
72-
assertTrue("Should contain symbolicNames", content.contains("\"symbolicNames\":"));
67+
String content = response.content().utf8ToString();
68+
assertTrue(content.contains("\"bundleVersion\":\"1.0\""));
69+
assertTrue(content.contains("\"grammarHash\":\"sha256:"));
70+
assertTrue(content.contains("\"startRuleIndex\":0"));
71+
assertTrue(content.contains("\"lexerSerializedATN\":"));
72+
assertTrue(content.contains("\"parserSerializedATN\":"));
73+
assertTrue(content.contains("\"lexerRuleNames\":"));
74+
assertTrue(content.contains("\"parserRuleNames\":"));
75+
assertTrue(content.contains("\"literalNames\":"));
76+
assertTrue(content.contains("\"symbolicNames\":"));
7377
}
7478

7579
@Test
@@ -79,30 +83,20 @@ public void testGetGrammar_BundleIsCached() throws Exception {
7983
.withMethod(RestRequest.Method.GET)
8084
.withPath("/_plugins/_ppl/_grammar")
8185
.build();
82-
8386
MockRestChannel channel1 = new MockRestChannel(request1, true);
84-
long startTime1 = System.currentTimeMillis();
85-
action.prepareRequest(request1, client).accept(channel1);
86-
long elapsed1 = System.currentTimeMillis() - startTime1;
87+
action.handleRequest(request1, channel1, client);
8788

8889
FakeRestRequest request2 =
8990
new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
9091
.withMethod(RestRequest.Method.GET)
9192
.withPath("/_plugins/_ppl/_grammar")
9293
.build();
93-
9494
MockRestChannel channel2 = new MockRestChannel(request2, true);
95-
long startTime2 = System.currentTimeMillis();
96-
action.prepareRequest(request2, client).accept(channel2);
97-
long elapsed2 = System.currentTimeMillis() - startTime2;
98-
99-
assertTrue(
100-
"Second request should be faster due to caching (elapsed1="
101-
+ elapsed1
102-
+ "ms, elapsed2="
103-
+ elapsed2
104-
+ "ms)",
105-
elapsed2 < elapsed1 || elapsed2 < 50); // Allow some variance
95+
action.handleRequest(request2, channel2, client);
96+
97+
String content1 = channel1.getResponse().content().utf8ToString();
98+
String content2 = channel2.getResponse().content().utf8ToString();
99+
assertEquals("Consecutive requests should return identical content", content1, content2);
106100
}
107101

108102
@Test
@@ -112,9 +106,8 @@ public void testInvalidateCache() throws Exception {
112106
.withMethod(RestRequest.Method.GET)
113107
.withPath("/_plugins/_ppl/_grammar")
114108
.build();
115-
116109
MockRestChannel channel1 = new MockRestChannel(request1, true);
117-
action.prepareRequest(request1, client).accept(channel1);
110+
action.handleRequest(request1, channel1, client);
118111
assertEquals(RestStatus.OK, channel1.getResponse().status());
119112

120113
action.invalidateCache();
@@ -124,10 +117,13 @@ public void testInvalidateCache() throws Exception {
124117
.withMethod(RestRequest.Method.GET)
125118
.withPath("/_plugins/_ppl/_grammar")
126119
.build();
127-
128120
MockRestChannel channel2 = new MockRestChannel(request2, true);
129-
action.prepareRequest(request2, client).accept(channel2);
121+
action.handleRequest(request2, channel2, client);
130122
assertEquals(RestStatus.OK, channel2.getResponse().status());
123+
124+
String content1 = channel1.getResponse().content().utf8ToString();
125+
String content2 = channel2.getResponse().content().utf8ToString();
126+
assertEquals("Grammar hash should be identical after cache invalidation and rebuild", content1, content2);
131127
}
132128

133129
/** Mock RestChannel to capture responses */
@@ -161,23 +157,34 @@ public boolean detailedErrorsEnabled() {
161157
}
162158

163159
@Override
164-
public org.opensearch.core.xcontent.XContentBuilder newBuilder() {
165-
return null;
160+
public boolean detailedErrorStackTraceEnabled() {
161+
return false;
166162
}
167163

168164
@Override
169-
public org.opensearch.core.xcontent.XContentBuilder newErrorBuilder() {
170-
return null;
165+
public XContentBuilder newBuilder() throws IOException {
166+
return XContentBuilder.builder(XContentType.JSON.xContent());
171167
}
172168

173169
@Override
174-
public org.opensearch.core.xcontent.XContentBuilder newBuilder(
175-
org.opensearch.core.xcontent.MediaType mediaType, boolean useFiltering) {
176-
return null;
170+
public XContentBuilder newErrorBuilder() throws IOException {
171+
return XContentBuilder.builder(XContentType.JSON.xContent());
172+
}
173+
174+
@Override
175+
public XContentBuilder newBuilder(MediaType mediaType, boolean useFiltering) throws IOException {
176+
return XContentBuilder.builder(XContentType.JSON.xContent());
177+
}
178+
179+
@Override
180+
public XContentBuilder newBuilder(
181+
MediaType requestContentType, MediaType responseContentType, boolean useFiltering)
182+
throws IOException {
183+
return XContentBuilder.builder(XContentType.JSON.xContent());
177184
}
178185

179186
@Override
180-
public ByteArrayOutputStream bytesOutput() {
187+
public BytesStreamOutput bytesOutput() {
181188
return null;
182189
}
183190
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.ppl.autocomplete;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
import org.opensearch.sql.executor.autocomplete.GrammarBundle;
15+
16+
public class PPLGrammarBundleBuilderTest {
17+
18+
private static GrammarBundle bundle;
19+
20+
@BeforeClass
21+
public static void buildBundle() {
22+
bundle = new PPLGrammarBundleBuilder().build();
23+
}
24+
25+
@Test
26+
public void bundleIsNotNull() {
27+
assertNotNull(bundle);
28+
}
29+
30+
@Test
31+
public void bundleVersionIsSet() {
32+
assertEquals("1.0", bundle.getBundleVersion());
33+
}
34+
35+
@Test
36+
public void grammarHashHasExpectedFormat() {
37+
String hash = bundle.getGrammarHash();
38+
assertNotNull(hash);
39+
assertTrue("grammarHash should start with 'sha256:'", hash.startsWith("sha256:"));
40+
assertTrue("grammarHash should be 71 chars (sha256: + 64 hex)", hash.length() == 71);
41+
}
42+
43+
@Test
44+
public void startRuleIndexIsZero() {
45+
assertEquals(0, bundle.getStartRuleIndex());
46+
}
47+
48+
@Test
49+
public void lexerATNIsNonEmpty() {
50+
assertNotNull(bundle.getLexerSerializedATN());
51+
assertTrue(bundle.getLexerSerializedATN().length > 0);
52+
}
53+
54+
@Test
55+
public void parserATNIsNonEmpty() {
56+
assertNotNull(bundle.getParserSerializedATN());
57+
assertTrue(bundle.getParserSerializedATN().length > 0);
58+
}
59+
60+
@Test
61+
public void lexerRuleNamesAreNonEmpty() {
62+
assertNotNull(bundle.getLexerRuleNames());
63+
assertTrue(bundle.getLexerRuleNames().length > 0);
64+
}
65+
66+
@Test
67+
public void parserRuleNamesAreNonEmpty() {
68+
assertNotNull(bundle.getParserRuleNames());
69+
assertTrue(bundle.getParserRuleNames().length > 0);
70+
}
71+
72+
@Test
73+
public void channelNamesAreNonEmpty() {
74+
assertNotNull(bundle.getChannelNames());
75+
assertTrue(bundle.getChannelNames().length > 0);
76+
}
77+
78+
@Test
79+
public void modeNamesAreNonEmpty() {
80+
assertNotNull(bundle.getModeNames());
81+
assertTrue(bundle.getModeNames().length > 0);
82+
}
83+
84+
@Test
85+
public void vocabularyIsNonEmpty() {
86+
assertNotNull(bundle.getLiteralNames());
87+
assertNotNull(bundle.getSymbolicNames());
88+
assertTrue(bundle.getLiteralNames().length > 0);
89+
assertTrue(bundle.getSymbolicNames().length > 0);
90+
}
91+
92+
@Test
93+
public void buildIsDeterministic() {
94+
GrammarBundle second = new PPLGrammarBundleBuilder().build();
95+
assertEquals(
96+
"Two builds of the same grammar should produce the same hash",
97+
bundle.getGrammarHash(),
98+
second.getGrammarHash());
99+
}
100+
}

0 commit comments

Comments
 (0)