xml_addressspace_ut.cpp
Go to the documentation of this file.
1 
12 
18 
19 #include <functional>
20 
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23 
24 // TODO Add tests for several appearing nodes in xml.
25 // TODO Add tests for all node classes and for invalid classe names.
26 
27 using namespace testing;
28 using namespace OpcUa;
29 using namespace OpcUa::Internal;
30 
32 {
33 protected:
34  virtual void SetUp()
35  {
36  const bool debug = false;
38  }
39 
40  virtual void TearDown()
41  {
42  NameSpace.reset();
43  }
44 
45 protected:
46  std::vector<ReferenceDescription> Browse(const NodeId & id) const
47  {
49  description.NodeToBrowse = id;
50  OpcUa::NodesQuery query;
51  query.NodesToBrowse.push_back(description);
52  return NameSpace->Browse(query);
53  }
54 
55  bool HasReference(std::vector<ReferenceDescription> refs, ReferenceId referenceId, NodeId targetNode) const
56  {
57  for (const ReferenceDescription ref : refs)
58  {
59  if (ref.TargetNodeId == targetNode && ref.ReferenceTypeId == referenceId)
60  {
61  return true;
62  }
63  }
64 
65  return false;
66  }
67 
68  template <typename T>
69  bool HasAttribute(const NodeId & object, OpcUa::AttributeId attribute, T value)
70  {
71  ReadParameters params;
72  ReadValueId id;
73  id.Node = object;
74  id.Attribute = attribute;
75  params.AttributesToRead.push_back(id);
76  std::vector<DataValue> values = NameSpace->Read(params);
77  EXPECT_EQ(values.size(), 1);
78 
79  if (values.size() != 1)
80  {
81  return false;
82  }
83 
84  const DataValue var = values.front();
85  EXPECT_EQ(var.Status, StatusCode::Good);
86  return var == value;
87  }
88 
89  std::string ConfigPath(const char * name)
90  {
91  return std::string("tests/server/") + name;
92  }
93 
94 protected:
95  Server::AddressSpace::UniquePtr NameSpace;
96 };
97 
98 TEST_F(XmlAddressSpace, ExceptionIfCannotLoadDocument)
99 {
101  ASSERT_THROW(loader.Load(ConfigPath("not_found.xml")), std::exception);
102 }
103 
104 TEST_F(XmlAddressSpace, ExceptionIfRootNodeInvalid)
105 {
107  ASSERT_THROW(loader.Load(ConfigPath("invalid_root.xml")), std::exception);
108 }
109 
110 TEST_F(XmlAddressSpace, ExceptionIfNodeVersionOfAddressSpace)
111 {
113  ASSERT_THROW(loader.Load(ConfigPath("no_version.xml")), std::exception);
114 }
115 
116 TEST_F(XmlAddressSpace, ExceptionIfUnknownVersion)
117 {
119  ASSERT_THROW(loader.Load(ConfigPath("invalid_version.xml")), std::exception);
120 }
121 
122 TEST_F(XmlAddressSpace, NoExceptionIfDocumentValid)
123 {
125  ASSERT_NO_THROW(loader.Load(ConfigPath("empty.xml")));
126 }
127 
128 TEST_F(XmlAddressSpace, BaseNodeHasId)
129 {
131  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
132 
133  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::NodeId, NumericNodeId((uint32_t)ObjectId::RootFolder, 10)));
134 }
135 
136 TEST_F(XmlAddressSpace, BaseNodeHasClass)
137 {
139  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
140 
141  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::NodeClass, (int32_t)NodeClass::Object));
142 }
143 
144 TEST_F(XmlAddressSpace, BaseNodeHasBrowseName)
145 {
147  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
148 
149  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::BrowseName, QualifiedName("Root")));
150 }
151 
152 TEST_F(XmlAddressSpace, BaseNodeHasDiaplayName)
153 {
155  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
156 
157  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::DisplayName, LocalizedText("Root")));
158 }
159 
160 TEST_F(XmlAddressSpace, BaseNodeHasDescription)
161 {
163  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
164 
165  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::Description, std::string("Root")));
166 }
167 
168 TEST_F(XmlAddressSpace, BaseNodeHasWriteMask)
169 {
171  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
172 
173  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::WriteMask, (uint32_t)10));
174 }
175 
176 TEST_F(XmlAddressSpace, BaseNodeHasUserWriteMask)
177 {
179  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
180 
181  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::UserWriteMask, (uint32_t)0));
182 }
183 
184 TEST_F(XmlAddressSpace, BaseNodeHasIsAbstract)
185 {
187  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
188 
189  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::IsAbstract, false));
190 }
191 
192 TEST_F(XmlAddressSpace, BaseNodeHasSymmetric)
193 {
195  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
196 
197  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::Symmetric, false));
198 }
199 
200 TEST_F(XmlAddressSpace, BaseNodeHasInverseName)
201 {
203  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
204 
205  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::InverseName, std::string("inverse_name")));
206 }
207 
208 TEST_F(XmlAddressSpace, BaseNodeHasContainsNoLoops)
209 {
211  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
212 
213  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::ContainsNoLoops, false));
214 }
215 
216 TEST_F(XmlAddressSpace, BaseNodeHasEventNotifier)
217 {
219  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
220 
221  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::EventNotifier, std::vector<uint8_t> {0}));
222 }
223 
224 TEST_F(XmlAddressSpace, BaseNodeHasValue)
225 {
227  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
228 
229  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::Value, (uint32_t)55));
230 }
231 
232 TEST_F(XmlAddressSpace, BaseNodeHasDataType)
233 {
235  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
236 
238 }
239 
240 TEST_F(XmlAddressSpace, BaseNodeHasValueRank)
241 {
243  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
244 
245  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::ValueRank, (int32_t)0));
246 }
247 
248 TEST_F(XmlAddressSpace, BaseNodeHasArrayDimensions)
249 {
251  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
252 
253  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::ArrayDimensions, (uint32_t)0));
254 }
255 
256 TEST_F(XmlAddressSpace, BaseNodeHasAccessLevel)
257 {
259  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
260 
261  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::AccessLevel, (uint32_t)0));
262 }
263 
264 TEST_F(XmlAddressSpace, BaseNodeHasUserAccessLevel)
265 {
267  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
268 
269  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::UserAccessLevel, (uint32_t)0));
270 }
271 
272 TEST_F(XmlAddressSpace, BaseNodeHasMinimumSamplingInterval)
273 {
275  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
276 
277  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::MinimumSamplingInterval, (uint32_t)100));
278 }
279 
280 TEST_F(XmlAddressSpace, BaseNodeHasHistorizing)
281 {
283  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
284 
285  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::Historizing, true));
286 }
287 
288 TEST_F(XmlAddressSpace, BaseNodeHasExecutable)
289 {
291  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
292 
293  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::Executable, false));
294 }
295 
296 TEST_F(XmlAddressSpace, BaseNodeHasUserExecutable)
297 {
299  ASSERT_NO_THROW(loader.Load(ConfigPath("base_node.xml")));
300 
301  ASSERT_TRUE(HasAttribute(NumericNodeId(84, 10), AttributeId::UserExecutable, false));
302 }
303 
304 TEST_F(XmlAddressSpace, InternalReference)
305 {
307  ASSERT_NO_THROW(loader.Load(ConfigPath("predefined_references.xml")));
308 
309  const NodeId targetNode = NumericNodeId(100, 10);
310  std::vector<ReferenceDescription> references = Browse(NumericNodeId(99, 10));
311  ASSERT_TRUE(HasReference(references, ReferenceId::References, targetNode));
312  ASSERT_TRUE(HasReference(references, ReferenceId::NonHierarchicalReferences, targetNode));
313  ASSERT_TRUE(HasReference(references, ReferenceId::HierarchicalReferences, targetNode));
314 
315  ASSERT_TRUE(HasReference(references, ReferenceId::References, targetNode));
316  ASSERT_TRUE(HasReference(references, ReferenceId::NonHierarchicalReferences, targetNode));
317  ASSERT_TRUE(HasReference(references, ReferenceId::HierarchicalReferences, targetNode));
318  ASSERT_TRUE(HasReference(references, ReferenceId::HasChild, targetNode));
319  ASSERT_TRUE(HasReference(references, ReferenceId::Organizes, targetNode));
320  ASSERT_TRUE(HasReference(references, ReferenceId::HasEventSource, targetNode));
321  ASSERT_TRUE(HasReference(references, ReferenceId::HasModellingRule, targetNode));
322  ASSERT_TRUE(HasReference(references, ReferenceId::HasEncoding, targetNode));
323  ASSERT_TRUE(HasReference(references, ReferenceId::HasDescription, targetNode));
324  ASSERT_TRUE(HasReference(references, ReferenceId::HasTypeDefinition, targetNode));
325  ASSERT_TRUE(HasReference(references, ReferenceId::GeneratesEvent, targetNode));
326  ASSERT_TRUE(HasReference(references, ReferenceId::Aggregates, targetNode));
327  ASSERT_TRUE(HasReference(references, ReferenceId::HasSubtype, targetNode));
328  ASSERT_TRUE(HasReference(references, ReferenceId::HasProperty, targetNode));
329  ASSERT_TRUE(HasReference(references, ReferenceId::HasComponent, targetNode));
330  ASSERT_TRUE(HasReference(references, ReferenceId::HasNotifier, targetNode));
331  ASSERT_TRUE(HasReference(references, ReferenceId::HasOrderedComponent, targetNode));
332  ASSERT_TRUE(HasReference(references, ReferenceId::HasModelParent, targetNode));
333  ASSERT_TRUE(HasReference(references, ReferenceId::FromState, targetNode));
334  ASSERT_TRUE(HasReference(references, ReferenceId::ToState, targetNode));
335  ASSERT_TRUE(HasReference(references, ReferenceId::HasCause, targetNode));
336  ASSERT_TRUE(HasReference(references, ReferenceId::HasEffect, targetNode));
337  ASSERT_TRUE(HasReference(references, ReferenceId::HasHistoricalConfiguration, targetNode));
338  ASSERT_TRUE(HasReference(references, ReferenceId::HasHistoricalEventConfiguration, targetNode));
339  ASSERT_TRUE(HasReference(references, ReferenceId::HasSubStateMachine, targetNode));
340  ASSERT_TRUE(HasReference(references, ReferenceId::HasEventHistory, targetNode));
341  ASSERT_TRUE(HasReference(references, ReferenceId::AlwaysGeneratesEvent, targetNode));
342  ASSERT_TRUE(HasReference(references, ReferenceId::HasTrueSubState, targetNode));
343  ASSERT_TRUE(HasReference(references, ReferenceId::HasFalseSubState, targetNode));
344  ASSERT_TRUE(HasReference(references, ReferenceId::HasCondition, targetNode));
345  ASSERT_FALSE(HasReference(references, ReferenceId::Unknown, targetNode));
346 }
347 
348 TEST_F(XmlAddressSpace, DISABLED_ExternalReference)
349 {
351  ASSERT_NO_THROW(loader.Load(ConfigPath("predefined_references.xml")));
352 
353  const NodeId targetNode = NumericNodeId(99, 10);
354  std::vector<ReferenceDescription> references = Browse(NumericNodeId(84));
355  ASSERT_TRUE(HasReference(references, ReferenceId::Organizes, targetNode));
356 }
const char References[]
Definition: strings.h:132
std::vector< OpcUa::ReadValueId > AttributesToRead
const char HasModellingRule[]
Definition: strings.h:75
const char HasProperty[]
Definition: strings.h:79
TEST_F(TestInfoTest, Names)
const char HasTypeDefinition[]
Definition: strings.h:81
const char HasEffect[]
Definition: strings.h:71
std::vector< BrowseDescription > NodesToBrowse
Definition: protocol/view.h:50
Server::AddressSpace::UniquePtr NameSpace
TypeWithSize< 4 >::UInt UInt32
const char HasModelParent[]
Definition: strings.h:76
std::vector< T > Browse(const NodeId &node, NodeClass nodeClassMask, Services::SharedPtr services)
Definition: model_impl.h:31
const char NodeId[]
Definition: strings.h:116
bool HasAttribute(const NodeId &object, OpcUa::AttributeId attribute, T value)
const char NonHierarchicalReferences[]
Definition: strings.h:118
name
Definition: setup.py:38
static Server::AddressSpace::SharedPtr NameSpace
const char HasEncoding[]
Definition: strings.h:72
const char HasHistoricalConfiguration[]
Definition: strings.h:74
const char HasDescription[]
Definition: strings.h:70
bool HasReference(std::vector< ReferenceDescription > refs, ReferenceId referenceId, NodeId targetNode) const
#define ASSERT_THROW(statement, expected_exception)
description
Definition: setup.py:43
std::vector< ReferenceDescription > Browse(const NodeId &id) const
const char HasEventSource[]
Definition: strings.h:73
const char HasNotifier[]
Definition: strings.h:77
const char Organizes[]
Definition: strings.h:123
const char GeneratesEvent[]
Definition: strings.h:64
#define ASSERT_NO_THROW(statement)
OPC UA Address space part. GNU LGPL.
const char HasCondition[]
Definition: strings.h:67
const char HasCause[]
Definition: strings.h:66
virtual void SetUp()
const char FromState[]
Definition: strings.h:63
const char HierarchicalReferences[]
Definition: strings.h:82
#define ASSERT_TRUE(condition)
const char HasComponent[]
Definition: strings.h:69
NodeClass
Definition: enums.h:39
const char HasOrderedComponent[]
Definition: strings.h:78
std::string ConfigPath(const char *name)
const char HasChild[]
Definition: strings.h:68
virtual void TearDown()
AddressSpace::UniquePtr CreateAddressSpace(const Common::Logger::SharedPtr &logger)
const char HasSubtype[]
Definition: strings.h:80
NodeId NumericNodeId(uint32_t value, uint16_t namespaceIndex=0)
Definition: nodeid.h:218
const char ToState[]
Definition: strings.h:196
StatusCode Status
Definition: data_value.h:30
const char Aggregates[]
Definition: strings.h:20
#define EXPECT_EQ(expected, actual)
#define ASSERT_FALSE(condition)


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