Skip to content

Commit

Permalink
Implementation of list method, with some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbrefvem committed Jan 28, 2018
1 parent 7e4f014 commit d9ada75
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
13 changes: 10 additions & 3 deletions jolokia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ def execute(self, *args, **kwargs):

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

def list(self, *args, **kwargs):
"""Return a list of all MBeans on all available MBean servers."""
pass
def list(self, path=None, *args, **kwargs):
"""Returns a list of all MBeans on all available MBean servers."""
data = {
'type': 'list',
'path': path
}

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

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

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

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

if not mbean or not attribute or not value:
raise IllegalArgumentException('set_attribute method has 3 required parameters: mbean, attribute, and value')
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@
"status": 200
}

VALID_LIST = {
'request': {
'path': 'java.lang/type=Memory/attr/HeapMemoryUsage',
'type': 'list'
},
'value': {
'rw': False,
'type': 'javax.management.openmbean.CompositeData',
'desc': 'HeapMemoryUsage'
},
'timestamp': 1517106359,
'status': 200
}

INVALID_LIST_PATH = {
'error_type': 'java.lang.IllegalArgumentException',
'error': 'java.lang.IllegalArgumentException : Illegal path element HeapMemoryUsage',
'status': 400
}


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

Expand Down Expand Up @@ -227,3 +247,11 @@ def mock_missing_mbean(*args, **kwargs):
def mock_valid_exec(*args, **Kwargs):

return _mock_base(VALID_EXEC_RESPONSE, 200, True)

def mock_valid_list(*args, **kwargs):

return _mock_base(VALID_LIST, 200, True)

def mock_invalid_path(*args, **kwargs):

return _mock_base(INVALID_LIST_PATH, 200, True)
34 changes: 34 additions & 0 deletions tests/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
import logging

from jolokia import JolokiaClient
from jolokia.exceptions import IllegalArgumentException
from unittest import TestCase
from .fixtures.responses import mock_valid_list, mock_invalid_path

logging.basicConfig(level=logging.DEBUG)


class TestList(TestCase):

def __init__(self, *args, **kwargs):
super(TestList, self).__init__(*args, **kwargs)
self.jc = JolokiaClient('http://localhost:8080/jolokia')

def test_valid_request(self):
setattr(self.jc.session, 'request', mock_valid_list)

resp_data = self.jc.list(path='java.lang/type=Memory/attr/HeapMemoryUsage')

assert type(resp_data) is dict
assert resp_data['status'] == 200
assert type(resp_data['value']) is dict

def test_invalid_path(self):
setattr(self.jc.session, 'request', mock_invalid_path)

resp_data = self.jc.list(path='java.lang/type=Memory/HeapMemoryUsage')

assert type(resp_data) is dict
assert resp_data['status'] == 400
assert resp_data['error_type'] == 'java.lang.IllegalArgumentException'
6 changes: 5 additions & 1 deletion tests/test_set_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def test_valid_request(self):

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

resp_data = self.jc.set_attribute('java.lang:type=ClassLoading', 'Verbose', True)
resp_data = self.jc.set_attribute(
mbean='java.lang:type=ClassLoading',
attribute='Verbose',
value=True
)

assert not resp_data['value']

Expand Down

0 comments on commit d9ada75

Please sign in to comment.