ifw-daq  3.0.0-pre2
IFW Data Acquisition modules
testStartDaqV2.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_libjson
4  * @copyright
5  * (c) Copyright ESO 2022
6  * All Rights Reserved
7  * ESO (eso.org) is an Intergovernmental Organisation, and therefore special legal conditions apply.
8  */
10 
11 #include <gtest/gtest.h>
12 
13 using namespace ::testing;
14 
15 namespace daq::json {
16 
17 class TestParseStartDaqV2Spec : public ::testing::Test {
18 public:
19  void SetUp() {
20  using namespace nlohmann;
21  m_spec = R"(
22  {
23  "id": "myid",
24  "awaitCompletionInterval": 3.0,
25  "mergeTarget": {
26  "sourcename": "primary"
27  },
28  "sources": [
29  {
30  "type": "primaryDataSource",
31  "sourceName": "primary",
32  "rrUri": "zpb.rr://prim",
33  "keywordRules": [
34  {
35  "type": "filter",
36  "selectionPatterns": [
37  "+e *",
38  "+v *"
39  ]
40  }
41  ]
42  },
43  {
44  "type": "metadataSource",
45  "sourceName": "metadata",
46  "rrUri": "zpb.rr://meta",
47  "initialKeywords": "user",
48  "keywordRules": [
49  {
50  "type": "transform",
51  "selectionPatterns": [
52  "+e INS1 *"
53  ],
54  "regex": "INS1 ",
55  "format": "INS2 "
56  }
57  ]
58  },
59  {
60  "type": "fitsKeywords",
61  "sourceName": "keywords",
62  "keywords": [
63  {
64  "type": "valueKeyword",
65  "name": "TELESCOP",
66  "value": "ESO-ELT"
67  }
68  ],
69  "keywordRules": [
70  {
71  "type": "transform",
72  "selectionPatterns": [
73  "+e INS1 *"
74  ],
75  "regex": "INS1 ",
76  "format": "INS2 "
77  }
78  ]
79  },
80  {
81  "type": "fitsFile",
82  "sourceName": "file",
83  "location": "host:/path/to.fits.",
84  "keywordRules": [
85  {
86  "type": "transform",
87  "selectionPatterns": [
88  "+e INS1 *"
89  ],
90  "regex": "INS1 ",
91  "format": "INS2 "
92  }
93  ]
94  }
95  ],
96  "filePrefix": "prefix"
97  }
98  )"_json;
99  }
100  nlohmann::json m_spec;
101 };
102 
103 TEST_F(TestParseStartDaqV2Spec, RoundTripParsing) {
104  auto spec_1 = ParseStartDaqV2Spec(m_spec);
105  auto spec_1_json = nlohmann::json();
106  to_json(spec_1_json, spec_1);
107 
108  auto spec_2 = ParseStartDaqV2Spec(spec_1_json);
109  EXPECT_EQ(spec_1, spec_2);
110 }
111 
112 TEST_F(TestParseStartDaqV2Spec, SuccessfulTemplate) {
113  auto spec = ParseStartDaqV2Spec(m_spec);
114 
115  EXPECT_EQ(spec.id, "myid");
116  EXPECT_EQ(spec.file_prefix, "prefix");
117  EXPECT_EQ(spec.await_completion_interval, std::chrono::milliseconds(3000));
118 
119  ASSERT_EQ(spec.sources.size(), 4u);
120 
121  {
122  auto const& prim = std::get<StartDaqV2Spec::PrimaryDataSource>(spec.sources[0]);
123  EXPECT_EQ(prim.source_name, "primary");
124  EXPECT_EQ(prim.rr_uri, "zpb.rr://prim");
125 
126  ASSERT_EQ(prim.keyword_rules.size(), 1u);
127  auto const& filter = std::get<KeywordFilter>(prim.keyword_rules.at(0));
128  ASSERT_EQ(filter.selection_patterns.size(), 2u);
129  EXPECT_EQ(filter.selection_patterns[0], "+e *");
130  EXPECT_EQ(filter.selection_patterns[1], "+v *");
131  }
132  {
133  auto const& meta = std::get<StartDaqV2Spec::MetadataSource>(spec.sources[1]);
134  EXPECT_EQ(meta.rr_uri, "zpb.rr://meta");
135  EXPECT_EQ(meta.source_name, "metadata");
136  EXPECT_EQ(meta.initial_keywords, InitialKeywords::User);
137 
138  ASSERT_EQ(meta.keyword_rules.size(), 1u);
139  auto const& transform = std::get<KeywordTransform>(meta.keyword_rules.at(0));
140  ASSERT_EQ(transform.selection_patterns.size(), 1u);
141  EXPECT_EQ(transform.selection_patterns[0], "+e INS1 *");
142  EXPECT_EQ(transform.regex, "INS1 ");
143  EXPECT_EQ(transform.format, "INS2 ");
144  }
145  {
146  auto const& kws = std::get<FitsKeywordsSource>(spec.sources[2]);
147  EXPECT_EQ(kws.source_name, "keywords");
148  }
149  {
150  auto const& file = std::get<FitsFileSource>(spec.sources[3]);
151  EXPECT_EQ(file.source_name, "file");
152  }
153 }
154 
155 TEST_F(TestParseStartDaqV2Spec, EmptyKeywordRulesModelledAsEmptyOptional) {
156  m_spec["sources"][0].erase("keywordRules");
157  m_spec["sources"][1].erase("keywordRules");
158  auto spec = ParseStartDaqV2Spec(m_spec);
159 
160  EXPECT_EQ(spec.id, "myid");
161  EXPECT_EQ(spec.file_prefix, "prefix");
162 
163  ASSERT_EQ(spec.sources.size(), 4u);
164 
165  {
166  auto const& prim = std::get<StartDaqV2Spec::PrimaryDataSource>(spec.sources[0]);
167  EXPECT_EQ(prim.source_name, "primary");
168  EXPECT_EQ(prim.rr_uri, "zpb.rr://prim");
169 
170  ASSERT_TRUE(prim.keyword_rules.empty());
171  }
172  {
173  auto const& meta = std::get<StartDaqV2Spec::MetadataSource>(spec.sources[1]);
174  EXPECT_EQ(meta.rr_uri, "zpb.rr://meta");
175  EXPECT_EQ(meta.source_name, "metadata");
176 
177  ASSERT_TRUE(meta.keyword_rules.empty());
178  }
179 }
180 
181 TEST_F(TestParseStartDaqV2Spec, NoIdIsModelledAsEmptyString) {
182  m_spec.erase("id");
183  auto spec = ParseStartDaqV2Spec(m_spec);
184  EXPECT_EQ(spec.id, "");
185 }
186 
187 TEST_F(TestParseStartDaqV2Spec, EmptydIsAllowed) {
188  m_spec["id"] = "";
189  auto spec = ParseStartDaqV2Spec(m_spec);
190  EXPECT_EQ(spec.id, "");
191 }
192 
193 TEST_F(TestParseStartDaqV2Spec, AwaitCompletionIntervalIsOptional) {
194  m_spec.erase("awaitCompletionInterval");
195  auto spec = ParseStartDaqV2Spec(m_spec);
196 
197  EXPECT_EQ(spec.await_completion_interval, std::nullopt);
198 }
199 
200 TEST_F(TestParseStartDaqV2Spec, AwaitCompletionIntervalAcceptsIntegers) {
201  m_spec["awaitCompletionInterval"] = 2;
202  auto spec = ParseStartDaqV2Spec(m_spec);
203 
204  EXPECT_EQ(spec.await_completion_interval, std::chrono::milliseconds(2000));
205 }
206 
207 TEST_F(TestParseStartDaqV2Spec, AwaitCompletionIntervalMustBePositive) {
208  m_spec["awaitCompletionInterval"] = -1.0;
209  EXPECT_THROW(ParseStartDaqV2Spec(m_spec), StartDaqV2SpecError);
210 }
211 
212 } // namespace daq::json
@ User
Keep only user-keywords.
TEST_F(TestParseLocation, ParseSucceeds)
Definition: testDpSpec.cpp:20
nlohmann::json m_spec
StartDaqV2Spec ParseStartDaqV2Spec(nlohmann::json const &json)
Parse StartDaqSpec.
Definition: startDaqV2.cpp:46
daq::json::TestParseStartDaqV2Spec _json
void to_json(nlohmann::json &out, KeywordFilter const &s)
EXPECT_EQ(meta.rr_uri, "zpb.rr://meta")
ASSERT_EQ(meta.keyword_rules.size(), 1u)
auto const & transform