28 #include <vtkDebugLeaks.h> 32 CPPUNIT_TEST_SUITE(mitkRESTServerTestSuite);
34 MITK_TEST(TwoListenerSameHostSamePort_OnlyOneOpened);
36 MITK_TEST(OpenMultipleListenerCloseOne_Succeed);
37 MITK_TEST(OpenMultipleListenerCloseAll_Succeed);
39 MITK_TEST(CloseListener_NoRequestPossible);
40 MITK_TEST(OpenListenerGetRequestDifferentPath_ReturnNotFound);
41 MITK_TEST(OpenListenerCloseAndReopen_Succeed);
43 CPPUNIT_TEST_SUITE_END();
47 web::json::value m_Data;
50 const web::json::value &,
51 const web::http::method &,
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"))
59 m_Data[U(
"result")] = web::json::value(U(
"awesome/type"));
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"));
83 CPPUNIT_FAIL(
"Getting Service in setUp() failed");
88 void OpenListener_Succeed()
90 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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());
96 void TwoListenerSameHostSamePort_OnlyOneOpened()
98 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
99 m_Service->
ReceiveRequest(U(
"http://localhost:8080/serverexample"),
this);
101 CPPUNIT_ASSERT_MESSAGE(
"Open two listener with a different path,same host, same port, observer map size is two",
103 CPPUNIT_ASSERT_MESSAGE(
"Open two listener with a different path,same host, same port, server map size is one",
107 void CloseListener_Succeed()
109 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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());
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());
120 void OpenMultipleListenerCloseOne_Succeed()
122 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
123 m_Service->
ReceiveRequest(U(
"http://localhost:8090/serverexample"),
this);
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());
130 CPPUNIT_ASSERT_MESSAGE(
"Closed one of two listeners, observer map is size is one",
132 CPPUNIT_ASSERT_MESSAGE(
"Closed one of two listener, server map size is one", 1 == m_Service->
GetServerMap().size());
135 void OpenMultipleListenerCloseAll_Succeed()
137 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
138 m_Service->
ReceiveRequest(U(
"http://localhost:8090/serverexample"),
this);
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());
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());
149 void OpenListenerGetRequestSamePath_ReturnExpectedJSON()
151 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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) {
159 result = resultTask.get();
169 CPPUNIT_ASSERT_MESSAGE(
"Opened listener and send request to same uri, returned expected JSON", result == m_Data);
172 void RequestToClosedListener()
174 web::json::value result;
176 m_Service->
SendRequest(U(
"http://localhost:8080/servertest"))
177 .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
181 void CloseListener_NoRequestPossible()
183 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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());
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());
196 void RequestToDifferentPathNotFound()
198 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
200 web::json::value result;
202 m_Service->
SendRequest(U(
"http://localhost:8080/serverexample"))
203 .then([&](pplx::task<web::json::value> resultTask) { result = resultTask.get(); })
207 void OpenListenerGetRequestDifferentPath_ReturnNotFound()
209 CPPUNIT_ASSERT_THROW(RequestToDifferentPathNotFound(),
mitk::Exception);
212 void OpenListenerCloseAndReopen_Succeed()
214 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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());
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());
224 m_Service->
ReceiveRequest(U(
"http://localhost:8080/servertest"),
this);
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());
230 void HandleHeader_Succeed()
233 headers.insert(mitk::RESTUtil::ParamMap::value_type(U(
"Content-Type"), U(
"awesome/type")));
235 m_Service->
SendRequest(U(
"http://localhost:8080/clienttest")).then([&](pplx::task<web::json::value> resultTask) {
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"));
ServiceReferenceU GetServiceReference(const std::string &clazz)
virtual void ReceiveRequest(const web::uri &uri, IRESTObserver *observer)=0
starts listening for requests if there isn't another observer listening and the port is free ...
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
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't instantiate exceptions manually...
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'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
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.