-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathentity_printer_test.cpp
More file actions
452 lines (365 loc) · 13.8 KB
/
Copy pathentity_printer_test.cpp
File metadata and controls
452 lines (365 loc) · 13.8 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//
// 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.
//
// 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 <cstdio>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <libxml/xmlwriter.h>
#include "mtconnect/agent.hpp"
#include "mtconnect/entity/entity.hpp"
#include "mtconnect/entity/xml_parser.hpp"
#include "mtconnect/entity/xml_printer.hpp"
#include "mtconnect/printer//xml_printer_helper.hpp"
#include "mtconnect/source/adapter/adapter.hpp"
using namespace std;
using namespace mtconnect;
using namespace mtconnect::entity;
using namespace std::literals;
// main
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class EntityPrinterTest : public testing::Test
{
protected:
void SetUp() override { m_writer = make_unique<printer::XmlWriter>(true); }
void TearDown() override { m_writer.reset(); }
FactoryPtr createFileArchetypeFactory()
{
auto fileProperty =
make_shared<Factory>(Requirements({Requirement("name", true), Requirement("VALUE", true)}));
auto fileProperties = make_shared<Factory>(Requirements(
{Requirement("FileProperty", ValueType::ENTITY, fileProperty, 1, Requirement::Infinite)}));
fileProperties->registerMatchers();
auto fileComment = make_shared<Factory>(
Requirements({Requirement("timestamp", true), Requirement("VALUE", true)}));
auto fileComments = make_shared<Factory>(Requirements(
{Requirement("FileComment", ValueType::ENTITY, fileComment, 1, Requirement::Infinite)}));
fileComments->registerMatchers();
auto fileArchetype = make_shared<Factory>(Requirements {
Requirement("assetId", true), Requirement("deviceUuid", true),
Requirement("timestamp", true), Requirement("removed", false), Requirement("name", true),
Requirement("mediaType", true), Requirement("applicationCategory", true),
Requirement("applicationType", true), Requirement("Description", false),
Requirement("FileComments", ValueType::ENTITY_LIST, fileComments, false),
Requirement("FileProperties", ValueType::ENTITY_LIST, fileProperties, false)});
auto root = make_shared<Factory>(
Requirements {Requirement("FileArchetype", ValueType::ENTITY, fileArchetype)});
return root;
}
std::unique_ptr<printer::XmlWriter> m_writer;
};
TEST_F(EntityPrinterTest, prints_simple_entity_with_file_properties_back_to_xml)
{
auto root = createFileArchetypeFactory();
auto doc = string {
"<FileArchetype applicationCategory=\"ASSEMBLY\" applicationType=\"DATA\" assetId=\"uuid\" "
"deviceUuid=\"duid\" mediaType=\"json\" name=\"xxxx\" timestamp=\"2020-12-01T10:00Z\">\n"
" <FileProperties>\n"
" <FileProperty name=\"one\">Round</FileProperty>\n"
" <FileProperty name=\"two\">Flat</FileProperty>\n"
" </FileProperties>\n"
"</FileArchetype>\n"};
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
ASSERT_EQ(doc, m_writer->getContent());
}
TEST_F(EntityPrinterTest, prints_file_archetype_with_description_element)
{
auto root = createFileArchetypeFactory();
auto doc = string {
"<FileArchetype applicationCategory=\"ASSEMBLY\" applicationType=\"DATA\" assetId=\"uuid\" "
"deviceUuid=\"duid\" mediaType=\"json\" name=\"xxxx\" timestamp=\"2020-12-01T10:00Z\">\n"
" <Description>Hello there Shaurabh</Description>\n"
" <FileProperties>\n"
" <FileProperty name=\"one\">Round</FileProperty>\n"
" <FileProperty name=\"two\">Flat</FileProperty>\n"
" </FileProperties>\n"
"</FileArchetype>\n"};
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
ASSERT_EQ(doc, m_writer->getContent());
}
TEST_F(EntityPrinterTest, prints_recursive_entity_lists_as_nested_xml)
{
auto component = make_shared<Factory>(Requirements {
Requirement("id", true),
Requirement("name", false),
Requirement("uuid", false),
});
auto components = make_shared<Factory>(Requirements(
{Requirement("Component", ValueType::ENTITY, component, 1, Requirement::Infinite)}));
components->registerMatchers();
components->registerFactory(regex(".+"), component);
component->addRequirements(
{Requirement("Components", ValueType::ENTITY_LIST, components, false)});
auto device = make_shared<Factory>(*component);
device->addRequirements(Requirements {
Requirement("name", true),
Requirement("uuid", true),
});
auto root = make_shared<Factory>(Requirements {Requirement("Device", ValueType::ENTITY, device)});
auto doc = string {
"<Device id=\"d1\" name=\"foo\" uuid=\"xxx\">\n"
" <Components>\n"
" <Systems id=\"s1\">\n"
" <Components>\n"
" <Electric id=\"e1\"/>\n"
" <Heating id=\"h1\"/>\n"
" </Components>\n"
" </Systems>\n"
" </Components>\n"
"</Device>\n"};
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
ASSERT_EQ(doc, m_writer->getContent());
}
TEST_F(EntityPrinterTest, prints_entity_children_in_declared_order)
{
auto component = make_shared<Factory>(Requirements {
Requirement("id", true),
Requirement("VALUE", false),
});
auto components = make_shared<Factory>(
Requirements({Requirement("ZFirstValue", ValueType::ENTITY, component),
Requirement("HSecondValue", ValueType::ENTITY, component),
Requirement("AThirdValue", ValueType::ENTITY, component),
Requirement("GFourthValue", ValueType::ENTITY, component),
Requirement("Simple", false), Requirement("Unordered", false)}));
components->setOrder({"ZFirstValue", "Simple", "HSecondValue", "AThirdValue", "GFourthValue"});
auto device = make_shared<Factory>(Requirements({
Requirement("name", true),
Requirement("Components", ValueType::ENTITY, components),
}));
auto root = make_shared<Factory>(Requirements {Requirement("Device", ValueType::ENTITY, device)});
auto doc = string {
"<Device name=\"foo\">\n"
" <Components>\n"
" <Unordered>Last</Unordered>\n"
" <HSecondValue id=\"a\">First</HSecondValue>\n"
" <GFourthValue id=\"b\">Second</GFourthValue>\n"
" <ZFirstValue id=\"c\">Third</ZFirstValue>\n"
" <Simple>Fourth</Simple>\n"
" <AThirdValue id=\"d\">Fifth</AThirdValue>\n"
" </Components>\n"
"</Device>\n"};
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
auto expected = string {
"<Device name=\"foo\">\n"
" <Components>\n"
" <ZFirstValue id=\"c\">Third</ZFirstValue>\n"
" <Simple>Fourth</Simple>\n"
" <HSecondValue id=\"a\">First</HSecondValue>\n"
" <AThirdValue id=\"d\">Fifth</AThirdValue>\n"
" <GFourthValue id=\"b\">Second</GFourthValue>\n"
" <Unordered>Last</Unordered>\n"
" </Components>\n"
"</Device>\n"};
ASSERT_EQ(expected, m_writer->getContent());
}
TEST_F(EntityPrinterTest, prints_raw_content_element_without_escaping)
{
auto definition =
make_shared<Factory>(Requirements({Requirement("format", false), Requirement("RAW", true)}));
auto root = make_shared<Factory>(
Requirements({Requirement("Definition", ValueType::ENTITY, definition, true)}));
auto doc = R"DOC(
<Definition format="XML">
<SomeContent with="stuff">
And some text
</SomeContent>
<AndMoreContent/>
And random text as well.
</Definition>
)DOC";
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
auto expected = R"DOC(<Definition format="XML"><SomeContent with="stuff">
And some text
</SomeContent><AndMoreContent/>
And random text as well.
</Definition>
)DOC";
ASSERT_EQ(expected, m_writer->getContent());
}
TEST_F(EntityPrinterTest, should_honor_include_hidden_parameter)
{
auto component = make_shared<Factory>(Requirements {
Requirement("id", true),
Requirement("name", false),
Requirement("uuid", false),
});
auto components = make_shared<Factory>(Requirements(
{Requirement("Component", ValueType::ENTITY, component, 1, Requirement::Infinite)}));
components->registerMatchers();
components->registerFactory(regex(".+"), component);
component->addRequirements(
{Requirement("Components", ValueType::ENTITY_LIST, components, false)});
auto device = make_shared<Factory>(*component);
device->addRequirements(Requirements {
Requirement("name", true),
Requirement("uuid", true),
});
auto root = make_shared<Factory>(Requirements {Requirement("Device", ValueType::ENTITY, device)});
auto doc = string {
"<Device id=\"d1\" name=\"foo\" uuid=\"xxx\">\n"
" <Components>\n"
" <Systems id=\"s1\">\n"
" <Components>\n"
" <Electric id=\"e1\"/>\n"
" <Heating id=\"h1\"/>\n"
" </Components>\n"
" </Systems>\n"
" </Components>\n"
"</Device>\n"};
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(root, doc, errors);
ASSERT_EQ(0, errors.size());
boost::uuids::detail::sha1 sha1;
unordered_map<string, string> idMap;
entity->createUniqueId(idMap, sha1);
entity::XmlPrinter printer(false);
printer.print(*m_writer, entity, {});
ASSERT_EQ(R"(<Device id="AdWDIt3OFtbuNhoe" name="foo" uuid="xxx">
<Components>
<Systems id="ZA2H50HmqkxmmoKk">
<Components>
<Electric id="hIltPgZ4hGIcD1Qz"/>
<Heating id="rErpcoXBmxMgHeub"/>
</Components>
</Systems>
</Components>
</Device>
)",
m_writer->getContent());
m_writer = make_unique<printer::XmlWriter>(true);
entity::XmlPrinter printer2(true);
printer2.print(*m_writer, entity, {});
ASSERT_EQ(R"(<Device id="AdWDIt3OFtbuNhoe" name="foo" originalId="d1" uuid="xxx">
<Components>
<Systems id="ZA2H50HmqkxmmoKk" originalId="s1">
<Components>
<Electric id="hIltPgZ4hGIcD1Qz" originalId="e1"/>
<Heating id="rErpcoXBmxMgHeub" originalId="h1"/>
</Components>
</Systems>
</Components>
</Device>
)",
m_writer->getContent());
}
class EntityPrinterNamespaceTest : public EntityPrinterTest
{
protected:
EntityPtr createDevice()
{
auto component = make_shared<Factory>(Requirements {
Requirement("id", true),
Requirement("name", false),
Requirement("uuid", false),
});
auto components = make_shared<Factory>(Requirements(
{Requirement("Component", ValueType::ENTITY, component, 1, Requirement::Infinite)}));
components->registerMatchers();
components->registerFactory(regex(".+"), component);
component->addRequirements(
{Requirement("Components", ValueType::ENTITY_LIST, components, false)});
auto device = make_shared<Factory>(*component);
device->addRequirements(Requirements {
Requirement("name", true),
Requirement("uuid", true),
});
auto root =
make_shared<Factory>(Requirements {Requirement("Device", ValueType::ENTITY, device)});
ErrorList errors;
auto s1 = components->create("System", Properties {{"id", "s1"s}}, errors);
EXPECT_EQ(0, errors.size());
EXPECT_TRUE(s1);
auto s2 = components->create("x:FlizGuard", Properties {{"id", "s2"s}}, errors);
EXPECT_EQ(0, errors.size());
EXPECT_TRUE(s2);
EntityList list {s1, s2};
auto c1 = device->create("Components", list, errors);
EXPECT_TRUE(c1);
EXPECT_EQ(0, errors.size());
auto entity = root->create(
"Device",
Properties {{"id", "d1"s}, {"uuid", "xxx"s}, {"name", "foo"s}, {"Components", c1}}, errors);
EXPECT_EQ(0, errors.size());
EXPECT_TRUE(entity);
return entity;
}
};
TEST_F(EntityPrinterNamespaceTest, test_namespace_removal_when_no_namespaces)
{
auto entity = createDevice();
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {});
auto expected = string {
R"DOC(<Device id="d1" name="foo" uuid="xxx">
<Components>
<System id="s1"/>
<FlizGuard id="s2"/>
</Components>
</Device>
)DOC"};
ASSERT_EQ(expected, m_writer->getContent());
}
TEST_F(EntityPrinterNamespaceTest, test_namespace_removal_with_namespaces)
{
auto entity = createDevice();
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {"x"});
auto expected = string {
R"DOC(<Device id="d1" name="foo" uuid="xxx">
<Components>
<System id="s1"/>
<x:FlizGuard id="s2"/>
</Components>
</Device>
)DOC"};
ASSERT_EQ(expected, m_writer->getContent());
}