1010import static org .junit .Assert .assertTrue ;
1111import static org .mockito .Mockito .mock ;
1212
13- import java .io .ByteArrayOutputStream ;
14- import java .nio .charset .StandardCharsets ;
13+ import java .io .IOException ;
1514import org .junit .Before ;
1615import org .junit .Test ;
16+ import org .opensearch .common .io .stream .BytesStreamOutput ;
17+ import org .opensearch .common .xcontent .XContentType ;
1718import org .opensearch .core .rest .RestStatus ;
19+ import org .opensearch .core .xcontent .MediaType ;
1820import org .opensearch .core .xcontent .NamedXContentRegistry ;
21+ import org .opensearch .core .xcontent .XContentBuilder ;
1922import org .opensearch .rest .RestChannel ;
2023import org .opensearch .rest .RestRequest ;
2124import org .opensearch .rest .RestResponse ;
22- import org .opensearch .test .OpenSearchTestCase ;
2325import org .opensearch .test .rest .FakeRestRequest ;
2426import 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 }
0 commit comments