changeset 33:eb6d219af210

test stow
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 02 Aug 2015 14:01:32 +0200
parents 682c4e2a1162
children 1047d7bc5210
files Plugins/DicomWeb/DicomWeb.py Plugins/DicomWeb/Run.py
diffstat 2 files changed, 98 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/DicomWeb/DicomWeb.py	Sun Aug 02 14:01:32 2015 +0200
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+
+# Orthanc - A Lightweight, RESTful DICOM Store
+# Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
+# Department, University Hospital of Liege, Belgium
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import os
+import sys
+import email
+import urllib2
+
+from email.mime.multipart import MIMEMultipart
+from email.mime.application import MIMEApplication
+
+sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
+from Toolbox import *
+
+
+def _AttachPart(related, path, contentType):
+    with open(path, 'rb') as f:
+        part = MIMEApplication(f.read(), contentType, email.encoders.encode_noop)
+        related.attach(part)
+
+
+def SendStow(orthanc, uri, dicom):
+    related = MIMEMultipart('related')
+    related.set_boundary('boundary_0123456789_boundary')
+
+    if isinstance(dicom, list):
+        for i in range(dicom):
+            _AttachPart(related, dicom[i], 'dicom')
+    else:
+        _AttachPart(related, dicom, 'dicom')
+
+    headers = dict(related.items())
+    body = related.as_string()
+
+    # Discard the header
+    body = body.split('\n\n', 1)[1]
+
+    headers['Content-Type'] = 'multipart/related; type=application/dicom; boundary=%s' % related.get_boundary()
+    headers['Accept'] = 'application/json'
+
+    return DoPost(orthanc, uri, body, headers = headers)
+
+
+def GetMultipart(uri, headers = {}):
+    tmp = urllib2.urlopen(uri)
+    info = str(tmp.info())
+    answer = tmp.read()
+
+    s = info + "\n" + answer
+
+    msg = email.message_from_string(s)
+
+    result = []
+
+    for i, part in enumerate(msg.walk(), 1):
+        payload = part.get_payload(decode = True)
+        if payload != None:
+            result.append(payload)
+
+    return result
--- a/Plugins/DicomWeb/Run.py	Sun Aug 02 13:14:58 2015 +0200
+++ b/Plugins/DicomWeb/Run.py	Sun Aug 02 14:01:32 2015 +0200
@@ -19,9 +19,11 @@
 
 
 import os
+import pprint
 import sys
 import argparse
 import unittest
+from DicomWeb import *
 
 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'Tests'))
 from Toolbox import *
@@ -49,7 +51,7 @@
 parser.add_argument('--wado',
                     default = '/wado',
                     help = 'Path to the WADO API')
-parser.add_argument('--dicom-web',
+parser.add_argument('--dicomweb',
                     default = '/dicom-web/',
                     help = 'Path to the DICOMweb API')
 parser.add_argument('--force', help = 'Do not warn the user',
@@ -140,6 +142,24 @@
         self.assertEqual(512, im.size[0])
         self.assertEqual(358, im.size[1])
 
+    def test_stow(self):
+        self.assertEqual(0, len(DoGet(ORTHANC, '/instances')))
+        SendStow(ORTHANC, args.dicomweb + '/studies', GetDatabasePath('Phenix/IM-0001-0001.dcm'))
+        self.assertEqual(1, len(DoGet(ORTHANC, '/instances')))
+        a = SendStow(ORTHANC, args.dicomweb + '/studies', GetDatabasePath('Phenix/IM-0001-0001.dcm'))
+        self.assertEqual(1, len(DoGet(ORTHANC, '/instances')))
+
+        self.assertEqual(0, len(a['00081198']['Value']))  # No error
+        self.assertEqual(1, len(a['00081199']['Value']))  # 1 success
+
+        self.assertTrue(a['00081190']['Value'][0].endswith('studies/2.16.840.1.113669.632.20.1211.10000098591'))
+        self.assertTrue(a['00081199']['Value'][0]['00081190']['Value'][0].
+                        endswith('series/1.2.840.113704.1.111.5692.1127828999.2/instances/1.2.840.113704.7.1.1.6632.1127829031.2'))
+
+        b = GetMultipart(a['00081190']['Value'][0])
+        self.assertEqual(1, len(b))
+        self.assertEqual(368852, len(b[0]))
+
 
 
 try: