Skip to content

Commit d9ada75

Browse files
committed
Implementation of list method, with some tests.
1 parent 7e4f014 commit d9ada75

4 files changed

Lines changed: 77 additions & 4 deletions

File tree

jolokia/api.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ def execute(self, *args, **kwargs):
2424

2525
return self.session.simple_post(self.base_url, data=kwargs)
2626

27-
def list(self, *args, **kwargs):
28-
"""Return a list of all MBeans on all available MBean servers."""
29-
pass
27+
def list(self, path=None, *args, **kwargs):
28+
"""Returns a list of all MBeans on all available MBean servers."""
29+
data = {
30+
'type': 'list',
31+
'path': path
32+
}
33+
34+
return self.session.simple_post(self.base_url, data=data)
3035

3136
@require_args(['mbean'], 'search method has 1 required keyword argument: mbean')
3237
def search(self, data=None, *args, **kwargs):
@@ -59,7 +64,9 @@ def get_attribute(self, mbean=None, attribute=None, path=None, *args, **kwargs):
5964

6065
return self.session.simple_post(self.base_url, data=data)
6166

67+
@require_args(['mbean', 'attribute', 'value'], 'set_attribute method has 3 required arguments: mbean, attribute, and value')
6268
def set_attribute(self, mbean=None, attribute=None, value=None, path=None, *args, **kwargs):
69+
"""Sets the value of an MBean's attribute"""
6370

6471
if not mbean or not attribute or not value:
6572
raise IllegalArgumentException('set_attribute method has 3 required parameters: mbean, attribute, and value')

tests/fixtures/responses.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@
154154
"status": 200
155155
}
156156

157+
VALID_LIST = {
158+
'request': {
159+
'path': 'java.lang/type=Memory/attr/HeapMemoryUsage',
160+
'type': 'list'
161+
},
162+
'value': {
163+
'rw': False,
164+
'type': 'javax.management.openmbean.CompositeData',
165+
'desc': 'HeapMemoryUsage'
166+
},
167+
'timestamp': 1517106359,
168+
'status': 200
169+
}
170+
171+
INVALID_LIST_PATH = {
172+
'error_type': 'java.lang.IllegalArgumentException',
173+
'error': 'java.lang.IllegalArgumentException : Illegal path element HeapMemoryUsage',
174+
'status': 400
175+
}
176+
157177

158178
def _mock_base(resp_data, status_code, ok, *args, **kwargs):
159179

@@ -227,3 +247,11 @@ def mock_missing_mbean(*args, **kwargs):
227247
def mock_valid_exec(*args, **Kwargs):
228248

229249
return _mock_base(VALID_EXEC_RESPONSE, 200, True)
250+
251+
def mock_valid_list(*args, **kwargs):
252+
253+
return _mock_base(VALID_LIST, 200, True)
254+
255+
def mock_invalid_path(*args, **kwargs):
256+
257+
return _mock_base(INVALID_LIST_PATH, 200, True)

tests/test_list.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
import logging
3+
4+
from jolokia import JolokiaClient
5+
from jolokia.exceptions import IllegalArgumentException
6+
from unittest import TestCase
7+
from .fixtures.responses import mock_valid_list, mock_invalid_path
8+
9+
logging.basicConfig(level=logging.DEBUG)
10+
11+
12+
class TestList(TestCase):
13+
14+
def __init__(self, *args, **kwargs):
15+
super(TestList, self).__init__(*args, **kwargs)
16+
self.jc = JolokiaClient('http://localhost:8080/jolokia')
17+
18+
def test_valid_request(self):
19+
setattr(self.jc.session, 'request', mock_valid_list)
20+
21+
resp_data = self.jc.list(path='java.lang/type=Memory/attr/HeapMemoryUsage')
22+
23+
assert type(resp_data) is dict
24+
assert resp_data['status'] == 200
25+
assert type(resp_data['value']) is dict
26+
27+
def test_invalid_path(self):
28+
setattr(self.jc.session, 'request', mock_invalid_path)
29+
30+
resp_data = self.jc.list(path='java.lang/type=Memory/HeapMemoryUsage')
31+
32+
assert type(resp_data) is dict
33+
assert resp_data['status'] == 400
34+
assert resp_data['error_type'] == 'java.lang.IllegalArgumentException'

tests/test_set_attribute.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ def test_valid_request(self):
4343

4444
setattr(self.jc.session, 'request', mock_valid_write)
4545

46-
resp_data = self.jc.set_attribute('java.lang:type=ClassLoading', 'Verbose', True)
46+
resp_data = self.jc.set_attribute(
47+
mbean='java.lang:type=ClassLoading',
48+
attribute='Verbose',
49+
value=True
50+
)
4751

4852
assert not resp_data['value']
4953

0 commit comments

Comments
 (0)