comparison UnitTestsSources/FileStorageTests.cpp @ 1364:111e23bb4904 query-retrieve

integration mainline->query-retrieve
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 21 May 2015 16:58:30 +0200
parents 6e7e5ed91c2d
children f967bdf8534e
comparison
equal deleted inserted replaced
953:f894be6e7cc1 1364:111e23bb4904
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include <ctype.h>
37 #include <glog/logging.h>
38
39 #include "../Core/FileStorage/FilesystemStorage.h"
40 #include "../OrthancServer/ServerIndex.h"
41 #include "../Core/Toolbox.h"
42 #include "../Core/OrthancException.h"
43 #include "../Core/Uuid.h"
44 #include "../Core/HttpServer/FilesystemHttpSender.h"
45 #include "../Core/HttpServer/BufferHttpSender.h"
46 #include "../Core/FileStorage/FileStorageAccessor.h"
47 #include "../Core/FileStorage/CompressedFileStorageAccessor.h"
48
49 using namespace Orthanc;
50
51
52 static void StringToVector(std::vector<uint8_t>& v,
53 const std::string& s)
54 {
55 v.resize(s.size());
56 for (size_t i = 0; i < s.size(); i++)
57 v[i] = s[i];
58 }
59
60
61 TEST(FilesystemStorage, Basic)
62 {
63 FilesystemStorage s("UnitTestsStorage");
64
65 std::string data = Toolbox::GenerateUuid();
66 std::string uid = Toolbox::GenerateUuid();
67 s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
68 std::string d;
69 s.Read(d, uid, FileContentType_Unknown);
70 ASSERT_EQ(d.size(), data.size());
71 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
72 ASSERT_EQ(s.GetSize(uid), data.size());
73 }
74
75 TEST(FilesystemStorage, Basic2)
76 {
77 FilesystemStorage s("UnitTestsStorage");
78
79 std::vector<uint8_t> data;
80 StringToVector(data, Toolbox::GenerateUuid());
81 std::string uid = Toolbox::GenerateUuid();
82 s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
83 std::string d;
84 s.Read(d, uid, FileContentType_Unknown);
85 ASSERT_EQ(d.size(), data.size());
86 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
87 ASSERT_EQ(s.GetSize(uid), data.size());
88 }
89
90 TEST(FilesystemStorage, EndToEnd)
91 {
92 FilesystemStorage s("UnitTestsStorage");
93 s.Clear();
94
95 std::list<std::string> u;
96 for (unsigned int i = 0; i < 10; i++)
97 {
98 std::string t = Toolbox::GenerateUuid();
99 std::string uid = Toolbox::GenerateUuid();
100 s.Create(uid.c_str(), &t[0], t.size(), FileContentType_Unknown);
101 u.push_back(uid);
102 }
103
104 std::set<std::string> ss;
105 s.ListAllFiles(ss);
106 ASSERT_EQ(10u, ss.size());
107
108 unsigned int c = 0;
109 for (std::list<std::string>::iterator
110 i = u.begin(); i != u.end(); i++, c++)
111 {
112 ASSERT_TRUE(ss.find(*i) != ss.end());
113 if (c < 5)
114 s.Remove(*i, FileContentType_Unknown);
115 }
116
117 s.ListAllFiles(ss);
118 ASSERT_EQ(5u, ss.size());
119
120 s.Clear();
121 s.ListAllFiles(ss);
122 ASSERT_EQ(0u, ss.size());
123 }
124
125
126 TEST(FileStorageAccessor, Simple)
127 {
128 FilesystemStorage s("UnitTestsStorage");
129 FileStorageAccessor accessor(s);
130
131 std::string data = "Hello world";
132 FileInfo info = accessor.Write(data, FileContentType_Dicom);
133
134 std::string r;
135 accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
136
137 ASSERT_EQ(data, r);
138 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
139 ASSERT_EQ(11u, info.GetUncompressedSize());
140 ASSERT_EQ(11u, info.GetCompressedSize());
141 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
142 }
143
144
145 TEST(FileStorageAccessor, NoCompression)
146 {
147 FilesystemStorage s("UnitTestsStorage");
148 CompressedFileStorageAccessor accessor(s);
149
150 accessor.SetCompressionForNextOperations(CompressionType_None);
151 std::string data = "Hello world";
152 FileInfo info = accessor.Write(data, FileContentType_Dicom);
153
154 std::string r;
155 accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
156
157 ASSERT_EQ(data, r);
158 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
159 ASSERT_EQ(11u, info.GetUncompressedSize());
160 ASSERT_EQ(11u, info.GetCompressedSize());
161 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
162 }
163
164
165 TEST(FileStorageAccessor, NoCompression2)
166 {
167 FilesystemStorage s("UnitTestsStorage");
168 CompressedFileStorageAccessor accessor(s);
169
170 accessor.SetCompressionForNextOperations(CompressionType_None);
171 std::vector<uint8_t> data;
172 StringToVector(data, "Hello world");
173 FileInfo info = accessor.Write(data, FileContentType_Dicom);
174
175 std::string r;
176 accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
177
178 ASSERT_EQ(0, memcmp(&r[0], &data[0], data.size()));
179 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
180 ASSERT_EQ(11u, info.GetUncompressedSize());
181 ASSERT_EQ(11u, info.GetCompressedSize());
182 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
183 }
184
185
186 TEST(FileStorageAccessor, Compression)
187 {
188 FilesystemStorage s("UnitTestsStorage");
189 CompressedFileStorageAccessor accessor(s);
190
191 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
192 std::string data = "Hello world";
193 FileInfo info = accessor.Write(data, FileContentType_Dicom);
194
195 std::string r;
196 accessor.Read(r, info.GetUuid(), FileContentType_Unknown);
197
198 ASSERT_EQ(data, r);
199 ASSERT_EQ(CompressionType_Zlib, info.GetCompressionType());
200 ASSERT_EQ(11u, info.GetUncompressedSize());
201 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
202 }
203
204
205 TEST(FileStorageAccessor, Mix)
206 {
207 FilesystemStorage s("UnitTestsStorage");
208 CompressedFileStorageAccessor accessor(s);
209
210 std::string r;
211 std::string compressedData = "Hello";
212 std::string uncompressedData = "HelloWorld";
213
214 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
215 FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom);
216
217 accessor.SetCompressionForNextOperations(CompressionType_None);
218 FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom);
219
220 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
221 accessor.Read(r, compressedInfo.GetUuid(), FileContentType_Unknown);
222 ASSERT_EQ(compressedData, r);
223
224 accessor.SetCompressionForNextOperations(CompressionType_None);
225 accessor.Read(r, compressedInfo.GetUuid(), FileContentType_Unknown);
226 ASSERT_NE(compressedData, r);
227
228 /*
229 // This test is too slow on Windows
230 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
231 ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid(), FileContentType_Unknown), OrthancException);
232 */
233 }