-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
GridFSSpec.scala
333 lines (297 loc) · 14.4 KB
/
GridFSSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mongodb.scala.gridfs
import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, File, InputStream }
import java.nio.ByteBuffer
import java.nio.channels.{ Channels, WritableByteChannel }
import scala.collection.JavaConverters._
import scala.collection.mutable.ListBuffer
import scala.io.Source
import scala.util.Try
import org.bson.{ BsonArray, BsonBinary, BsonInt32 }
import org.mongodb.scala._
import org.mongodb.scala.bson.collection.mutable
import org.mongodb.scala.bson.{ BsonDocument, BsonInt64, BsonObjectId, BsonString }
import org.mongodb.scala.model.Indexes.ascending
import org.reactivestreams.Publisher
import org.scalatest.Inspectors.forEvery
class GridFSSpec extends RequiresMongoDBISpec with FuturesSpec {
private val filesCollectionName = "fs.files"
private val chunksCollectionName = "fs.chunks"
lazy val files = new File(getClass.getResource("/gridfs-tests").toURI).listFiles.filter(_.getName.endsWith(".json"))
var gridFSBucket: Option[GridFSBucket] = None
var filesCollection: Option[MongoCollection[Document]] = None
var chunksCollection: Option[MongoCollection[Document]] = None
forEvery(files) { (file: File) =>
s"Running ${file.getName} tests" should "pass all scenarios" in withDatabase(databaseName) { database =>
gridFSBucket = Some(GridFSBucket(database))
filesCollection = Some(database.getCollection(filesCollectionName))
chunksCollection = Some(database.getCollection(chunksCollectionName))
val definition = BsonDocument(Source.fromFile(file).getLines.mkString)
val data = definition.getDocument("data")
val tests = definition.getArray("tests").asScala.map(_.asDocument())
forEvery(tests) { (test: BsonDocument) =>
info(test.getString("description").getValue)
val arrange: BsonDocument = test.getDocument("arrange", BsonDocument())
val action: BsonDocument = test.getDocument("act", BsonDocument())
val assertion: BsonDocument = test.getDocument("assert", BsonDocument())
arrangeGridFS(data, arrange)
actionGridFS(action, assertion)
}
}
}
// scalastyle:off cyclomatic.complexity
private def arrangeGridFS(data: BsonDocument, arrange: BsonDocument): Unit = {
gridFSBucket.map(_.drop()).get.futureValue
val filesDocuments: List[Document] = processFiles(data.getArray("files", new BsonArray))
val chunksDocuments: List[Document] = processChunks(data.getArray("chunks", new BsonArray))
if (filesDocuments.nonEmpty) filesCollection.map(_.insertMany(filesDocuments)).get.futureValue
if (chunksDocuments.nonEmpty) chunksCollection.map(_.insertMany(chunksDocuments)).get.futureValue
for (fileToArrange <- arrange.getArray("data", new BsonArray).asScala) {
val document = fileToArrange.asDocument
if (document.containsKey("delete") && document.containsKey("deletes")) {
for (toDelete <- document.getArray("deletes").asScala) {
val collection = document.getString("delete") match {
case isFilesCollection(_) => filesCollection.get
case isChunksCollection(_) => chunksCollection.get
case x => throw new IllegalArgumentException(s"Unknown collection to delete: $x")
}
val query = toDelete.asDocument.getDocument("q")
val limit: Int = toDelete.asDocument.getInt32("limit").getValue
limit match {
case 1 => collection.deleteOne(query).futureValue
case _ => collection.deleteMany(query).futureValue
}
}
} else if (document.containsKey("insert") && document.containsKey("documents")) {
document.getString("insert") match {
case isFilesCollection(_) =>
filesCollection.map(_.insertMany(processFiles(document.getArray("documents")))).get.futureValue
case isChunksCollection(_) =>
chunksCollection.map(_.insertMany(processChunks(document.getArray("documents")))).get.futureValue
case x => throw new IllegalArgumentException(s"Unknown collection to insert data into: $x")
}
} else if (document.containsKey("update") && document.containsKey("updates")) {
val collection = document.getString("update") match {
case isFilesCollection(_) => filesCollection.get
case isChunksCollection(_) => chunksCollection.get
case x => throw new IllegalArgumentException(s"Unknown collection to update: $x")
}
for (rawUpdate <- document.getArray("updates").asScala) {
val query: Document = rawUpdate.asDocument.getDocument("q")
val update: mutable.Document = mutable.Document(rawUpdate.asDocument.getDocument("u"))
update.put("$set", parseHexDocument(update.get[BsonDocument]("$set").get))
collection.updateMany(query, update).head().futureValue
}
} else {
throw new IllegalArgumentException("Unsupported arrange: " + document)
}
}
}
// scalastyle:on cyclomatic.complexity
private def actionGridFS(action: BsonDocument, assertion: BsonDocument): Unit = {
if (!action.isEmpty) {
action.getString("operation").getValue match {
case "delete" => doDelete(action.getDocument("arguments"), assertion)
case "download" => doDownload(action.getDocument("arguments"), assertion)
case "download_by_name" => doDownloadByName(action.getDocument("arguments"), assertion)
case "upload" => doUpload(action.getDocument("arguments"), assertion)
case x => throw new IllegalArgumentException(s"Unknown operation: $x")
}
}
}
private def doDelete(arguments: BsonDocument, assertion: BsonDocument): Unit = {
val result = Try(gridFSBucket.map(_.delete(arguments.getObjectId("id").getValue)).get.futureValue)
assertion.containsKey("error") match {
case true => result should be a Symbol("failure")
case false =>
result should be a Symbol("success")
for (rawDataItem <- assertion.getArray("data").asScala) {
val dataItem: BsonDocument = rawDataItem.asDocument
for (deletedItem <- dataItem.getArray("deletes", new BsonArray).asScala) {
val delete: String = dataItem.getString("delete", new BsonString("none")).getValue
val id: BsonObjectId = delete match {
case "expected.files" => deletedItem.asDocument.getDocument("q").getObjectId("_id")
case "expected.chunks" => deletedItem.asDocument.getDocument("q").getObjectId("files_id")
}
val filesCount: Long = getFilesCount(new BsonDocument("_id", id))
val chunksCount: Long = getChunksCount(new BsonDocument("files_id", id))
filesCount should equal(0)
chunksCount should equal(0)
}
}
}
}
private def doDownload(arguments: BsonDocument, assertion: BsonDocument): Unit = {
val outputStream = new ByteArrayOutputStream
val channel = Channels.newChannel(outputStream)
val result = Try(
gridFSBucket
.map(
_.downloadToObservable(arguments.getObjectId("id").getValue)
)
.get
.futureValue
.foreach { byteBuffer: ByteBuffer =>
channel.write(byteBuffer)
}
)
assertion.containsKey("error") match {
case true =>
result should be a Symbol("failure")
case false =>
result should be a Symbol("success")
outputStream.toByteArray.map("%02x".format(_)).mkString.toLowerCase should equal(
assertion.getDocument("result").getString("$hex").getValue
)
}
channel.close()
outputStream.close()
}
private def doDownloadByName(arguments: BsonDocument, assertion: BsonDocument): Unit = {
val outputStream = new ByteArrayOutputStream
val channel = Channels.newChannel(outputStream)
val options: GridFSDownloadOptions = new GridFSDownloadOptions()
Option(arguments.get("options")).map(opts => options.revision(opts.asDocument().getInt32("revision").getValue))
val result = Try(
gridFSBucket
.map(
_.downloadToObservable(
arguments.getString("filename").getValue,
options
)
)
.get
.futureValue
.foreach { byteBuffer: ByteBuffer =>
channel.write(byteBuffer)
}
)
channel.close()
outputStream.close()
assertion.containsKey("error") match {
case true =>
result should be a Symbol("failure")
case false =>
result should be a Symbol("success")
outputStream.toByteArray.map("%02x".format(_)).mkString.toLowerCase should equal(
assertion.getDocument("result").getString("$hex").getValue
)
}
}
//scalastyle:off method.length
private def doUpload(rawArguments: BsonDocument, assertion: BsonDocument): Unit = {
val arguments: BsonDocument = parseHexDocument(rawArguments, "source")
val filename: String = arguments.getString("filename").getValue
val source: Observable[ByteBuffer] = Observable(Seq(ByteBuffer.wrap(arguments.getBinary("source").getData)))
val rawOptions: Document = arguments.getDocument("options", new BsonDocument())
val options: GridFSUploadOptions = new GridFSUploadOptions()
rawOptions.get[BsonInt32]("chunkSizeBytes").map(chunkSize => options.chunkSizeBytes(chunkSize.getValue))
rawOptions.get[BsonDocument]("metadata").map(doc => options.metadata(doc))
val result = Try(
gridFSBucket
.map(
_.uploadFromObservable(filename, source, options).head()
)
.get
.futureValue
)
assertion.containsKey("error") match {
case true =>
result should be a Symbol("failure")
/*
// We don't need to read anything more so don't see the extra chunk
if (!assertion.getString("error").getValue == "ExtraChunk") assertNotNull("Should have thrown an exception", error)
*/
case false =>
result should be a Symbol("success")
val objectId = result.get
for (rawDataItem <- assertion.getArray("data", new BsonArray).asScala) {
val dataItem: BsonDocument = rawDataItem.asDocument
val insert: String = dataItem.getString("insert", new BsonString("none")).getValue
insert match {
case "expected.files" =>
val documents: List[Document] = processFiles(dataItem.getArray("documents", new BsonArray))
getFilesCount(new BsonDocument) should equal(documents.size)
val actual: Document = filesCollection.map(_.find().first().head()).get.futureValue
for (expected <- documents) {
expected.get("length") should equal(actual.get("length"))
expected.get("chunkSize") should equal(actual.get("chunkSize"))
expected.get("filename") should equal(actual.get("filename"))
if (expected.contains("metadata")) expected.get("metadata") should equal(actual.get("metadata"))
}
case "expected.chunks" =>
val documents: List[Document] = processChunks(dataItem.getArray("documents", new BsonArray))
getChunksCount(new BsonDocument) should equal(documents.size)
val actualDocuments: Seq[Document] = chunksCollection.map(_.find().sort(ascending("n"))).get.futureValue
for ((expected, actual) <- documents zip actualDocuments) {
objectId should equal(actual.get[BsonObjectId]("files_id").get.getValue())
expected.get("n") should equal(actual.get("n"))
expected.get("data") should equal(actual.get("data"))
}
}
}
}
}
//scalastyle:on method.length
private def getChunksCount(filter: BsonDocument): Long =
chunksCollection.map(col => col.countDocuments(filter).head()).get.futureValue
private def getFilesCount(filter: BsonDocument): Long =
filesCollection.map(col => col.countDocuments(filter).head()).get.futureValue
private def processFiles(bsonArray: BsonArray): List[Document] = {
val documents = ListBuffer[Document]()
for (rawDocument <- bsonArray.getValues.asScala) {
if (rawDocument.isDocument) {
val document: BsonDocument = rawDocument.asDocument
if (document.get("length").isInt32) document.put("length", BsonInt64(document.getInt32("length").getValue))
if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty) document.remove("metadata")
if (document.containsKey("aliases") && document.getArray("aliases").getValues.size == 0)
document.remove("aliases")
if (document.containsKey("contentType") && document.getString("contentType").getValue.length == 0)
document.remove("contentType")
documents += document
}
}
documents.toList
}
private def processChunks(bsonArray: BsonArray): List[Document] = {
val documents = ListBuffer[Document]()
for (rawDocument <- bsonArray.getValues.asScala) {
if (rawDocument.isDocument) documents += parseHexDocument(rawDocument.asDocument)
}
documents.toList
}
private def parseHexDocument(document: BsonDocument): BsonDocument = parseHexDocument(document, "data")
private def parseHexDocument(document: BsonDocument, hexDocument: String): BsonDocument = {
if (document.contains(hexDocument) && document.get(hexDocument).isDocument) {
val bytes: Array[Byte] = document
.getDocument(hexDocument)
.getString("$hex")
.getValue
.sliding(2, 2)
.map(i => Integer.parseInt(i, 16).toByte)
.toArray
document.put(hexDocument, new BsonBinary(bytes))
}
document
}
private object isFilesCollection {
def unapply(name: BsonString): Option[Boolean] = if (name.getValue == filesCollectionName) Some(true) else None
}
private object isChunksCollection {
def unapply(name: BsonString): Option[Boolean] = if (name.getValue == chunksCollectionName) Some(true) else None
}
}