5 #include <boost/test/included/unit_test.hpp>
9 using namespace boost::unit_test_framework;
17 static void testClassSide();
19 static void testClientSide();
23 std::cout <<
"Enter AdditionalVariableTest with EmptySet" << std::endl;
33 string additionalVariableRootFolder =
"AdditionalVars";
34 UA_ObjectAttributes oAttr;
35 memset(&oAttr, 0,
sizeof(UA_ObjectAttributes));
36 UA_Server_addObjectNode(serverSet->
mappedServer, UA_NODEID_STRING(1, (
char*)additionalVariableRootFolder.c_str()),
37 UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
38 UA_QUALIFIEDNAME(0, (
char*)
"AdditionalVarsFolder"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL,
42 UA_NODEID_STRING(1, (
char*)additionalVariableRootFolder.c_str()),
"Name",
"Value",
"Description");
45 BOOST_CHECK(addVar1.
getValue() ==
"Value");
49 if(serverThread->joinable()) {
61 std::cout <<
"Enter AdditionalVariableTest with ExampleSet and ClientSide testing" << std::endl;
72 string additionalVariableRootFolder =
"AdditionalVars";
73 UA_ObjectAttributes oAttr;
74 memset(&oAttr, 0,
sizeof(UA_ObjectAttributes));
75 UA_Server_addObjectNode(serverSet->
mappedServer, UA_NODEID_STRING(1, (
char*)additionalVariableRootFolder.c_str()),
76 UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
77 UA_QUALIFIEDNAME(0, (
char*)
"AdditionalVarsFolder"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), oAttr, NULL,
81 UA_NODEID_STRING(1, (
char*)additionalVariableRootFolder.c_str()),
"Name",
"Value",
"Description");
84 UA_Client* client = UA_Client_new();
85 UA_ClientConfig* cc = UA_Client_getConfig(client);
86 UA_ClientConfig_setDefault(cc);
87 string endpointURL =
"opc.tcp://localhost:" + to_string(serverSet->
opcuaPort);
88 UA_StatusCode retval = UA_Client_connect(client, endpointURL.c_str());
91 while(retval != UA_STATUSCODE_GOOD & k < 10) {
92 retval = UA_Client_connect(client, endpointURL.c_str());
97 if(retval != UA_STATUSCODE_GOOD) {
98 std::cout <<
"Failed to connect to server"
99 <<
"opc.tcp://localhost:" << serverSet->
opcuaPort << std::endl;
103 UA_BrowseRequest bReq;
104 UA_BrowseRequest_init(&bReq);
105 bReq.requestedMaxReferencesPerNode = 0;
106 bReq.nodesToBrowse = UA_BrowseDescription_new();
107 bReq.nodesToBrowseSize = 1;
108 bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
109 bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
110 UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
112 for(
size_t i = 0; i < bResp.resultsSize; ++i) {
113 for(
size_t j = 0; j < bResp.results[i].referencesSize; ++j) {
114 UA_ReferenceDescription* ref = &(bResp.results[i].references[j]);
115 if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
116 size_t lenBrowseName = (int)ref->browseName.name.length;
117 std::string browseNameFound(
reinterpret_cast<char const*
>(ref->browseName.name.data), lenBrowseName);
119 UA_BrowseRequest bReq2;
120 UA_BrowseRequest_init(&bReq2);
121 bReq2.requestedMaxReferencesPerNode = 0;
122 bReq2.nodesToBrowse = UA_BrowseDescription_new();
123 bReq2.nodesToBrowseSize = 1;
124 bReq2.nodesToBrowse[0].nodeId = ref->nodeId.nodeId;
125 bReq2.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL;
126 UA_BrowseResponse bResp2 = UA_Client_Service_browse(client, bReq2);
128 UA_NodeId valueNodeId = UA_NODEID_NULL;
131 UA_ReferenceDescription* refe;
132 for(
size_t m = 0; m < bResp2.resultsSize; ++m) {
133 for(
size_t k = 0; k < bResp2.results[m].referencesSize; ++k) {
134 refe = &(bResp2.results[m].references[k]);
135 if(refe->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
136 size_t lenBrowseName2 = (int)refe->browseName.name.length;
137 string browseNameFound2(
reinterpret_cast<char const*
>(refe->browseName.name.data), lenBrowseName2);
139 if(browseNameFound2 ==
"Value") {
140 valueNodeId = refe->nodeId.nodeId;
141 name = browseNameFound;
147 UA_BrowseRequest_clear(&bReq2);
148 UA_BrowseResponse_clear(&bResp2);
150 if(!UA_NodeId_isNull(&valueNodeId)) {
151 UA_Variant valueToCheck;
152 UA_Variant_init(&valueToCheck);
153 UA_StatusCode retvalValue = UA_Client_readValueAttribute(client, valueNodeId, &valueToCheck);
154 if(retvalValue != UA_STATUSCODE_GOOD) {
158 UA_Variant datatypeToCheck;
159 UA_Variant_init(&datatypeToCheck);
160 UA_StatusCode retvalValueDatatype = UA_Client_readValueAttribute(client, valueNodeId, &datatypeToCheck);
161 if(retvalValueDatatype != UA_STATUSCODE_GOOD) {
165 UA_NodeId datatypeId;
166 if(retvalValue == UA_STATUSCODE_GOOD) {
167 UA_StatusCode retvalDatatype = UA_Client_readDataTypeAttribute(client, valueNodeId, &datatypeId);
168 if(retvalDatatype != UA_STATUSCODE_GOOD) {
176 if(retvalValue == UA_STATUSCODE_GOOD) {
177 string datatype =
"";
183 BOOST_CHECK(valName ==
"Value");
185 BOOST_CHECK((datatypeId.identifier.numeric - 1) == UA_TYPES_STRING);
190 UA_Variant_clear(&valueToCheck);
191 UA_Variant_clear(&datatypeToCheck);
197 UA_BrowseRequest_clear(&bReq);
198 UA_BrowseResponse_clear(&bResp);
200 UA_Client_disconnect(client);
201 UA_Client_delete(client);
205 if(serverThread->joinable()) {
206 serverThread->join();
210 cout <<
"Delete ServerSet" << endl;
213 cout <<
"Delete ServerThread" << endl;