-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathagent_device_test.cpp
More file actions
404 lines (331 loc) · 12.9 KB
/
Copy pathagent_device_test.cpp
File metadata and controls
404 lines (331 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// Copyright 2009-2026, AMT – The Association For Manufacturing Technology (“AMT”)
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/// @file
/// Tests related to agent device
// Ensure that gtest is the first header otherwise Windows raises an error
#include <gtest/gtest.h>
// Keep this comment to keep gtest.h above. (clang-format off/on is not working here!)
#include <boost/asio/read_until.hpp>
#include <boost/asio/write.hpp>
#include "agent_test_helper.hpp"
#include "json_helper.hpp"
#include "mtconnect/agent.hpp"
#include "mtconnect/device_model/agent_device.hpp"
#include "mtconnect/source/adapter/adapter.hpp"
#include "mtconnect/version.h"
using namespace std;
using namespace mtconnect;
using namespace mtconnect::source::adapter;
using namespace device_model;
using namespace entity;
namespace asio = boost::asio;
using tcp = boost::asio::ip::tcp;
namespace ip = boost::asio::ip;
namespace sys = boost::system;
namespace config = mtconnect::configuration;
// main
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class AgentDeviceTest : public testing::Test
{
protected:
void SetUp() override
{
m_agentTestHelper = make_unique<AgentTestHelper>();
auto version = to_string(AGENT_VERSION_MAJOR) + "." + to_string(AGENT_VERSION_MINOR);
m_agentTestHelper->createAgent("/samples/test_config.xml", 8, 4, version, 25);
m_agentId = to_string(getCurrentTimeInSec());
m_agentDevice = m_agentTestHelper->m_agent->getAgentDevice();
}
void TearDown() override
{
m_agentTestHelper->m_ioContext.stop();
if (m_server)
m_server->close();
m_server.reset();
m_acceptor.reset();
m_agentTestHelper.reset();
}
void addAdapter(bool suppress = false)
{
ConfigOptions options {{config::SuppressIPAddress, suppress}};
m_agentTestHelper->addAdapter(options, "127.0.0.1", m_port, "LinuxCNC");
m_agentTestHelper->m_adapter->setReconnectInterval(1s);
}
void startServer(const std::string addr = "127.0.0.1")
{
tcp::endpoint sp(ip::make_address(addr), m_port);
m_connected = false;
m_acceptor = make_unique<tcp::acceptor>(m_agentTestHelper->m_ioContext, sp);
ASSERT_TRUE(m_acceptor->is_open());
tcp::endpoint ep = m_acceptor->local_endpoint();
m_port = ep.port();
m_acceptor->async_accept([this](sys::error_code ec, tcp::socket socket) {
if (ec)
cout << ec.category().message(ec.value()) << ": " << ec.message() << endl;
ASSERT_FALSE(ec);
ASSERT_TRUE(socket.is_open());
m_connected = true;
m_server = make_unique<tcp::socket>(std::move(socket));
});
}
template <typename Rep, typename Period>
void runUntil(chrono::duration<Rep, Period> to, function<bool()> pred)
{
for (int runs = 0; runs < 10 && !pred(); runs++)
{
m_agentTestHelper->m_ioContext.run_one_for(to);
}
EXPECT_TRUE(pred());
}
public:
AgentDevicePtr m_agentDevice;
std::string m_agentId;
std::unique_ptr<AgentTestHelper> m_agentTestHelper;
unsigned short m_port {0};
std::unique_ptr<asio::ip::tcp::socket> m_server;
std::unique_ptr<tcp::acceptor> m_acceptor;
bool m_connected;
string m_line;
};
/// @test check if the agent device was added to the agent
TEST_F(AgentDeviceTest, should_create_the_agent_device)
{
ASSERT_NE(nullptr, m_agentDevice);
ASSERT_EQ(2, m_agentTestHelper->m_agent->getDevices().size());
/// - Veriify the name of the agent device is Agent
ASSERT_EQ("Agent", m_agentDevice->getName().str());
}
/// @test check that the data items for agent and device added, removed, and changed were added
TEST_F(AgentDeviceTest, should_add_data_items_to_the_agent_devcie)
{
ASSERT_NE(nullptr, m_agentDevice);
auto avail = m_agentDevice->getDeviceDataItem("agent_avail");
ASSERT_NE(nullptr, avail);
ASSERT_EQ("AVAILABILITY", avail->getType());
auto added = m_agentDevice->getDeviceDataItem("device_added");
ASSERT_NE(nullptr, added);
ASSERT_EQ("DEVICE_ADDED", added->getType());
auto removed = m_agentDevice->getDeviceDataItem("device_removed");
ASSERT_NE(nullptr, removed);
ASSERT_EQ("DEVICE_REMOVED", removed->getType());
auto changed = m_agentDevice->getDeviceDataItem("device_changed");
ASSERT_NE(nullptr, changed);
ASSERT_EQ("DEVICE_CHANGED", changed->getType());
}
/// @test verify device added was updated
TEST_F(AgentDeviceTest, should_have_device_added_in_buffer)
{
auto agent = m_agentTestHelper->getAgent();
auto device = agent->findDeviceByUUIDorName("000");
auto &circ = agent->getCircularBuffer();
ASSERT_TRUE(device);
auto uuid = *device->getUuid();
ASSERT_EQ("000", uuid);
auto found = false;
auto rest = m_agentTestHelper->getRestService();
for (auto seq = circ.getSequence() - 1; !found && seq > 0ull; seq--)
{
auto event = circ.getFromBuffer(seq);
if (event->getDataItem()->getType() == "DEVICE_ADDED" && uuid == event->getValue<string>())
{
found = true;
}
}
ASSERT_TRUE(found);
}
#define AGENT_PATH "//m:Agent"
#define AGENT_DATA_ITEMS_PATH AGENT_PATH "/m:DataItems"
#define ADAPTERS_PATH AGENT_PATH "/m:Components/m:Adapters"
#define ADAPTER_PATH ADAPTERS_PATH "/m:Components/m:Adapter"
#define ADAPTER_DATA_ITEMS_PATH ADAPTER_PATH "/m:DataItems"
#define ID_PREFIX "_127.0.0.1_21788"
#define ID_PREFIX_SUPP "_d0c33d4315"
/// @test verify adapter component is added
TEST_F(AgentDeviceTest, should_add_component_and_data_items_for_adapter)
{
m_port = 21788;
addAdapter();
{
PARSE_XML_RESPONSE("/Agent/probe");
auto version = to_string(AGENT_VERSION_MAJOR) + "." + to_string(AGENT_VERSION_MINOR);
ASSERT_XML_PATH_EQUAL(doc, AGENT_PATH "@mtconnectVersion", version.c_str());
ASSERT_XML_PATH_COUNT(doc, ADAPTERS_PATH "/*", 1);
ASSERT_XML_PATH_EQUAL(doc, ADAPTER_PATH "@id", ID_PREFIX);
ASSERT_XML_PATH_EQUAL(doc, ADAPTER_PATH "@name", "shdr://127.0.0.1:21788");
ASSERT_XML_PATH_EQUAL(
doc, ADAPTER_DATA_ITEMS_PATH "/m:DataItem[@id='" ID_PREFIX "_adapter_uri']@type",
"ADAPTER_URI");
ASSERT_XML_PATH_EQUAL(doc,
ADAPTER_DATA_ITEMS_PATH "/m:DataItem[@id='" ID_PREFIX
"_adapter_uri']/m:Constraints/m:Value",
m_agentTestHelper->m_adapter->getName().c_str());
}
}
/// @test check that the ip address was suppressed when requested
TEST_F(AgentDeviceTest, should_suppress_address_ip_address_when_configured)
{
m_port = 21788;
addAdapter(true);
{
PARSE_XML_RESPONSE("/Agent/probe");
ASSERT_XML_PATH_COUNT(doc, ADAPTERS_PATH "/*", 1);
ASSERT_XML_PATH_EQUAL(doc, ADAPTER_PATH "@id", ID_PREFIX_SUPP);
ASSERT_XML_PATH_EQUAL(doc, ADAPTER_PATH "@name", "LinuxCNC");
ASSERT_XML_PATH_COUNT(
doc, ADAPTER_DATA_ITEMS_PATH "/m:DataItem[@id='" ID_PREFIX_SUPP "_adapter_uri']", 0);
}
}
#define AGENT_DEVICE_STREAM "//m:DeviceStream[@name='Agent']"
#define AGENT_DEVICE_DEVICE_STREAM AGENT_DEVICE_STREAM "/m:ComponentStream[@component='Agent']"
#define AGENT_DEVICE_ADAPTER_STREAM AGENT_DEVICE_STREAM "/m:ComponentStream[@component='Adapter']"
/// @test verify the data items for the adapter were added and populated
TEST_F(AgentDeviceTest, should_observe_the_adapter_data_items)
{
{
PARSE_XML_RESPONSE("/Agent/current");
}
addAdapter();
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_DEVICE_STREAM "/m:Events/m:Availability", "AVAILABLE");
ASSERT_XML_PATH_COUNT(doc, AGENT_DEVICE_STREAM "/*", 2);
ASSERT_XML_PATH_COUNT(doc, AGENT_DEVICE_DEVICE_STREAM "/*", 1);
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_DEVICE_STREAM "/m:Events/m:DeviceAdded", "000");
ASSERT_XML_PATH_COUNT(doc, AGENT_DEVICE_ADAPTER_STREAM "/*", 2);
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:AdapterURI",
m_agentTestHelper->m_adapter->getName().c_str());
}
}
/// @test checks the adapter connection status updates when the adapter connects and disconnects
TEST_F(AgentDeviceTest, should_track_adapter_connection_status)
{
srand(int32_t(chrono::system_clock::now().time_since_epoch().count()));
m_port = rand() % 10000 + 5000;
addAdapter();
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:AdapterURI",
m_agentTestHelper->m_adapter->getName().c_str());
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:ConnectionStatus",
"UNAVAILABLE");
}
m_agentTestHelper->m_adapter->start();
m_agentTestHelper->m_ioContext.run_for(1500ms);
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:ConnectionStatus",
"LISTEN");
}
startServer();
runUntil(10s, [this]() { return m_connected; });
m_agentTestHelper->m_ioContext.run_for(10ms);
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:ConnectionStatus",
"ESTABLISHED");
}
ASSERT_TRUE(m_server);
m_server->close();
m_acceptor->close();
runUntil(1s, [this]() { return !m_agentTestHelper->m_adapter->isConnected(); });
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:ConnectionStatus",
"CLOSED");
}
m_agentTestHelper->m_ioContext.run_for(1500ms);
{
PARSE_XML_RESPONSE("/Agent/current");
ASSERT_XML_PATH_EQUAL(doc, AGENT_DEVICE_ADAPTER_STREAM "/m:Events/m:ConnectionStatus",
"LISTEN");
}
}
/// @test verify the Agent Device uuid can be set in the configuration file
TEST_F(AgentDeviceTest, verify_uuid_can_be_set_in_configuration)
{
m_agentTestHelper = make_unique<AgentTestHelper>();
auto version = to_string(AGENT_VERSION_MAJOR) + "." + to_string(AGENT_VERSION_MINOR);
m_agentTestHelper->createAgent("/samples/test_config.xml", 8, 4, version, 25, false, true,
{{mtconnect::configuration::AgentDeviceUUID, "HELLO_KITTY"s}});
m_agentId = to_string(getCurrentTimeInSec());
m_agentDevice = m_agentTestHelper->m_agent->getAgentDevice();
ASSERT_EQ("HELLO_KITTY", *m_agentDevice->getUuid());
}
/// @test validate the use of deviceType rest parameter to select only the Agent or Devices for
/// probe
TEST_F(AgentDeviceTest, should_only_return_only_devices_of_device_type_for_probe)
{
using namespace mtconnect::sink::rest_sink;
m_port = 21788;
addAdapter();
{
QueryMap query {{"deviceType", "Agent"}};
PARSE_XML_RESPONSE_QUERY("/probe", query);
ASSERT_XML_PATH_COUNT(doc, "//m:Device", 0);
ASSERT_XML_PATH_COUNT(doc, "//m:Agent", 1);
}
{
QueryMap query {{"deviceType", "Device"}};
PARSE_XML_RESPONSE_QUERY("/probe", query);
ASSERT_XML_PATH_COUNT(doc, "//m:Device", 1);
ASSERT_XML_PATH_COUNT(doc, "//m:Agent", 0);
}
}
/// @test validate the use of deviceType rest parameter to select only the Agent or Devices for
/// current
TEST_F(AgentDeviceTest, should_only_return_only_devices_of_device_type_for_current)
{
using namespace mtconnect::sink::rest_sink;
m_port = 21788;
addAdapter();
{
QueryMap query {{"deviceType", "Agent"}};
PARSE_XML_RESPONSE_QUERY("/current", query);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='Agent']", 1);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='LinuxCNC']", 0);
}
{
QueryMap query {{"deviceType", "Device"}};
PARSE_XML_RESPONSE_QUERY("/current", query);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='Agent']", 0);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='LinuxCNC']", 1);
}
}
/// @test validate the use of deviceType rest parameter to select only the Agent or Devices for
/// sample
TEST_F(AgentDeviceTest, should_only_return_only_devices_of_device_type_for_sample)
{
using namespace mtconnect::sink::rest_sink;
m_port = 21788;
addAdapter();
{
QueryMap query {{"deviceType", "Agent"}};
PARSE_XML_RESPONSE_QUERY("/sample", query);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='Agent']", 1);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='LinuxCNC']", 0);
}
{
QueryMap query {{"deviceType", "Device"}};
PARSE_XML_RESPONSE_QUERY("/sample", query);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='Agent']", 0);
ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream[@name='LinuxCNC']", 1);
}
}