00001 /****************************************************************************** 00002 * Copyright (C) 2013-2014 by Alexander Rykovanov * 00003 * rykovanov.as@gmail.com * 00004 * * 00005 * This library is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; version 3 of the License. * 00008 * * 00009 * This library is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU Lesser General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU Lesser General Public License * 00015 * along with this library; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00018 ******************************************************************************/ 00019 00020 00021 #include <opc/ua/model.h> 00022 00023 #include <opc/common/addons_core/addon_manager.h> 00024 #include <opc/ua/protocol/attribute_ids.h> 00025 #include <opc/ua/protocol/status_codes.h> 00026 #include <opc/ua/services/services.h> 00027 #include <opc/ua/server/address_space.h> 00028 #include <opc/ua/server/standard_address_space.h> 00029 00030 #include "address_space_registry_test.h" 00031 #include "services_registry_test.h" 00032 #include "standard_namespace_test.h" 00033 00034 #include <gmock/gmock.h> 00035 #include <gtest/gtest.h> 00036 00037 using namespace testing; 00038 00039 00040 class ModelObjectType : public Test 00041 { 00042 protected: 00043 virtual void SetUp() 00044 { 00045 const bool debug = false; 00046 Addons = Common::CreateAddonsManager(); 00047 00048 OpcUa::Test::RegisterServicesRegistry(*Addons); 00049 OpcUa::Test::RegisterAddressSpace(*Addons); 00050 OpcUa::Test::RegisterStandardNamespace(*Addons); 00051 Addons->Start(); 00052 00053 OpcUa::Server::ServicesRegistry::SharedPtr addon = Addons->GetAddon<OpcUa::Server::ServicesRegistry>(OpcUa::Server::ServicesRegistryAddonId); 00054 Services = addon->GetServer(); 00055 } 00056 00057 virtual void TearDown() 00058 { 00059 Services.reset(); 00060 Addons->Stop(); 00061 Addons.reset(); 00062 } 00063 00064 OpcUa::NodeId CreateEmptyObjectType() 00065 { 00066 OpcUa::NodeManagementServices::SharedPtr nodes = Services->NodeManagement(); 00067 OpcUa::AddNodesItem item; 00068 item.BrowseName = OpcUa::QualifiedName("empty_object_type"); 00069 item.Class = OpcUa::NodeClass::ObjectType; 00070 item.ParentNodeId = OpcUa::ObjectId::BaseObjectType; 00071 item.ReferenceTypeId = OpcUa::ObjectId::HasSubtype; 00072 00073 OpcUa::ObjectTypeAttributes attrs; 00074 attrs.Description = OpcUa::LocalizedText("empty_object_type"); 00075 attrs.DisplayName = OpcUa::LocalizedText("empty_object_type"); 00076 attrs.IsAbstract = false; 00077 item.Attributes = attrs; 00078 std::vector<OpcUa::AddNodesResult> result = nodes->AddNodes({item}); 00079 return result[0].AddedNodeId; 00080 } 00081 00082 OpcUa::NodeId CreateObjectTypeWithOneVariable() 00083 { 00084 const OpcUa::NodeId& objectId = CreateEmptyObjectType(); 00085 OpcUa::AddNodesItem variable; 00086 variable.BrowseName = OpcUa::QualifiedName("new_variable1"); 00087 variable.Class = OpcUa::NodeClass::Variable; 00088 variable.ParentNodeId = objectId; 00089 variable.ReferenceTypeId = OpcUa::ObjectId::HasProperty; 00090 OpcUa::VariableAttributes attrs; 00091 attrs.DisplayName = OpcUa::LocalizedText("new_variable"); 00092 variable.Attributes = attrs; 00093 Services->NodeManagement()->AddNodes({variable}); 00094 return objectId; 00095 } 00096 00097 OpcUa::NodeId CreateObjectTypeWithOneUntypedObject() 00098 { 00099 const OpcUa::NodeId& objectId = CreateEmptyObjectType(); 00100 OpcUa::AddNodesItem object; 00101 object.BrowseName = OpcUa::QualifiedName("new_sub_object1"); 00102 object.Class = OpcUa::NodeClass::Object; 00103 object.ParentNodeId = objectId; 00104 object.ReferenceTypeId = OpcUa::ObjectId::HasComponent; 00105 OpcUa::ObjectAttributes attrs; 00106 attrs.DisplayName = OpcUa::LocalizedText("new_sub_object"); 00107 object.Attributes = attrs; 00108 Services->NodeManagement()->AddNodes({object}); 00109 return objectId; 00110 } 00111 00112 OpcUa::NodeId CreateObjectTypeWithOneTypedObject() 00113 { 00114 const OpcUa::NodeId& resultTypeId = CreateEmptyObjectType(); 00115 const OpcUa::NodeId& objectTypeWithVar = CreateObjectTypeWithOneVariable(); 00116 OpcUa::AddNodesItem object; 00117 object.BrowseName = OpcUa::QualifiedName("new_sub_object1"); 00118 object.Class = OpcUa::NodeClass::Object; 00119 object.ParentNodeId = resultTypeId; 00120 object.ReferenceTypeId = OpcUa::ObjectId::HasComponent; 00121 object.TypeDefinition = objectTypeWithVar; 00122 OpcUa::ObjectAttributes attrs; 00123 attrs.DisplayName = OpcUa::LocalizedText("new_sub_object"); 00124 object.Attributes = attrs; 00125 Services->NodeManagement()->AddNodes({object}); 00126 return resultTypeId; 00127 } 00128 00129 protected: 00130 Common::AddonsManager::UniquePtr Addons; 00131 OpcUa::Services::SharedPtr Services; 00132 }; 00133 00134 00135 TEST_F(ModelObjectType, ServerAccessObjectTypes) 00136 { 00137 OpcUa::Model::Server server(Services); 00138 OpcUa::Model::ObjectType baseObjectType = server.GetObjectType(OpcUa::ObjectId::BaseObjectType); 00139 ASSERT_EQ(baseObjectType.GetId(), OpcUa::ObjectId::BaseObjectType); 00140 ASSERT_EQ(baseObjectType.GetDisplayName(), OpcUa::LocalizedText(OpcUa::Names::BaseObjectType)); 00141 ASSERT_EQ(baseObjectType.GetBrowseName(), OpcUa::QualifiedName(OpcUa::Names::BaseObjectType)); 00142 ASSERT_EQ(baseObjectType.IsAbstract(), false); 00143 } 00144 00145 TEST_F(ModelObjectType, ObjectTypeAllowsAccessToSubtypes) 00146 { 00147 OpcUa::Model::Server server(Services); 00148 OpcUa::Model::ObjectType baseObjectType = server.GetObjectType(OpcUa::ObjectId::BaseObjectType); 00149 std::vector<OpcUa::Model::ObjectType> subTypes = baseObjectType.SubTypes(); 00150 ASSERT_FALSE(subTypes.empty()); 00151 } 00152 00153 TEST_F(ModelObjectType, ObjectTypeAllowsAccessToVariables) 00154 { 00155 OpcUa::Model::Server server(Services); 00156 OpcUa::Model::ObjectType serverType = server.GetObjectType(OpcUa::ObjectId::ServerType); 00157 std::vector<OpcUa::Model::Variable> variables = serverType.Variables(); 00158 ASSERT_FALSE(variables.empty()); 00159 } 00160 00161 TEST_F(ModelObjectType, ObjectTypeAllowsAccessToObjects) 00162 { 00163 OpcUa::Model::Server server(Services); 00164 OpcUa::Model::ObjectType serverType = server.GetObjectType(OpcUa::ObjectId::ServerType); 00165 std::vector<OpcUa::Model::Object> objects = serverType.Objects(); 00166 ASSERT_FALSE(objects.empty()); 00167 }