From d9ada759ede4886a5f2567528cd7445a6f8cc3e9 Mon Sep 17 00:00:00 2001 From: Will Refvem Date: Sat, 27 Jan 2018 21:46:04 -0500 Subject: [PATCH] Implementation of list method, with some tests. --- jolokia/api.py | 13 ++++++++++--- tests/fixtures/responses.py | 28 ++++++++++++++++++++++++++++ tests/test_list.py | 34 ++++++++++++++++++++++++++++++++++ tests/test_set_attribute.py | 6 +++++- 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/jolokia/api.py b/jolokia/api.py index eec1eb9..516f415 100644 --- a/jolokia/api.py +++ b/jolokia/api.py @@ -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): @@ -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') diff --git a/tests/fixtures/responses.py b/tests/fixtures/responses.py index 8a84dcb..19e8f9c 100644 --- a/tests/fixtures/responses.py +++ b/tests/fixtures/responses.py @@ -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): @@ -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) diff --git a/tests/test_list.py b/tests/test_list.py index e69de29..d5dad2c 100644 --- a/tests/test_list.py +++ b/tests/test_list.py @@ -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' diff --git a/tests/test_set_attribute.py b/tests/test_set_attribute.py index 986f370..ffd42f0 100644 --- a/tests/test_set_attribute.py +++ b/tests/test_set_attribute.py @@ -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']