Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkRESTServerTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifdef _WIN32
14 #include <Windows.h>
15 #endif
16 
17 #include <mitkTestFixture.h>
18 #include <mitkTestingMacros.h>
19 
20 #include <mitkIRESTManager.h>
21 #include <mitkIRESTObserver.h>
22 #include <mitkRESTServer.h>
23 
24 #include <usGetModuleContext.h>
25 #include <usModuleContext.h>
26 #include <usServiceReference.h>
27 
28 #include <vtkDebugLeaks.h>
29 
30 class mitkRESTServerTestSuite : public mitk::TestFixture, mitk::IRESTObserver
31 {
32  CPPUNIT_TEST_SUITE(mitkRESTServerTestSuite);
33  MITK_TEST(OpenListener_Succeed);
34  MITK_TEST(TwoListenerSameHostSamePort_OnlyOneOpened);
35  MITK_TEST(CloseListener_Succeed);
36  MITK_TEST(OpenMultipleListenerCloseOne_Succeed);
37  MITK_TEST(OpenMultipleListenerCloseAll_Succeed);
38  // MITK_TEST(OpenListenerGetRequestSamePath_ReturnExpectedJSON); GET requests do not support content yet?
39  MITK_TEST(CloseListener_NoRequestPossible);
40  MITK_TEST(OpenListenerGetRequestDifferentPath_ReturnNotFound);
41  MITK_TEST(OpenListenerCloseAndReopen_Succeed);
42  MITK_TEST(HandleHeader_Succeed);
43  CPPUNIT_TEST_SUITE_END();
44 
45 public:
47  web::json::value m_Data;
48 
49  web::http::http_response Notify(const web::uri &,
50  const web::json::value &,
51  const web::http::method &,
52  const mitk::RESTUtil::ParamMap &headers) override
53  {
54  auto response = web::http::http_response();
55  response.set_body(m_Data);
56  mitk::RESTUtil::ParamMap::const_iterator contentTypePos = headers.find(U("Content-Type"));
57  if (contentTypePos != headers.end() && contentTypePos->second == U("awesome/type"))
58  {
59  m_Data[U("result")] = web::json::value(U("awesome/type"));
60  }
61 
62  return response;
63  }
64 
69  void setUp() override
70  {
71  m_Data = web::json::value();
72  m_Data[U("userId")] = web::json::value(1);
73  m_Data[U("id")] = web::json::value(1);
74  m_Data[U("title")] = web::json::value(U("this is a title"));
75  m_Data[U("body")] = web::json::value(U("this is a body"));
76 
78 
79  if (serviceRef)
80  m_Service = us::GetModuleContext()->GetService(serviceRef);
81 
82  if (!m_Service)
83  CPPUNIT_FAIL("Getting Service in setUp() failed");
84  }
85 
86  void tearDown() override { m_Service->HandleDeleteObserver(this); }
87 
88  void OpenListener_Succeed()
89  {
90  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
91 
92  CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size());
93  CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size());
94  }
95 
96  void TwoListenerSameHostSamePort_OnlyOneOpened()
97  {
98  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
99  m_Service->ReceiveRequest(U("http://localhost:8080/serverexample"), this);
100 
101  CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, observer map size is two",
102  2 == m_Service->GetObservers().size());
103  CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, server map size is one",
104  1 == m_Service->GetServerMap().size());
105  }
106 
107  void CloseListener_Succeed()
108  {
109  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
110 
111  CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size());
112  CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size());
113 
114  m_Service->HandleDeleteObserver(this);
115 
116  CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size());
117  CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size());
118  }
119 
120  void OpenMultipleListenerCloseOne_Succeed()
121  {
122  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
123  m_Service->ReceiveRequest(U("http://localhost:8090/serverexample"), this);
124 
125  CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", 2 == m_Service->GetObservers().size());
126  CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", 2 == m_Service->GetServerMap().size());
127 
128  m_Service->HandleDeleteObserver(this, U("http://localhost:8080/servertest"));
129 
130  CPPUNIT_ASSERT_MESSAGE("Closed one of two listeners, observer map is size is one",
131  1 == m_Service->GetObservers().size());
132  CPPUNIT_ASSERT_MESSAGE("Closed one of two listener, server map size is one", 1 == m_Service->GetServerMap().size());
133  }
134 
135  void OpenMultipleListenerCloseAll_Succeed()
136  {
137  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
138  m_Service->ReceiveRequest(U("http://localhost:8090/serverexample"), this);
139 
140  CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", 2 == m_Service->GetObservers().size());
141  CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", 2 == m_Service->GetServerMap().size());
142 
143  m_Service->HandleDeleteObserver(this);
144 
145  CPPUNIT_ASSERT_MESSAGE("Closed all listeners, observer map is empty", 0 == m_Service->GetObservers().size());
146  CPPUNIT_ASSERT_MESSAGE("Closed all listeners, server map is empty", 0 == m_Service->GetServerMap().size());
147  }
148 
149  void OpenListenerGetRequestSamePath_ReturnExpectedJSON()
150  {
151  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
152 
153  web::json::value result;
154  auto body = web::json::value();
155  m_Service->SendRequest(U("http://localhost:8080/servertest"))
156  .then([&](pplx::task<web::json::value> resultTask) {
157  try
158  {
159  result = resultTask.get();
160  }
161  catch (const mitk::Exception &exception)
162  {
163  MITK_ERROR << exception.what();
164  return;
165  }
166  })
167  .wait();
168 
169  CPPUNIT_ASSERT_MESSAGE("Opened listener and send request to same uri, returned expected JSON", result == m_Data);
170  }
171 
172  void RequestToClosedListener()
173  {
174  web::json::value result;
175 
176  m_Service->SendRequest(U("http://localhost:8080/servertest"))
177  .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
178  .wait();
179  }
180 
181  void CloseListener_NoRequestPossible()
182  {
183  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
184 
185  CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size());
186  CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size());
187 
188  m_Service->HandleDeleteObserver(this);
189 
190  CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size());
191  CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size());
192 
193  CPPUNIT_ASSERT_THROW(RequestToClosedListener(), mitk::Exception);
194  }
195 
196  void RequestToDifferentPathNotFound()
197  {
198  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
199 
200  web::json::value result;
201 
202  m_Service->SendRequest(U("http://localhost:8080/serverexample"))
203  .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
204  .wait();
205  }
206 
207  void OpenListenerGetRequestDifferentPath_ReturnNotFound()
208  {
209  CPPUNIT_ASSERT_THROW(RequestToDifferentPathNotFound(), mitk::Exception);
210  }
211 
212  void OpenListenerCloseAndReopen_Succeed()
213  {
214  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
215 
216  CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size());
217  CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size());
218 
219  m_Service->HandleDeleteObserver(this);
220 
221  CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size());
222  CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size());
223 
224  m_Service->ReceiveRequest(U("http://localhost:8080/servertest"), this);
225 
226  CPPUNIT_ASSERT_MESSAGE("Reopened listener, observer map size is one", 1 == m_Service->GetObservers().size());
227  CPPUNIT_ASSERT_MESSAGE("Reopened listener, server map size is one", 1 == m_Service->GetServerMap().size());
228  }
229 
230  void HandleHeader_Succeed()
231  {
232  mitk::RESTUtil::ParamMap headers;
233  headers.insert(mitk::RESTUtil::ParamMap::value_type(U("Content-Type"), U("awesome/type")));
234 
235  m_Service->SendRequest(U("http://localhost:8080/clienttest")).then([&](pplx::task<web::json::value> resultTask) {
236  // Do something when a single task is done
237  try
238  {
239  auto result = resultTask.get();
240  CPPUNIT_ASSERT_MESSAGE("Sent Header is not successfull transfered to server",
241  result[U("result")].as_string() == U("awesome/type"));
242  }
243  catch (const mitk::Exception &exception)
244  {
245  MITK_ERROR << exception.what();
246  return;
247  }
248  });
249  }
250 };
251 
252 MITK_TEST_SUITE_REGISTRATION(mitkRESTServer)
ServiceReferenceU GetServiceReference(const std::string &clazz)
virtual void ReceiveRequest(const web::uri &uri, IRESTObserver *observer)=0
starts listening for requests if there isn&#39;t another observer listening and the port is free ...
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
This is a microservice interface for managing REST requests.
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
void * GetService(const ServiceReferenceBase &reference)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Test fixture for parameterized tests.
virtual pplx::task< web::json::value > SendRequest(const web::uri &uri, const RequestType &type=RequestType::Get, const std::map< utility::string_t, utility::string_t > headers={})=0
Executes a HTTP request in the mitkRESTClient class.
virtual web::http::http_response Notify(const web::uri &uri, const web::json::value &data, const web::http::method &method, const mitk::RESTUtil::ParamMap &headers)=0
Called if there&#39;s an incoming request for the observer, observer implements how to handle request...
web::http::http_response http_response
mitk::PlanePositionManagerService * m_Service
virtual const std::map< std::pair< int, utility::string_t >, IRESTObserver * > & GetObservers()=0
std::map< utility::string_t, utility::string_t > ParamMap
Definition: mitkRESTUtil.h:27
virtual void HandleDeleteObserver(IRESTObserver *observer, const web::uri &uri={})=0
Handles the deletion of an observer for all or a specific uri.
virtual const std::map< int, RESTServer * > & GetServerMap()=0
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.