|
| 1 | +package org.opensearch.ml.rest; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Rule; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.rules.ExpectedException; |
| 9 | +import org.opensearch.ml.action.stats.MLStatsNodesRequest; |
| 10 | +import org.opensearch.ml.plugin.MachineLearningPlugin; |
| 11 | +import org.opensearch.ml.stats.MLStat; |
| 12 | +import org.opensearch.ml.stats.MLStats; |
| 13 | +import org.opensearch.ml.stats.StatNames; |
| 14 | +import org.opensearch.ml.stats.suppliers.CounterSupplier; |
| 15 | +import org.opensearch.rest.RestRequest; |
| 16 | + |
| 17 | +import org.opensearch.test.OpenSearchTestCase; |
| 18 | +import org.opensearch.test.rest.FakeRestRequest; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | + |
| 28 | + |
| 29 | +public class RestStatsMLActionTests extends OpenSearchTestCase { |
| 30 | + @Rule |
| 31 | + public ExpectedException thrown= ExpectedException.none(); |
| 32 | + |
| 33 | + RestStatsMLAction restAction; |
| 34 | + MLStats mlStats; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setup() { |
| 38 | + Map<String, MLStat<?>> statMap = ImmutableMap |
| 39 | + .<String, MLStat<?>>builder() |
| 40 | + .put(StatNames.ML_EXECUTING_TASK_COUNT.getName(), new MLStat<>(false, new CounterSupplier())) |
| 41 | + .build(); |
| 42 | + mlStats = new MLStats(statMap); |
| 43 | + restAction = new RestStatsMLAction(mlStats); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testsplitCommaSeparatedParam() { |
| 48 | + Map<String, String> param = ImmutableMap |
| 49 | + .<String, String>builder() |
| 50 | + .put("nodeId", "111,222") |
| 51 | + .build(); |
| 52 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 53 | + .withMethod(RestRequest.Method.GET) |
| 54 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/") |
| 55 | + .withParams(param) |
| 56 | + .build(); |
| 57 | + Optional<String[]> nodeId = restAction.splitCommaSeparatedParam(fakeRestRequest, "nodeId"); |
| 58 | + String[] array = nodeId.get(); |
| 59 | + Assert.assertEquals(array[0], "111"); |
| 60 | + Assert.assertEquals(array[1], "222"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testIsAllStatsRequested() { |
| 65 | + List<String> requestedStats1 = new ArrayList<>(Arrays.asList("stat1", "stat2")); |
| 66 | + Assert.assertTrue(!restAction.isAllStatsRequested(requestedStats1)); |
| 67 | + List<String> requestedStats2 = new ArrayList<>(); |
| 68 | + Assert.assertTrue(restAction.isAllStatsRequested(requestedStats2)); |
| 69 | + List<String> requestedStats3 = new ArrayList<>(Arrays.asList(MLStatsNodesRequest.ALL_STATS_KEY)); |
| 70 | + Assert.assertTrue(restAction.isAllStatsRequested(requestedStats3)); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testStatsSetContainsAllStatsKey() { |
| 75 | + thrown.expect(IllegalArgumentException.class); |
| 76 | + thrown.expectMessage(MLStatsNodesRequest.ALL_STATS_KEY); |
| 77 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 78 | + .withMethod(RestRequest.Method.GET) |
| 79 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/") |
| 80 | + .build(); |
| 81 | + Set<String> validStats = new HashSet<>(); |
| 82 | + validStats.add("stat1"); |
| 83 | + validStats.add("stat2"); |
| 84 | + List<String> requestedStats = new ArrayList<>(Arrays.asList("stat1", "stat2",MLStatsNodesRequest.ALL_STATS_KEY)); |
| 85 | + restAction.getStatsToBeRetrieved(fakeRestRequest, validStats, requestedStats); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testStatsSetContainsInvalidStats() { |
| 90 | + thrown.expect(IllegalArgumentException.class); |
| 91 | + thrown.expectMessage("unrecognized"); |
| 92 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 93 | + .withMethod(RestRequest.Method.GET) |
| 94 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/") |
| 95 | + .build(); |
| 96 | + Set<String> validStats = new HashSet<>(); |
| 97 | + validStats.add("stat1"); |
| 98 | + validStats.add("stat2"); |
| 99 | + List<String> requestedStats = new ArrayList<>(Arrays.asList("stat1", "stat2","invalidStat")); |
| 100 | + restAction.getStatsToBeRetrieved(fakeRestRequest, validStats, requestedStats); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testGetRequestAllStats() { |
| 105 | + Map<String, String> param = ImmutableMap |
| 106 | + .<String, String>builder() |
| 107 | + .put("nodeId", "111,222") |
| 108 | + .put("stat", MLStatsNodesRequest.ALL_STATS_KEY) |
| 109 | + .build(); |
| 110 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 111 | + .withMethod(RestRequest.Method.GET) |
| 112 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/{stat}") |
| 113 | + .withParams(param) |
| 114 | + .build(); |
| 115 | + MLStatsNodesRequest request = restAction.getRequest(fakeRestRequest); |
| 116 | + Assert.assertEquals(request.getStatsToBeRetrieved().size(), 1); |
| 117 | + Assert.assertTrue(request.getStatsToBeRetrieved().contains(StatNames.ML_EXECUTING_TASK_COUNT.getName())); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetRequestEmptyStats() { |
| 122 | + Map<String, String> param = ImmutableMap |
| 123 | + .<String, String>builder() |
| 124 | + .put("nodeId", "111,222") |
| 125 | + .build(); |
| 126 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 127 | + .withMethod(RestRequest.Method.GET) |
| 128 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/") |
| 129 | + .withParams(param) |
| 130 | + .build(); |
| 131 | + MLStatsNodesRequest request = restAction.getRequest(fakeRestRequest); |
| 132 | + Assert.assertEquals(request.getStatsToBeRetrieved().size(), 1); |
| 133 | + Assert.assertTrue(request.getStatsToBeRetrieved().contains(StatNames.ML_EXECUTING_TASK_COUNT.getName())); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testGetRequestSpecifyStats() { |
| 138 | + Map<String, String> param = ImmutableMap |
| 139 | + .<String, String>builder() |
| 140 | + .put("nodeId", "111,222") |
| 141 | + .put("stat", StatNames.ML_EXECUTING_TASK_COUNT.getName()) |
| 142 | + .build(); |
| 143 | + FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(xContentRegistry()) |
| 144 | + .withMethod(RestRequest.Method.GET) |
| 145 | + .withPath(MachineLearningPlugin.ML_BASE_URI + "/{nodeId}/stats/{stat}") |
| 146 | + .withParams(param) |
| 147 | + .build(); |
| 148 | + MLStatsNodesRequest request = restAction.getRequest(fakeRestRequest); |
| 149 | + Assert.assertEquals(request.getStatsToBeRetrieved().size(), 1); |
| 150 | + Assert.assertTrue(request.getStatsToBeRetrieved().contains(StatNames.ML_EXECUTING_TASK_COUNT.getName())); |
| 151 | + } |
| 152 | +} |
0 commit comments