File: STOWRSResponse.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 (194 lines) | stat: -rw-r--r-- 7,001 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
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
#define BOOST_TEST_MODULE STOWRSResponse
#include <boost/test/unit_test.hpp>

#include <sstream>

#include <boost/lexical_cast.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/version.hpp>

#include <json/json.h>

#include "odil/DataSet.h"
#include "odil/json_converter.h"
#include "odil/registry.h"
#include "odil/webservices/ItemWithParameters.h"
#include "odil/webservices/STOWRSResponse.h"
#include "odil/webservices/multipart_related.h"
#include "odil/webservices/Utils.h"
#include "odil/xml_converter.h"

struct Fixture
{
    std::shared_ptr<odil::DataSet> data_set;

    Fixture()
    {
        this->data_set = std::make_shared<odil::DataSet>();
        this->data_set->add(
            "RetrieveURL", {"http://example.com/dicom/studies/7.8.9"});
        this->data_set->add(
            "FailedSOPSequence", {std::make_shared<odil::DataSet>()});

        auto referenceSOPSequence_item1 = std::make_shared<odil::DataSet>();
        referenceSOPSequence_item1->add(
            "ReferencedSOPClassUID", {odil::registry::MRImageStorage});
        referenceSOPSequence_item1->add("ReferencedSOPInstanceUID", {"1.2.3.6.1"});
        referenceSOPSequence_item1->add("RetrieveURL",
            {"http://example.com/dicom/studies/7.8.9/series/4.5/instances/1.2.3.6.1"});

        auto referenceSOPSequence_item2 = std::make_shared<odil::DataSet>();
        referenceSOPSequence_item2->add(
            "ReferencedSOPClassUID", {odil::registry::RawDataStorage});
        referenceSOPSequence_item2->add(
            "ReferencedSOPInstanceUID", {"1.2.3.6.2"});
        referenceSOPSequence_item2->add("RetrieveURL",
            {"http://example.com/dicom/studies/7.8.9/series/4.5/instances/1.2.3.6.2"});

        this->data_set->add(
            "ReferencedSOPSequence",
            {referenceSOPSequence_item1, referenceSOPSequence_item2});
    }
};


BOOST_AUTO_TEST_CASE(Constructor)
{
    odil::webservices::STOWRSResponse const response;
    BOOST_REQUIRE(response.get_store_instance_responses()->empty());
}

BOOST_FIXTURE_TEST_CASE(RespondDICOM_XML, Fixture)
{
    odil::webservices::STOWRSResponse response;
    response.set_representation(odil::webservices::Representation::DICOM_XML);
    response.set_store_instance_responses(data_set);
    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();
    auto const content_type = boost::lexical_cast<
        odil::webservices::ItemWithParameters>(http.get_header("Content-Type"));
    BOOST_REQUIRE_EQUAL(content_type.name, "application/dicom+xml");

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

BOOST_FIXTURE_TEST_CASE(RespondDICOM_JSON, Fixture)
{
    odil::webservices::STOWRSResponse response;
    response.set_representation(odil::webservices::Representation::DICOM_JSON);
    response.set_store_instance_responses(data_set);
    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();
    auto const content_type = boost::lexical_cast<
        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());

    BOOST_REQUIRE(*data_set == *odil::as_dataset(array[0]));
}

BOOST_FIXTURE_TEST_CASE(Equality, Fixture)
{
    odil::webservices::HTTPResponse http_response;
    http_response.set_header("Content-Type", "application/dicom+xml");
    http_response.set_status(200);
    http_response.set_reason("OK");
    auto const xml = as_xml(data_set);
    std::ostringstream body;
#if BOOST_VERSION >= 105600
    typedef boost::property_tree::xml_writer_settings<std::string> SettingsType;
#else
    typedef boost::property_tree::xml_writer_settings<char> SettingsType;
#endif
    boost::property_tree::write_xml(body, xml, SettingsType());
    http_response.set_body(body.str());

    odil::webservices::STOWRSResponse response(http_response);

    odil::webservices::STOWRSResponse response_2;
    response_2.set_representation(odil::webservices::Representation::DICOM_XML);
    response_2.set_store_instance_responses(data_set);
    response_2.set_warning(false);
    response_2.set_reason("OK");

    BOOST_REQUIRE(response == response_2);
}

BOOST_FIXTURE_TEST_CASE(Difference, Fixture)
{
    // first http_response
    odil::webservices::HTTPResponse http_response;
    http_response.set_header("Content-Type", "application/dicom+xml");
    http_response.set_status(200);
    http_response.set_reason("OK");
    auto const xml = as_xml(data_set);
    std::ostringstream body;
#if BOOST_VERSION >= 105600
    typedef boost::property_tree::xml_writer_settings<std::string> SettingsType;
#else
    typedef boost::property_tree::xml_writer_settings<char> SettingsType;
#endif
    boost::property_tree::write_xml(body, xml, SettingsType());
    http_response.set_body(body.str());

    odil::webservices::STOWRSResponse response(http_response);

    // second http_response
    odil::webservices::HTTPResponse http_response_2;
    http_response_2.set_header("Content-Type", "application/dicom+xml");
    http_response_2.set_status(400);
    http_response_2.set_reason("Bad Request");

    auto ds = std::make_shared<odil::DataSet>();
    auto const xml_2 = as_xml(ds);
    std::ostringstream body_2;
#if BOOST_VERSION >= 105600
    typedef boost::property_tree::xml_writer_settings<std::string> SettingsType;
#else
    typedef boost::property_tree::xml_writer_settings<char> SettingsType;
#endif
    boost::property_tree::write_xml(body_2, xml_2, SettingsType());
    http_response_2.set_body(body_2.str());

    odil::webservices::STOWRSResponse response_2(http_response_2);

    BOOST_REQUIRE(response != response_2);
}

BOOST_AUTO_TEST_CASE(Reason)
{
    odil::webservices::STOWRSResponse response;
    response.set_reason("Unauthorized");
    BOOST_REQUIRE_EQUAL(response.get_reason(),"Unauthorized");
}

BOOST_AUTO_TEST_CASE(InvalidReason)
{
    odil::webservices::STOWRSResponse response;
    BOOST_REQUIRE_THROW(response.set_reason("Unrecognize"), odil::Exception);
}

BOOST_AUTO_TEST_CASE(FailureCode)
{
    odil::webservices::STOWRSResponse response;
    response.set_failure_code(503);
    BOOST_REQUIRE_EQUAL(response.get_failure_code(), 503);
}

BOOST_AUTO_TEST_CASE(Representation)
{
    odil::webservices::STOWRSResponse response;
    response.set_representation(odil::webservices::Representation::DICOM_JSON);
    BOOST_REQUIRE(response.get_representation() == odil::webservices::Representation::DICOM_JSON);
}