56002
|
1 |
import com.oracle.jmx.remote.rest.http.HttpResponse;
|
|
2 |
import com.oracle.jmx.remote.rest.http.HttpUtil;
|
|
3 |
import com.oracle.jmx.remote.rest.json.JSONArray;
|
|
4 |
import com.oracle.jmx.remote.rest.json.JSONElement;
|
|
5 |
import com.oracle.jmx.remote.rest.json.JSONObject;
|
|
6 |
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
|
|
7 |
import com.oracle.jmx.remote.rest.json.parser.JSONParser;
|
|
8 |
import com.oracle.jmx.remote.rest.mapper.JSONMapper;
|
|
9 |
import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
|
55994
|
10 |
import org.testng.Assert;
|
|
11 |
import org.testng.annotations.AfterClass;
|
|
12 |
import org.testng.annotations.BeforeClass;
|
|
13 |
import org.testng.annotations.DataProvider;
|
|
14 |
import org.testng.annotations.Test;
|
|
15 |
|
56002
|
16 |
import javax.management.*;
|
|
17 |
import javax.management.remote.*;
|
|
18 |
import javax.management.remote.rest.PlatformRestAdapter;
|
|
19 |
import java.io.*;
|
|
20 |
import java.lang.management.ManagementFactory;
|
|
21 |
import java.net.HttpURLConnection;
|
|
22 |
import java.net.URL;
|
|
23 |
import java.util.*;
|
|
24 |
import java.util.stream.Collectors;
|
|
25 |
import java.util.stream.Stream;
|
|
26 |
|
56003
|
27 |
/* @test
|
|
28 |
* @summary Unit tests for Rest adapter
|
|
29 |
* @library /test/lib
|
|
30 |
* @modules java.management.rest/com.oracle.jmx.remote.rest.http
|
|
31 |
* java.management.rest/com.oracle.jmx.remote.rest.json
|
|
32 |
* java.management.rest/com.oracle.jmx.remote.rest.json.parser
|
|
33 |
* java.management.rest/com.oracle.jmx.remote.rest.mapper
|
|
34 |
* @build RestAdapterTest
|
|
35 |
* @run testng/othervm RestAdapterTest
|
|
36 |
*/
|
|
37 |
|
55994
|
38 |
@Test
|
|
39 |
public class RestAdapterTest {
|
|
40 |
|
56002
|
41 |
private JMXConnectorServer cs;
|
|
42 |
private String restUrl = "";
|
|
43 |
JMXConnector connector;
|
55994
|
44 |
|
56002
|
45 |
@BeforeClass
|
|
46 |
public void setupServers() throws IOException {
|
|
47 |
MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
|
|
48 |
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
|
|
49 |
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, platformMBeanServer);
|
|
50 |
cs.start();
|
|
51 |
JMXServiceURL addr = cs.getAddress();
|
|
52 |
connector = JMXConnectorFactory.connect(addr, null);
|
|
53 |
|
|
54 |
String testSrcRoot = System.getProperty("test.src") + File.separator;
|
|
55 |
String configFile = testSrcRoot + "mgmt.properties";
|
|
56 |
File f = new File(configFile);
|
|
57 |
Properties properties = null;
|
55994
|
58 |
if (f.exists()) {
|
56002
|
59 |
properties = new Properties();
|
|
60 |
properties.load(new FileInputStream(f));
|
55994
|
61 |
}
|
56002
|
62 |
|
|
63 |
PlatformRestAdapter.init(properties);
|
|
64 |
restUrl = PlatformRestAdapter.getBaseURL();
|
|
65 |
}
|
55994
|
66 |
|
56002
|
67 |
@AfterClass
|
|
68 |
public void tearDownServers() throws IOException {
|
|
69 |
connector.close();
|
|
70 |
cs.stop();
|
|
71 |
PlatformRestAdapter.stop();
|
|
72 |
}
|
|
73 |
|
|
74 |
private Set<String> rmiGetAllMBeans() {
|
|
75 |
try {
|
|
76 |
MBeanServerConnection mBeanServer = connector.getMBeanServerConnection();
|
|
77 |
Set<ObjectInstance> objectInstances = mBeanServer.queryMBeans(null, null);
|
|
78 |
return objectInstances.stream().map(a -> a.getObjectName().toString()).collect(Collectors.toSet());
|
|
79 |
} catch (Exception ex) {
|
|
80 |
throw new RuntimeException(ex);
|
55994
|
81 |
}
|
|
82 |
}
|
|
83 |
|
56002
|
84 |
private Map<String, Object> rmiGetAttributes(String name) {
|
|
85 |
try {
|
|
86 |
MBeanServerConnection mBeanServer = connector.getMBeanServerConnection();
|
|
87 |
ObjectName objectName = new ObjectName(name);
|
|
88 |
MBeanInfo mInfo = mBeanServer.getMBeanInfo(objectName);
|
|
89 |
String[] attrs = Stream.of(mInfo.getAttributes())
|
|
90 |
.map(MBeanAttributeInfo::getName)
|
|
91 |
.toArray(String[]::new);
|
|
92 |
|
|
93 |
Map<String, Object> result = new LinkedHashMap<>();
|
|
94 |
AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs);
|
|
95 |
List<String> missingAttrs = new ArrayList<>(Arrays.asList(attrs));
|
|
96 |
attrVals.asList().forEach(a -> {
|
|
97 |
missingAttrs.remove(a.getName());
|
|
98 |
result.put(a.getName(), a.getValue());
|
|
99 |
});
|
55994
|
100 |
|
56002
|
101 |
for (String attr : missingAttrs) {
|
|
102 |
try {
|
|
103 |
mBeanServer.getAttribute(objectName, attr);
|
|
104 |
result.put(attr, "< Error: No such attribute >");
|
|
105 |
} catch (RuntimeException ex) {
|
|
106 |
if (ex.getCause() instanceof UnsupportedOperationException) {
|
|
107 |
result.put(attr, "< Attribute not supported >");
|
|
108 |
} else if (ex.getCause() instanceof IllegalArgumentException) {
|
|
109 |
result.put(attr, "< Invalid attributes >");
|
|
110 |
}
|
|
111 |
} catch (AttributeNotFoundException e) {
|
|
112 |
result.put(attr, "< Attribute not found >");
|
|
113 |
}
|
|
114 |
}
|
|
115 |
return result;
|
|
116 |
} catch (Exception ex) {
|
|
117 |
throw new RuntimeException(ex);
|
55994
|
118 |
}
|
|
119 |
}
|
|
120 |
|
56002
|
121 |
private Set<String> rmiGetOperations(String name) {
|
|
122 |
try {
|
|
123 |
MBeanServerConnection mBeanServer = connector.getMBeanServerConnection();
|
|
124 |
ObjectName objectName = new ObjectName(name);
|
|
125 |
MBeanOperationInfo[] operationInfos = mBeanServer.getMBeanInfo(objectName).getOperations();
|
|
126 |
Set<String> rmiOps = Stream.of(operationInfos)
|
|
127 |
.map(MBeanFeatureInfo::getName).collect(Collectors.toSet());
|
|
128 |
return rmiOps;
|
|
129 |
} catch (Exception ex) {
|
|
130 |
throw new RuntimeException(ex);
|
55994
|
131 |
}
|
|
132 |
}
|
|
133 |
|
56002
|
134 |
private Set<String> restGetAllMBeans() {
|
|
135 |
try {
|
|
136 |
String url = restUrl + "/platform/mbeans";
|
|
137 |
Set<String> mbeanNames = new HashSet<>();
|
|
138 |
do {
|
|
139 |
HttpResponse httpResponse = executeHttpRequest(url);
|
|
140 |
if (httpResponse.getCode() != 200)
|
|
141 |
throw new RuntimeException(httpResponse.getBody());
|
|
142 |
|
|
143 |
String firstPage = httpResponse.getBody();
|
|
144 |
JSONParser parser = new JSONParser(firstPage);
|
|
145 |
JSONObject root = (JSONObject) parser.parse();
|
|
146 |
JSONArray mbeansNode = (JSONArray) root.get("mbeans");
|
|
147 |
for (JSONElement je : mbeansNode) {
|
|
148 |
JSONObject jobj = (JSONObject) je;
|
|
149 |
JSONPrimitive jp = (JSONPrimitive) jobj.get("name");
|
|
150 |
String name = (String) jp.getValue();
|
|
151 |
mbeanNames.add(name);
|
|
152 |
}
|
|
153 |
|
|
154 |
JSONObject linkObj = (JSONObject) root.get("_links");
|
|
155 |
if (linkObj == null) {
|
|
156 |
break;
|
|
157 |
}
|
|
158 |
if (linkObj.get("next") == null)
|
|
159 |
break;
|
|
160 |
|
|
161 |
JSONPrimitive element = (JSONPrimitive) linkObj.get("next");
|
|
162 |
String nextUrl = (String) element.getValue();
|
|
163 |
if (nextUrl.equalsIgnoreCase(url)) {
|
|
164 |
break;
|
|
165 |
} else {
|
|
166 |
url = nextUrl;
|
|
167 |
}
|
|
168 |
} while (true);
|
|
169 |
return mbeanNames;
|
|
170 |
} catch (Exception ex) {
|
|
171 |
throw new RuntimeException(ex);
|
|
172 |
}
|
55994
|
173 |
}
|
|
174 |
|
56002
|
175 |
private JSONObject restGetAttributes(String name) {
|
|
176 |
try {
|
|
177 |
String url = "/platform/mbeans/" + name;
|
|
178 |
HttpResponse httpResponse = executeHttpRequest(url);
|
|
179 |
if (httpResponse.getCode() == 200) {
|
|
180 |
JSONParser parser = new JSONParser(httpResponse.getBody());
|
|
181 |
JSONElement root = parser.parse();
|
|
182 |
JSONElement element = ((JSONObject) root).get("attributes");
|
|
183 |
if (element != null && element instanceof JSONObject)
|
|
184 |
return (JSONObject) element;
|
|
185 |
else {
|
|
186 |
return new JSONObject();
|
|
187 |
}
|
|
188 |
} else {
|
|
189 |
throw new RuntimeException("HTTP GET for [" + url + "] failed, response = " + httpResponse.getBody());
|
|
190 |
}
|
|
191 |
} catch (Exception ex) {
|
|
192 |
throw new RuntimeException(ex);
|
55994
|
193 |
}
|
|
194 |
}
|
|
195 |
|
56002
|
196 |
private JSONArray restGetOperations(String name) {
|
55994
|
197 |
try {
|
56002
|
198 |
String url = "/platform/mbeans/" + name;
|
|
199 |
HttpResponse httpResponse = executeHttpRequest(url);
|
|
200 |
if (httpResponse.getCode() == 200) {
|
|
201 |
JSONParser parser = new JSONParser(httpResponse.getBody());
|
|
202 |
JSONElement root = parser.parse();
|
|
203 |
JSONElement element = ((JSONObject) root).get("operations");
|
|
204 |
if (element != null && element instanceof JSONArray)
|
|
205 |
return (JSONArray) element;
|
|
206 |
else {
|
|
207 |
return new JSONArray();
|
|
208 |
}
|
|
209 |
} else {
|
|
210 |
throw new RuntimeException("HTTP GET for [" + url + "] failed, response = " + httpResponse.getBody());
|
|
211 |
}
|
|
212 |
} catch (Exception ex) {
|
|
213 |
throw new RuntimeException(ex);
|
|
214 |
}
|
|
215 |
}
|
55994
|
216 |
|
56002
|
217 |
private HttpResponse executeHttpRequest(String inputUrl) {
|
|
218 |
return executeHttpRequest(inputUrl, "", false);
|
|
219 |
}
|
55994
|
220 |
|
56002
|
221 |
private HttpResponse executeHttpRequest(String inputUrl, String body, boolean isPost) {
|
|
222 |
try {
|
|
223 |
if (inputUrl != null && !inputUrl.isEmpty()) {
|
|
224 |
URL url;
|
|
225 |
if (!inputUrl.startsWith("http")) {
|
|
226 |
url = new URL(HttpUtil.escapeUrl(restUrl + inputUrl));
|
|
227 |
} else {
|
|
228 |
url = new URL(inputUrl);
|
|
229 |
}
|
|
230 |
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
231 |
con.setRequestProperty("Content-Type", "application/json;");
|
|
232 |
String userCredentials = "username1:password1";
|
|
233 |
String basicAuth = "Basic " + Base64.getEncoder().encodeToString(userCredentials.getBytes());
|
|
234 |
con.setRequestProperty("Authorization", basicAuth);
|
|
235 |
if (isPost) {
|
|
236 |
con.setDoOutput(true);
|
|
237 |
con.setRequestMethod("POST");
|
|
238 |
|
|
239 |
try (OutputStreamWriter out = new OutputStreamWriter(
|
|
240 |
con.getOutputStream())) {
|
|
241 |
out.write(body);
|
|
242 |
out.flush();
|
|
243 |
}
|
|
244 |
} else {
|
|
245 |
con.setDoOutput(false);
|
|
246 |
}
|
55994
|
247 |
|
56002
|
248 |
try {
|
|
249 |
int status = con.getResponseCode();
|
|
250 |
StringBuilder sbuf;
|
|
251 |
if (status == 200) {
|
|
252 |
try (BufferedReader br = new BufferedReader(
|
|
253 |
new InputStreamReader(con.getInputStream()))) {
|
|
254 |
sbuf = new StringBuilder();
|
|
255 |
String input;
|
|
256 |
while ((input = br.readLine()) != null) {
|
|
257 |
sbuf.append(input);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
} else {
|
|
261 |
try (BufferedReader br = new BufferedReader(
|
|
262 |
new InputStreamReader(con.getErrorStream()))) {
|
|
263 |
sbuf = new StringBuilder();
|
|
264 |
String input;
|
|
265 |
while ((input = br.readLine()) != null) {
|
|
266 |
sbuf.append(input);
|
|
267 |
}
|
|
268 |
}
|
|
269 |
}
|
|
270 |
return new HttpResponse(status, sbuf.toString());
|
|
271 |
} catch (IOException e) {
|
|
272 |
e.printStackTrace();
|
|
273 |
}
|
55994
|
274 |
}
|
|
275 |
} catch (Exception ex) {
|
56002
|
276 |
throw new RuntimeException(ex);
|
55994
|
277 |
}
|
|
278 |
return null;
|
|
279 |
}
|
|
280 |
|
56002
|
281 |
private void verifyHttpResponse(HttpResponse httpResponse) {
|
|
282 |
try {
|
|
283 |
Assert.assertEquals(httpResponse.getCode(), 200);
|
|
284 |
JSONParser parser = new JSONParser(httpResponse.getBody());
|
|
285 |
parser.parse();
|
|
286 |
} catch (Exception ex) {
|
|
287 |
throw new RuntimeException(ex);
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
private JSONObject restGetMBeanOperationsBulkOp(String name) {
|
|
292 |
JSONArray jsonArray = restGetOperations(name);
|
|
293 |
JSONArray ops = jsonArray.stream()
|
|
294 |
.filter(a -> ((JSONObject) a).get("arguments") == null)
|
|
295 |
.map((JSONElement a) -> ((JSONObject) a).get("name"))
|
|
296 |
.collect(Collectors.toCollection(JSONArray::new));
|
|
297 |
JSONObject result = new JSONObject();
|
|
298 |
if (ops != null && !ops.isEmpty()) {
|
|
299 |
result.put("operations", ops);
|
|
300 |
}
|
|
301 |
return result;
|
|
302 |
}
|
|
303 |
|
|
304 |
private JSONObject restGetMBeanAttributeBulkOp(String name) {
|
|
305 |
try {
|
|
306 |
MBeanServerConnection mBeanServer = connector.getMBeanServerConnection();
|
|
307 |
ObjectName objectName = new ObjectName(name);
|
|
308 |
|
|
309 |
MBeanAttributeInfo[] attrInfos = mBeanServer.getMBeanInfo(objectName).getAttributes();
|
|
310 |
Set<String> writableAttrs = Stream.of(attrInfos)
|
|
311 |
.filter(MBeanAttributeInfo::isWritable)
|
|
312 |
.map(MBeanFeatureInfo::getName).collect(Collectors.toSet());
|
|
313 |
|
|
314 |
JSONObject writeAttrMap = new JSONObject();
|
|
315 |
List<String> invalidAttrs = Arrays.asList("< Attribute not supported >",
|
|
316 |
"< Invalid attributes >",
|
|
317 |
"< Attribute not found >",
|
|
318 |
"< Error: No such attribute >");
|
|
319 |
|
|
320 |
JSONObject attrMap = restGetAttributes(name);
|
|
321 |
writableAttrs.stream().filter(a -> {
|
|
322 |
JSONElement element = attrMap.get(a);
|
|
323 |
if (element instanceof JSONPrimitive
|
|
324 |
&& ((JSONPrimitive) element).getValue() instanceof String) {
|
|
325 |
String attrVal = (String) ((JSONPrimitive) element).getValue();
|
|
326 |
if (invalidAttrs.contains(attrVal))
|
|
327 |
return false;
|
|
328 |
}
|
|
329 |
return true;
|
|
330 |
}).forEach(a -> writeAttrMap.put(a, attrMap.get(a)));
|
|
331 |
|
|
332 |
JSONObject jsonObject = new JSONObject();
|
|
333 |
JSONArray attrs = new JSONArray();
|
|
334 |
attrMap.keySet().forEach(a -> attrs.add(new JSONPrimitive(a)));
|
|
335 |
if (attrs != null && !attrs.isEmpty()) {
|
|
336 |
jsonObject.put("get", attrs);
|
55994
|
337 |
}
|
56002
|
338 |
if (writeAttrMap != null && !writeAttrMap.isEmpty()) {
|
|
339 |
jsonObject.put("set", writeAttrMap);
|
|
340 |
}
|
|
341 |
|
|
342 |
JSONObject result = new JSONObject();
|
|
343 |
if (jsonObject != null && !jsonObject.isEmpty()) {
|
|
344 |
result.put("attributes", jsonObject);
|
|
345 |
}
|
|
346 |
return result;
|
|
347 |
} catch (Exception ex) {
|
|
348 |
ex.getCause().printStackTrace();
|
|
349 |
throw new RuntimeException(ex);
|
|
350 |
}
|
|
351 |
}
|
|
352 |
|
|
353 |
@DataProvider(name = "allMBeans")
|
|
354 |
private Object[][] allMBeans() {
|
|
355 |
Set<String> names = restGetAllMBeans();
|
|
356 |
Object[] objects = names.stream().toArray(Object[]::new);
|
|
357 |
Object[][] result = new Object[objects.length][1];
|
|
358 |
for (int i = 0; i < objects.length; i++) {
|
|
359 |
result[i][0] = objects[i];
|
|
360 |
}
|
|
361 |
return result;
|
|
362 |
}
|
55994
|
363 |
|
56002
|
364 |
@Test
|
|
365 |
public void testAllMBeanServers() {
|
|
366 |
try {
|
|
367 |
HttpResponse httpResponse = executeHttpRequest(restUrl);
|
|
368 |
Assert.assertEquals(httpResponse.getCode(), 200);
|
|
369 |
JSONParser parser = new JSONParser(httpResponse.getBody());
|
|
370 |
JSONObject parse = (JSONObject) parser.parse();
|
|
371 |
JSONArray links = (JSONArray) parse.get("mBeanServers");
|
|
372 |
for(JSONElement elem : links) {
|
|
373 |
JSONObject jobj = (JSONObject) elem;
|
|
374 |
String link = (String) ((JSONPrimitive)jobj.get("href")).getValue();
|
|
375 |
HttpResponse hr = executeHttpRequest(link);
|
|
376 |
Assert.assertEquals(httpResponse.getCode(), 200);
|
|
377 |
JSONParser parser1 = new JSONParser(hr.getBody());
|
|
378 |
JSONObject parse1 = (JSONObject) parser1.parse();
|
|
379 |
JSONObject links1 = (JSONObject) parse1.get("_links");
|
|
380 |
String link3 = (String) ((JSONPrimitive)links1.get("mbeans")).getValue();
|
|
381 |
verifyHttpResponse(executeHttpRequest(link3));
|
|
382 |
}
|
|
383 |
} catch (Exception ex) {
|
|
384 |
throw new RuntimeException(ex);
|
|
385 |
}
|
|
386 |
}
|
|
387 |
|
|
388 |
@Test
|
|
389 |
public void testAllMBeans() {
|
|
390 |
Set<String> restMbeans = restGetAllMBeans();
|
|
391 |
Set<String> rmiMbeans = rmiGetAllMBeans();
|
|
392 |
long count = restMbeans.stream().filter(mbeanName -> !rmiMbeans.contains(mbeanName)).count();
|
|
393 |
Assert.assertEquals(count, 0);
|
|
394 |
}
|
|
395 |
|
|
396 |
public void testMBeanFiltering() {
|
|
397 |
String url = restUrl + "/platform/mbeans?";
|
|
398 |
List<String> filtersOk = Arrays.asList("query=*:type=DiagnosticCommand,*",
|
|
399 |
"query=java.lang:*&page=2",
|
|
400 |
"query=java.lang:*&page=1",
|
|
401 |
"query=*:type=Diag*");
|
|
402 |
|
|
403 |
List<String> filtersKo = Arrays.asList("","*:type=DiagnosticCommand,*","query=java.lang:*&page=1&invalid=4");
|
|
404 |
|
|
405 |
for(String filter : filtersOk) {
|
|
406 |
HttpResponse httpResponse = executeHttpRequest(url + filter);
|
|
407 |
Assert.assertEquals(httpResponse.getCode(),200);
|
|
408 |
}
|
|
409 |
|
|
410 |
for(String filter : filtersKo) {
|
|
411 |
HttpResponse httpResponse = executeHttpRequest(url + filter);
|
|
412 |
Assert.assertEquals(httpResponse.getCode(),HttpURLConnection.HTTP_BAD_REQUEST);
|
|
413 |
}
|
|
414 |
}
|
|
415 |
|
|
416 |
@Test
|
|
417 |
public void testAllMBeanInfo() {
|
|
418 |
Set<String> names = restGetAllMBeans();
|
|
419 |
for (String name : names) {
|
|
420 |
String url = "/platform/mbeans/" + name + "/info";
|
|
421 |
HttpResponse httpResponse = executeHttpRequest(url);
|
|
422 |
verifyHttpResponse(httpResponse);
|
|
423 |
}
|
|
424 |
}
|
|
425 |
|
|
426 |
@Test(priority = 3)
|
|
427 |
public void testMbeanNoArgOperations() {
|
|
428 |
Set<String> mbeans = restGetAllMBeans();
|
|
429 |
for (String name : mbeans) {
|
|
430 |
JSONArray restoperations = restGetOperations(name);
|
|
431 |
Set<String> rmiOps = rmiGetOperations(name);
|
|
432 |
Set<String> restOps = restoperations.stream().map((JSONElement a) -> {
|
|
433 |
JSONElement elem = ((JSONObject) a).get("name");
|
|
434 |
if (elem instanceof JSONPrimitive
|
|
435 |
&& (((JSONPrimitive) elem).getValue() instanceof String)) {
|
|
436 |
return (String) ((JSONPrimitive) elem).getValue();
|
55994
|
437 |
} else {
|
56002
|
438 |
return null;
|
55994
|
439 |
}
|
56002
|
440 |
}).collect(Collectors.toSet());
|
|
441 |
Assert.assertEquals(rmiOps, restOps);
|
|
442 |
|
|
443 |
for (JSONElement jsonElement : restoperations) {
|
|
444 |
JSONObject jsonObject = (JSONObject) jsonElement;
|
|
445 |
String opUrl = (String) ((JSONPrimitive) jsonObject.get("href")).getValue();
|
|
446 |
if (jsonObject.get("arguments") == null) {
|
|
447 |
HttpResponse httpResponse = executeHttpRequest(opUrl, "", true);
|
|
448 |
verifyHttpResponse(httpResponse);
|
|
449 |
}
|
55994
|
450 |
}
|
|
451 |
}
|
56002
|
452 |
}
|
|
453 |
|
|
454 |
@Test(priority = 2)
|
|
455 |
public void testMBeanSetAttributes() {
|
|
456 |
try {
|
|
457 |
Set<String> mbeans = restGetAllMBeans();
|
|
458 |
MBeanServerConnection mBeanServer = connector.getMBeanServerConnection();
|
|
459 |
for (String name : mbeans) {
|
|
460 |
ObjectName objectName = new ObjectName(name);
|
|
461 |
String url = "/platform/mbeans/" + objectName.toString();
|
|
462 |
JSONObject attrMap = restGetAttributes(name);
|
|
463 |
MBeanAttributeInfo[] attrInfos = mBeanServer.getMBeanInfo(objectName).getAttributes();
|
|
464 |
Set<String> writableAttrs = Stream.of(attrInfos)
|
|
465 |
.filter(MBeanAttributeInfo::isWritable)
|
|
466 |
.map(MBeanFeatureInfo::getName).collect(Collectors.toSet());
|
|
467 |
|
|
468 |
if (writableAttrs.isEmpty())
|
|
469 |
continue;
|
|
470 |
|
|
471 |
JSONObject writeAttrMap = new JSONObject();
|
|
472 |
List<String> invalidAttrs = Arrays.asList(new String[]{"< Attribute not supported >",
|
|
473 |
"< Invalid attributes >",
|
|
474 |
"< Attribute not found >",
|
|
475 |
"< Error: No such attribute >"});
|
|
476 |
|
|
477 |
writableAttrs.stream().filter(a -> {
|
|
478 |
JSONElement element = attrMap.get(a);
|
|
479 |
if (element instanceof JSONPrimitive
|
|
480 |
&& ((JSONPrimitive) element).getValue() instanceof String) {
|
|
481 |
String attrVal = (String) ((JSONPrimitive) element).getValue();
|
|
482 |
if (invalidAttrs.contains(attrVal))
|
|
483 |
return false;
|
|
484 |
}
|
|
485 |
return true;
|
|
486 |
}).forEach(a -> writeAttrMap.put(a, attrMap.get(a)));
|
|
487 |
HttpResponse httpResponse = executeHttpRequest(url, writeAttrMap.toJsonString(), true);
|
|
488 |
if (httpResponse.getCode() == 200) {
|
|
489 |
String body = httpResponse.getBody();
|
|
490 |
JSONParser parser = new JSONParser(body);
|
|
491 |
JSONObject jsonObject = (JSONObject) parser.parse();
|
|
492 |
Assert.assertEquals(jsonObject.size(), writeAttrMap.size());
|
|
493 |
Assert.assertEquals(jsonObject.keySet(), writeAttrMap.keySet());
|
|
494 |
|
|
495 |
for (JSONElement elem : jsonObject.values()) {
|
|
496 |
String output = (String) ((JSONPrimitive) elem).getValue();
|
|
497 |
Assert.assertEquals(output.equalsIgnoreCase("success"), true);
|
|
498 |
}
|
|
499 |
}
|
|
500 |
}
|
|
501 |
} catch (Exception ex) {
|
|
502 |
throw new RuntimeException(ex);
|
|
503 |
}
|
|
504 |
}
|
|
505 |
|
|
506 |
@Test(priority = 1)
|
|
507 |
public void testMBeanGetAttributes() {
|
|
508 |
try {
|
|
509 |
Set<String> mbeans = restGetAllMBeans();
|
|
510 |
for (String name : mbeans) {
|
|
511 |
Map<String, Object> rmiAttrs = rmiGetAttributes(name);
|
|
512 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(rmiAttrs);
|
|
513 |
JSONObject rmiJson = (JSONObject) typeMapper.toJsonValue(rmiAttrs);
|
|
514 |
JSONObject restJson = restGetAttributes(name);
|
|
515 |
Assert.assertEquals(restJson.size(), rmiJson.size());
|
|
516 |
Assert.assertEquals(rmiJson.keySet(), restJson.keySet());
|
|
517 |
}
|
|
518 |
} catch (Exception ex) {
|
|
519 |
throw new RuntimeException(ex);
|
|
520 |
}
|
55994
|
521 |
}
|
56002
|
522 |
|
|
523 |
@Test(priority = 4)
|
|
524 |
public void testThreadMXBeanBulkRequest() {
|
|
525 |
String name = "java.lang:type=Threading";
|
|
526 |
String url = "/platform/mbeans/java.lang:type=Threading";
|
|
527 |
|
|
528 |
JSONObject jsonObject = restGetAttributes(name);
|
|
529 |
JSONArray jarr = (JSONArray) jsonObject.get("AllThreadIds");
|
|
530 |
|
|
531 |
Long[] threadIds = jarr.stream().map(a -> {
|
|
532 |
if (a instanceof JSONPrimitive && ((JSONPrimitive) a).getValue() instanceof Long) {
|
|
533 |
return (Long) ((JSONPrimitive) a).getValue();
|
|
534 |
} else {
|
|
535 |
return -1;
|
|
536 |
}
|
|
537 |
}).toArray(Long[]::new);
|
|
538 |
|
|
539 |
JSONObject args = new JSONObject();
|
|
540 |
JSONArray array = new JSONArray();
|
|
541 |
Stream.of(threadIds).forEach(a -> array.add(new JSONPrimitive(a)));
|
|
542 |
args.put("p0", array);
|
|
543 |
args.put("p1", new JSONPrimitive(true));
|
|
544 |
args.put("p2", new JSONPrimitive(true));
|
|
545 |
JSONObject jobj1 = new JSONObject();
|
|
546 |
jobj1.put("getThreadInfo", args);
|
|
547 |
|
|
548 |
JSONObject jobj2 = new JSONObject();
|
|
549 |
|
|
550 |
jobj2.putAll(restGetMBeanAttributeBulkOp(name));
|
|
551 |
jobj2.put("operations", jobj1);
|
|
552 |
|
|
553 |
HttpResponse httpResponse = executeHttpRequest(url, jobj2.toJsonString(), true);
|
|
554 |
verifyHttpResponse(httpResponse);
|
|
555 |
|
|
556 |
JSONArray arr = new JSONArray();
|
|
557 |
arr.add(new JSONPrimitive("findMonitorDeadlockedThreads"));
|
|
558 |
arr.add(new JSONPrimitive("resetPeakThreadCount"));
|
|
559 |
arr.add(new JSONPrimitive("findDeadlockedThreads"));
|
|
560 |
arr.add(jobj1);
|
|
561 |
|
|
562 |
jobj2.clear();
|
|
563 |
jobj2.putAll(restGetMBeanAttributeBulkOp(name));
|
|
564 |
jobj2.put("operations", arr);
|
|
565 |
|
|
566 |
httpResponse = executeHttpRequest(url, jobj2.toJsonString(), true);
|
|
567 |
verifyHttpResponse(httpResponse);
|
|
568 |
|
|
569 |
jobj2.clear();
|
|
570 |
jobj2.putAll(restGetMBeanAttributeBulkOp(name));
|
|
571 |
jobj2.put("operations", "resetPeakThreadCount");
|
|
572 |
|
|
573 |
httpResponse = executeHttpRequest(url, jobj2.toJsonString(), true);
|
|
574 |
verifyHttpResponse(httpResponse);
|
55994
|
575 |
}
|
56002
|
576 |
|
|
577 |
@Test(priority = 5)
|
|
578 |
public void testThreadMXBeanThreadInfo() {
|
|
579 |
String name = "java.lang:type=Threading";
|
|
580 |
|
|
581 |
JSONObject jsonObject = restGetAttributes(name);
|
|
582 |
JSONArray jarr = (JSONArray) jsonObject.get("AllThreadIds");
|
|
583 |
|
|
584 |
Long[] threadIds = jarr.stream().map(a -> {
|
|
585 |
if (a instanceof JSONPrimitive && ((JSONPrimitive) a).getValue() instanceof Long) {
|
|
586 |
return (Long) ((JSONPrimitive) a).getValue();
|
|
587 |
} else {
|
|
588 |
return -1;
|
|
589 |
}
|
|
590 |
}).toArray(Long[]::new);
|
|
591 |
|
|
592 |
JSONArray operations = restGetOperations("java.lang:type=Threading");
|
|
593 |
|
|
594 |
JSONObject threadInfoRequest = (JSONObject) operations.stream()
|
|
595 |
.filter(a -> {
|
|
596 |
JSONObject jobj = (JSONObject) a;
|
|
597 |
JSONElement elem = ((JSONObject) a).get("name");
|
|
598 |
if (elem instanceof JSONPrimitive
|
|
599 |
&& (((JSONPrimitive) elem).getValue() instanceof String)) {
|
|
600 |
return ((JSONPrimitive) elem).getValue().equals("getThreadInfo");
|
|
601 |
} else {
|
|
602 |
return false;
|
|
603 |
}
|
|
604 |
}).findFirst().get();
|
|
605 |
|
|
606 |
String postUrl1 = (String) ((JSONPrimitive) threadInfoRequest.get("href")).getValue();
|
|
607 |
|
|
608 |
// Build arguments
|
|
609 |
// 1. getThreadInfo(long id)
|
|
610 |
JSONObject args = new JSONObject();
|
|
611 |
args.put("p0", new JSONPrimitive(threadIds[0]));
|
|
612 |
HttpResponse httpResponse = executeHttpRequest(postUrl1, args.toJsonString(), true);
|
|
613 |
verifyHttpResponse(httpResponse);
|
|
614 |
args.clear();
|
|
615 |
|
|
616 |
// 2. getThreadInfo(long[] ids)
|
|
617 |
JSONArray array = new JSONArray();
|
|
618 |
Stream.of(threadIds).forEach(a -> array.add(new JSONPrimitive(a)));
|
|
619 |
args.put("p0", array);
|
|
620 |
httpResponse = executeHttpRequest(postUrl1, args.toJsonString(), true);
|
|
621 |
verifyHttpResponse(httpResponse);
|
|
622 |
|
|
623 |
//3. getThreadInfo(long[] ids, boolean lockedMonitors, boolean lockedSynchronizers)
|
|
624 |
args.put("p1", new JSONPrimitive(true));
|
|
625 |
args.put("p2", new JSONPrimitive(true));
|
|
626 |
httpResponse = executeHttpRequest(postUrl1, args.toJsonString(), true);
|
|
627 |
verifyHttpResponse(httpResponse);
|
|
628 |
args.clear();
|
|
629 |
|
|
630 |
//4. getThreadInfo(long id, int maxDepth)
|
|
631 |
args.put("p0", new JSONPrimitive(threadIds[0]));
|
|
632 |
args.put("p1", new JSONPrimitive(10));
|
|
633 |
httpResponse = executeHttpRequest(postUrl1, args.toJsonString(), true);
|
|
634 |
verifyHttpResponse(httpResponse);
|
|
635 |
args.clear();
|
|
636 |
|
|
637 |
//5. getThreadInfo(long[] ids, int maxDepth)
|
|
638 |
JSONArray jarr1 = new JSONArray();
|
|
639 |
Stream.of(threadIds).forEach(a -> jarr1.add(new JSONPrimitive(a)));
|
|
640 |
args.put("p0", jarr1);
|
|
641 |
args.put("p1", new JSONPrimitive(10));
|
|
642 |
httpResponse = executeHttpRequest(postUrl1, args.toJsonString(), true);
|
|
643 |
verifyHttpResponse(httpResponse);
|
|
644 |
}
|
|
645 |
|
|
646 |
@Test(priority = 4)
|
|
647 |
public void testAllMBeansBulkRequest() {
|
|
648 |
Set<String> allNames = restGetAllMBeans();
|
|
649 |
String url = restUrl + "/platform/mbeans";
|
|
650 |
JSONObject result = new JSONObject();
|
|
651 |
for (String name : allNames) {
|
|
652 |
JSONObject attrNode = restGetMBeanAttributeBulkOp(name);
|
|
653 |
JSONObject opsNode = restGetMBeanOperationsBulkOp(name);
|
|
654 |
JSONObject jobj = new JSONObject();
|
|
655 |
if (attrNode != null && !attrNode.isEmpty()) {
|
|
656 |
jobj.putAll(attrNode);
|
|
657 |
}
|
|
658 |
if (opsNode != null && !opsNode.isEmpty()) {
|
|
659 |
jobj.putAll(opsNode);
|
|
660 |
}
|
|
661 |
result.put(name, jobj);
|
|
662 |
}
|
|
663 |
HttpResponse httpResponse = executeHttpRequest(url, result.toJsonString(), true);
|
|
664 |
verifyHttpResponse(httpResponse);
|
|
665 |
}
|
|
666 |
} |