-
Notifications
You must be signed in to change notification settings - Fork 5
/
ElementsTest.scala
526 lines (441 loc) · 24.5 KB
/
ElementsTest.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package se.nimsa.dicom.data
import java.net.URI
import java.time.{LocalDate, ZoneOffset}
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.testkit.TestKit
import akka.util.ByteString
import org.scalatest.{Assertion, BeforeAndAfterAll, FlatSpecLike, Matchers}
import se.nimsa.dicom.data.DicomParsing.defaultCharacterSet
import se.nimsa.dicom.data.DicomParts.HeaderPart
import se.nimsa.dicom.data.Elements._
import se.nimsa.dicom.data.TestData.{studyDate => testStudyDate, _}
import se.nimsa.dicom.streams.ElementFlows.elementFlow
import se.nimsa.dicom.streams.ElementSink._
import se.nimsa.dicom.streams.ParseFlow.parseFlow
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, ExecutionContextExecutor}
class ElementsTest extends TestKit(ActorSystem("ElementsSpec")) with FlatSpecLike with Matchers with BeforeAndAfterAll {
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val ec: ExecutionContextExecutor = system.dispatcher
override def afterAll(): Unit = system.terminate()
def create(elements: ElementSet*): Elements = Elements(defaultCharacterSet, systemZone, elements.toVector)
def toElements(bytes: ByteString): Elements = Await.result(Source.single(bytes).via(parseFlow).via(elementFlow).runWith(elementSink), 5.seconds)
val studyDate: ValueElement = ValueElement.fromString(Tag.StudyDate, "20041230")
val patientName: ValueElement = ValueElement.fromString(Tag.PatientName, "John^Doe")
val patientID1: ValueElement = ValueElement.fromString(Tag.PatientID, "12345678")
val patientID2: ValueElement = ValueElement.fromString(Tag.PatientID, "87654321")
val patientID3: ValueElement = ValueElement.fromString(Tag.PatientID, "18273645")
val seq: Sequence = Sequence(Tag.DerivationCodeSequence, indeterminateLength, List(
Item(indeterminateLength, Elements(defaultCharacterSet, systemZone, Vector(patientID1))),
Item(indeterminateLength, Elements(defaultCharacterSet, systemZone, Vector(patientID2)))
))
val elements: Elements = create(studyDate, seq, patientName)
"Elements" should "return an existing element" in {
elements(Tag.PatientName) shouldBe Some(patientName)
elements(TagPath.fromTag(Tag.PatientName)) shouldBe Some(patientName)
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 1).thenTag(Tag.PatientID)) shouldBe Some(patientID1)
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 2).thenTag(Tag.PatientID)) shouldBe Some(patientID2)
}
it should "support empty and contains tests" in {
elements.isEmpty shouldBe false
elements.nonEmpty shouldBe true
elements.contains(Tag.StudyDate) shouldBe true
elements.head shouldBe studyDate
}
it should "support sorting elements" in {
val unsorted = create(patientName, studyDate)
unsorted.head shouldBe patientName
val sorted = unsorted.sorted()
sorted.head shouldBe studyDate
}
it should "return None for missing element" in {
elements(Tag.SeriesDate) shouldBe None
}
it should "return value elements only" in {
elements.getValueElement(Tag.PatientName) shouldBe defined
elements.getValueElement(Tag.SeriesDate) shouldBe empty
elements.getValueElement(Tag.DerivationCodeSequence) shouldBe empty
}
it should "return the value of an element" in {
val value = elements.getValue(Tag.PatientName)
value shouldBe defined
value.get.length should be > 0
elements.getValue(Tag.SeriesDate) shouldBe empty
}
it should "return the bytes of an element" in {
elements.getBytes(Tag.PatientName) shouldBe defined
}
it should "return all strings in value with VM > 1" in {
val elements = create(ValueElement.fromString(Tag.ImageType, """ORIGINAL\RECON TOMO"""), seq)
val strings = elements.getStrings(Tag.ImageType)
strings should have length 2
strings(1) shouldBe "RECON TOMO"
elements.getStrings(Tag.SeriesDate) should have length 0
elements.getStrings(Tag.DerivationCodeSequence) should have length 0
}
it should "return a concatenated string in value with VM > 1" in {
val elements = create(ValueElement.fromString(Tag.ImageType, """ORIGINAL\RECON TOMO"""))
val singleString = elements.getSingleString(Tag.ImageType)
singleString shouldBe Some("""ORIGINAL\RECON TOMO""")
}
it should "return the first string of a value with VM > 1" in {
val elements = create(ValueElement.fromString(Tag.ImageType, """ORIGINAL\RECON TOMO"""))
val string = elements.getString(Tag.ImageType)
string shouldBe Some("ORIGINAL")
}
it should "return shorts" in {
val elements = create(ValueElement.fromString(Tag.ReferencedFrameNumber, """1\2\3"""))
elements.getShorts(Tag.ReferencedFrameNumber) shouldBe Seq(1, 2, 3).map(_.toShort)
elements.getShort(Tag.ReferencedFrameNumber) shouldBe Some(1.toShort)
}
it should "return ints" in {
val elements = create(ValueElement.fromString(Tag.ReferencedFrameNumber, """1\2\3"""))
elements.getInts(Tag.ReferencedFrameNumber) shouldBe Seq(1, 2, 3)
elements.getInt(Tag.ReferencedFrameNumber) shouldBe Some(1)
}
it should "return longs" in {
val elements = create(ValueElement.fromString(Tag.ReferencedFrameNumber, """1\2\3"""))
elements.getLongs(Tag.ReferencedFrameNumber) shouldBe Seq(1L, 2L, 3L)
elements.getLong(Tag.ReferencedFrameNumber) shouldBe Some(1L)
}
it should "return floats" in {
val elements = create(ValueElement.fromString(Tag.ReferencedFrameNumber, """1\2\3"""))
elements.getFloats(Tag.ReferencedFrameNumber) shouldBe Seq(1f, 2f, 3f)
elements.getFloat(Tag.ReferencedFrameNumber) shouldBe Some(1f)
}
it should "return doubles" in {
val elements = create(ValueElement.fromString(Tag.ReferencedFrameNumber, """1\2\3"""))
elements.getDoubles(Tag.ReferencedFrameNumber) shouldBe Seq(1.0, 2.0, 3.0)
elements.getDouble(Tag.ReferencedFrameNumber) shouldBe Some(1.0)
}
it should "return dates" in {
val dates = Seq(LocalDate.parse("2005-01-01"), LocalDate.parse("2010-01-01"))
val elements = create(ValueElement(Tag.StudyDate, Value.fromDates(VR.DA, dates)))
elements.getDates(Tag.StudyDate) shouldBe dates
elements.getDate(Tag.StudyDate) shouldBe dates.headOption
}
it should "return date times" in {
val dateTimes = Seq(LocalDate.parse("2005-01-01"), LocalDate.parse("2010-01-01"))
.map(_.atStartOfDay(ZoneOffset.of("+04:00")))
val elements = create(ValueElement(Tag.InstanceCoercionDateTime, Value.fromDateTimes(VR.DT, dateTimes)))
elements.getDateTimes(Tag.InstanceCoercionDateTime) shouldBe dateTimes
elements.getDateTime(Tag.InstanceCoercionDateTime) shouldBe dateTimes.headOption
}
it should "return patient names" in {
val names = Seq("Doe^John", "Doe^Jane")
val patientNames = names.flatMap(PatientName.parse)
val elements = create(ValueElement.fromString(Tag.PatientName, names.mkString("\\")))
elements.getPatientNames(Tag.PatientName) shouldBe patientNames
elements.getPatientName(Tag.PatientName) shouldBe patientNames.headOption
}
it should "return sequences" in {
val seq = elements.getSequence(Tag.DerivationCodeSequence)
seq shouldBe defined
seq.get.tag shouldBe Tag.DerivationCodeSequence
elements.getSequence(Tag.PatientName) shouldBe empty
}
it should "return items" in {
val item = elements.getItem(Tag.DerivationCodeSequence, 1)
item shouldBe defined
elements.getItem(Tag.DerivationCodeSequence, 0) shouldBe empty
elements.getItem(Tag.DerivationCodeSequence, 2) shouldBe defined
elements.getItem(Tag.DerivationCodeSequence, 3) shouldBe empty
item shouldBe elements.getSequence(Tag.DerivationCodeSequence).get.item(1)
}
it should "return nested elements" in {
elements.getNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 1)).get shouldBe create(patientID1)
elements.getNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 2)).get shouldBe create(patientID2)
elements.getNested(Tag.DerivationCodeSequence, 1) shouldBe elements.getNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 1))
}
it should "return deeply nested elements" in {
val elements = create(seq + Item(indeterminateLength, create(seq)))
elements.getNested(TagPath
.fromSequence(Tag.DerivationCodeSequence, 3)
.thenSequence(Tag.DerivationCodeSequence, 1)).get shouldBe create(patientID1)
}
it should "not support using wildcards when getting nested elements" in {
val elements = create(seq + Item(indeterminateLength, create(seq)))
intercept[IllegalArgumentException] {
elements.getNested(TagPath
.fromSequence(Tag.DerivationCodeSequence)
.thenSequence(Tag.DerivationCodeSequence, 1))
}
}
it should "return fragments" in {
val elements = create(studyDate, Fragments(Tag.PixelData, VR.OB, Some(Nil),
List(Fragment(4, Value(ByteString(1, 2, 3, 4))))))
elements.getFragments(Tag.PixelData) shouldBe defined
elements.getFragments(Tag.SeriesDate) shouldBe empty
elements.getFragments(Tag.StudyDate) shouldBe empty
}
it should "return elements based on tag condition" in {
val elements2 = elements.set(patientID3)
elements2.filter(_.tag == Tag.PatientID) shouldBe Elements(elements.characterSets, elements.zoneOffset, Vector(patientID3))
}
it should "remove element if present" in {
elements.remove(Tag.DerivationCodeSequence) shouldBe Elements(elements.characterSets, elements.zoneOffset, Vector(studyDate, patientName))
elements.remove(Tag.PatientName) shouldBe elements.copy(data = Vector(studyDate, seq))
elements.remove(Tag.StudyDate) shouldBe elements.copy(data = Vector(seq, patientName))
elements.remove(Tag.Modality) shouldBe elements
}
it should "set elements in the correct position" in {
val characterSets = ValueElement.fromString(Tag.SpecificCharacterSet, "CS1 ")
val modality = ValueElement.fromString(Tag.Modality, "NM")
elements.set(patientID3).data shouldBe Vector(studyDate, seq, patientName, patientID3)
elements.set(characterSets).data shouldBe Vector(characterSets, studyDate, seq, patientName)
elements.set(modality).data shouldBe Vector(studyDate, modality, seq, patientName)
}
it should "set elements in sequences" in {
val updated = elements.setNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 2), studyDate)
updated(TagPath.fromSequence(Tag.DerivationCodeSequence, 2).thenTag(Tag.StudyDate)).get shouldBe studyDate
}
it should "not support using wildcards when setting sequences" in {
val elements = create(seq + Item(indeterminateLength, create(seq)))
intercept[IllegalArgumentException] {
elements.setNested(TagPath.fromSequence(Tag.DerivationCodeSequence).thenSequence(Tag.DerivationCodeSequence, 1), studyDate)
}
}
it should "overwrite element if already present" in {
val newPatientName = patientName.setValue(Value(ByteString("Jane^Doe")))
val updated = elements.set(newPatientName)
updated.size shouldBe elements.size
updated.getValueElement(Tag.PatientName).get.value.bytes.utf8String shouldBe "Jane^Doe"
}
it should "set value" in {
val updated = elements.setValue(Tag.SeriesDate, VR.DA, Value.fromString(VR.DA, "20100101"))
updated.getDate(Tag.SeriesDate).get shouldBe LocalDate.parse("2010-01-01")
}
it should "set bytes" in {
val updated = elements.setBytes(Tag.SeriesDate, VR.DA, ByteString("20100101"))
updated.getDate(Tag.SeriesDate).get shouldBe LocalDate.parse("2010-01-01")
}
it should "set strings" in {
val names = Seq("Smith^Dr", "Jones^Dr")
elements.setStrings(Tag.ReferringPhysicianName, names)
.getStrings(Tag.ReferringPhysicianName) shouldBe names
elements.setString(Tag.ReferringPhysicianName, names.head)
.getStrings(Tag.ReferringPhysicianName) shouldBe Seq(names.head)
}
it should "set shorts" in {
elements.setShorts(Tag.ReferencedFrameNumber, Seq(1, 2, 3))
.getShorts(Tag.ReferencedFrameNumber) shouldBe Seq(1, 2, 3)
elements.setShort(Tag.ReferencedFrameNumber, 42)
.getShorts(Tag.ReferencedFrameNumber) shouldBe Seq(42)
}
it should "set ints" in {
elements.setInts(Tag.ReferencePixelX0, Seq(1, 2, 3))
.getInts(Tag.ReferencePixelX0) shouldBe Seq(1, 2, 3)
elements.setInt(Tag.ReferencePixelX0, 42)
.getInts(Tag.ReferencePixelX0) shouldBe Seq(42)
}
it should "set longs" in {
elements.setLongs(Tag.SimpleFrameList, Seq(1, 2, 3))
.getLongs(Tag.SimpleFrameList) shouldBe Seq(1, 2, 3)
elements.setLong(Tag.SimpleFrameList, 42)
.getLongs(Tag.SimpleFrameList) shouldBe Seq(42)
}
it should "set floats" in {
elements.setFloats(Tag.RecommendedDisplayFrameRateInFloat, Seq(1f, 2f, 3f))
.getFloats(Tag.RecommendedDisplayFrameRateInFloat) shouldBe Seq(1f, 2f, 3f)
elements.setFloat(Tag.RecommendedDisplayFrameRateInFloat, 42f)
.getFloats(Tag.RecommendedDisplayFrameRateInFloat) shouldBe Seq(42f)
}
it should "set doubles" in {
elements.setDoubles(Tag.TimeRange, Seq(1.0, 2.0, 3.0))
.getDoubles(Tag.TimeRange) shouldBe Seq(1.0, 2.0, 3.0)
elements.setDouble(Tag.TimeRange, 42.0)
.getDoubles(Tag.TimeRange) shouldBe Seq(42.0)
}
it should "set dates" in {
val dates = Seq(LocalDate.parse("2005-01-01"), LocalDate.parse("2010-01-01"))
elements.setDates(Tag.StudyDate, dates)
.getDates(Tag.StudyDate) shouldBe dates
elements.setDate(Tag.StudyDate, dates.head)
.getDates(Tag.StudyDate) shouldBe Seq(dates.head)
}
it should "set date times" in {
val dateTimes = Seq(LocalDate.parse("2005-01-01"), LocalDate.parse("2010-01-01"))
.map(_.atStartOfDay(ZoneOffset.of("+04:00")))
elements.setDateTimes(Tag.InstanceCoercionDateTime, dateTimes)
.getDateTimes(Tag.InstanceCoercionDateTime) shouldBe dateTimes
elements.setDateTime(Tag.InstanceCoercionDateTime, dateTimes.head)
.getDateTimes(Tag.InstanceCoercionDateTime) shouldBe Seq(dateTimes.head)
}
it should "set patient names" in {
val names = Seq("Doe^John", "Doe^Jane")
val patientNames = names.flatMap(PatientName.parse)
elements.setPatientNames(Tag.PatientName, patientNames)
.getPatientNames(Tag.PatientName) shouldBe patientNames
elements.setPatientName(Tag.PatientName, patientNames.head)
.getPatientNames(Tag.PatientName) shouldBe Seq(patientNames.head)
}
it should "set URI" in {
val uri = new URI("https://example.com:8080/path?q1=45")
elements.setURI(Tag.StorageURL, uri).getURI(Tag.StorageURL) shouldBe Some(uri)
}
it should "update character sets" in {
val updatedCs1 = elements.setCharacterSets(CharacterSets(ByteString("\\ISO 2022 IR 127"))).characterSets
updatedCs1.charsetNames shouldBe Seq("", "ISO 2022 IR 127")
val updatedCs2 = elements.set(ValueElement.fromString(Tag.SpecificCharacterSet, "\\ISO 2022 IR 13")).characterSets
updatedCs2.charsetNames shouldBe Seq("", "ISO 2022 IR 13")
}
it should "update zone offset" in {
val updatedZo1 = elements.setZoneOffset(ZoneOffset.of("-06:00")).zoneOffset
updatedZo1.toString shouldBe "-06:00"
val updatedZo2 = elements.set(ValueElement.fromString(Tag.TimezoneOffsetFromUTC, "+04:00")).zoneOffset
updatedZo2.toString shouldBe "+04:00"
val updatedZo3 = elements.set(ValueElement.fromString(Tag.TimezoneOffsetFromUTC, "bad zone offset string")).zoneOffset
updatedZo3 shouldBe elements.zoneOffset
}
it should "aggregate the bytes of all its elements" in {
val bytes = preamble ++ fmiGroupLength(transferSyntaxUID()) ++ transferSyntaxUID() ++ // FMI
testStudyDate() ++
sequence(Tag.DerivationCodeSequence) ++ item() ++ testStudyDate() ++ itemDelimitation() ++ item() ++ // sequence
sequence(Tag.DerivationCodeSequence) ++ item() ++ testStudyDate() ++ itemDelimitation() ++ sequenceDelimitation() ++ // nested sequence (determinate length)
itemDelimitation() ++ sequenceDelimitation() ++
patientNameJohnDoe() ++ // attribute
pixeDataFragments() ++ item(0) ++ item(4) ++ ByteString(1, 2, 3, 4) ++ sequenceDelimitation()
val elements = toElements(bytes)
elements.toBytes() shouldBe bytes
}
it should "return an empty byte string when aggregating bytes with no data" in {
Elements.empty().toBytes(withPreamble = false) shouldBe ByteString.empty
}
it should "render an informative string representation" in {
val s = elements.toString
s.count(_.toString == System.lineSeparator) shouldBe 9
}
it should "return the specified element based on tag path" in {
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 2).thenTag(Tag.PatientID)) shouldBe Some(patientID2)
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 3).thenTag(Tag.PatientID)) shouldBe None
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 3).thenTag(Tag.PatientID)) shouldBe None
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 2).thenTag(Tag.PatientName)) shouldBe None
elements(TagPath.fromSequence(Tag.AbstractPriorCodeSequence, 1).thenTag(Tag.PatientID)) shouldBe None
}
it should "not accept wildcards in tag paths" in {
intercept[IllegalArgumentException] {
elements(TagPath.fromSequence(Tag.DerivationCodeSequence).thenTag(Tag.PatientID))
}
}
it should "return the specified seqeunce based on tag path" in {
elements.getNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 1)) shouldBe seq.item(1).map(_.elements)
}
it should "update element specified by tag path" in {
elements(TagPath.fromSequence(Tag.DerivationCodeSequence, 1).thenTag(Tag.PatientID)) shouldBe Some(patientID1)
val e2 = elements.setNested(TagPath.fromSequence(Tag.DerivationCodeSequence, 1), patientID2)
e2(TagPath.fromSequence(Tag.DerivationCodeSequence, 1).thenTag(Tag.PatientID)) shouldBe Some(patientID2)
}
it should "provide a legible toString" in {
val updated = elements.set(Fragments(Tag.PixelData, VR.OB, None, List(Fragment(4, Value(ByteString(1, 2, 3, 4))))))
updated.toString.count(_ == System.lineSeparator.charAt(0)) shouldBe updated.toElements.length - 1
}
it should "create file meta information" in {
val fmiList = Elements.fileMetaInformationElements("iuid", "cuid", "ts")
val fmi = Elements.empty().set(fmiList)
fmi.getInt(Tag.FileMetaInformationGroupLength).get shouldBe
(12 + 5 * 8 + 2 + 4 + 4 + 2 +
padToEvenLength(ByteString(Implementation.classUid), Tag.ImplementationClassUID).length +
padToEvenLength(ByteString(Implementation.versionName), Tag.ImplementationVersionName).length)
fmi.getBytes(Tag.FileMetaInformationVersion).get shouldBe ByteString(0, 1)
fmi.getString(Tag.MediaStorageSOPClassUID).get shouldBe "cuid"
fmi.getString(Tag.MediaStorageSOPInstanceUID).get shouldBe "iuid"
fmi.getString(Tag.TransferSyntaxUID).get shouldBe "ts"
fmi.getString(Tag.ImplementationClassUID).get shouldBe Implementation.classUid
fmi.getString(Tag.ImplementationVersionName).get shouldBe Implementation.versionName
}
"Elements data classes" should "return the correct byte representation" in {
PreambleElement.toBytes should have length (128 + 4)
PreambleElement.toBytes.takeRight(4).utf8String shouldBe "DICM"
ValueElement(Tag.StudyDate, Value.fromString(VR.DA, "20010101")).toBytes shouldBe HeaderPart(Tag.StudyDate, VR.DA, 8).bytes ++ ByteString("20010101")
SequenceElement(Tag.DerivationCodeSequence, 10).toBytes shouldBe sequence(Tag.DerivationCodeSequence, 10)
FragmentsElement(Tag.PixelData, VR.OW).toBytes shouldBe pixeDataFragments()
FragmentElement(1, 4, Value(ByteString(1, 2, 3, 4))).toBytes shouldBe item(4) ++ ByteString(1, 2, 3, 4)
ItemElement(1, 10).toBytes shouldBe item(10)
ItemDelimitationElement(1).toBytes shouldBe itemDelimitation()
SequenceDelimitationElement().toBytes shouldBe sequenceDelimitation()
Sequence(Tag.DerivationCodeSequence, indeterminateLength, List(Item(indeterminateLength, Elements.empty()))).toBytes shouldBe sequence(Tag.DerivationCodeSequence) ++ item(indeterminateLength) ++ itemDelimitation() ++ sequenceDelimitation()
Fragments(Tag.PixelData, VR.OW, Some(Nil), List(Fragment(4, Value(ByteString(1, 2, 3, 4))))).toBytes shouldBe pixeDataFragments() ++ item(0) ++ item(4) ++ ByteString(1, 2, 3, 4) ++ sequenceDelimitation()
}
it should "have expected string representations in terms of number of lines" in {
def checkString(string: String, nLines: Int): Assertion = string.count(_ == '\n') shouldBe nLines - 1
checkString(PreambleElement.toString, 1)
checkString(ValueElement(Tag.StudyDate, Value.fromString(VR.DA, "20010101")).toString, 1)
checkString(SequenceElement(Tag.DerivationCodeSequence, 10).toString, 1)
checkString(FragmentsElement(Tag.PixelData, VR.OW).toString, 1)
checkString(FragmentElement(1, 4, Value(ByteString(1, 2, 3, 4))).toString, 1)
checkString(ItemElement(1, 10).toString, 1)
checkString(ItemDelimitationElement(1).toString, 1)
checkString(SequenceDelimitationElement().toString, 1)
checkString(Sequence(Tag.DerivationCodeSequence, indeterminateLength, List(Item(indeterminateLength, Elements.empty()))).toString, 1)
checkString(Fragments(Tag.PixelData, VR.OW, Some(Nil), List(Fragment(4, Value(ByteString(1, 2, 3, 4))))).toString, 1)
}
"Fragments" should "be empty" in {
val bytes = pixeDataFragments() ++ sequenceDelimitation()
val elements = Await.result(
Source.single(bytes)
.via(parseFlow)
.via(elementFlow)
.runWith(elementSink),
5.seconds)
val fragments = elements.getFragments(Tag.PixelData).get
fragments.size shouldBe 0
fragments.offsets shouldBe empty
}
it should "convert an empty first item to an empty offsets list" in {
val bytes = pixeDataFragments() ++ item(0) ++ item(4) ++ ByteString(1, 2, 3, 4) ++ sequenceDelimitation()
val fragments = toElements(bytes).getFragments(Tag.PixelData).get
fragments.offsets shouldBe defined
fragments.offsets.get shouldBe empty
fragments.size shouldBe 1
}
it should "convert first item to offsets" in {
val bytes = pixeDataFragments() ++ item(8) ++ intToBytesLE(0) ++ intToBytesLE(456) ++ item(4) ++
ByteString(1, 2, 3, 4) ++ sequenceDelimitation()
val fragments = toElements(bytes).getFragments(Tag.PixelData).get
fragments.offsets shouldBe defined
fragments.offsets.get shouldBe List(0, 456)
}
it should "support access to frames based on fragments and offsets" in {
val bytes = pixeDataFragments() ++ item(8) ++ intToBytesLE(0) ++ intToBytesLE(6) ++ item(4) ++
ByteString(1, 2, 3, 4) ++ item(4) ++ ByteString(5, 6, 7, 8) ++ sequenceDelimitation()
val iter = toElements(bytes).getFragments(Tag.PixelData).get.frameIterator
iter.hasNext shouldBe true
iter.next shouldBe ByteString(1, 2, 3, 4, 5, 6)
iter.hasNext shouldBe true
iter.next shouldBe ByteString(7, 8)
iter.hasNext shouldBe false
}
it should "return an empty iterator when offsets list and/or fragments are empty" in {
val bytes1 = pixeDataFragments() ++ sequenceDelimitation()
val bytes2 = pixeDataFragments() ++ item(0) ++ sequenceDelimitation()
val bytes3 = pixeDataFragments() ++ item(0) ++ item(0) ++ sequenceDelimitation()
val bytes4 = pixeDataFragments() ++ item(4) ++ intToBytesLE(0) ++ sequenceDelimitation()
val bytes5 = pixeDataFragments() ++ item(4) ++ intToBytesLE(0) ++ item(0) ++ sequenceDelimitation()
val iter1 = toElements(bytes1).getFragments(Tag.PixelData).get.frameIterator
val iter2 = toElements(bytes2).getFragments(Tag.PixelData).get.frameIterator
val iter3 = toElements(bytes3).getFragments(Tag.PixelData).get.frameIterator
val iter4 = toElements(bytes4).getFragments(Tag.PixelData).get.frameIterator
val iter5 = toElements(bytes5).getFragments(Tag.PixelData).get.frameIterator
iter1.hasNext shouldBe false
iter2.hasNext shouldBe false
iter3.hasNext shouldBe false
iter4.hasNext shouldBe false
iter5.hasNext shouldBe false
}
it should "support many frames per fragment and many fragments per frame" in {
val bytes1 = pixeDataFragments() ++ item(12) ++ List(0, 2, 3).map(intToBytesLE).reduce(_ ++ _) ++ item(4) ++
ByteString(1, 2, 3, 4) ++ sequenceDelimitation()
val bytes2 = pixeDataFragments() ++ item(0) ++ item(2) ++ ByteString(1, 2) ++
item(2) ++ ByteString(1, 2) ++ item(2) ++ ByteString(1, 2) ++ item(2) ++ ByteString(1, 2) ++ sequenceDelimitation()
val iter1 = toElements(bytes1).getFragments(Tag.PixelData).get.frameIterator
val iter2 = toElements(bytes2).getFragments(Tag.PixelData).get.frameIterator
iter1.next shouldBe ByteString(1, 2)
iter1.next shouldBe ByteString(3)
iter1.next shouldBe ByteString(4)
iter1.hasNext shouldBe false
iter2.next shouldBe ByteString(1, 2, 1, 2, 1, 2, 1, 2)
iter2.hasNext shouldBe false
}
}