File: QIDORSResponse.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (166 lines) | stat: -rw-r--r-- 5,947 bytes parent folder | download | duplicates (5)
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
#define BOOST_TEST_MODULE QIDORSResponse
#include <boost/test/unit_test.hpp>

#include <sstream>
#include <vector>

#include <boost/property_tree/xml_parser.hpp>

#include <json/json.h>

#include "odil/DataSet.h"
#include "odil/json_converter.h"
#include "odil/registry.h"
#include "odil/StringStream.h"
#include "odil/webservices/HTTPResponse.h"
#include "odil/webservices/ItemWithParameters.h"
#include "odil/webservices/multipart_related.h"
#include "odil/webservices/QIDORSResponse.h"
#include "odil/webservices/Utils.h"
#include "odil/xml_converter.h"

#include <iostream>

struct Fixture
{
    odil::Value::DataSets data_sets;

    Fixture()
    {
        auto data_set_1 = std::make_shared<odil::DataSet>();
        data_set_1->add("SOPClassUID", {odil::registry::RawDataStorage});
        data_set_1->add("SOPInstanceUID", {"1.2.3.4"});
        data_set_1->add("PatientID", {"DJ1234"});
        data_set_1->add("PixelSpacing", {1.5, 2.5});

        auto data_set_2 = std::make_shared<odil::DataSet>();
        data_set_2->add("SOPClassUID", {odil::registry::MRImageStorage});
        data_set_2->add("SOPInstanceUID", {"1.2.3.5"});
        data_set_2->add("PatientName", {"Doe^John"});
        data_set_2->add("PatientAge", {"042Y"});

        this->data_sets = { data_set_1, data_set_2 };
    }
};

BOOST_AUTO_TEST_CASE(Constructor)
{
    odil::webservices::QIDORSResponse const response;
    BOOST_REQUIRE(response.get_data_sets().empty());
}

BOOST_AUTO_TEST_CASE(Constructor_HttpReq)
{
    odil::webservices::HTTPResponse http_response;
    http_response.set_status(200);
    http_response.set_header("Content-Type", "application/dicom+json");
    http_response.set_body("[]");
    odil::webservices::QIDORSResponse const response(http_response);

    BOOST_REQUIRE(response.get_representation() == odil::webservices::Representation::DICOM_JSON);
    BOOST_REQUIRE(response.get_media_type() == "application/dicom+json");
}

BOOST_FIXTURE_TEST_CASE(DataSets, Fixture)
{
    odil::webservices::QIDORSResponse response;
    response.set_data_sets(data_sets);
    BOOST_REQUIRE(response.get_data_sets() == data_sets);
}

BOOST_FIXTURE_TEST_CASE(RespondDICOMJSON, Fixture)
{
    odil::webservices::QIDORSResponse response;
    response.set_representation(odil::webservices::Representation::DICOM_JSON);
    response.set_data_sets(data_sets);
    BOOST_REQUIRE_EQUAL(response.get_media_type(), "application/dicom+json");
    BOOST_REQUIRE(response.get_representation() == odil::webservices::Representation::DICOM_JSON);

    auto const http = response.get_http_response();
    BOOST_REQUIRE(!odil::webservices::is_multipart_related(http));
    auto const content_type = odil::as<odil::webservices::ItemWithParameters>(
        http.get_header("Content-Type"));
    BOOST_REQUIRE_EQUAL(content_type.name, "application/dicom+json");


    std::istringstream stream(http.get_body());
    Json::Value array;
    stream >> array;
    BOOST_REQUIRE(array.isArray());

    odil::Value::DataSets response_data_sets;
    std::transform(
        array.begin(), array.end(), std::back_inserter(response_data_sets),
        static_cast<std::shared_ptr<odil::DataSet> (*)(Json::Value const &)>(
            odil::as_dataset));

    BOOST_REQUIRE(response_data_sets == data_sets);
}

BOOST_FIXTURE_TEST_CASE(RespondDICOMXML, Fixture)
{
    odil::webservices::QIDORSResponse response;
    response.set_representation(odil::webservices::Representation::DICOM_XML);
    response.set_data_sets(data_sets);
    BOOST_REQUIRE_EQUAL(response.get_media_type(), "application/dicom+xml");
    BOOST_REQUIRE(response.get_representation() == odil::webservices::Representation::DICOM_XML);

    auto const http = response.get_http_response();
    BOOST_REQUIRE(odil::webservices::is_multipart_related(http));
    BOOST_REQUIRE_EQUAL(odil::webservices::count_parts(http), data_sets.size());
    auto const content_type = odil::as<odil::webservices::ItemWithParameters>(
        http.get_header("Content-Type"));
    BOOST_REQUIRE_EQUAL(
        content_type.name_parameters.at("type"), "application/dicom+xml");

    std::vector<odil::webservices::Message> parts;
    odil::webservices::transform_parts(
        http, std::back_inserter(parts),
        [](odil::webservices::Message const & m) { return m; });

    for(unsigned int i=0; i<data_sets.size(); ++i)
    {
        auto const & data_set = data_sets[i];
        auto const transfer_syntax =
            data_set->get_transfer_syntax().empty()
                ?odil::registry::ExplicitVRLittleEndian
                :data_set->get_transfer_syntax();

        auto const & part = parts[i];
        auto const content_type = odil::as<odil::webservices::ItemWithParameters>(
                part.get_header("Content-Type"));

        BOOST_REQUIRE_EQUAL(content_type.name, "application/dicom+xml");

        std::stringstream stream(part.get_body());
        boost::property_tree::ptree xml;
        boost::property_tree::read_xml(stream, xml);
        BOOST_REQUIRE(*data_set == *odil::as_dataset(xml));
    }
}

BOOST_AUTO_TEST_CASE(EmptyResponse)
{
    odil::webservices::QIDORSResponse response;
    auto const http = response.get_http_response();
    BOOST_REQUIRE(http.get_status() == 204);
    BOOST_REQUIRE_EQUAL(http.get_reason(), "No Content");
}

BOOST_FIXTURE_TEST_CASE(Equality, Fixture)
{
    odil::webservices::QIDORSResponse response, response2;
    response.set_representation(odil::webservices::Representation::DICOM_XML);
    response2.set_representation(odil::webservices::Representation::DICOM_XML);
    response.set_data_sets(data_sets);
    response2.set_data_sets(data_sets);
    BOOST_REQUIRE(response == response2);
}

BOOST_FIXTURE_TEST_CASE(Difference, Fixture)
{
    odil::webservices::QIDORSResponse response, response2;
    response.set_representation(odil::webservices::Representation::DICOM_XML);
    response.set_data_sets(data_sets);
    BOOST_REQUIRE(response != response2);
}