comparison UnitTestsSources/FileStorageTests.cpp @ 967:dfc076546821

add suffix Tests to unit test sources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jun 2014 15:36:38 +0200
parents UnitTestsSources/FileStorage.cpp@84513f2ee1f3
children 1d60316c3618
comparison
equal deleted inserted replaced
966:886652370ff2 967:dfc076546821
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * 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/FileStorage.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(FileStorage, Basic)
62 {
63 FileStorage s("UnitTestsStorage");
64
65 std::string data = Toolbox::GenerateUuid();
66 std::string uid = s.Create(data);
67 std::string d;
68 s.ReadFile(d, uid);
69 ASSERT_EQ(d.size(), data.size());
70 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
71 ASSERT_EQ(s.GetCompressedSize(uid), data.size());
72 }
73
74 TEST(FileStorage, Basic2)
75 {
76 FileStorage s("UnitTestsStorage");
77
78 std::vector<uint8_t> data;
79 StringToVector(data, Toolbox::GenerateUuid());
80 std::string uid = s.Create(data);
81 std::string d;
82 s.ReadFile(d, uid);
83 ASSERT_EQ(d.size(), data.size());
84 ASSERT_FALSE(memcmp(&d[0], &data[0], data.size()));
85 ASSERT_EQ(s.GetCompressedSize(uid), data.size());
86 }
87
88 TEST(FileStorage, EndToEnd)
89 {
90 FileStorage s("UnitTestsStorage");
91 s.Clear();
92
93 std::list<std::string> u;
94 for (unsigned int i = 0; i < 10; i++)
95 {
96 u.push_back(s.Create(Toolbox::GenerateUuid()));
97 }
98
99 std::set<std::string> ss;
100 s.ListAllFiles(ss);
101 ASSERT_EQ(10u, ss.size());
102
103 unsigned int c = 0;
104 for (std::list<std::string>::iterator
105 i = u.begin(); i != u.end(); i++, c++)
106 {
107 ASSERT_TRUE(ss.find(*i) != ss.end());
108 if (c < 5)
109 s.Remove(*i);
110 }
111
112 s.ListAllFiles(ss);
113 ASSERT_EQ(5u, ss.size());
114
115 s.Clear();
116 s.ListAllFiles(ss);
117 ASSERT_EQ(0u, ss.size());
118 }
119
120
121 TEST(FileStorageAccessor, Simple)
122 {
123 FileStorage s("UnitTestsStorage");
124 FileStorageAccessor accessor(s);
125
126 std::string data = "Hello world";
127 FileInfo info = accessor.Write(data, FileContentType_Dicom);
128
129 std::string r;
130 accessor.Read(r, info.GetUuid());
131
132 ASSERT_EQ(data, r);
133 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
134 ASSERT_EQ(11u, info.GetUncompressedSize());
135 ASSERT_EQ(11u, info.GetCompressedSize());
136 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
137 }
138
139
140 TEST(FileStorageAccessor, NoCompression)
141 {
142 FileStorage s("UnitTestsStorage");
143 CompressedFileStorageAccessor accessor(s);
144
145 accessor.SetCompressionForNextOperations(CompressionType_None);
146 std::string data = "Hello world";
147 FileInfo info = accessor.Write(data, FileContentType_Dicom);
148
149 std::string r;
150 accessor.Read(r, info.GetUuid());
151
152 ASSERT_EQ(data, r);
153 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
154 ASSERT_EQ(11u, info.GetUncompressedSize());
155 ASSERT_EQ(11u, info.GetCompressedSize());
156 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
157 }
158
159
160 TEST(FileStorageAccessor, NoCompression2)
161 {
162 FileStorage s("UnitTestsStorage");
163 CompressedFileStorageAccessor accessor(s);
164
165 accessor.SetCompressionForNextOperations(CompressionType_None);
166 std::vector<uint8_t> data;
167 StringToVector(data, "Hello world");
168 FileInfo info = accessor.Write(data, FileContentType_Dicom);
169
170 std::string r;
171 accessor.Read(r, info.GetUuid());
172
173 ASSERT_EQ(0, memcmp(&r[0], &data[0], data.size()));
174 ASSERT_EQ(CompressionType_None, info.GetCompressionType());
175 ASSERT_EQ(11u, info.GetUncompressedSize());
176 ASSERT_EQ(11u, info.GetCompressedSize());
177 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
178 }
179
180
181 TEST(FileStorageAccessor, Compression)
182 {
183 FileStorage s("UnitTestsStorage");
184 CompressedFileStorageAccessor accessor(s);
185
186 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
187 std::string data = "Hello world";
188 FileInfo info = accessor.Write(data, FileContentType_Dicom);
189
190 std::string r;
191 accessor.Read(r, info.GetUuid());
192
193 ASSERT_EQ(data, r);
194 ASSERT_EQ(CompressionType_Zlib, info.GetCompressionType());
195 ASSERT_EQ(11u, info.GetUncompressedSize());
196 ASSERT_EQ(FileContentType_Dicom, info.GetContentType());
197 }
198
199
200 TEST(FileStorageAccessor, Mix)
201 {
202 FileStorage s("UnitTestsStorage");
203 CompressedFileStorageAccessor accessor(s);
204
205 std::string r;
206 std::string compressedData = "Hello";
207 std::string uncompressedData = "HelloWorld";
208
209 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
210 FileInfo compressedInfo = accessor.Write(compressedData, FileContentType_Dicom);
211
212 accessor.SetCompressionForNextOperations(CompressionType_None);
213 FileInfo uncompressedInfo = accessor.Write(uncompressedData, FileContentType_Dicom);
214
215 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
216 accessor.Read(r, compressedInfo.GetUuid());
217 ASSERT_EQ(compressedData, r);
218
219 accessor.SetCompressionForNextOperations(CompressionType_None);
220 accessor.Read(r, compressedInfo.GetUuid());
221 ASSERT_NE(compressedData, r);
222
223 /*
224 // This test is too slow on Windows
225 accessor.SetCompressionForNextOperations(CompressionType_Zlib);
226 ASSERT_THROW(accessor.Read(r, uncompressedInfo.GetUuid()), OrthancException);
227 */
228 }