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): ...@@ -20,36 +20,40 @@ def convertNodeValue(node_id, node_value):
return node_value return node_value
# XXX: what should be the OPCUA container? # 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: 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) id = default_id)
# do selection of oepration in a REST fashion based in HTTP method # do selection of operation in a REST fashion based in HTTP method
http_method = context.REQUEST.method if http_method is None:
context.log(http_method) http_method = context.REQUEST.method
if http_method == "GET": if http_method == "GET":
# assume read request, return what was saved. # assume read request, return what was saved.
return json.dumps(default_opcua_document.getNodeDict()) return json.dumps(default_opcua_document.getNodeDict())
elif http_method == "POST": elif http_method == "POST":
# assume write request # assume write request
try: if data is None:
data = context.REQUEST.data try:
except AttributeError as e: data = context.REQUEST.data
log("Warning: 'data' is missing from request.") except AttributeError as e:
raise e log("Warning: 'data' is missing from request.")
else: raise e
data = json.loads(data)
if 'node' in data.keys(): data = json.loads(data)
# log only set requests for now if 'node' in data.keys():
node_id = data['node'] # log only set requests for now
node_value = data['val'] node_id = data['node']
# set to "ERP5 OPCUA Document" but only if it's not a None value node_value = data['val']
if node_value is not None and node_value != "None": # set to "ERP5 OPCUA Document" but only if it's not a None value
log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id)) if node_value is not None and node_value != "None":
node_dict = default_opcua_document.getNodeDict() log("Set '%s' = '%s' to %s as %s" %(node_id, node_value, default_opcua_document, node_id))
node_dict[node_id] = convertNodeValue(node_id, node_value) node_dict = default_opcua_document.getNodeDict()
default_opcua_document.setNodeDict(node_dict) node_dict[node_id] = convertNodeValue(node_id, node_value)
log("Changed = %s" %default_opcua_document.getNodeDict()) default_opcua_document.setNodeDict(node_dict)
log("Changed = %s" %default_opcua_document.getNodeDict())
return json.dumps(default_opcua_document.getNodeDict())
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <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>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# #
############################################################################## ##############################################################################
import json
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase): class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase):
...@@ -36,10 +36,35 @@ class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase): ...@@ -36,10 +36,35 @@ class TestDataInestOPCUADocumentgestion(ERP5TypeTestCase):
def getTitle(self): def getTitle(self):
return "OPC UA Document" return "OPC UA Document"
def test_01_SetAndGet(self): def test_01_SetAndGet(self):
""" """
XXX: implement accordingly 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