test.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 import unittest
3 import test_opcua as opcua
4 
5 endpointUrl = "opc.tcp://cool_server:4841"
6 computer = opcua.Server(endpointUrl)
7 
8 class EndpointsTests(unittest.TestCase):
9  def test_find_servers(self):
10  apps = computer.find_servers()
11  self.assertGreater(len(apps), 0, "No applications returned.")
12 
13  for app in apps:
14  self._check_application(app)
15 
16  def test_get_endpoints(self):
17  endpoints = computer.get_endpoints()
18  self.assertEqual(len(endpoints), 1, "No endpoints returned.")
19 
20  endpoint = endpoints[0]
21  self.assertEqual(endpoint.url, endpointUrl, 'Endpoint has unexpected url: ' + endpoint.url)
22  self.assertEqual(endpoint.security_mode, opcua.MessageSecurityMode.NONE, 'Endpoint has unexpected securityMode: ' + str(endpoint.security_mode))
23  self.assertEqual(endpoint.security_policy_uri, "SecurityPolicyURI", 'Endpoint has unexpected security policy uri: ' + endpoint.security_policy_uri)
24  self.assertEqual(endpoint.transport_profile_uri, "TransportProfileURI", 'Endpoint has unexpected transport profile uri: ' + endpoint.transport_profile_uri)
25  self.assertEqual(endpoint.security_level, 1, 'Endpoint has unexpected transport profile uri: ' + str(endpoint.security_level))
26 
27  self.assertEqual(len(endpoint.user_identify_tokens), 1, 'Endpoint has number of identify tokens: ' + str(len(endpoint.user_identify_tokens)))
28  token = endpoint.user_identify_tokens[0]
29  self.assertEqual(token.policy_id, "PolicyId", "UserTokenPolicy has unexpected PolicyId: " + token.policy_id)
30  self.assertEqual(token.issued_token_type, "IssuedTokenType", "UserTokenPolicy has unexpected IssuedTokenType: " + token.issued_token_type)
31  self.assertEqual(token.issuer_endpoint_url, "IssuerEndpointURL", "UserTokenPolicy has unexpected IssuedEndpointURL: " + token.issuer_endpoint_url)
32  self.assertEqual(token.security_policy_uri, "SecurityPolicyURI", "UserTokenPolicy has unexpected SecurityPolicyURI: " + token.security_policy_uri)
33  self.assertEqual(token.token_type, opcua.UserIdentifyTokenType.USERNAME, "UserTokenPolicy has unexpected UserIdentifyTokenType: " + str(token.token_type))
34 
35  self._check_application(endpoint.server_description)
36 
37  def test_browse(self):
38  params = opcua.BrowseParameters();
39  params.node_to_browse.namespace = 1;
40  params.node_to_browse.identifier = opcua.ObjectId.ROOT_FOLDER
41  params.direction = opcua.BrowseDirection.BOTH
42  params.reference_type_id.namespace = 2;
43  params.reference_type_id.identifier = opcua.ObjectId.ORGANIZES
44  params.include_subtypes = True
45  params.node_classes = opcua.NodeClass.OBJECT | opcua.NodeClass.VARIABLE
46  params.result_mask = 2;
47  references = computer.browse(params);
48  self.assertEqual(len(references), 1, "Number of browsed references is invalid: " + str(len(references)))
49 
50  ref = references[0]
51  self.assertEqual(ref.browse_name.name, "Name", "Unexpected BrowseName at reference: " + str(ref.browse_name.name))
52  self.assertEqual(ref.browse_name.namespace_index, 1, "Unexpected NamespaceIndex in browse name: " + str(ref.browse_name.namespace_index))
53  self.assertEqual(ref.display_name, "Text", "Unexpected DisplayName: " + str(ref.display_name))
54  self.assertEqual(ref.is_forward, True, "Unexpected IsForward: " + str(ref.is_forward))
55  self.assertEqual(ref.reference_type_id.namespace_index, 2, "Unexpected reference_type_id.namespace_index: " + str(ref.reference_type_id.namespace_index))
56  self.assertEqual(ref.reference_type_id.identifier, "Identifier", "Unexpected reference_type_id.identifier: " + str(ref.reference_type_id.identifier))
57  self.assertEqual(ref.target_node_class, opcua.NodeClass.VARIABLE, "Unexpected reference_type_id.identifier: " + str(ref.target_node_class))
58  self.assertEqual(ref.target_node_id.namespace_index, 3, "Unexpected target_node_id.namespace_index: " + str(ref.target_node_id.namespace_index))
59  self.assertEqual(ref.target_node_id.identifier, 4, "Unexpected target_node_id.identifier: " + str(ref.target_node_id.identifier))
60  self.assertEqual(ref.target_node_type_definition.namespace_index, 5, "Unexpected target_node_type_definitions.namespace_index: " + str(ref.target_node_type_definition.namespace_index))
61  self.assertEqual(ref.target_node_type_definition.identifier, 6, "Unexpected target_node_type_definition.identifier: " + str(ref.target_node_type_definition.identifier))
62 
63  def test_read(self):
64  attributeParams = opcua.ReadValueId()
65  attributeParams.node.namespace_index = 1
66  attributeParams.node.identifier = 2
67  attributeParams.attribute = opcua.AttributeId.Value
68  attributeParams.index_range = "1:2"
69  attributeParams.data_encoding.namespace_index = 3
70  attributeParams.data_encoding.name = "binary"
71 
72  params = opcua.ReadParameters()
73  params.max_age = 1;
74  params.timestamps_to_return = opcua.TimestampsToReturn.BOTH
75  params.attributes_to_read.append(attributeParams);
76 
77  values = computer.read(params)
78  self.assertEqual(len(values), 1, "Invalid number of read parameters: " + str(len(values)))
79 
80  data = values[0]
81  self.assertEqual(data.value, "value", "Invalid value was read:" + str(data.value))
82  self.assertEqual(data.status, 0x806F0000, "Invalid status:" + str(data.status))
83  self.assertEqual(data.server_picoseconds, 1, "Invalid server_picoseconds:" + str(data.server_picoseconds))
84  self.assertEqual(data.server_timestamp, 2, "Invalid server_timestamp:" + str(data.server_timestamp))
85  self.assertEqual(data.source_picoseconds, 3, "Invalid source_picoseconds:" + str(data.source_picoseconds))
86  self.assertEqual(data.source_timestamp, 4, "Invalid source_timestamp:" + str(data.source_timestamp))
87 
88  def test_write(self):
89  value = opcua.WriteValue()
90  value.attribute = opcua.AttributeId.Value
91  value.node.namespace_index = 1
92  value.node.identifier = "node"
93  value.numeric_range = "1:2"
94  value.data.value = "value"
95  value.data.status = 0x806F0000
96  value.data.server_picoseconds = 1
97  value.data.server_timestamp = 2
98  value.data.source_picoseconds = 3
99  value.data.source_timestamp = 4
100 
101  statuses = computer.write([value]);
102  self.assertEqual(len(statuses), 1, "Invalid number of statuses.")
103  self.assertEqual(statuses[0], 0x806F0000, "Invalid status.")
104 
105  def _check_application(self, app):
106  self.assertEqual(app.name, "Name", "Application has invalid name.")
107  self.assertEqual(app.uri, "URI", "Application has invalid uri.")
108  self.assertEqual(app.product_uri, "ProductURI", "Application has invalid ProductURI.")
109  self.assertEqual(app.type, opcua.ApplicationType.Client, "Application has unexpected Type of application.")
110  self.assertEqual(app.gateway_server_uri, "GatewayServerURI", "Application has invalid GatewayServerURI.")
111  self.assertEqual(app.discovery_profile_uri, "DiscoveryProfileURI", "Application has invalid DiscoveryProfileURI.")
112  self.assertEqual(len(app.discovery_urls), 1, "Application has invalid number of DiscoveryURLs.")
113  self.assertEqual(app.discovery_urls[0], endpointUrl, "Application has invalid Endpoint url: " + app.discovery_urls[0])
114 
115 
116 if __name__ == '__main__':
117  unittest.main()
def test_read(self)
Definition: test.py:63
def test_write(self)
Definition: test.py:88
def _check_application(self, app)
Definition: test.py:105
def test_get_endpoints(self)
Definition: test.py:16
def test_browse(self)
Definition: test.py:37
def test_find_servers(self)
Definition: test.py:9


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08