コード例 #1
0
        public void GivenBodyPartWithInvalidContentType_WhenReading_ThenUnsupportedMediaTypeExceptionShouldBeThrown()
        {
            IMultipartReader multipartReader = SetupMultipartReader(
                _ => new MultipartBodyPart("application/dicom+json", _stream),
                _ => null);

            Assert.ThrowsAsync <UnsupportedMediaTypeException>(() => _dicomInstanceEntryReader.ReadAsync(DefaultContentType, _stream, DefaultCancellationToken));
        }
コード例 #2
0
        private IMultipartReader SetupMultipartReader(Func <CallInfo, MultipartBodyPart> returnThis, params Func <CallInfo, MultipartBodyPart>[] returnThese)
        {
            IMultipartReader multipartReader = Substitute.For <IMultipartReader>();

            multipartReader.ReadNextBodyPartAsync(DefaultCancellationToken).Returns(returnThis, returnThese);

            _multipartReaderFactory.Create(DefaultContentType, _stream)
            .Returns(multipartReader);

            return(multipartReader);
        }
コード例 #3
0
        public async Task GivenAnException_WhenReading_ThenAlreadyProcessedStreamsShouldBeDisposed()
        {
            IMultipartReader multipartReader = SetupMultipartReader(
                _ => new MultipartBodyPart(DefaultBodyPartContentType, _stream),
                _ => throw new Exception());

            await Assert.ThrowsAsync <Exception>(() => _dicomInstanceEntryReader.ReadAsync(
                                                     DefaultContentType,
                                                     _stream,
                                                     DefaultCancellationToken));

            Assert.Throws <ObjectDisposedException>(() => _stream.ReadByte());
        }
コード例 #4
0
        public async Task GivenBodyPartWithValidContentType_WhenReading_ThenCorrectResultsShouldBeReturned()
        {
            IMultipartReader multipartReader = SetupMultipartReader(
                _ => new MultipartBodyPart(DefaultBodyPartContentType, _stream),
                _ => null);

            IReadOnlyCollection <IDicomInstanceEntry> results = await _dicomInstanceEntryReader.ReadAsync(
                DefaultContentType,
                _stream,
                DefaultCancellationToken);

            Assert.NotNull(results);
            Assert.Collection(
                results,
                async item =>
            {
                Assert.IsType <StreamOriginatedDicomInstanceEntry>(item);
                Assert.Same(_stream, await item.GetStreamAsync(DefaultCancellationToken));
            });
        }
コード例 #5
0
        public async Task GivenAnExceptionWhileDisposing_WhenReading_ThenItShouldNotInterfereWithDisposingOtherInstances()
        {
            var streamToBeDisposed = new MemoryStream();

            IMultipartReader multipartReader = SetupMultipartReader(
                _ => new MultipartBodyPart(DefaultBodyPartContentType, streamToBeDisposed),
                _ =>
            {
                // Dispose the previous stream so that when the code cleans up the resource, it throws exception.
                streamToBeDisposed.Dispose();

                return(new MultipartBodyPart(DefaultBodyPartContentType, _stream));
            },
                _ => throw new Exception());

            await Assert.ThrowsAsync <Exception>(() => _dicomInstanceEntryReader.ReadAsync(
                                                     DefaultContentType,
                                                     _stream,
                                                     DefaultCancellationToken));

            Assert.Throws <ObjectDisposedException>(() => _stream.ReadByte());
        }
コード例 #6
0
        /// <inheritdoc />
        public async Task <IReadOnlyList <IDicomInstanceEntry> > ReadAsync(string contentType, Stream stream, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNullOrWhiteSpace(contentType, nameof(contentType));
            EnsureArg.IsNotNull(stream, nameof(stream));

            IMultipartReader multipartReader = _multipartReaderFactory.Create(contentType, stream);

            var dicomInstanceEntries = new List <StreamOriginatedDicomInstanceEntry>();

            MultipartBodyPart bodyPart;

            try
            {
                while ((bodyPart = await multipartReader.ReadNextBodyPartAsync(cancellationToken)) != null)
                {
                    // Check the content type to make sure we can process.
                    if (!KnownContentTypes.ApplicationDicom.Equals(bodyPart.ContentType, StringComparison.OrdinalIgnoreCase))
                    {
                        // TODO: Currently, we only support application/dicom. Support for metadata + bulkdata is coming.
                        throw new UnsupportedMediaTypeException(
                                  string.Format(CultureInfo.InvariantCulture, DicomCoreResource.UnsupportedContentType, bodyPart.ContentType));
                    }

                    dicomInstanceEntries.Add(new StreamOriginatedDicomInstanceEntry(bodyPart.SeekableStream));
                }
            }
            catch (Exception)
            {
                // Encountered an error while processing, release all resources.
                IEnumerable <Task> disposeTasks = dicomInstanceEntries.Select(DisposeResourceAsync);

                await Task.WhenAll(disposeTasks);

                throw;
            }

            return(dicomInstanceEntries);
        }