-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathfixture_test.cpp
More file actions
251 lines (219 loc) · 7.85 KB
/
Copy pathfixture_test.cpp
File metadata and controls
251 lines (219 loc) · 7.85 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
//
// 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 <date/date.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include "json_helper.hpp"
#include "mtconnect/agent.hpp"
#include "mtconnect/asset/asset.hpp"
#include "mtconnect/asset/fixture.hpp"
#include "mtconnect/entity/entity.hpp"
#include "mtconnect/entity/json_printer.hpp"
#include "mtconnect/entity/xml_parser.hpp"
#include "mtconnect/entity/xml_printer.hpp"
#include "mtconnect/printer//xml_printer.hpp"
#include "mtconnect/printer//xml_printer_helper.hpp"
#include "mtconnect/source/adapter/adapter.hpp"
using json = nlohmann::json;
using namespace std;
using namespace mtconnect;
using namespace mtconnect::entity;
using namespace mtconnect::source::adapter;
using namespace mtconnect::asset;
using namespace mtconnect::printer;
// main
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class FixtureTest : public testing::Test
{
protected:
void SetUp() override
{
Fixture::registerAsset();
m_writer = make_unique<printer::XmlWriter>(true);
}
void TearDown() override { m_writer.reset(); }
std::unique_ptr<printer::XmlWriter> m_writer;
};
TEST_F(FixtureTest, minimal_fixture_definition)
{
using namespace date;
const auto doc = R"DOC(
<Fixture assetId="7ae770f0-c11e-013a-c34c-4e7f553bbb76">
<ManufactureDate>2022-05-20</ManufactureDate>
<CalibrationDate>2022-05-21</CalibrationDate>
<InspectionDate>2022-05-22</InspectionDate>
<NextInspectionDate>2022-05-23</NextInspectionDate>
<Measurements>
<Length maximum="5.2" minimum="4.95" nominal="5" units="MILLIMETER">5.1</Length>
<Diameter maximum="1.4" minimum="0.95" nominal="1.25" units="MILLIMETER">1.27</Diameter>
</Measurements>
<FixtureId>XXXYYY</FixtureId>
<FixtureNumber>12345</FixtureNumber>
<ClampingMethod>CLAMP</ClampingMethod>
<MountingMethod>MOUNT</MountingMethod>
</Fixture>
)DOC";
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(Asset::getRoot(), doc, errors);
ASSERT_EQ(0, errors.size());
auto asset = dynamic_cast<Asset *>(entity.get());
ASSERT_NE(nullptr, asset);
ASSERT_EQ("7ae770f0-c11e-013a-c34c-4e7f553bbb76", asset->getAssetId());
ASSERT_FALSE(asset->getTimestamp());
ASSERT_FALSE(asset->getDeviceUuid());
auto date = asset->get<Timestamp>("ManufactureDate");
auto ymd = year_month_day(floor<days>(date));
ASSERT_EQ(2022, int(ymd.year()));
ASSERT_EQ(5, unsigned(ymd.month()));
ASSERT_EQ(20, unsigned(ymd.day()));
date = asset->get<Timestamp>("CalibrationDate");
ymd = year_month_day(floor<days>(date));
ASSERT_EQ(2022, int(ymd.year()));
ASSERT_EQ(5, unsigned(ymd.month()));
ASSERT_EQ(21, unsigned(ymd.day()));
date = asset->get<Timestamp>("InspectionDate");
ymd = year_month_day(floor<days>(date));
ASSERT_EQ(2022, int(ymd.year()));
ASSERT_EQ(5, unsigned(ymd.month()));
ASSERT_EQ(22, unsigned(ymd.day()));
date = asset->get<Timestamp>("NextInspectionDate");
ymd = year_month_day(floor<days>(date));
ASSERT_EQ(2022, int(ymd.year()));
ASSERT_EQ(5, unsigned(ymd.month()));
ASSERT_EQ(23, unsigned(ymd.day()));
ASSERT_EQ("XXXYYY", asset->get<string>("FixtureId"));
ASSERT_EQ(12345, asset->get<int64_t>("FixtureNumber"));
ASSERT_EQ("CLAMP", asset->get<string>("ClampingMethod"));
ASSERT_EQ("MOUNT", asset->get<string>("MountingMethod"));
auto meas = asset->getList("Measurements");
ASSERT_TRUE(meas);
ASSERT_EQ(2, meas->size());
auto it = meas->begin();
ASSERT_EQ("Length", (*it)->getName());
ASSERT_EQ("MILLIMETER", get<string>((*it)->getProperty("units")));
ASSERT_EQ(5.0, get<double>((*it)->getProperty("nominal")));
ASSERT_EQ(4.95, get<double>((*it)->getProperty("minimum")));
ASSERT_EQ(5.2, get<double>((*it)->getProperty("maximum")));
ASSERT_EQ(5.1, get<double>((*it)->getProperty("VALUE")));
it++;
ASSERT_EQ("Diameter", (*it)->getName());
ASSERT_EQ("MILLIMETER", get<string>((*it)->getProperty("units")));
ASSERT_EQ(1.25, get<double>((*it)->getProperty("nominal")));
ASSERT_EQ(0.95, get<double>((*it)->getProperty("minimum")));
ASSERT_EQ(1.4, get<double>((*it)->getProperty("maximum")));
ASSERT_EQ(1.27, get<double>((*it)->getProperty("VALUE")));
}
TEST_F(FixtureTest, should_round_trip_xml)
{
using namespace date;
const auto doc =
R"DOC(<Fixture assetId="7ae770f0-c11e-013a-c34c-4e7f553bbb76">
<ManufactureDate>2022-05-20T00:00:00Z</ManufactureDate>
<CalibrationDate>2022-05-21T00:00:00Z</CalibrationDate>
<InspectionDate>2022-05-22T00:00:00Z</InspectionDate>
<NextInspectionDate>2022-05-23T00:00:00Z</NextInspectionDate>
<Measurements>
<Length maximum="5.2" minimum="4.95" nominal="5" units="MILLIMETER">5.1</Length>
<Diameter maximum="1.4" minimum="0.95" nominal="1.25" units="MILLIMETER">1.27</Diameter>
</Measurements>
<FixtureId>XXXYYY</FixtureId>
<FixtureNumber>12345</FixtureNumber>
<ClampingMethod>CLAMP</ClampingMethod>
<MountingMethod>MOUNT</MountingMethod>
</Fixture>
)DOC";
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(Asset::getRoot(), doc, errors);
ASSERT_EQ(0, errors.size());
entity::XmlPrinter printer;
printer.print(*m_writer, entity, {"x"});
string content = m_writer->getContent();
ASSERT_EQ(doc, content);
}
TEST_F(FixtureTest, should_generate_json)
{
using namespace date;
const auto doc =
R"DOC(<Fixture assetId="7ae770f0-c11e-013a-c34c-4e7f553bbb76">
<ManufactureDate>2022-05-20T00:00:00Z</ManufactureDate>
<CalibrationDate>2022-05-21T00:00:00Z</CalibrationDate>
<InspectionDate>2022-05-22T00:00:00Z</InspectionDate>
<NextInspectionDate>2022-05-23T00:00:00Z</NextInspectionDate>
<Measurements>
<Length maximum="5.2" minimum="4.95" nominal="5" units="MILLIMETER">5.1</Length>
<Diameter maximum="1.4" minimum="0.95" nominal="1.25" units="MILLIMETER">1.27</Diameter>
</Measurements>
<FixtureId>XXXYYY</FixtureId>
<FixtureNumber>12345</FixtureNumber>
<ClampingMethod>CLAMP</ClampingMethod>
<MountingMethod>MOUNT</MountingMethod>
</Fixture>
)DOC";
ErrorList errors;
entity::XmlParser parser;
auto entity = parser.parse(Asset::getRoot(), doc, errors);
ASSERT_EQ(0, errors.size());
entity::JsonEntityPrinter jsonPrinter(2, true);
auto json = jsonPrinter.print(entity);
ASSERT_EQ(R"({
"Fixture": {
"CalibrationDate": "2022-05-21T00:00:00Z",
"ClampingMethod": "CLAMP",
"FixtureId": "XXXYYY",
"FixtureNumber": 12345,
"InspectionDate": "2022-05-22T00:00:00Z",
"ManufactureDate": "2022-05-20T00:00:00Z",
"Measurements": {
"Diameter": [
{
"value": 1.27,
"maximum": 1.4,
"minimum": 0.95,
"nominal": 1.25,
"units": "MILLIMETER"
}
],
"Length": [
{
"value": 5.1,
"maximum": 5.2,
"minimum": 4.95,
"nominal": 5.0,
"units": "MILLIMETER"
}
]
},
"MountingMethod": "MOUNT",
"NextInspectionDate": "2022-05-23T00:00:00Z",
"assetId": "7ae770f0-c11e-013a-c34c-4e7f553bbb76"
}
})",
json);
}