ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
test_opcua_additionalvariable.cpp
Go to the documentation of this file.
1 #include "ua_typeconversion.h"
3 #include <test_sample_data.h>
4 
5 #include <boost/test/included/unit_test.hpp>
6 
7 #include <string.h>
8 
9 using namespace boost::unit_test_framework;
10 
11 /*
12  * ProcessVariableTest
13  *
14  */
16  public:
17  static void testClassSide();
18 
19  static void testClientSide();
20 };
21 
23  std::cout << "Enter AdditionalVariableTest with EmptySet" << std::endl;
24 
26  thread* serverThread = new std::thread(UA_Server_run, serverSet->mappedServer, &serverSet->runUAServer);
27 
28  // check server
29  if(serverSet->mappedServer == nullptr) {
30  BOOST_CHECK(false);
31  }
32 
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,
39  NULL);
40 
41  ua_additionalvariable addVar1(serverSet->mappedServer,
42  UA_NODEID_STRING(1, (char*)additionalVariableRootFolder.c_str()), "Name", "Value", "Description");
43 
44  BOOST_CHECK(addVar1.getSourceTimeStamp() != 0);
45  BOOST_CHECK(addVar1.getValue() == "Value");
46 
47  serverSet->runUAServer = UA_FALSE;
48 
49  if(serverThread->joinable()) {
50  serverThread->join();
51  }
52 
53  delete serverSet;
54  serverSet = NULL;
55 
56  delete serverThread;
57  serverThread = NULL;
58 }
59 
61  std::cout << "Enter AdditionalVariableTest with ExampleSet and ClientSide testing" << std::endl;
62 
64 
65  thread* serverThread = new std::thread(UA_Server_run, serverSet->mappedServer, &serverSet->runUAServer);
66 
67  // check server
68  if(serverSet->mappedServer == nullptr) {
69  BOOST_CHECK(false);
70  }
71 
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,
78  NULL);
79  // add set
80  ua_additionalvariable addVar1(serverSet->mappedServer,
81  UA_NODEID_STRING(1, (char*)additionalVariableRootFolder.c_str()), "Name", "Value", "Description");
82 
83  // Create client to connect to server
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());
89  sleep(1);
90  int k = 1;
91  while(retval != UA_STATUSCODE_GOOD & k < 10) {
92  retval = UA_Client_connect(client, endpointURL.c_str());
93  sleep(1);
94  k++;
95  }
96 
97  if(retval != UA_STATUSCODE_GOOD) {
98  std::cout << "Failed to connect to server"
99  << "opc.tcp://localhost:" << serverSet->opcuaPort << std::endl;
100  BOOST_CHECK(false);
101  }
102 
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; // return everything
110  UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq);
111 
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);
118 
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);
127 
128  UA_NodeId valueNodeId = UA_NODEID_NULL;
129  string name = "";
130 
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);
138 
139  if(browseNameFound2 == "Value") {
140  valueNodeId = refe->nodeId.nodeId;
141  name = browseNameFound;
142  // cout << "Checking ProcessVariable: " << name << endl;
143  }
144  }
145  }
146  }
147  UA_BrowseRequest_clear(&bReq2);
148  UA_BrowseResponse_clear(&bResp2);
149 
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) {
155  BOOST_CHECK(false);
156  }
157 
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) {
162  BOOST_CHECK(false);
163  }
164 
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) {
169  BOOST_CHECK(false);
170  }
171  }
172  else {
173  BOOST_CHECK(false);
174  }
175 
176  if(retvalValue == UA_STATUSCODE_GOOD) {
177  string datatype = "";
178  string valName = "";
179 
180  // Check Description -> for all the same
181  UASTRING_TO_CPPSTRING(((UA_String) * ((UA_String*)valueToCheck.data)), valName)
182  // cout << "Description: " << valName << endl;
183  BOOST_CHECK(valName == "Value");
184 
185  BOOST_CHECK((datatypeId.identifier.numeric - 1) == UA_TYPES_STRING);
186  }
187  else {
188  BOOST_CHECK(false);
189  }
190  UA_Variant_clear(&valueToCheck);
191  UA_Variant_clear(&datatypeToCheck);
192  }
193  }
194  }
195  }
196 
197  UA_BrowseRequest_clear(&bReq);
198  UA_BrowseResponse_clear(&bResp);
199 
200  UA_Client_disconnect(client);
201  UA_Client_delete(client);
202 
203  serverSet->runUAServer = UA_FALSE;
204 
205  if(serverThread->joinable()) {
206  serverThread->join();
207  }
208 
209  // serverSet->server_nl.deleteMembers(&serverSet->server_nl);
210  cout << "Delete ServerSet" << endl;
211  delete serverSet;
212  serverSet = NULL;
213  cout << "Delete ServerThread" << endl;
214  delete serverThread;
215  serverThread = NULL;
216 }
217 
218 class AdditionalVariableTestSuite : public test_suite {
219  public:
220  AdditionalVariableTestSuite() : test_suite("ua_additionalvariable Test Suite") {
221  add(BOOST_TEST_CASE(&AdditionalVariableTest::testClassSide));
222  add(BOOST_TEST_CASE(&AdditionalVariableTest::testClientSide));
223  }
224 };
225 
226 test_suite* init_unit_test_suite(int /*argc*/, char** /*argv[]*/) {
227  framework::master_test_suite().add(new AdditionalVariableTestSuite);
228  return 0;
229 }
AdditionalVariableTest::testClassSide
static void testClassSide()
Definition: test_opcua_additionalvariable.cpp:22
ChimeraTK::ua_additionalvariable::getSourceTimeStamp
UA_DateTime getSourceTimeStamp()
Get the SourceTimeStamp from node in the OPC UA server Virtual methode which returned Timestamp is se...
Definition: csa_additionalvariable.cpp:157
TestFixtureServerSet::runUAServer
UA_Boolean runUAServer
Definition: test_sample_data.h:99
ChimeraTK::ua_additionalvariable::getValue
string getValue()
Definition: csa_additionalvariable.cpp:77
AdditionalVariableTest::testClientSide
static void testClientSide()
Definition: test_opcua_additionalvariable.cpp:60
csa_additionalvariable.h
AdditionalVariableTestSuite
Definition: test_opcua_additionalvariable.cpp:218
AdditionalVariableTest
Definition: test_opcua_additionalvariable.cpp:15
test_sample_data.h
TestFixtureServerSet::mappedServer
UA_Server * mappedServer
Definition: test_sample_data.h:97
UASTRING_TO_CPPSTRING
#define UASTRING_TO_CPPSTRING(_p_uastring, _p_cppstring)
Definition: ua_typeconversion.h:53
TestFixtureServerSet
Definition: test_sample_data.h:93
AdditionalVariableTestSuite::AdditionalVariableTestSuite
AdditionalVariableTestSuite()
Definition: test_opcua_additionalvariable.cpp:220
ChimeraTK::ua_additionalvariable
This class represent a additional variable from <variableMap.xml> in the information model of a OPC U...
Definition: csa_additionalvariable.h:36
ua_typeconversion.h
init_unit_test_suite
test_suite * init_unit_test_suite(int, char **)
Definition: test_opcua_additionalvariable.cpp:226
TestFixtureServerSet::opcuaPort
uint32_t opcuaPort
Definition: test_sample_data.h:94