comparison OrthancServer/UnitTestsSources/FileStorageTests.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents UnitTestsSources/FileStorageTests.cpp@94f4a18a79cc
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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 #include "PrecompiledHeadersUnitTests.h"
35 #include "gtest/gtest.h"
36
37 #include <ctype.h>
38
39 #include "../Core/FileStorage/FilesystemStorage.h"
40 #include "../Core/FileStorage/StorageAccessor.h"
41 #include "../Core/HttpServer/BufferHttpSender.h"
42 #include "../Core/HttpServer/FilesystemHttpSender.h"
43 #include "../Core/Logging.h"
44 #include "../Core/OrthancException.h"
45 #include "../Core/Toolbox.h"
46 #include "../OrthancServer/ServerIndex.h"
47
48 using namespace Orthanc;
49
50
51 static void StringToVector(std::vector<uint8_t>& v,
52 const std::string& s)
53 {
54 v.resize(s.size());
55 for (size_t i = 0; i < s.size(); i++)
56 v[i] = s[i];
57 }
58
59
60 TEST(FilesystemStorage, Basic)
61 {
62 FilesystemStorage s("UnitTestsStorage");
63
64 std::string data = Toolbox::GenerateUuid();
65 std::string uid = Toolbox::GenerateUuid();
66 s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
67 std::string d;
68 s.Read(d, uid, FileContentType_Unknown);
69 ASSERT_EQ(d.size(), data.size());
70 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
71 ASSERT_EQ(s.GetSize(uid), data.size());
72 }
73
74 TEST(FilesystemStorage, Basic2)
75 {
76 FilesystemStorage s("UnitTestsStorage");
77
78 std::vector<uint8_t> data;
79 StringToVector(data, Toolbox::GenerateUuid());
80 std::string uid = Toolbox::GenerateUuid();
81 s.Create(uid.c_str(), &data[0], data.size(), FileContentType_Unknown);
82 std::string d;
83 s.Read(d, uid, FileContentType_Unknown);
84 ASSERT_EQ(d.size(), data.size());
85 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
86 ASSERT_EQ(s.GetSize(uid), data.size());
87 }
88
89 TEST(FilesystemStorage, EndToEnd)
90 {
91 FilesystemStorage s("UnitTestsStorage");
92 s.Clear();
93
94 std::list<std::string> u;
95 for (unsigned int i = 0; i < 10; i++)
96 {
97 std::string t = Toolbox::GenerateUuid();
98 std::string uid = Toolbox::GenerateUuid();
99 s.Create(uid.c_str(), &t[0], t.size(), FileContentType_Unknown);
100 u.push_back(uid);
101 }
102
103 std::set<std::string> ss;
104 s.ListAllFiles(ss);
105 ASSERT_EQ(10u, ss.size());
106
107 unsigned int c = 0;
108 for (std::list<std::string>::iterator
109 i = u.begin(); i != u.end(); i++, c++)
110 {
111 ASSERT_TRUE(ss.find(*i) != ss.end());
112 if (c < 5)
113 s.Remove(*i, FileContentType_Unknown);
114 }
115
116 s.ListAllFiles(ss);
117 ASSERT_EQ(5u, ss.size());
118
119 s.Clear();
120 s.ListAllFiles(ss);
121 ASSERT_EQ(0u, ss.size());
122 }
123
124
125 TEST(StorageAccessor, NoCompression)
126 {
127 FilesystemStorage s("UnitTestsStorage");
128 StorageAccessor accessor(s);
129
130 std::string data = "Hello world";
131 FileInfo info = accessor.Write(data, FileContentType_Dicom, CompressionType_None, true);
132
133 std::string r;
134 accessor.Read(r, info);
135
136 ASSERT_EQ(data, r);
137 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
138 ASSERT_EQ(11u, info.GetUncompressedSize());
139 ASSERT_EQ(11u, info.GetCompressedSize());
140 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
141 ASSERT_EQ("3e25960a79dbc69b674cd4ec67a72c62", info.GetUncompressedMD5());
142 ASSERT_EQ(info.GetUncompressedMD5(), info.GetCompressedMD5());
143 }
144
145
146 TEST(StorageAccessor, Compression)
147 {
148 FilesystemStorage s("UnitTestsStorage");
149 StorageAccessor accessor(s);
150
151 std::string data = "Hello world";
152 FileInfo info = accessor.Write(data, FileContentType_DicomAsJson, CompressionType_ZlibWithSize, true);
153
154 std::string r;
155 accessor.Read(r, info);
156
157 ASSERT_EQ(data, r);
158 ASSERT_EQ(CompressionType_ZlibWithSize, info.GetCompressionType());
159 ASSERT_EQ(11u, info.GetUncompressedSize());
160 ASSERT_EQ(FileContentType_DicomAsJson, info.GetContentType());
161 ASSERT_EQ("3e25960a79dbc69b674cd4ec67a72c62", info.GetUncompressedMD5());
162 ASSERT_NE(info.GetUncompressedMD5(), info.GetCompressedMD5());
163 }
164
165
166 TEST(StorageAccessor, Mix)
167 {
168 FilesystemStorage s("UnitTestsStorage");
169 StorageAccessor accessor(s);
170
171 std::string r;
172 std::string compressedData = "Hello";
173 std::string uncompressedData = "HelloWorld";
174
175 FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom, CompressionType_ZlibWithSize, false);
176 FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom, CompressionType_None, false);
177
178 accessor.Read(r, compressedInfo);
179 ASSERT_EQ(compressedData, r);
180
181 accessor.Read(r, uncompressedInfo);
182 ASSERT_EQ(uncompressedData, r);
183 ASSERT_NE(compressedData, r);
184
185 /*
186 // This test is too slow on Windows
187 accessor.SetCompressionForNextOperations(CompressionType_ZlibWithSize);
188 ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid(), FileContentType_Unknown), OrthancException);
189 */
190 }