comparison OrthancFramework/UnitTestsSources/FileStorageTests.cpp @ 4058:2a8bf0991be0 framework

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