model_object_ut.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 
21 #include <opc/ua/model.h>
22 
29 
31 #include "services_registry_test.h"
33 
34 #include <gmock/gmock.h>
35 #include <gtest/gtest.h>
36 
37 using namespace testing;
38 
39 
40 class ModelObject : public Test
41 {
42 protected:
43  virtual void SetUp()
44  {
46  Logger = spdlog::stderr_color_mt("test");
47  Logger->set_level(spdlog::level::info);
48  Addons = Common::CreateAddonsManager(Logger);
49 
53  Addons->Start();
54 
55  OpcUa::Server::ServicesRegistry::SharedPtr addon = Addons->GetAddon<OpcUa::Server::ServicesRegistry>(OpcUa::Server::ServicesRegistryAddonId);
56  Services = addon->GetServer();
57  }
58 
59  virtual void TearDown()
60  {
61  Services.reset();
62  Addons->Stop();
63  Addons.reset();
64  }
65 
67  {
68  OpcUa::NodeManagementServices::SharedPtr nodes = Services->NodeManagement();
70  item.BrowseName = OpcUa::QualifiedName("object_type");
74 
76  attrs.Description = OpcUa::LocalizedText("object_type");
77  attrs.DisplayName = OpcUa::LocalizedText("object_type");
78  attrs.IsAbstract = false;
79  item.Attributes = attrs;
80  std::vector<OpcUa::AddNodesResult> result = nodes->AddNodes({item});
81  return result[0].AddedNodeId;
82  }
83 
85  {
86  const OpcUa::NodeId & objectId = CreateEmptyObjectType();
87  OpcUa::AddNodesItem variable;
88  variable.BrowseName = OpcUa::QualifiedName("variable");
90  variable.ParentNodeId = objectId;
93  attrs.DisplayName = OpcUa::LocalizedText("variable");
94  variable.Attributes = attrs;
95  Services->NodeManagement()->AddNodes({variable});
96  return objectId;
97  }
98 
100  {
101  const OpcUa::NodeId & objectId = CreateEmptyObjectType();
102  OpcUa::AddNodesItem object;
103  object.BrowseName = OpcUa::QualifiedName("sub_object");
104  object.Class = OpcUa::NodeClass::Object;
105  object.ParentNodeId = objectId;
106  object.ReferenceTypeId = OpcUa::ObjectId::HasComponent;
108  attrs.DisplayName = OpcUa::LocalizedText("sub_object");
109  object.Attributes = attrs;
110  Services->NodeManagement()->AddNodes({object});
111  return objectId;
112  }
113 
115  {
116  const OpcUa::NodeId & resultTypeId = CreateEmptyObjectType();
117  const OpcUa::NodeId & objectTypeWithVar = CreateObjectTypeWithOneVariable();
118  OpcUa::AddNodesItem object;
119  object.BrowseName = OpcUa::QualifiedName("sub_object");
120  object.Class = OpcUa::NodeClass::Object;
121  object.ParentNodeId = resultTypeId;
122  object.ReferenceTypeId = OpcUa::ObjectId::HasComponent;
123  object.TypeDefinition = objectTypeWithVar;
125  attrs.DisplayName = OpcUa::LocalizedText("sub_object");
126  object.Attributes = attrs;
127  Services->NodeManagement()->AddNodes({object});
128  return resultTypeId;
129  }
130 
131 protected:
132  Common::Logger::SharedPtr Logger;
133  Common::AddonsManager::UniquePtr Addons;
134  OpcUa::Services::SharedPtr Services;
135 };
136 
137 
138 TEST_F(ModelObject, ServerCanAccessToRootObject)
139 {
140  OpcUa::Model::Server server(Services);
141  OpcUa::Model::Object rootObject = server.RootObject();
142 
144 }
145 
146 TEST_F(ModelObject, ObjectCanCreateVariable)
147 {
148  OpcUa::Model::Server server(Services);
149  OpcUa::Model::Object rootObject = server.RootObject();
150  OpcUa::QualifiedName name("new_variable");
151  OpcUa::Variant value = 8;
152  OpcUa::Model::Variable variable = rootObject.CreateVariable(name, value);
153 
155  ASSERT_EQ(variable.GetBrowseName(), name);
157  ASSERT_EQ(variable.GetValue(), value);
158 }
159 
160 TEST_F(ModelObject, CanInstantiateEmptyObjectType)
161 {
162  const OpcUa::NodeId & typeId = CreateEmptyObjectType();
163  OpcUa::Model::ObjectType objectType(typeId, Services);
165  const char * objectDesc = "Empty object.";
166  const OpcUa::QualifiedName browseName("empty_object");
167  const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
168  OpcUa::Model::Object object(objectId, Services);
169 
170  ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
171  ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
172 
173  std::vector<OpcUa::Model::Variable> variables = object.GetVariables();
174  ASSERT_EQ(variables.size(), 0);
175 }
176 
177 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneVariable)
178 {
179  const OpcUa::NodeId & typeId = CreateObjectTypeWithOneVariable();
180  OpcUa::Model::ObjectType objectType(typeId, Services);
182  const char * objectDesc = "object_with_var.";
183  const OpcUa::QualifiedName browseName("object_with_var");
184  const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
185  OpcUa::Model::Object object(objectId, Services);
186 
187  ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
188  ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
189 
190  std::vector<OpcUa::Model::Variable> variables = object.GetVariables();
191  ASSERT_EQ(variables.size(), 1);
192 }
193 
194 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneUntypedObject)
195 {
196  const OpcUa::NodeId & typeId = CreateObjectTypeWithOneUntypedObject();
197  OpcUa::Model::ObjectType objectType(typeId, Services);
199  const char * objectDesc = "object_with_var.";
200  const OpcUa::QualifiedName browseName("object_with_var");
201  const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
202  OpcUa::Model::Object object(objectId, Services);
203 
204  ASSERT_NE(object.GetId(), OpcUa::ObjectId::Null);
205  ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
206 
207  std::vector<OpcUa::Model::Object> objects = object.GetObjects();
208  ASSERT_EQ(objects.size(), 1);
209 }
210 
211 TEST_F(ModelObject, CanInstantiateObjectTypeWithOneTypedObject)
212 {
213  // Type with one property - empty object with type that has a variable.
214  // ObjectType1
215  // +-object - ObjectType2
216  //
217  // ObjectType2
218  // +-variable
219 
220  const OpcUa::NodeId & typeId = CreateObjectTypeWithOneTypedObject();
221  OpcUa::Model::ObjectType objectType(typeId, Services);
222  // we will create objects under root folder.
224  const char * objectDesc = "object_with_var.";
225  const OpcUa::QualifiedName browseName("object_with_var");
226  // Instantiate object type we have created first.
227  // Get only id of that object.
228  const OpcUa::NodeId objectId = rootObject.CreateObject(objectType, browseName, objectDesc).GetId();
229  // This constructor will read all parameters of created object.
230  // Restored object structure should be next:
231  // Object1 - ObjectType1
232  // +-Object2 - ObjectType2
233  // +-variable
234  OpcUa::Model::Object object(objectId, Services);
235 
236  ASSERT_EQ(object.GetId(), objectId);
237  ASSERT_EQ(object.GetBrowseName(), browseName) << "Real name: " << object.GetBrowseName().Name;
238 
239  // Created object will have one sub object.
240  std::vector<OpcUa::Model::Object> objects = object.GetObjects();
241  ASSERT_EQ(objects.size(), 1);
242  const OpcUa::Model::Object & subObject = objects[0];
243  // Sub object in the source object type dedn't have any sub objects.
244  // But it has a type definition which has one variable.
245  // And new instantiated object have to restore full hierarchy.
246  std::vector<OpcUa::Model::Variable> variables;
247  ASSERT_NO_THROW(variables = subObject.GetVariables());
248  ASSERT_EQ(variables.size(), 1);
249 }
250 
252 {
255  element.IncludeSubtypes = true;
256  element.TargetName.Name = browseName;
257  return element;
258 }
259 
260 TEST_F(ModelObject, CanAccessVariableByBrowsePath)
261 {
262  OpcUa::Model::Server server(Services);
264  OpcUa::RelativePath path;
268 
269  OpcUa::Model::Variable buildNumber = serverObject.GetVariable(path);
271 }
272 
273 TEST_F(ModelObject, CanAccessVariableByQualifiedName)
274 {
275  std::cout << "1" << std::endl;
276  OpcUa::Model::Server server(Services);
277  std::cout << "2" << std::endl;
278  OpcUa::Model::Object rootObject = server.RootObject();
279  std::cout << "3" << std::endl;
281  std::cout << "4" << std::endl;
282  OpcUa::Model::Object serverObject = rootObject.CreateObject(serverType, OpcUa::QualifiedName("Server"));
283  std::cout << "5" << std::endl;
285  std::cout << "6" << std::endl;
287  std::cout << "7" << std::endl;
288 }
const char ServerStatus[]
Definition: strings.h:162
Object GetObject(const NodeId &id) const
TEST_F(TestInfoTest, Names)
OpcUa::NodeId CreateObjectTypeWithOneTypedObject()
OpcUa::Services::SharedPtr Services
const char ServicesRegistryAddonId[]
DataValue GetValue() const
name
Definition: setup.py:38
OpcUa::RelativePathElement GetHierarchicalElement(const std::string &browseName)
OpcUa::NodeId CreateEmptyObjectType()
OpcUa::NodeId CreateObjectTypeWithOneVariable()
#define ASSERT_NE(val1, val2)
Variable CreateVariable(const QualifiedName &browseName, const Variant &value)
Create a new variable.
virtual void SetUp()
void RegisterStandardNamespace(Common::AddonsManager &addons)
objects
Definition: client.py:46
AddonsManager::UniquePtr CreateAddonsManager(const Common::Logger::SharedPtr &logger)
Get instance of addons core.
std::vector< RelativePathElement > Elements
Definition: types.h:125
void RegisterServicesRegistry(Common::AddonsManager &addons)
void RegisterAddressSpace(Common::AddonsManager &addons)
#define ASSERT_NO_THROW(statement)
QualifiedName GetBrowseName() const
Definition: model_node.cpp:48
OpcUa::NodeId CreateObjectTypeWithOneUntypedObject()
Object RootObject() const
#define ASSERT_EQ(val1, val2)
virtual std::shared_ptr< OpcUa::Services > GetServer() const =0
const char BuildNumber[]
Definition: strings.h:36
std::string Name
Definition: types.h:74
const char BuildInfo[]
Definition: strings.h:34
Variable GetVariable(const QualifiedName &name) const
Get variable of the object by it name.
ObjectType GetObjectType(const NodeId &typeId) const
Object CreateObject(const ObjectType &type, const QualifiedName &browseName)
Common::AddonsManager::UniquePtr Addons
NodeId GetId() const
Definition: model_node.cpp:43
Common::Logger::SharedPtr Logger
#define EXPECT_EQ(expected, actual)
std::shared_ptr< logger > stderr_color_mt(const std::string &logger_name)
Definition: spdlog_impl.h:150
server
Definition: server.py:19
virtual void TearDown()
QualifiedName TargetName
Definition: types.h:120
LocalizedText GetDisplayName() const
Definition: model_node.cpp:53
void drop_all()
Definition: spdlog_impl.h:260


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