address_space_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 
23 
26 
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 
30 using namespace testing;
31 
32 class AddressSpace : public Test
33 {
34 protected:
35  virtual void SetUp()
36  {
38  Logger = spdlog::stderr_color_mt("test");
39  Logger->set_level(spdlog::level::info);
42  }
43 
44  virtual void TearDown()
45  {
46  NameSpace.reset();
47  }
48 
50  {
53  item.BrowseName = OpcUa::QualifiedName("value");
56  std::vector<OpcUa::AddNodesResult> newNodesResult = NameSpace->AddNodes({item});
57  return newNodesResult[0].AddedNodeId;
58  }
59 
60 protected:
61  OpcUa::Server::AddressSpace::UniquePtr NameSpace;
62  Common::Logger::SharedPtr Logger;
63 };
64 
65 TEST_F(AddressSpace, GeneratesNodeIdIfPassNull)
66 {
67  OpcUa::AddNodesItem newNode;
68  newNode.BrowseName.Name = "newNode";
70  std::vector<OpcUa::AddNodesResult> results = NameSpace->AddNodes({newNode});
71  ASSERT_EQ(results.size(), 1);
72  const OpcUa::AddNodesResult & result = results[0];
73  EXPECT_EQ(result.Status, OpcUa::StatusCode::Good);
74  EXPECT_NE(result.AddedNodeId, OpcUa::NodeId(OpcUa::ObjectId::Null));
75 }
76 
77 TEST_F(AddressSpace, GeneratesIfNodeIdDuplicated)
78 {
79  OpcUa::AddNodesItem newNode;
80  newNode.BrowseName.Name = "newNode";
83  std::vector<OpcUa::AddNodesResult> results = NameSpace->AddNodes({newNode});
84  ASSERT_EQ(results.size(), 1);
85  const OpcUa::AddNodesResult & result = results[0];
87  EXPECT_EQ(result.AddedNodeId, OpcUa::ObjectId::Null);
88 }
89 
90 TEST_F(AddressSpace, ReadAttributes)
91 {
92  OpcUa::ReadParameters readParams;
94  readParams.AttributesToRead.push_back(value);
95  std::vector<OpcUa::DataValue> results = NameSpace->Read(readParams);
96  ASSERT_EQ(results.size(), 1);
97  const OpcUa::DataValue & result = results[0];
98  ASSERT_NE(result.Encoding | OpcUa::DATA_VALUE, 0);
100 }
101 
102 TEST_F(AddressSpace, CallsDataChangeCallbackOnWrite)
103 {
104  OpcUa::NodeId valueId = CreateValue();
105  OpcUa::NodeId callbackId;
106  OpcUa::AttributeId callbackAttr;
107  OpcUa::DataValue callbackValue;
108  bool callbackCalled = false;
109  unsigned callbackHandle = NameSpace->AddDataChangeCallback(valueId, OpcUa::AttributeId::Value, [&](const OpcUa::NodeId & id, OpcUa::AttributeId attr, const OpcUa::DataValue & value)
110  {
111  callbackId = id;
112  callbackAttr = attr;
113  callbackValue = value;
114  callbackCalled = true;
115  });
116 
117  EXPECT_NE(callbackHandle, 0);
118 
119  OpcUa::WriteValue value;
121  value.NodeId = valueId;
122  value.Value = 10;
123  std::vector<OpcUa::StatusCode> result = NameSpace->Write({value});
124  ASSERT_EQ(result.size(), 1);
126  EXPECT_EQ(callbackId, valueId);
127  EXPECT_EQ(callbackValue, 10);
128  EXPECT_EQ(callbackAttr, OpcUa::AttributeId::Value);
129 
130  callbackCalled = false;
131  NameSpace->DeleteDataChangeCallback(callbackHandle);
132  result = NameSpace->Write({value});
133  ASSERT_EQ(result.size(), 1);
135  ASSERT_FALSE(callbackCalled);
136 }
137 
138 TEST_F(AddressSpace, ValueCallbackIsCalled)
139 {
140  OpcUa::NodeId valueId = CreateValue();
141  OpcUa::StatusCode code = NameSpace->SetValueCallback(valueId, OpcUa::AttributeId::Value, []()
142  {
143  return OpcUa::DataValue(10);
144  });
145 
147 
148  OpcUa::ReadParameters readParams;
149  readParams.AttributesToRead.push_back(OpcUa::ToReadValueId(valueId, OpcUa::AttributeId::Value));
150  std::vector<OpcUa::DataValue> result = NameSpace->Read(readParams);
151  ASSERT_EQ(result.size(), 1);
152  EXPECT_TRUE(result[0].Encoding & OpcUa::DATA_VALUE);
153  EXPECT_EQ(result[0].Value, 10);
154 }
std::vector< OpcUa::ReadValueId > AttributesToRead
OpcUa::NodeId CreateValue()
ReadValueId ToReadValueId(NodeId id, AttributeId attr)
Definition: protocol.h:7
TEST_F(TestInfoTest, Names)
#define EXPECT_TRUE(condition)
virtual void SetUp()
static Server::AddressSpace::SharedPtr NameSpace
#define ASSERT_NE(val1, val2)
Common::Logger::SharedPtr Logger
bool Value(const T &value, M matcher)
void FillStandardNamespace(OpcUa::NodeManagementServices &registry, const Common::Logger::SharedPtr &logger)
#define EXPECT_NE(expected, actual)
const char Root[]
Definition: strings.h:137
#define ASSERT_EQ(val1, val2)
std::string Name
Definition: types.h:74
const uint8_t DATA_VALUE
Definition: data_value.h:18
virtual void TearDown()
OpcUa::DataValue Value
AddressSpace::UniquePtr CreateAddressSpace(const Common::Logger::SharedPtr &logger)
OpcUa::Server::AddressSpace::UniquePtr NameSpace
OpcUa::NodeId NodeId
#define EXPECT_EQ(expected, actual)
std::shared_ptr< logger > stderr_color_mt(const std::string &logger_name)
Definition: spdlog_impl.h:150
#define ASSERT_FALSE(condition)
OpcUa::AttributeId AttributeId
void drop_all()
Definition: spdlog_impl.h:260


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