model_object_ut.cpp
Go to the documentation of this file.
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 ModelObject : 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("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("object_type");
00075     attrs.DisplayName = OpcUa::LocalizedText("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("variable");
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("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("sub_object");
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("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("sub_object");
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("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(ModelObject, ServerCanAccessToRootObject)
00136 {
00137   OpcUa::Model::Server server(Services);
00138   OpcUa::Model::Object rootObject = server.RootObject();
00139 
00140   ASSERT_EQ(rootObject.GetId(), OpcUa::ObjectId::RootFolder);
00141 }
00142 
00143 TEST_F(ModelObject, ObjectCanCreateVariable)
00144 {
00145   OpcUa::Model::Server server(Services);
00146   OpcUa::Model::Object rootObject = server.RootObject();
00147   OpcUa::QualifiedName name("new_variable");
00148   OpcUa::Variant value = 8;
00149   OpcUa::Model::Variable variable = rootObject.CreateVariable(name, value);
00150 
00151   ASSERT_NE(variable.GetId(), OpcUa::ObjectId::Null);
00152   ASSERT_EQ(variable.GetBrowseName(), name);
00153   ASSERT_EQ(variable.GetDisplayName(), OpcUa::LocalizedText(name.Name));
00154   ASSERT_EQ(variable.GetValue(), value);
00155 }
00156 
00157 TEST_F(ModelObject, CanInstantiateEmptyObjectType)
00158 {
00159   const OpcUa::NodeId& typeId = CreateEmptyObjectType();
00160   OpcUa::Model::ObjectType objectType(typeId, Services);
00161   OpcUa::Model::Object rootObject(OpcUa::ObjectId::RootFolder, Services);
00162   const char* objectDesc = "Empty object.";
00163   const OpcUa::QualifiedName browseName("empty_object");
00164   const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
00165   OpcUa::Model::Object object(objectId, Services);
00166 
00167   ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
00168   ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
00169 
00170   std::vector<OpcUa::Model::Variable> variables = object.GetVariables();
00171   ASSERT_EQ(variables.size(), 0);
00172 }
00173 
00174 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneVariable)
00175 {
00176   const OpcUa::NodeId& typeId = CreateObjectTypeWithOneVariable();
00177   OpcUa::Model::ObjectType objectType(typeId, Services);
00178   OpcUa::Model::Object rootObject(OpcUa::ObjectId::RootFolder, Services);
00179   const char* objectDesc = "object_with_var.";
00180   const OpcUa::QualifiedName browseName("object_with_var");
00181   const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
00182   OpcUa::Model::Object object(objectId, Services);
00183 
00184   ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
00185   ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
00186 
00187   std::vector<OpcUa::Model::Variable> variables = object.GetVariables();
00188   ASSERT_EQ(variables.size(), 1);
00189 }
00190 
00191 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneUntypedObject)
00192 {
00193   const OpcUa::NodeId& typeId = CreateObjectTypeWithOneUntypedObject();
00194   OpcUa::Model::ObjectType objectType(typeId, Services);
00195   OpcUa::Model::Object rootObject(OpcUa::ObjectId::RootFolder, Services);
00196   const char* objectDesc = "object_with_var.";
00197   const OpcUa::QualifiedName browseName("object_with_var");
00198   const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
00199   OpcUa::Model::Object object(objectId, Services);
00200 
00201   ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
00202   ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
00203 
00204   std::vector<OpcUa::Model::Object> objects = object.GetObjects();
00205   ASSERT_EQ(objects.size(), 1);
00206 }
00207 
00208 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneTypedObject)
00209 {
00210   // Type with one property - empty object with type that has a variable.
00211   // ObjectType1
00212   //   +-object - ObjectType2
00213   //
00214   // ObjectType2
00215   //   +-variable
00216 
00217   const OpcUa::NodeId& typeId = CreateObjectTypeWithOneTypedObject();
00218   OpcUa::Model::ObjectType objectType(typeId, Services);
00219   // we will create objects under root folder.
00220   OpcUa::Model::Object rootObject(OpcUa::ObjectId::RootFolder, Services);
00221   const char* objectDesc = "object_with_var.";
00222   const OpcUa::QualifiedName browseName("object_with_var");
00223   // Instantiate object type we have created first.
00224   // Get only id of that object.
00225   const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
00226   // This constructor will read all parameters of created object.
00227   // Restored object structure should be next:
00228   // Object1 - ObjectType1
00229   //   +-Object2 - ObjectType2
00230   //       +-variable
00231   OpcUa::Model::Object object(objectId, Services);
00232 
00233   ASSERT_EQ(object.GetId(), objectId);
00234   ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
00235 
00236   // Created object will have one sub object.
00237   std::vector<OpcUa::Model::Object> objects = object.GetObjects();
00238   ASSERT_EQ(objects.size(), 1);
00239   const OpcUa::Model::Object& subObject = objects[0];
00240   // Sub object in the source object type dedn't have any sub objects.
00241   // But it has a type definition which has one variable.
00242   // And new instantiated object have to restore full hierarchy.
00243   std::vector<OpcUa::Model::Variable> variables;
00244   ASSERT_NO_THROW(variables = subObject.GetVariables());
00245   ASSERT_EQ(variables.size(), 1);
00246 }
00247 
00248 OpcUa::RelativePathElement GetHierarchicalElement(const std::string& browseName)
00249 {
00250   OpcUa::RelativePathElement element;
00251   element.ReferenceTypeId = OpcUa::ObjectId::HierarchicalReferences;
00252   element.IncludeSubtypes = true;
00253   element.TargetName.Name = browseName;
00254   return element;
00255 }
00256 
00257 TEST_F(ModelObject, CanAccessVariableByBrowsePath)
00258 {
00259   OpcUa::Model::Server server(Services);
00260   OpcUa::Model::Object serverObject = server.GetObject(OpcUa::ObjectId::Server);
00261   OpcUa::RelativePath path;
00262   path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::ServerStatus));
00263   path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::BuildInfo));
00264   path.Elements.push_back(GetHierarchicalElement(OpcUa::Names::BuildNumber));
00265 
00266   OpcUa::Model::Variable buildNumber = serverObject.GetVariable(path);
00267   EXPECT_EQ(buildNumber.GetBrowseName(), OpcUa::QualifiedName(OpcUa::Names::BuildNumber));
00268 }
00269 
00270 TEST_F(ModelObject, CanAccessVariableByQualifiedName)
00271 {
00272   std::cout << "1" << std::endl;
00273   OpcUa::Model::Server server(Services);
00274   std::cout << "2" << std::endl;
00275   OpcUa::Model::Object rootObject = server.RootObject();
00276   std::cout << "3" << std::endl;
00277   OpcUa::Model::ObjectType serverType = server.GetObjectType(OpcUa::ObjectId::ServerType);
00278   std::cout << "4" << std::endl;
00279   OpcUa::Model::Object serverObject = rootObject.CreateObject(serverType, OpcUa::QualifiedName("Server"));
00280   std::cout << "5" << std::endl;
00281   OpcUa::Model::Variable serverStatus = serverObject.GetVariable(OpcUa::QualifiedName(OpcUa::Names::ServerStatus));
00282   std::cout << "6" << std::endl;
00283   EXPECT_EQ(serverStatus.GetBrowseName(), OpcUa::QualifiedName(OpcUa::Names::ServerStatus));
00284   std::cout << "7" << std::endl;
00285 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:56