address_space_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 #include <opc/ua/protocol/object_ids.h>
00021 #include <opc/ua/protocol/attribute_ids.h>
00022 #include <opc/ua/protocol/status_codes.h>
00023 
00024 #include <opc/ua/server/address_space.h>
00025 #include <opc/ua/server/standard_address_space.h>
00026 
00027 #include <gmock/gmock.h>
00028 #include <gtest/gtest.h>
00029 
00030 using namespace testing;
00031 
00032 class AddressSpace : public Test
00033 {
00034 protected:
00035   virtual void SetUp()
00036   {
00037     const bool debug = false;
00038     NameSpace = OpcUa::Server::CreateAddressSpace(debug);
00039     OpcUa::Server::FillStandardNamespace(*NameSpace, debug);
00040   }
00041 
00042   virtual void TearDown()
00043   {
00044     NameSpace.reset();
00045   }
00046 
00047   OpcUa::NodeId CreateValue()
00048   {
00049     OpcUa::AddNodesItem item;
00050     item.Attributes = OpcUa::VariableAttributes();
00051     item.BrowseName = OpcUa::QualifiedName("value");
00052     item.Class = OpcUa::NodeClass::Variable;
00053     item.ParentNodeId = OpcUa::ObjectId::RootFolder;
00054     std::vector<OpcUa::AddNodesResult> newNodesResult = NameSpace->AddNodes({item});
00055     return newNodesResult[0].AddedNodeId;
00056   }
00057 
00058 protected:
00059   OpcUa::Server::AddressSpace::UniquePtr NameSpace;
00060 };
00061 
00062 TEST_F(AddressSpace, GeneratesNodeIdIfPassNull)
00063 {
00064   OpcUa::AddNodesItem newNode;
00065   newNode.BrowseName.Name = "newNode";
00066   newNode.Attributes = OpcUa::ObjectAttributes();
00067   std::vector<OpcUa::AddNodesResult> results = NameSpace->AddNodes({newNode});
00068   ASSERT_EQ(results.size(), 1);
00069   const OpcUa::AddNodesResult& result = results[0];
00070   EXPECT_EQ(result.Status, OpcUa::StatusCode::Good);
00071   EXPECT_NE(result.AddedNodeId, OpcUa::NodeId(OpcUa::ObjectId::Null));
00072 }
00073 
00074 TEST_F(AddressSpace, GeneratesIfNodeIdDuplicated)
00075 {
00076   OpcUa::AddNodesItem newNode;
00077   newNode.BrowseName.Name = "newNode";
00078   newNode.RequestedNewNodeId = OpcUa::ObjectId::RootFolder;
00079   newNode.Attributes = OpcUa::ObjectAttributes();
00080   std::vector<OpcUa::AddNodesResult> results = NameSpace->AddNodes({newNode});
00081   ASSERT_EQ(results.size(), 1);
00082   const OpcUa::AddNodesResult& result = results[0];
00083   EXPECT_EQ(result.Status, OpcUa::StatusCode::BadNodeIdExists);
00084   EXPECT_EQ(result.AddedNodeId, OpcUa::ObjectId::Null);
00085 }
00086 
00087 TEST_F(AddressSpace, ReadAttributes)
00088 {
00089   OpcUa::ReadParameters readParams;
00090   OpcUa::ReadValueId value = ToReadValueId(OpcUa::ObjectId::RootFolder, OpcUa::AttributeId::BrowseName);
00091   readParams.AttributesToRead.push_back(value);
00092   std::vector<OpcUa::DataValue> results = NameSpace->Read(readParams);
00093   ASSERT_EQ(results.size(), 1);
00094   const OpcUa::DataValue& result = results[0];
00095   ASSERT_NE(result.Encoding | OpcUa::DATA_VALUE, 0);
00096   EXPECT_EQ(result.Value, OpcUa::QualifiedName(OpcUa::Names::Root));
00097 }
00098 
00099 TEST_F(AddressSpace, CallsDataChangeCallbackOnWrite)
00100 {
00101   OpcUa::NodeId valueId = CreateValue();
00102   OpcUa::NodeId callbackId;
00103   OpcUa::AttributeId callbackAttr;
00104   OpcUa::DataValue callbackValue;
00105   bool callbackCalled = false;
00106   unsigned callbackHandle = NameSpace->AddDataChangeCallback(valueId, OpcUa::AttributeId::Value, [&](const OpcUa::NodeId& id, OpcUa::AttributeId attr, const OpcUa::DataValue& value){
00107     callbackId = id;
00108     callbackAttr = attr;
00109     callbackValue = value;
00110     callbackCalled = true;
00111   });
00112 
00113   EXPECT_NE(callbackHandle, 0);
00114 
00115   OpcUa::WriteValue value;
00116   value.AttributeId = OpcUa::AttributeId::Value;
00117   value.NodeId = valueId;
00118   value.Value = 10;
00119   std::vector<OpcUa::StatusCode> result = NameSpace->Write({value});
00120   ASSERT_EQ(result.size(), 1);
00121   EXPECT_EQ(result[0], OpcUa::StatusCode::Good);
00122   EXPECT_EQ(callbackId, valueId);
00123   EXPECT_EQ(callbackValue, 10);
00124   EXPECT_EQ(callbackAttr, OpcUa::AttributeId::Value);
00125 
00126   callbackCalled = false;
00127   NameSpace->DeleteDataChangeCallback(callbackHandle);
00128   result = NameSpace->Write({value});
00129   ASSERT_EQ(result.size(), 1);
00130   EXPECT_EQ(result[0], OpcUa::StatusCode::Good);
00131   ASSERT_FALSE(callbackCalled);
00132 }
00133 
00134 TEST_F(AddressSpace, ValueCallbackIsCalled)
00135 {
00136   OpcUa::NodeId valueId = CreateValue();
00137   OpcUa::StatusCode code = NameSpace->SetValueCallback(valueId, OpcUa::AttributeId::Value, [](){
00138     return OpcUa::DataValue(10);
00139   });
00140 
00141   ASSERT_EQ(code, OpcUa::StatusCode::Good);
00142 
00143   OpcUa::ReadParameters readParams;
00144   readParams.AttributesToRead.push_back(OpcUa::ToReadValueId(valueId, OpcUa::AttributeId::Value));
00145   std::vector<OpcUa::DataValue> result = NameSpace->Read(readParams);
00146   ASSERT_EQ(result.size(), 1);
00147   EXPECT_TRUE(result[0].Encoding & OpcUa::DATA_VALUE);
00148   EXPECT_EQ(result[0].Value, 10);
00149 }


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