diff --git a/plugins/workload-management/src/javaRestTest/java/org/opensearch/rest/WorkloadManagementRestIT.java b/plugins/workload-management/src/javaRestTest/java/org/opensearch/rest/WorkloadManagementRestIT.java index e49edf5955708..7e1d61e57b6f7 100644 --- a/plugins/workload-management/src/javaRestTest/java/org/opensearch/rest/WorkloadManagementRestIT.java +++ b/plugins/workload-management/src/javaRestTest/java/org/opensearch/rest/WorkloadManagementRestIT.java @@ -14,42 +14,36 @@ import org.opensearch.client.ResponseException; import org.opensearch.test.rest.OpenSearchRestTestCase; +import java.io.IOException; + public class WorkloadManagementRestIT extends OpenSearchRestTestCase { public void testCreate() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - client().performRequest(new Request("DELETE", "_wlm/query_group/analytics")); + performOperation("DELETE", "_wlm/query_group/analytics", null); } public void testMultipleCreate() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics2", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics2", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - Request request2 = new Request("PUT", "_wlm/query_group"); - request2.setJsonEntity(getCreateJson("users", "soft", 0.2, 0.1)); - Response response2 = client().performRequest(request2); + Response response2 = performOperation("PUT", "_wlm/query_group", getCreateJson("users", "soft", 0.2, 0.1)); assertEquals(response2.getStatusLine().getStatusCode(), 200); - Request request3 = new Request("PUT", "_wlm/query_group"); - request3.setJsonEntity(getCreateJson("users2", "soft", 0.41, 0.71)); - assertThrows(ResponseException.class, () -> client().performRequest(request3)); - client().performRequest(new Request("DELETE", "_wlm/query_group/analytics2")); - client().performRequest(new Request("DELETE", "_wlm/query_group/users")); + assertThrows( + ResponseException.class, + () -> performOperation("PUT", "_wlm/query_group", getCreateJson("users2", "soft", 0.41, 0.71)) + ); + performOperation("DELETE", "_wlm/query_group/analytics2", null); + performOperation("DELETE", "_wlm/query_group/users", null); } public void testGet() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics3", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics3", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - Request request2 = new Request("GET", "_wlm/query_group/analytics3"); - Response response2 = client().performRequest(request2); + Response response2 = performOperation("GET", "_wlm/query_group/analytics3", null); assertEquals(response2.getStatusLine().getStatusCode(), 200); String responseBody2 = EntityUtils.toString(response2.getEntity()); assertTrue(responseBody2.contains("\"name\":\"analytics3\"")); @@ -57,36 +51,26 @@ public void testGet() throws Exception { assertTrue(responseBody2.contains("\"cpu\":0.4")); assertTrue(responseBody2.contains("\"memory\":0.2")); - Request request3 = new Request("GET", "_wlm/query_group/analytics97"); - assertThrows(ResponseException.class, () -> client().performRequest(request3)); - client().performRequest(new Request("DELETE", "_wlm/query_group/analytics3")); + assertThrows(ResponseException.class, () -> performOperation("GET", "_wlm/query_group/analytics97", null)); + performOperation("DELETE", "_wlm/query_group/analytics3", null); } public void testDelete() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics4", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics4", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - Request request2 = new Request("DELETE", "_wlm/query_group/analytics4"); - Response response2 = client().performRequest(request2); + Response response2 = performOperation("DELETE", "_wlm/query_group/analytics4", null); assertEquals(response2.getStatusLine().getStatusCode(), 200); - String responseBody2 = EntityUtils.toString(response2.getEntity()); - assertTrue(responseBody2.contains("\"acknowledged\":true")); + assertTrue(EntityUtils.toString(response2.getEntity()).contains("\"acknowledged\":true")); - Request request3 = new Request("DELETE", "_wlm/query_group/analytics99"); - assertThrows(ResponseException.class, () -> client().performRequest(request3)); + assertThrows(ResponseException.class, () -> performOperation("DELETE", "_wlm/query_group/analytics99", null)); } public void testUpdate() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics5", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics5", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - Request request2 = new Request("PUT", "_wlm/query_group/analytics5"); - request2.setJsonEntity(getUpdateJson("monitor", 0.41, 0.21)); - Response response2 = client().performRequest(request2); + Response response2 = performOperation("PUT", "_wlm/query_group/analytics5", getUpdateJson("monitor", 0.41, 0.21)); assertEquals(response2.getStatusLine().getStatusCode(), 200); String responseBody2 = EntityUtils.toString(response2.getEntity()); assertTrue(responseBody2.contains("\"name\":\"analytics5\"")); @@ -94,31 +78,28 @@ public void testUpdate() throws Exception { assertTrue(responseBody2.contains("\"cpu\":0.41")); assertTrue(responseBody2.contains("\"memory\":0.21")); - Request request3 = new Request("PUT", "_wlm/query_group/analytics5"); - request3.setJsonEntity( - "{\n" + " \"resource_limits\": {\n" + " \"cpu\" : 1.1,\n" + " \"memory\" : -0.1\n" + " }\n" + "}'" + String json = "{\n" + + " \"resource_limits\": {\n" + + " \"cpu\" : 1.1,\n" + + " \"memory\" : -0.1\n" + + " }\n" + + "}'"; + assertThrows(ResponseException.class, () -> performOperation("PUT", "_wlm/query_group/analytics5", json)); + assertThrows( + ResponseException.class, + () -> performOperation("PUT", "_wlm/query_group/analytics98", getUpdateJson("monitor", 0.43, 0.23)) ); - assertThrows(ResponseException.class, () -> client().performRequest(request3)); - - Request request4 = new Request("PUT", "_wlm/query_group/analytics98"); - request4.setJsonEntity(getUpdateJson("monitor", 0.43, 0.23)); - assertThrows(ResponseException.class, () -> client().performRequest(request4)); - client().performRequest(new Request("DELETE", "_wlm/query_group/analytics5")); + performOperation("DELETE", "_wlm/query_group/analytics5", null); } public void testCRUD() throws Exception { - Request request = new Request("PUT", "_wlm/query_group"); - request.setJsonEntity(getCreateJson("analytics6", "enforced", 0.4, 0.2)); - Response response = client().performRequest(request); + Response response = performOperation("PUT", "_wlm/query_group", getCreateJson("analytics6", "enforced", 0.4, 0.2)); assertEquals(response.getStatusLine().getStatusCode(), 200); - Request request1 = new Request("PUT", "_wlm/query_group/analytics6"); - request1.setJsonEntity(getUpdateJson("monitor", 0.41, 0.21)); - Response response1 = client().performRequest(request1); + Response response1 = performOperation("PUT", "_wlm/query_group/analytics6", getUpdateJson("monitor", 0.41, 0.21)); assertEquals(response1.getStatusLine().getStatusCode(), 200); - Request request2 = new Request("GET", "_wlm/query_group/analytics6"); - Response response2 = client().performRequest(request2); + Response response2 = performOperation("GET", "_wlm/query_group/analytics6", null); assertEquals(response2.getStatusLine().getStatusCode(), 200); String responseBody2 = EntityUtils.toString(response2.getEntity()); assertTrue(responseBody2.contains("\"name\":\"analytics6\"")); @@ -126,29 +107,26 @@ public void testCRUD() throws Exception { assertTrue(responseBody2.contains("\"cpu\":0.41")); assertTrue(responseBody2.contains("\"memory\":0.21")); - Request request3 = new Request("PUT", "_wlm/query_group"); - request3.setJsonEntity(getCreateJson("users3", "monitor", 0.6, 0.8)); - assertThrows(ResponseException.class, () -> client().performRequest(request3)); + assertThrows( + ResponseException.class, + () -> performOperation("PUT", "_wlm/query_group", getCreateJson("users3", "monitor", 0.6, 0.8)) + ); - Request request4 = new Request("PUT", "_wlm/query_group"); - request4.setJsonEntity(getCreateJson("users3", "monitor", 0.59, 0.79)); - Response response4 = client().performRequest(request4); + Response response4 = performOperation("PUT", "_wlm/query_group", getCreateJson("users3", "monitor", 0.59, 0.79)); assertEquals(response4.getStatusLine().getStatusCode(), 200); - Request request5 = new Request("DELETE", "_wlm/query_group/analytics6"); - Response response5 = client().performRequest(request5); + Response response5 = performOperation("DELETE", "_wlm/query_group/analytics6", null); assertEquals(response5.getStatusLine().getStatusCode(), 200); String responseBody5 = EntityUtils.toString(response5.getEntity()); assertTrue(responseBody5.contains("\"acknowledged\":true")); - Request request6 = new Request("GET", "_wlm/query_group/"); - Response response6 = client().performRequest(request6); + Response response6 = performOperation("GET", "_wlm/query_group", null); assertEquals(response6.getStatusLine().getStatusCode(), 200); String responseBody6 = EntityUtils.toString(response6.getEntity()); assertTrue(responseBody6.contains("\"query_groups\"")); assertTrue(responseBody6.contains("\"users3\"")); assertFalse(responseBody6.contains("\"analytics6\"")); - client().performRequest(new Request("DELETE", "_wlm/query_group/users3")); + performOperation("DELETE", "_wlm/query_group/users3", null); } static String getCreateJson(String name, String resiliencyMode, double cpu, double memory) { @@ -185,4 +163,12 @@ static String getUpdateJson(String resiliencyMode, double cpu, double memory) { + " }\n" + "}"; } + + Response performOperation(String method, String uriPath, String json) throws IOException { + Request request = new Request(method, uriPath); + if (json != null) { + request.setJsonEntity(json); + } + return client().performRequest(request); + } }