Mercurial > hg > orthanc
comparison UnitTestsSources/ServerIndexTests.cpp @ 726:edffcf3ce7ab
sonar
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 19 Feb 2014 12:39:17 +0100 |
parents | UnitTestsSources/ServerIndex.cpp@9d1973813d8b |
children | 1dee6e9bdbf4 |
comparison
equal
deleted
inserted
replaced
725:ef4569ae7952 | 726:edffcf3ce7ab |
---|---|
1 #include "gtest/gtest.h" | |
2 | |
3 #include "../OrthancServer/DatabaseWrapper.h" | |
4 #include "../OrthancServer/ServerContext.h" | |
5 #include "../OrthancServer/ServerIndex.h" | |
6 #include "../Core/Uuid.h" | |
7 #include "../Core/DicomFormat/DicomNullValue.h" | |
8 | |
9 #include <ctype.h> | |
10 #include <glog/logging.h> | |
11 #include <algorithm> | |
12 | |
13 using namespace Orthanc; | |
14 | |
15 namespace | |
16 { | |
17 class ServerIndexListener : public IServerIndexListener | |
18 { | |
19 public: | |
20 std::vector<std::string> deletedFiles_; | |
21 std::string ancestorId_; | |
22 ResourceType ancestorType_; | |
23 | |
24 void Reset() | |
25 { | |
26 ancestorId_ = ""; | |
27 deletedFiles_.clear(); | |
28 } | |
29 | |
30 virtual void SignalRemainingAncestor(ResourceType type, | |
31 const std::string& publicId) | |
32 { | |
33 ancestorId_ = publicId; | |
34 ancestorType_ = type; | |
35 } | |
36 | |
37 virtual void SignalFileDeleted(const FileInfo& info) | |
38 { | |
39 const std::string fileUuid = info.GetUuid(); | |
40 deletedFiles_.push_back(fileUuid); | |
41 LOG(INFO) << "A file must be removed: " << fileUuid; | |
42 } | |
43 }; | |
44 } | |
45 | |
46 | |
47 TEST(DatabaseWrapper, Simple) | |
48 { | |
49 ServerIndexListener listener; | |
50 DatabaseWrapper index(listener); | |
51 | |
52 int64_t a[] = { | |
53 index.CreateResource("a", ResourceType_Patient), // 0 | |
54 index.CreateResource("b", ResourceType_Study), // 1 | |
55 index.CreateResource("c", ResourceType_Series), // 2 | |
56 index.CreateResource("d", ResourceType_Instance), // 3 | |
57 index.CreateResource("e", ResourceType_Instance), // 4 | |
58 index.CreateResource("f", ResourceType_Instance), // 5 | |
59 index.CreateResource("g", ResourceType_Study) // 6 | |
60 }; | |
61 | |
62 ASSERT_EQ("a", index.GetPublicId(a[0])); | |
63 ASSERT_EQ("b", index.GetPublicId(a[1])); | |
64 ASSERT_EQ("c", index.GetPublicId(a[2])); | |
65 ASSERT_EQ("d", index.GetPublicId(a[3])); | |
66 ASSERT_EQ("e", index.GetPublicId(a[4])); | |
67 ASSERT_EQ("f", index.GetPublicId(a[5])); | |
68 ASSERT_EQ("g", index.GetPublicId(a[6])); | |
69 | |
70 ASSERT_EQ(ResourceType_Patient, index.GetResourceType(a[0])); | |
71 ASSERT_EQ(ResourceType_Study, index.GetResourceType(a[1])); | |
72 ASSERT_EQ(ResourceType_Series, index.GetResourceType(a[2])); | |
73 ASSERT_EQ(ResourceType_Instance, index.GetResourceType(a[3])); | |
74 ASSERT_EQ(ResourceType_Instance, index.GetResourceType(a[4])); | |
75 ASSERT_EQ(ResourceType_Instance, index.GetResourceType(a[5])); | |
76 ASSERT_EQ(ResourceType_Study, index.GetResourceType(a[6])); | |
77 | |
78 { | |
79 Json::Value t; | |
80 index.GetAllPublicIds(t, ResourceType_Patient); | |
81 | |
82 ASSERT_EQ(1u, t.size()); | |
83 ASSERT_EQ("a", t[0u].asString()); | |
84 | |
85 index.GetAllPublicIds(t, ResourceType_Series); | |
86 ASSERT_EQ(1u, t.size()); | |
87 ASSERT_EQ("c", t[0u].asString()); | |
88 | |
89 index.GetAllPublicIds(t, ResourceType_Study); | |
90 ASSERT_EQ(2u, t.size()); | |
91 | |
92 index.GetAllPublicIds(t, ResourceType_Instance); | |
93 ASSERT_EQ(3u, t.size()); | |
94 } | |
95 | |
96 index.SetGlobalProperty(GlobalProperty_FlushSleep, "World"); | |
97 | |
98 index.AttachChild(a[0], a[1]); | |
99 index.AttachChild(a[1], a[2]); | |
100 index.AttachChild(a[2], a[3]); | |
101 index.AttachChild(a[2], a[4]); | |
102 index.AttachChild(a[6], a[5]); | |
103 | |
104 int64_t parent; | |
105 ASSERT_FALSE(index.LookupParent(parent, a[0])); | |
106 ASSERT_TRUE(index.LookupParent(parent, a[1])); ASSERT_EQ(a[0], parent); | |
107 ASSERT_TRUE(index.LookupParent(parent, a[2])); ASSERT_EQ(a[1], parent); | |
108 ASSERT_TRUE(index.LookupParent(parent, a[3])); ASSERT_EQ(a[2], parent); | |
109 ASSERT_TRUE(index.LookupParent(parent, a[4])); ASSERT_EQ(a[2], parent); | |
110 ASSERT_TRUE(index.LookupParent(parent, a[5])); ASSERT_EQ(a[6], parent); | |
111 ASSERT_FALSE(index.LookupParent(parent, a[6])); | |
112 | |
113 std::string s; | |
114 | |
115 ASSERT_FALSE(index.GetParentPublicId(s, a[0])); | |
116 ASSERT_FALSE(index.GetParentPublicId(s, a[6])); | |
117 ASSERT_TRUE(index.GetParentPublicId(s, a[1])); ASSERT_EQ("a", s); | |
118 ASSERT_TRUE(index.GetParentPublicId(s, a[2])); ASSERT_EQ("b", s); | |
119 ASSERT_TRUE(index.GetParentPublicId(s, a[3])); ASSERT_EQ("c", s); | |
120 ASSERT_TRUE(index.GetParentPublicId(s, a[4])); ASSERT_EQ("c", s); | |
121 ASSERT_TRUE(index.GetParentPublicId(s, a[5])); ASSERT_EQ("g", s); | |
122 | |
123 std::list<std::string> l; | |
124 index.GetChildrenPublicId(l, a[0]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("b", l.front()); | |
125 index.GetChildrenPublicId(l, a[1]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("c", l.front()); | |
126 index.GetChildrenPublicId(l, a[3]); ASSERT_EQ(0u, l.size()); | |
127 index.GetChildrenPublicId(l, a[4]); ASSERT_EQ(0u, l.size()); | |
128 index.GetChildrenPublicId(l, a[5]); ASSERT_EQ(0u, l.size()); | |
129 index.GetChildrenPublicId(l, a[6]); ASSERT_EQ(1u, l.size()); ASSERT_EQ("f", l.front()); | |
130 | |
131 index.GetChildrenPublicId(l, a[2]); ASSERT_EQ(2u, l.size()); | |
132 if (l.front() == "d") | |
133 { | |
134 ASSERT_EQ("e", l.back()); | |
135 } | |
136 else | |
137 { | |
138 ASSERT_EQ("d", l.back()); | |
139 ASSERT_EQ("e", l.front()); | |
140 } | |
141 | |
142 std::list<MetadataType> md; | |
143 index.ListAvailableMetadata(md, a[4]); | |
144 ASSERT_EQ(0u, md.size()); | |
145 | |
146 index.AddAttachment(a[4], FileInfo("my json file", FileContentType_DicomAsJson, 42, "md5", | |
147 CompressionType_Zlib, 21, "compressedMD5")); | |
148 index.AddAttachment(a[4], FileInfo("my dicom file", FileContentType_Dicom, 42, "md5")); | |
149 index.AddAttachment(a[6], FileInfo("world", FileContentType_Dicom, 44, "md5")); | |
150 index.SetMetadata(a[4], MetadataType_Instance_RemoteAet, "PINNACLE"); | |
151 | |
152 index.ListAvailableMetadata(md, a[4]); | |
153 ASSERT_EQ(1u, md.size()); | |
154 ASSERT_EQ(MetadataType_Instance_RemoteAet, md.front()); | |
155 index.SetMetadata(a[4], MetadataType_ModifiedFrom, "TUTU"); | |
156 index.ListAvailableMetadata(md, a[4]); | |
157 ASSERT_EQ(2u, md.size()); | |
158 index.DeleteMetadata(a[4], MetadataType_ModifiedFrom); | |
159 index.ListAvailableMetadata(md, a[4]); | |
160 ASSERT_EQ(1u, md.size()); | |
161 ASSERT_EQ(MetadataType_Instance_RemoteAet, md.front()); | |
162 | |
163 ASSERT_EQ(21u + 42u + 44u, index.GetTotalCompressedSize()); | |
164 ASSERT_EQ(42u + 42u + 44u, index.GetTotalUncompressedSize()); | |
165 | |
166 DicomMap m; | |
167 m.SetValue(0x0010, 0x0010, "PatientName"); | |
168 index.SetMainDicomTags(a[3], m); | |
169 | |
170 int64_t b; | |
171 ResourceType t; | |
172 ASSERT_TRUE(index.LookupResource("g", b, t)); | |
173 ASSERT_EQ(7, b); | |
174 ASSERT_EQ(ResourceType_Study, t); | |
175 | |
176 ASSERT_TRUE(index.LookupMetadata(s, a[4], MetadataType_Instance_RemoteAet)); | |
177 ASSERT_FALSE(index.LookupMetadata(s, a[4], MetadataType_Instance_IndexInSeries)); | |
178 ASSERT_EQ("PINNACLE", s); | |
179 ASSERT_EQ("PINNACLE", index.GetMetadata(a[4], MetadataType_Instance_RemoteAet)); | |
180 ASSERT_EQ("None", index.GetMetadata(a[4], MetadataType_Instance_IndexInSeries, "None")); | |
181 | |
182 ASSERT_TRUE(index.LookupGlobalProperty(s, GlobalProperty_FlushSleep)); | |
183 ASSERT_FALSE(index.LookupGlobalProperty(s, static_cast<GlobalProperty>(42))); | |
184 ASSERT_EQ("World", s); | |
185 ASSERT_EQ("World", index.GetGlobalProperty(GlobalProperty_FlushSleep)); | |
186 ASSERT_EQ("None", index.GetGlobalProperty(static_cast<GlobalProperty>(42), "None")); | |
187 | |
188 FileInfo att; | |
189 ASSERT_TRUE(index.LookupAttachment(att, a[4], FileContentType_DicomAsJson)); | |
190 ASSERT_EQ("my json file", att.GetUuid()); | |
191 ASSERT_EQ(21u, att.GetCompressedSize()); | |
192 ASSERT_EQ("md5", att.GetUncompressedMD5()); | |
193 ASSERT_EQ("compressedMD5", att.GetCompressedMD5()); | |
194 ASSERT_EQ(42u, att.GetUncompressedSize()); | |
195 ASSERT_EQ(CompressionType_Zlib, att.GetCompressionType()); | |
196 | |
197 ASSERT_TRUE(index.LookupAttachment(att, a[6], FileContentType_Dicom)); | |
198 ASSERT_EQ("world", att.GetUuid()); | |
199 ASSERT_EQ(44u, att.GetCompressedSize()); | |
200 ASSERT_EQ("md5", att.GetUncompressedMD5()); | |
201 ASSERT_EQ("md5", att.GetCompressedMD5()); | |
202 ASSERT_EQ(44u, att.GetUncompressedSize()); | |
203 ASSERT_EQ(CompressionType_None, att.GetCompressionType()); | |
204 | |
205 ASSERT_EQ(0u, listener.deletedFiles_.size()); | |
206 ASSERT_EQ(7u, index.GetTableRecordCount("Resources")); | |
207 ASSERT_EQ(3u, index.GetTableRecordCount("AttachedFiles")); | |
208 ASSERT_EQ(1u, index.GetTableRecordCount("Metadata")); | |
209 ASSERT_EQ(1u, index.GetTableRecordCount("MainDicomTags")); | |
210 index.DeleteResource(a[0]); | |
211 | |
212 ASSERT_EQ(2u, listener.deletedFiles_.size()); | |
213 ASSERT_FALSE(std::find(listener.deletedFiles_.begin(), | |
214 listener.deletedFiles_.end(), | |
215 "my json file") == listener.deletedFiles_.end()); | |
216 ASSERT_FALSE(std::find(listener.deletedFiles_.begin(), | |
217 listener.deletedFiles_.end(), | |
218 "my dicom file") == listener.deletedFiles_.end()); | |
219 | |
220 ASSERT_EQ(2u, index.GetTableRecordCount("Resources")); | |
221 ASSERT_EQ(0u, index.GetTableRecordCount("Metadata")); | |
222 ASSERT_EQ(1u, index.GetTableRecordCount("AttachedFiles")); | |
223 ASSERT_EQ(0u, index.GetTableRecordCount("MainDicomTags")); | |
224 index.DeleteResource(a[5]); | |
225 ASSERT_EQ(0u, index.GetTableRecordCount("Resources")); | |
226 ASSERT_EQ(0u, index.GetTableRecordCount("AttachedFiles")); | |
227 ASSERT_EQ(2u, index.GetTableRecordCount("GlobalProperties")); | |
228 | |
229 ASSERT_EQ(3u, listener.deletedFiles_.size()); | |
230 ASSERT_FALSE(std::find(listener.deletedFiles_.begin(), | |
231 listener.deletedFiles_.end(), | |
232 "world") == listener.deletedFiles_.end()); | |
233 } | |
234 | |
235 | |
236 | |
237 | |
238 TEST(DatabaseWrapper, Upward) | |
239 { | |
240 ServerIndexListener listener; | |
241 DatabaseWrapper index(listener); | |
242 | |
243 int64_t a[] = { | |
244 index.CreateResource("a", ResourceType_Patient), // 0 | |
245 index.CreateResource("b", ResourceType_Study), // 1 | |
246 index.CreateResource("c", ResourceType_Series), // 2 | |
247 index.CreateResource("d", ResourceType_Instance), // 3 | |
248 index.CreateResource("e", ResourceType_Instance), // 4 | |
249 index.CreateResource("f", ResourceType_Study), // 5 | |
250 index.CreateResource("g", ResourceType_Series), // 6 | |
251 index.CreateResource("h", ResourceType_Series) // 7 | |
252 }; | |
253 | |
254 index.AttachChild(a[0], a[1]); | |
255 index.AttachChild(a[1], a[2]); | |
256 index.AttachChild(a[2], a[3]); | |
257 index.AttachChild(a[2], a[4]); | |
258 index.AttachChild(a[1], a[6]); | |
259 index.AttachChild(a[0], a[5]); | |
260 index.AttachChild(a[5], a[7]); | |
261 | |
262 { | |
263 Json::Value j; | |
264 index.GetChildren(j, a[0]); | |
265 ASSERT_EQ(2u, j.size()); | |
266 ASSERT_TRUE((j[0u] == "b" && j[1u] == "f") || | |
267 (j[1u] == "b" && j[0u] == "f")); | |
268 | |
269 index.GetChildren(j, a[1]); | |
270 ASSERT_EQ(2u, j.size()); | |
271 ASSERT_TRUE((j[0u] == "c" && j[1u] == "g") || | |
272 (j[1u] == "c" && j[0u] == "g")); | |
273 | |
274 index.GetChildren(j, a[2]); | |
275 ASSERT_EQ(2u, j.size()); | |
276 ASSERT_TRUE((j[0u] == "d" && j[1u] == "e") || | |
277 (j[1u] == "d" && j[0u] == "e")); | |
278 | |
279 index.GetChildren(j, a[3]); ASSERT_EQ(0u, j.size()); | |
280 index.GetChildren(j, a[4]); ASSERT_EQ(0u, j.size()); | |
281 index.GetChildren(j, a[5]); ASSERT_EQ(1u, j.size()); ASSERT_EQ("h", j[0u].asString()); | |
282 index.GetChildren(j, a[6]); ASSERT_EQ(0u, j.size()); | |
283 index.GetChildren(j, a[7]); ASSERT_EQ(0u, j.size()); | |
284 } | |
285 | |
286 listener.Reset(); | |
287 index.DeleteResource(a[3]); | |
288 ASSERT_EQ("c", listener.ancestorId_); | |
289 ASSERT_EQ(ResourceType_Series, listener.ancestorType_); | |
290 | |
291 listener.Reset(); | |
292 index.DeleteResource(a[4]); | |
293 ASSERT_EQ("b", listener.ancestorId_); | |
294 ASSERT_EQ(ResourceType_Study, listener.ancestorType_); | |
295 | |
296 listener.Reset(); | |
297 index.DeleteResource(a[7]); | |
298 ASSERT_EQ("a", listener.ancestorId_); | |
299 ASSERT_EQ(ResourceType_Patient, listener.ancestorType_); | |
300 | |
301 listener.Reset(); | |
302 index.DeleteResource(a[6]); | |
303 ASSERT_EQ("", listener.ancestorId_); // No more ancestor | |
304 } | |
305 | |
306 | |
307 TEST(DatabaseWrapper, PatientRecycling) | |
308 { | |
309 ServerIndexListener listener; | |
310 DatabaseWrapper index(listener); | |
311 | |
312 std::vector<int64_t> patients; | |
313 for (int i = 0; i < 10; i++) | |
314 { | |
315 std::string p = "Patient " + boost::lexical_cast<std::string>(i); | |
316 patients.push_back(index.CreateResource(p, ResourceType_Patient)); | |
317 index.AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10, | |
318 "md5-" + boost::lexical_cast<std::string>(i))); | |
319 ASSERT_FALSE(index.IsProtectedPatient(patients[i])); | |
320 } | |
321 | |
322 ASSERT_EQ(10u, index.GetTableRecordCount("Resources")); | |
323 ASSERT_EQ(10u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
324 | |
325 listener.Reset(); | |
326 | |
327 index.DeleteResource(patients[5]); | |
328 index.DeleteResource(patients[0]); | |
329 ASSERT_EQ(8u, index.GetTableRecordCount("Resources")); | |
330 ASSERT_EQ(8u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
331 | |
332 ASSERT_EQ(2u, listener.deletedFiles_.size()); | |
333 ASSERT_EQ("Patient 5", listener.deletedFiles_[0]); | |
334 ASSERT_EQ("Patient 0", listener.deletedFiles_[1]); | |
335 | |
336 int64_t p; | |
337 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[1]); | |
338 index.DeleteResource(p); | |
339 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[2]); | |
340 index.DeleteResource(p); | |
341 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[3]); | |
342 index.DeleteResource(p); | |
343 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[4]); | |
344 index.DeleteResource(p); | |
345 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[6]); | |
346 index.DeleteResource(p); | |
347 index.DeleteResource(patients[8]); | |
348 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[7]); | |
349 index.DeleteResource(p); | |
350 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[9]); | |
351 index.DeleteResource(p); | |
352 ASSERT_FALSE(index.SelectPatientToRecycle(p)); | |
353 | |
354 ASSERT_EQ(10u, listener.deletedFiles_.size()); | |
355 ASSERT_EQ(0u, index.GetTableRecordCount("Resources")); | |
356 ASSERT_EQ(0u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
357 } | |
358 | |
359 | |
360 TEST(DatabaseWrapper, PatientProtection) | |
361 { | |
362 ServerIndexListener listener; | |
363 DatabaseWrapper index(listener); | |
364 | |
365 std::vector<int64_t> patients; | |
366 for (int i = 0; i < 5; i++) | |
367 { | |
368 std::string p = "Patient " + boost::lexical_cast<std::string>(i); | |
369 patients.push_back(index.CreateResource(p, ResourceType_Patient)); | |
370 index.AddAttachment(patients[i], FileInfo(p, FileContentType_Dicom, i + 10, | |
371 "md5-" + boost::lexical_cast<std::string>(i))); | |
372 ASSERT_FALSE(index.IsProtectedPatient(patients[i])); | |
373 } | |
374 | |
375 ASSERT_EQ(5u, index.GetTableRecordCount("Resources")); | |
376 ASSERT_EQ(5u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
377 | |
378 ASSERT_FALSE(index.IsProtectedPatient(patients[2])); | |
379 index.SetProtectedPatient(patients[2], true); | |
380 ASSERT_TRUE(index.IsProtectedPatient(patients[2])); | |
381 ASSERT_EQ(4u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
382 ASSERT_EQ(5u, index.GetTableRecordCount("Resources")); | |
383 | |
384 index.SetProtectedPatient(patients[2], true); | |
385 ASSERT_TRUE(index.IsProtectedPatient(patients[2])); | |
386 ASSERT_EQ(4u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
387 index.SetProtectedPatient(patients[2], false); | |
388 ASSERT_FALSE(index.IsProtectedPatient(patients[2])); | |
389 ASSERT_EQ(5u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
390 index.SetProtectedPatient(patients[2], false); | |
391 ASSERT_FALSE(index.IsProtectedPatient(patients[2])); | |
392 ASSERT_EQ(5u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
393 | |
394 ASSERT_EQ(5u, index.GetTableRecordCount("Resources")); | |
395 ASSERT_EQ(5u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
396 index.SetProtectedPatient(patients[2], true); | |
397 ASSERT_TRUE(index.IsProtectedPatient(patients[2])); | |
398 ASSERT_EQ(4u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
399 index.SetProtectedPatient(patients[2], false); | |
400 ASSERT_FALSE(index.IsProtectedPatient(patients[2])); | |
401 ASSERT_EQ(5u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
402 index.SetProtectedPatient(patients[3], true); | |
403 ASSERT_TRUE(index.IsProtectedPatient(patients[3])); | |
404 ASSERT_EQ(4u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
405 | |
406 ASSERT_EQ(5u, index.GetTableRecordCount("Resources")); | |
407 ASSERT_EQ(0u, listener.deletedFiles_.size()); | |
408 | |
409 // Unprotecting a patient puts it at the last position in the recycling queue | |
410 int64_t p; | |
411 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[0]); | |
412 index.DeleteResource(p); | |
413 ASSERT_TRUE(index.SelectPatientToRecycle(p, patients[1])); ASSERT_EQ(p, patients[4]); | |
414 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[1]); | |
415 index.DeleteResource(p); | |
416 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[4]); | |
417 index.DeleteResource(p); | |
418 ASSERT_FALSE(index.SelectPatientToRecycle(p, patients[2])); | |
419 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[2]); | |
420 index.DeleteResource(p); | |
421 // "patients[3]" is still protected | |
422 ASSERT_FALSE(index.SelectPatientToRecycle(p)); | |
423 | |
424 ASSERT_EQ(4u, listener.deletedFiles_.size()); | |
425 ASSERT_EQ(1u, index.GetTableRecordCount("Resources")); | |
426 ASSERT_EQ(0u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
427 | |
428 index.SetProtectedPatient(patients[3], false); | |
429 ASSERT_EQ(1u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
430 ASSERT_FALSE(index.SelectPatientToRecycle(p, patients[3])); | |
431 ASSERT_TRUE(index.SelectPatientToRecycle(p, patients[2])); | |
432 ASSERT_TRUE(index.SelectPatientToRecycle(p)); ASSERT_EQ(p, patients[3]); | |
433 index.DeleteResource(p); | |
434 | |
435 ASSERT_EQ(5u, listener.deletedFiles_.size()); | |
436 ASSERT_EQ(0u, index.GetTableRecordCount("Resources")); | |
437 ASSERT_EQ(0u, index.GetTableRecordCount("PatientRecyclingOrder")); | |
438 } | |
439 | |
440 | |
441 | |
442 TEST(DatabaseWrapper, Sequence) | |
443 { | |
444 ServerIndexListener listener; | |
445 DatabaseWrapper index(listener); | |
446 | |
447 ASSERT_EQ(1u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); | |
448 ASSERT_EQ(2u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); | |
449 ASSERT_EQ(3u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); | |
450 ASSERT_EQ(4u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); | |
451 } | |
452 | |
453 | |
454 | |
455 TEST(DatabaseWrapper, LookupTagValue) | |
456 { | |
457 ServerIndexListener listener; | |
458 DatabaseWrapper index(listener); | |
459 | |
460 int64_t a[] = { | |
461 index.CreateResource("a", ResourceType_Study), // 0 | |
462 index.CreateResource("b", ResourceType_Study), // 1 | |
463 index.CreateResource("c", ResourceType_Study), // 2 | |
464 index.CreateResource("d", ResourceType_Series) // 3 | |
465 }; | |
466 | |
467 DicomMap m; | |
468 m.Clear(); m.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "0"); index.SetMainDicomTags(a[0], m); | |
469 m.Clear(); m.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "1"); index.SetMainDicomTags(a[1], m); | |
470 m.Clear(); m.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "0"); index.SetMainDicomTags(a[2], m); | |
471 m.Clear(); m.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "0"); index.SetMainDicomTags(a[3], m); | |
472 | |
473 std::list<int64_t> s; | |
474 | |
475 index.LookupTagValue(s, DICOM_TAG_STUDY_INSTANCE_UID, "0"); | |
476 ASSERT_EQ(2u, s.size()); | |
477 ASSERT_TRUE(std::find(s.begin(), s.end(), a[0]) != s.end()); | |
478 ASSERT_TRUE(std::find(s.begin(), s.end(), a[2]) != s.end()); | |
479 | |
480 index.LookupTagValue(s, "0"); | |
481 ASSERT_EQ(3u, s.size()); | |
482 ASSERT_TRUE(std::find(s.begin(), s.end(), a[0]) != s.end()); | |
483 ASSERT_TRUE(std::find(s.begin(), s.end(), a[2]) != s.end()); | |
484 ASSERT_TRUE(std::find(s.begin(), s.end(), a[3]) != s.end()); | |
485 | |
486 index.LookupTagValue(s, DICOM_TAG_STUDY_INSTANCE_UID, "1"); | |
487 ASSERT_EQ(1u, s.size()); | |
488 ASSERT_TRUE(std::find(s.begin(), s.end(), a[1]) != s.end()); | |
489 | |
490 index.LookupTagValue(s, "1"); | |
491 ASSERT_EQ(1u, s.size()); | |
492 ASSERT_TRUE(std::find(s.begin(), s.end(), a[1]) != s.end()); | |
493 | |
494 | |
495 /*{ | |
496 std::list<std::string> s; | |
497 context.GetIndex().LookupTagValue(s, DICOM_TAG_STUDY_INSTANCE_UID, "1.2.250.1.74.20130819132500.29000036381059"); | |
498 for (std::list<std::string>::iterator i = s.begin(); i != s.end(); i++) | |
499 { | |
500 std::cout << "*** " << *i << std::endl;; | |
501 } | |
502 }*/ | |
503 } | |
504 | |
505 | |
506 | |
507 TEST(ServerIndex, AttachmentRecycling) | |
508 { | |
509 const std::string path = "OrthancStorageUnitTests"; | |
510 Toolbox::RemoveFile(path + "/index"); | |
511 ServerContext context(path, ":memory:"); // The SQLite DB is in memory | |
512 ServerIndex& index = context.GetIndex(); | |
513 | |
514 index.SetMaximumStorageSize(10); | |
515 | |
516 Json::Value tmp; | |
517 index.ComputeStatistics(tmp); | |
518 ASSERT_EQ(0, tmp["CountPatients"].asInt()); | |
519 ASSERT_EQ(0, boost::lexical_cast<int>(tmp["TotalDiskSize"].asString())); | |
520 | |
521 ServerIndex::Attachments attachments; | |
522 | |
523 std::vector<std::string> ids; | |
524 for (int i = 0; i < 10; i++) | |
525 { | |
526 std::string id = boost::lexical_cast<std::string>(i); | |
527 DicomMap instance; | |
528 instance.SetValue(DICOM_TAG_PATIENT_ID, "patient-" + id); | |
529 instance.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "study-" + id); | |
530 instance.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "series-" + id); | |
531 instance.SetValue(DICOM_TAG_SOP_INSTANCE_UID, "instance-" + id); | |
532 ASSERT_EQ(StoreStatus_Success, index.Store(instance, attachments, "")); | |
533 | |
534 DicomInstanceHasher hasher(instance); | |
535 ids.push_back(hasher.HashPatient()); | |
536 ids.push_back(hasher.HashStudy()); | |
537 ids.push_back(hasher.HashSeries()); | |
538 ids.push_back(hasher.HashInstance()); | |
539 } | |
540 | |
541 index.ComputeStatistics(tmp); | |
542 ASSERT_EQ(10, tmp["CountPatients"].asInt()); | |
543 ASSERT_EQ(0, boost::lexical_cast<int>(tmp["TotalDiskSize"].asString())); | |
544 | |
545 for (size_t i = 0; i < ids.size(); i++) | |
546 { | |
547 FileInfo info(Toolbox::GenerateUuid(), FileContentType_Dicom, 1, "md5"); | |
548 index.AddAttachment(info, ids[i]); | |
549 | |
550 index.ComputeStatistics(tmp); | |
551 ASSERT_GE(10, boost::lexical_cast<int>(tmp["TotalDiskSize"].asString())); | |
552 } | |
553 | |
554 // Because the DB is in memory, the SQLite index must not have been created | |
555 ASSERT_THROW(Toolbox::GetFileSize(path + "/index"), OrthancException); | |
556 } |