Commit 5d030d5e authored by Ivan Tyagov's avatar Ivan Tyagov

Add testing adn adjust API.

parent eac96b40
......@@ -20,36 +20,40 @@ def convertNodeValue(node_id, node_value):
return node_value
# XXX: what should be the OPCUA container?
default_id = "1"
default_opcua_document = context.opcua_document_module.get(default_id, None)
if default_opcua_document is None:
context.opcua_document_module.newContent(portal_type = "OPCUA Document",
default_id = "1"
default_opcua_document = context.opcua_document_module.get(default_id, None)
if default_opcua_document is None:
context.opcua_document_module.newContent(portal_type = "OPCUA Document",
id = default_id)
# do selection of oepration in a REST fashion based in HTTP method
http_method = context.REQUEST.method
context.log(http_method)
# do selection of operation in a REST fashion based in HTTP method
if http_method is None:
http_method = context.REQUEST.method
if http_method == "GET":
# assume read request, return what was saved.
return json.dumps(default_opcua_document.getNodeDict())
elif http_method == "POST":
# assume write request
try:
data = context.REQUEST.data
except AttributeError as e:
log("Warning: 'data' is missing from request.")
raise e
else:
data = json.loads(data)
if 'node' in data.keys():
# log only set requests for now
node_id = data['node']
node_value = data['val']
# set to "ERP5 OPCUA Document" but only if it's not a None value
if node_value is not None and node_value != "None":
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id))
node_dict = default_opcua_document.getNodeDict()
node_dict[node_id] = convertNodeValue(node_id, node_value)
default_opcua_document.setNodeDict(node_dict)
log("Changed = %s" %default_opcua_document.getNodeDict())
if data is None:
try:
data = context.REQUEST.data
except AttributeError as e:
log("Warning: 'data' is missing from request.")
raise e
data = json.loads(data)
if 'node' in data.keys():
# log only set requests for now
node_id = data['node']
node_value = data['val']
# set to "ERP5 OPCUA Document" but only if it's not a None value
if node_value is not None and node_value != "None":
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id))
node_dict = default_opcua_document.getNodeDict()
node_dict[node_id] = convertNodeValue(node_id, node_value)
default_opcua_document.setNodeDict(node_dict)
log("Changed = %s" %default_opcua_document.getNodeDict())
return json.dumps(default_opcua_document.getNodeDict())
......@@ -50,7 +50,7 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>*args, **kwargs</string> </value>
<value> <string>http_method=None, default_opcua_document=None, data=None, *args, **kwargs</string> </value>
</item>
<item>
<key> <string>id</string> </key>
......
......@@ -24,7 +24,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import json
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase):
......@@ -36,10 +36,35 @@ class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase):
def getTitle(self):
return "OPC UA Document"
def test_01_SetAndGet(self):
"""
XXX: implement accordingly
"""
self.assertEqual(1, 1)
# create placeholder
default_id = "1"
default_opcua_document = self.portal.opcua_document_module.get(default_id, None)
if default_opcua_document is None:
self.portal.opcua_document_module.newContent(portal_type = "OPCUA Document",
id = default_id)
self.tic()
# cleanup
default_opcua_document.setNodeDict({})
self.tic()
# test HTTP GET returns empy dict
result_dict = self.portal.ERP5Site_handleOPCUARequest(http_method="GET",
default_opcua_document=default_opcua_document)
self.assertEqual(json.dumps({}), result_dict)
# test setting over a client
node_id = "n1=1;i=810"
node_val = 10
opcua_structure = {"node": node_id,
"val":node_val}
result_json = self.portal.ERP5Site_handleOPCUARequest(http_method="POST",
default_opcua_document=default_opcua_document,
data = json.dumps(opcua_structure))
# convert from JSON string to dict
result_dict = json.loads(result_json)
self.assertEqual({node_id: node_val}, result_dict)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment