Skip to content

Commit

Permalink
add UT coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Ruirui Zhang <mariazrr@amazon.com>
  • Loading branch information
ruai0511 committed Aug 22, 2024
1 parent ebb2397 commit 6b1be98
Showing 1 changed file with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,33 @@

package org.opensearch.plugin.wlm.rest;

import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.CheckedConsumer;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.plugin.wlm.action.DeleteQueryGroupAction;
import org.opensearch.plugin.wlm.action.DeleteQueryGroupRequest;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

import java.util.List;

import org.mockito.ArgumentCaptor;

import static org.opensearch.rest.RestRequest.Method.DELETE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

public class RestDeleteQueryGroupActionTests extends OpenSearchTestCase {

/**
* Test case to validate the construction for RestDeleteQueryGroupAction
*/
Expand All @@ -30,4 +48,35 @@ public void testConstruction() {
assertEquals(DELETE, route.getMethod());
assertEquals("_wlm/query_group/{name}", route.getPath());
}

/**
* Test case to validate the prepareRequest logic for RestDeleteQueryGroupAction
*/
@SuppressWarnings("unchecked")
public void testPrepareRequest() throws Exception {
RestDeleteQueryGroupAction restDeleteQueryGroupAction = new RestDeleteQueryGroupAction();
NodeClient nodeClient = mock(NodeClient.class);
RestRequest realRequest = new FakeRestRequest();
RestRequest spyRequest = spy(realRequest);

doReturn(TimeValue.timeValueSeconds(30)).when(spyRequest).paramAsTime(eq("cluster_manager_timeout"), any(TimeValue.class));
doReturn(TimeValue.timeValueSeconds(60)).when(spyRequest).paramAsTime(eq("timeout"), any(TimeValue.class));

CheckedConsumer<RestChannel, Exception> consumer = restDeleteQueryGroupAction.prepareRequest(spyRequest, nodeClient);
assertNotNull(consumer);
ArgumentCaptor<DeleteQueryGroupRequest> requestCaptor = ArgumentCaptor.forClass(DeleteQueryGroupRequest.class);
ArgumentCaptor<RestToXContentListener<AcknowledgedResponse>> listenerCaptor = ArgumentCaptor.forClass(RestToXContentListener.class);
doNothing().when(nodeClient).execute(eq(DeleteQueryGroupAction.INSTANCE), requestCaptor.capture(), listenerCaptor.capture());

consumer.accept(mock(RestChannel.class));
DeleteQueryGroupRequest capturedRequest = requestCaptor.getValue();
assertEquals(null, capturedRequest.getName());
assertEquals(TimeValue.timeValueSeconds(30), capturedRequest.clusterManagerNodeTimeout());
assertEquals(TimeValue.timeValueSeconds(60), capturedRequest.timeout());
verify(nodeClient).execute(
eq(DeleteQueryGroupAction.INSTANCE),
any(DeleteQueryGroupRequest.class),
any(RestToXContentListener.class)
);
}
}

0 comments on commit 6b1be98

Please sign in to comment.