00001
00002 import unittest
00003 import test_opcua as opcua
00004
00005 endpointUrl = "opc.tcp://cool_server:4841"
00006 computer = opcua.Server(endpointUrl)
00007
00008 class EndpointsTests(unittest.TestCase):
00009 def test_find_servers(self):
00010 apps = computer.find_servers()
00011 self.assertGreater(len(apps), 0, "No applications returned.")
00012
00013 for app in apps:
00014 self._check_application(app)
00015
00016 def test_get_endpoints(self):
00017 endpoints = computer.get_endpoints()
00018 self.assertEqual(len(endpoints), 1, "No endpoints returned.")
00019
00020 endpoint = endpoints[0]
00021 self.assertEqual(endpoint.url, endpointUrl, 'Endpoint has unexpected url: ' + endpoint.url)
00022 self.assertEqual(endpoint.security_mode, opcua.MessageSecurityMode.NONE, 'Endpoint has unexpected securityMode: ' + str(endpoint.security_mode))
00023 self.assertEqual(endpoint.security_policy_uri, "SecurityPolicyURI", 'Endpoint has unexpected security policy uri: ' + endpoint.security_policy_uri)
00024 self.assertEqual(endpoint.transport_profile_uri, "TransportProfileURI", 'Endpoint has unexpected transport profile uri: ' + endpoint.transport_profile_uri)
00025 self.assertEqual(endpoint.security_level, 1, 'Endpoint has unexpected transport profile uri: ' + str(endpoint.security_level))
00026
00027 self.assertEqual(len(endpoint.user_identify_tokens), 1, 'Endpoint has number of identify tokens: ' + str(len(endpoint.user_identify_tokens)))
00028 token = endpoint.user_identify_tokens[0]
00029 self.assertEqual(token.policy_id, "PolicyId", "UserTokenPolicy has unexpected PolicyId: " + token.policy_id)
00030 self.assertEqual(token.issued_token_type, "IssuedTokenType", "UserTokenPolicy has unexpected IssuedTokenType: " + token.issued_token_type)
00031 self.assertEqual(token.issuer_endpoint_url, "IssuerEndpointURL", "UserTokenPolicy has unexpected IssuedEndpointURL: " + token.issuer_endpoint_url)
00032 self.assertEqual(token.security_policy_uri, "SecurityPolicyURI", "UserTokenPolicy has unexpected SecurityPolicyURI: " + token.security_policy_uri)
00033 self.assertEqual(token.token_type, opcua.UserIdentifyTokenType.USERNAME, "UserTokenPolicy has unexpected UserIdentifyTokenType: " + str(token.token_type))
00034
00035 self._check_application(endpoint.server_description)
00036
00037 def test_browse(self):
00038 params = opcua.BrowseParameters();
00039 params.node_to_browse.namespace = 1;
00040 params.node_to_browse.identifier = opcua.ObjectId.ROOT_FOLDER
00041 params.direction = opcua.BrowseDirection.BOTH
00042 params.reference_type_id.namespace = 2;
00043 params.reference_type_id.identifier = opcua.ObjectId.ORGANIZES
00044 params.include_subtypes = True
00045 params.node_classes = opcua.NodeClass.OBJECT | opcua.NodeClass.VARIABLE
00046 params.result_mask = 2;
00047 references = computer.browse(params);
00048 self.assertEqual(len(references), 1, "Number of browsed references is invalid: " + str(len(references)))
00049
00050 ref = references[0]
00051 self.assertEqual(ref.browse_name.name, "Name", "Unexpected BrowseName at reference: " + str(ref.browse_name.name))
00052 self.assertEqual(ref.browse_name.namespace_index, 1, "Unexpected NamespaceIndex in browse name: " + str(ref.browse_name.namespace_index))
00053 self.assertEqual(ref.display_name, "Text", "Unexpected DisplayName: " + str(ref.display_name))
00054 self.assertEqual(ref.is_forward, True, "Unexpected IsForward: " + str(ref.is_forward))
00055 self.assertEqual(ref.reference_type_id.namespace_index, 2, "Unexpected reference_type_id.namespace_index: " + str(ref.reference_type_id.namespace_index))
00056 self.assertEqual(ref.reference_type_id.identifier, "Identifier", "Unexpected reference_type_id.identifier: " + str(ref.reference_type_id.identifier))
00057 self.assertEqual(ref.target_node_class, opcua.NodeClass.VARIABLE, "Unexpected reference_type_id.identifier: " + str(ref.target_node_class))
00058 self.assertEqual(ref.target_node_id.namespace_index, 3, "Unexpected target_node_id.namespace_index: " + str(ref.target_node_id.namespace_index))
00059 self.assertEqual(ref.target_node_id.identifier, 4, "Unexpected target_node_id.identifier: " + str(ref.target_node_id.identifier))
00060 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))
00061 self.assertEqual(ref.target_node_type_definition.identifier, 6, "Unexpected target_node_type_definition.identifier: " + str(ref.target_node_type_definition.identifier))
00062
00063 def test_read(self):
00064 attributeParams = opcua.ReadValueId()
00065 attributeParams.node.namespace_index = 1
00066 attributeParams.node.identifier = 2
00067 attributeParams.attribute = opcua.AttributeId.Value
00068 attributeParams.index_range = "1:2"
00069 attributeParams.data_encoding.namespace_index = 3
00070 attributeParams.data_encoding.name = "binary"
00071
00072 params = opcua.ReadParameters()
00073 params.max_age = 1;
00074 params.timestamps_to_return = opcua.TimestampsToReturn.BOTH
00075 params.attributes_to_read.append(attributeParams);
00076
00077 values = computer.read(params)
00078 self.assertEqual(len(values), 1, "Invalid number of read parameters: " + str(len(values)))
00079
00080 data = values[0]
00081 self.assertEqual(data.value, "value", "Invalid value was read:" + str(data.value))
00082 self.assertEqual(data.status, 0x806F0000, "Invalid status:" + str(data.status))
00083 self.assertEqual(data.server_picoseconds, 1, "Invalid server_picoseconds:" + str(data.server_picoseconds))
00084 self.assertEqual(data.server_timestamp, 2, "Invalid server_timestamp:" + str(data.server_timestamp))
00085 self.assertEqual(data.source_picoseconds, 3, "Invalid source_picoseconds:" + str(data.source_picoseconds))
00086 self.assertEqual(data.source_timestamp, 4, "Invalid source_timestamp:" + str(data.source_timestamp))
00087
00088 def test_write(self):
00089 value = opcua.WriteValue()
00090 value.attribute = opcua.AttributeId.Value
00091 value.node.namespace_index = 1
00092 value.node.identifier = "node"
00093 value.numeric_range = "1:2"
00094 value.data.value = "value"
00095 value.data.status = 0x806F0000
00096 value.data.server_picoseconds = 1
00097 value.data.server_timestamp = 2
00098 value.data.source_picoseconds = 3
00099 value.data.source_timestamp = 4
00100
00101 statuses = computer.write([value]);
00102 self.assertEqual(len(statuses), 1, "Invalid number of statuses.")
00103 self.assertEqual(statuses[0], 0x806F0000, "Invalid status.")
00104
00105 def _check_application(self, app):
00106 self.assertEqual(app.name, "Name", "Application has invalid name.")
00107 self.assertEqual(app.uri, "URI", "Application has invalid uri.")
00108 self.assertEqual(app.product_uri, "ProductURI", "Application has invalid ProductURI.")
00109 self.assertEqual(app.type, opcua.ApplicationType.Client, "Application has unexpected Type of application.")
00110 self.assertEqual(app.gateway_server_uri, "GatewayServerURI", "Application has invalid GatewayServerURI.")
00111 self.assertEqual(app.discovery_profile_uri, "DiscoveryProfileURI", "Application has invalid DiscoveryProfileURI.")
00112 self.assertEqual(len(app.discovery_urls), 1, "Application has invalid number of DiscoveryURLs.")
00113 self.assertEqual(app.discovery_urls[0], endpointUrl, "Application has invalid Endpoint url: " + app.discovery_urls[0])
00114
00115
00116 if __name__ == '__main__':
00117 unittest.main()