-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathjson_printer_asset_test.cpp
More file actions
318 lines (269 loc) · 11.4 KB
/
Copy pathjson_printer_asset_test.cpp
File metadata and controls
318 lines (269 loc) · 11.4 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
//
// 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 <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <nlohmann/json.hpp>
#include "json_helper.hpp"
#include "mtconnect/asset/cutting_tool.hpp"
#include "mtconnect/asset/file_asset.hpp"
#include "mtconnect/buffer/checkpoint.hpp"
#include "mtconnect/device_model/data_item/data_item.hpp"
#include "mtconnect/device_model/device.hpp"
#include "mtconnect/entity/xml_parser.hpp"
#include "mtconnect/observation/observation.hpp"
#include "mtconnect/parser/xml_parser.hpp"
#include "mtconnect/printer//json_printer.hpp"
#include "mtconnect/printer//xml_printer.hpp"
#include "mtconnect/utilities.hpp"
#include "test_utilities.hpp"
using json = nlohmann::json;
using namespace std;
using namespace mtconnect;
using namespace mtconnect::buffer;
using namespace mtconnect::asset;
// main
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class JsonPrinterAssetTest : public testing::Test
{
protected:
void SetUp() override
{
CuttingToolArchetype::registerAsset();
CuttingTool::registerAsset();
FileArchetypeAsset::registerAsset();
FileAsset::registerAsset();
m_printer = std::make_unique<printer::JsonPrinter>(1, true);
m_parser = std::make_unique<entity::XmlParser>();
}
void TearDown() override
{
m_printer.reset();
m_parser.reset();
}
AssetPtr parseAsset(const std::string& xml, entity::ErrorList& errors)
{
auto entity = m_parser->parse(Asset::getRoot(), xml, errors);
AssetPtr asset;
for (auto& error : errors)
{
cout << error->what() << endl;
}
if (entity)
asset = dynamic_pointer_cast<Asset>(entity);
return asset;
}
std::unique_ptr<printer::JsonPrinter> m_printer;
std::unique_ptr<entity::XmlParser> m_parser;
};
TEST_F(JsonPrinterAssetTest, asset_header_contains_instance_id_buffer_size_and_count)
{
AssetList asset;
m_printer->setSenderName("MachineXXX");
auto doc = m_printer->printAssets(123, 1024, 10, asset);
auto jdoc = json::parse(doc);
auto it = jdoc.begin();
ASSERT_EQ(string("MTConnectAssets"), it.key());
ASSERT_EQ(123, jdoc.at("/MTConnectAssets/Header/instanceId"_json_pointer).get<int32_t>());
ASSERT_EQ(1024, jdoc.at("/MTConnectAssets/Header/assetBufferSize"_json_pointer).get<int32_t>());
ASSERT_EQ(10, jdoc.at("/MTConnectAssets/Header/assetCount"_json_pointer).get<int32_t>());
ASSERT_EQ("MachineXXX", jdoc.at("/MTConnectAssets/Header/sender"_json_pointer).get<string>());
}
TEST_F(JsonPrinterAssetTest, cutting_tool_asset_is_printed_with_serial_number_and_tool_id)
{
auto xml = getFile("asset1.xml");
entity::ErrorList errors;
AssetPtr a = parseAsset(xml, errors);
ASSERT_TRUE(a);
AssetList assetList {a};
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto asset = jdoc.at("/MTConnectAssets/Assets"_json_pointer);
ASSERT_TRUE(asset.is_array());
ASSERT_EQ(1_S, asset.size());
auto cuttingTool = asset.at(0);
ASSERT_EQ(string("1"), cuttingTool.at("/CuttingTool/serialNumber"_json_pointer).get<string>());
ASSERT_EQ(string("KSSP300R4SD43L240"),
cuttingTool.at("/CuttingTool/toolId"_json_pointer).get<string>());
ASSERT_EQ(string("KSSP300R4SD43L240.1"),
cuttingTool.at("/CuttingTool/assetId"_json_pointer).get<string>());
ASSERT_EQ(string("2011-05-11T13:55:22Z"),
cuttingTool.at("/CuttingTool/timestamp"_json_pointer).get<string>());
ASSERT_EQ(string("KMT,Parlec"),
cuttingTool.at("/CuttingTool/manufacturers"_json_pointer).get<string>());
ASSERT_EQ(string("Cutting tool ..."),
cuttingTool.at("/CuttingTool/Description"_json_pointer).get<string>());
}
TEST_F(JsonPrinterAssetTest, cutting_tool_life_cycle_contains_status_tool_life_and_speeds)
{
auto xml = getFile("asset1.xml");
entity::ErrorList errors;
AssetPtr a = parseAsset(xml, errors);
ASSERT_TRUE(a);
AssetList assetList {a};
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto asset = jdoc.at("/MTConnectAssets/Assets"_json_pointer);
ASSERT_TRUE(asset.is_array());
ASSERT_EQ(1_S, asset.size());
auto cuttingTool = asset.at(0);
auto lifeCycle = cuttingTool.at("/CuttingTool/CuttingToolLifeCycle"_json_pointer);
ASSERT_TRUE(lifeCycle.is_object());
auto status = lifeCycle.at("/CutterStatus/0/Status/value"_json_pointer);
ASSERT_EQ(string("NEW"), status.get<string>());
auto toolLife = lifeCycle.at("/ToolLife"_json_pointer);
ASSERT_TRUE(toolLife.is_array());
auto life = toolLife.at(0);
ASSERT_TRUE(life.is_object());
ASSERT_EQ(string("PART_COUNT"), life.at("/type"_json_pointer).get<string>());
ASSERT_EQ(string("DOWN"), life.at("/countDirection"_json_pointer).get<string>());
ASSERT_EQ(300.0, life.at("/limit"_json_pointer).get<double>());
ASSERT_EQ(200.0, life.at("/value"_json_pointer).get<double>());
auto speed = lifeCycle.at("/ProcessSpindleSpeed"_json_pointer);
ASSERT_EQ(13300.0, speed.at("/maximum"_json_pointer).get<double>());
ASSERT_EQ(605.0, speed.at("/nominal"_json_pointer).get<double>());
ASSERT_EQ(10000.0, speed.at("/value"_json_pointer).get<double>());
auto feed = lifeCycle.at("/ProcessFeedRate"_json_pointer);
ASSERT_EQ(222.0, feed.at("/value"_json_pointer).get<double>());
}
TEST_F(JsonPrinterAssetTest, cutting_measurements_are_printed_as_array_with_codes_and_values)
{
auto xml = getFile("asset1.xml");
entity::ErrorList errors;
AssetPtr asset = parseAsset(xml, errors);
ASSERT_TRUE(asset);
AssetList assetList {asset};
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto lifeCycle =
jdoc.at("/MTConnectAssets/Assets/0/CuttingTool/CuttingToolLifeCycle"_json_pointer);
ASSERT_TRUE(lifeCycle.is_object());
auto measurements = lifeCycle.at("/Measurements"_json_pointer);
ASSERT_TRUE(measurements.is_array());
ASSERT_EQ(7_S, measurements.size());
auto diameter = measurements.at(0);
ASSERT_TRUE(diameter.is_object());
ASSERT_EQ("BDX"_S, diameter.at("/BodyDiameterMax/code"_json_pointer).get<string>());
ASSERT_EQ(73.25, diameter.at("/BodyDiameterMax/value"_json_pointer).get<double>());
auto length = measurements.at(4);
ASSERT_TRUE(length.is_object());
ASSERT_EQ("LF"_S, length.at("/BodyLengthMax/code"_json_pointer).get<string>());
ASSERT_EQ(120.65, length.at("/BodyLengthMax/nominal"_json_pointer).get<double>());
ASSERT_EQ(120.404, length.at("/BodyLengthMax/minimum"_json_pointer).get<double>());
ASSERT_EQ(120.904, length.at("/BodyLengthMax/maximum"_json_pointer).get<double>());
ASSERT_EQ(120.65, length.at("/BodyLengthMax/value"_json_pointer).get<double>());
}
TEST_F(JsonPrinterAssetTest, cutting_items_are_printed_with_indices_grade_and_measurements)
{
auto xml = getFile("asset1.xml");
entity::ErrorList errors;
AssetPtr asset = parseAsset(xml, errors);
ASSERT_TRUE(asset);
AssetList assetList {asset};
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto cuttingItems = jdoc.at(
"/MTConnectAssets/Assets/0/"
"CuttingTool/CuttingToolLifeCycle/"
"CuttingItems"_json_pointer);
ASSERT_EQ(24, cuttingItems["count"].get<int>());
auto items = cuttingItems["list"];
ASSERT_TRUE(items.is_array());
ASSERT_EQ(6_S, items.size());
auto item = items.at(0);
ASSERT_TRUE(item.is_object());
ASSERT_EQ("1-4"_S, item.at("/CuttingItem/indices"_json_pointer).get<string>());
ASSERT_EQ(string("SDET43PDER8GB"), item.at("/CuttingItem/itemId"_json_pointer).get<string>());
ASSERT_EQ(string("KC725M"), item.at("/CuttingItem/grade"_json_pointer).get<string>());
ASSERT_EQ(string("KMT"), item.at("/CuttingItem/manufacturers"_json_pointer).get<string>());
ASSERT_EQ(string("FLANGE: 1-4, ROW: 1"),
item.at("/CuttingItem/Locus"_json_pointer).get<string>());
auto measurements = item.at("/CuttingItem/Measurements"_json_pointer);
ASSERT_TRUE(measurements.is_array());
ASSERT_EQ(4_S, measurements.size());
ASSERT_EQ("RE"_S, measurements.at("/3/CornerRadius/code"_json_pointer).get<string>());
ASSERT_EQ(0.8, measurements.at("/3/CornerRadius/nominal"_json_pointer).get<double>());
ASSERT_EQ(0.8, measurements.at("/3/CornerRadius/value"_json_pointer).get<double>());
}
TEST_F(JsonPrinterAssetTest, cutting_tool_archetype_is_printed_with_definition_and_format)
{
auto xml = getFile("cutting_tool_archetype.xml");
entity::ErrorList errors;
AssetPtr asset = parseAsset(xml, errors);
ASSERT_TRUE(asset);
AssetList assetList {asset};
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto tool = jdoc.at(
"/MTConnectAssets/Assets/0/"
"CuttingToolArchetype"_json_pointer);
ASSERT_TRUE(tool.is_object());
auto def = tool.at("/CuttingToolDefinition"_json_pointer);
ASSERT_TRUE(def.is_object());
ASSERT_EQ("EXPRESS"_S, def.at("/format"_json_pointer).get<string>());
ASSERT_EQ(string("Some Express..."), def.at("/value"_json_pointer).get<string>());
}
TEST_F(JsonPrinterAssetTest, json_printer_version_2_with_multiple_assets)
{
m_printer = std::make_unique<printer::JsonPrinter>(2, true);
AssetList assetList;
auto xml1 = getFile("asset1.xml");
entity::ErrorList errors;
AssetPtr asset = parseAsset(xml1, errors);
asset->setAssetId("FIRST");
ASSERT_TRUE(asset);
assetList.push_back(asset);
auto xml2 = getFile("cutting_tool_archetype.xml");
asset = parseAsset(xml2, errors);
asset->setAssetId("SECOND");
ASSERT_TRUE(asset);
assetList.push_back(asset);
asset = parseAsset(xml1, errors);
ASSERT_TRUE(asset);
asset->setAssetId("THIRD");
assetList.push_back(asset);
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
auto assets = jdoc.at("/MTConnectAssets/Assets"_json_pointer);
ASSERT_TRUE(assets.is_object());
auto tools = assets.at("/CuttingTool"_json_pointer);
ASSERT_TRUE(tools.is_array());
ASSERT_EQ(2_S, tools.size());
ASSERT_EQ("FIRST", tools.at("/0/assetId"_json_pointer));
ASSERT_EQ("THIRD", tools.at("/1/assetId"_json_pointer));
auto arch = assets.at("/CuttingToolArchetype/0"_json_pointer);
ASSERT_TRUE(arch.is_object());
ASSERT_EQ("SECOND", arch.at("/assetId"_json_pointer));
}
TEST_F(JsonPrinterAssetTest, assets_schema_url_is_set_in_document_when_configured)
{
m_printer->setAssetsSchema("https://example.org/MTConnectAssets_2.0.schema.json");
AssetList assetList;
auto doc = m_printer->printAssets(123, 1024, 10, assetList);
auto jdoc = json::parse(doc);
ASSERT_EQ("https://example.org/MTConnectAssets_2.0.schema.json",
jdoc.at("/$schema"_json_pointer).get<string>());
}