Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 }