1010
1111import org .opensearch .cluster .ClusterName ;
1212import org .opensearch .cluster .ClusterState ;
13+ import org .opensearch .cluster .ClusterStateUpdateTask ;
1314import org .opensearch .cluster .metadata .Metadata ;
1415import org .opensearch .cluster .metadata .QueryGroup ;
1516import org .opensearch .cluster .service .ClusterService ;
1617import org .opensearch .common .collect .Tuple ;
1718import org .opensearch .common .settings .ClusterSettings ;
1819import org .opensearch .common .settings .Settings ;
20+ import org .opensearch .core .action .ActionListener ;
21+ import org .opensearch .plugin .wlm .QueryGroupTestUtils ;
22+ import org .opensearch .plugin .wlm .action .CreateQueryGroupResponse ;
1923import org .opensearch .search .ResourceType ;
2024import org .opensearch .test .OpenSearchTestCase ;
2125import org .opensearch .threadpool .ThreadPool ;
2630import java .util .List ;
2731import java .util .Map ;
2832
33+ import org .mockito .ArgumentCaptor ;
34+
2935import static org .opensearch .cluster .metadata .QueryGroup .builder ;
3036import static org .opensearch .plugin .wlm .QueryGroupTestUtils .MEMORY_STRING ;
3137import static org .opensearch .plugin .wlm .QueryGroupTestUtils .MONITOR_STRING ;
3440import static org .opensearch .plugin .wlm .QueryGroupTestUtils ._ID_ONE ;
3541import static org .opensearch .plugin .wlm .QueryGroupTestUtils ._ID_TWO ;
3642import static org .opensearch .plugin .wlm .QueryGroupTestUtils .assertEqualQueryGroups ;
43+ import static org .opensearch .plugin .wlm .QueryGroupTestUtils .clusterSettings ;
3744import static org .opensearch .plugin .wlm .QueryGroupTestUtils .clusterSettingsSet ;
3845import static org .opensearch .plugin .wlm .QueryGroupTestUtils .preparePersistenceServiceSetup ;
3946import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupList ;
4047import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupOne ;
4148import static org .opensearch .plugin .wlm .QueryGroupTestUtils .queryGroupTwo ;
4249import static org .opensearch .plugin .wlm .service .QueryGroupPersistenceService .QUERY_GROUP_COUNT_SETTING_NAME ;
50+ import static org .opensearch .plugin .wlm .service .QueryGroupPersistenceService .SOURCE ;
51+ import static org .mockito .ArgumentMatchers .any ;
52+ import static org .mockito .ArgumentMatchers .eq ;
53+ import static org .mockito .Mockito .anyString ;
54+ import static org .mockito .Mockito .doAnswer ;
4355import static org .mockito .Mockito .mock ;
56+ import static org .mockito .Mockito .times ;
57+ import static org .mockito .Mockito .verify ;
4458
4559public class QueryGroupPersistenceServiceTests extends OpenSearchTestCase {
4660
4761 /**
48- * Test case to validate the creation logic of a single QueryGroup
62+ * Test case to validate the creation logic of a QueryGroup
4963 */
5064 public void testCreateQueryGroup () {
5165 Tuple <QueryGroupPersistenceService , ClusterState > setup = preparePersistenceServiceSetup (new HashMap <>());
@@ -136,6 +150,9 @@ public void testCreateQueryGroupOverflowCount() {
136150 assertThrows (RuntimeException .class , () -> queryGroupPersistenceService1 .saveQueryGroupInClusterState (toCreate , clusterState ));
137151 }
138152
153+ /**
154+ * Tests the invalid value of {@code node.query_group.max_count}
155+ */
139156 public void testInvalidMaxQueryGroupCount () {
140157 Settings settings = Settings .builder ().put (QUERY_GROUP_COUNT_SETTING_NAME , 2 ).build ();
141158 ClusterSettings clusterSettings = new ClusterSettings (settings , clusterSettingsSet ());
@@ -147,4 +164,84 @@ public void testInvalidMaxQueryGroupCount() {
147164 );
148165 assertThrows (IllegalArgumentException .class , () -> queryGroupPersistenceService .setMaxQueryGroupCount (-1 ));
149166 }
167+
168+ /**
169+ * Tests the valid value of {@code node.query_group.max_count}
170+ */
171+ public void testValidMaxSandboxCountSetting () {
172+ Settings settings = Settings .builder ().put (QUERY_GROUP_COUNT_SETTING_NAME , 100 ).build ();
173+ ClusterService clusterService = new ClusterService (settings , clusterSettings (), mock (ThreadPool .class ));
174+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
175+ clusterService ,
176+ settings ,
177+ clusterSettings ()
178+ );
179+ queryGroupPersistenceService .setMaxQueryGroupCount (50 );
180+ assertEquals (50 , queryGroupPersistenceService .getMaxQueryGroupCount ());
181+ }
182+
183+ /**
184+ * Tests PersistInClusterStateMetadata function
185+ */
186+ public void testPersistInClusterStateMetadata () {
187+ ClusterService clusterService = mock (ClusterService .class );
188+ @ SuppressWarnings ("unchecked" )
189+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
190+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
191+ clusterService ,
192+ QueryGroupTestUtils .settings (),
193+ clusterSettings ()
194+ );
195+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
196+ verify (clusterService ).submitStateUpdateTask (eq (SOURCE ), any ());
197+ }
198+
199+ /**
200+ * Tests PersistInClusterStateMetadata function with inner functions
201+ */
202+ public void testPersistInClusterStateMetadataInner () {
203+ ClusterService clusterService = mock (ClusterService .class );
204+ @ SuppressWarnings ("unchecked" )
205+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
206+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
207+ clusterService ,
208+ QueryGroupTestUtils .settings (),
209+ clusterSettings ()
210+ );
211+ ArgumentCaptor <ClusterStateUpdateTask > captor = ArgumentCaptor .forClass (ClusterStateUpdateTask .class );
212+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
213+ verify (clusterService , times (1 )).submitStateUpdateTask (eq (SOURCE ), captor .capture ());
214+ ClusterStateUpdateTask capturedTask = captor .getValue ();
215+ assertEquals (queryGroupPersistenceService .createQueryGroupThrottlingKey , capturedTask .getClusterManagerThrottlingKey ());
216+
217+ doAnswer (invocation -> {
218+ ClusterStateUpdateTask task = invocation .getArgument (1 );
219+ task .clusterStateProcessed (SOURCE , mock (ClusterState .class ), mock (ClusterState .class ));
220+ return null ;
221+ }).when (clusterService ).submitStateUpdateTask (anyString (), any ());
222+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
223+ verify (listener ).onResponse (any (CreateQueryGroupResponse .class ));
224+ }
225+
226+ /**
227+ * Tests PersistInClusterStateMetadata function with failure
228+ */
229+ public void testPersistInClusterStateMetadataFailure () {
230+ ClusterService clusterService = mock (ClusterService .class );
231+ @ SuppressWarnings ("unchecked" )
232+ ActionListener <CreateQueryGroupResponse > listener = mock (ActionListener .class );
233+ QueryGroupPersistenceService queryGroupPersistenceService = new QueryGroupPersistenceService (
234+ clusterService ,
235+ QueryGroupTestUtils .settings (),
236+ clusterSettings ()
237+ );
238+ doAnswer (invocation -> {
239+ ClusterStateUpdateTask task = invocation .getArgument (1 );
240+ Exception exception = new RuntimeException ("Test Exception" );
241+ task .onFailure (SOURCE , exception );
242+ return null ;
243+ }).when (clusterService ).submitStateUpdateTask (anyString (), any ());
244+ queryGroupPersistenceService .persistInClusterStateMetadata (queryGroupOne , listener );
245+ verify (listener ).onFailure (any (RuntimeException .class ));
246+ }
150247}
0 commit comments