diff --git a/Modules/CppRestSdk/test/mitkRESTClientTest.cpp b/Modules/CppRestSdk/test/mitkRESTClientTest.cpp index f1f0ef5fa0..b8ef66943a 100644 --- a/Modules/CppRestSdk/test/mitkRESTClientTest.cpp +++ b/Modules/CppRestSdk/test/mitkRESTClientTest.cpp @@ -1,281 +1,272 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Testing #include "mitkTestFixture.h" #include "mitkTestingMacros.h" // MITK includes #include "mitkRESTClient.h" // VTK includes #include #include "mitkIRESTManager.h" #include #include #include #include #include #include class mitkRESTClientTestSuite : public mitk::TestFixture, mitk::IRESTObserver { CPPUNIT_TEST_SUITE(mitkRESTClientTestSuite); MITK_TEST(GetRequestValidURI_ReturnsExpectedJSON); MITK_TEST(MultipleGetRequestValidURI_AllTasksFinish); MITK_TEST(PutRequestValidURI_ReturnsExpectedJSON); MITK_TEST(PostRequestValidURI_ReturnsExpectedJSON); MITK_TEST(GetRequestInvalidURI_ThrowsException); MITK_TEST(PutRequestInvalidURI_ThrowsException); MITK_TEST(PostRequestInvalidURI_ThrowsException); CPPUNIT_TEST_SUITE_END(); public: mitk::IRESTManager *m_Service; web::json::value Notify(const web::uri &uri, const web::json::value &data) override { web::json::value returnData = data; returnData[L"userId"] = web::json::value(1); returnData[L"id"] = web::json::value(1); returnData[L"title"] = web::json::value(U("this is a title")); returnData[L"body"] = web::json::value(U("this is a body")); return returnData; } /** * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used * members for a new test case. (If the members are not used in a test, the method does not need to be called). */ void setUp() override { us::ServiceReference serviceRef = us::GetModuleContext()->GetServiceReference(); if (serviceRef) { m_Service = us::GetModuleContext()->GetService(serviceRef); } - if (m_Service) + + if (!m_Service) { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_FAIL("Getting Service in setUp() failed"); } + + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); } void tearDown() override { - if (m_Service) - { - m_Service->HandleDeleteObserver(this); - } + m_Service->HandleDeleteObserver(this); } void GetRequestValidURI_ReturnsExpectedJSON() { web::json::value *result = new web::json::value(); web::json::value data; //TODO: data als memebrvariable/ in setup/ in methode data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); - if (m_Service) - { - m_Service->SendRequest(L"http://localhost:8080/test") - .then([=](pplx::task resultTask) { - try - { - *result = resultTask.get(); - } - catch (const mitk::Exception &exception) - { - MITK_ERROR << exception.what(); - return; - } - }) - .wait(); - } + + m_Service->SendRequest(L"http://localhost:8080/test") + .then([=](pplx::task resultTask) { + try + { + *result = resultTask.get(); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }) + .wait(); + CPPUNIT_ASSERT_MESSAGE("Result is the expected JSON value", *result == data); } void MultipleGetRequestValidURI_AllTasksFinish() { int *count = new int(0); - if (m_Service) - { + // Create multiple tasks e.g. as shown below //TODO: vector konstruktoren anschauen / emplace_back std::vector> tasks; - for (int i = 0; i < 20; ++i) - { - pplx::task singleTask = - m_Service->SendRequest(L"http://localhost:8080/test") - .then([=](pplx::task resultTask) { - // Do something when a single task is done - try - { - resultTask.get(); - *count +=1; - } - catch (const mitk::Exception &exception) - { - MITK_ERROR << exception.what(); - return; - } - }); - tasks.push_back(singleTask); - } + for (int i = 0; i < 20; ++i) + { + pplx::task singleTask = + m_Service->SendRequest(L"http://localhost:8080/test") + .then([=](pplx::task resultTask) { + // Do something when a single task is done + try + { + resultTask.get(); + *count +=1; + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }); + tasks.push_back(singleTask); + } // Create a joinTask which includes all tasks you've created auto joinTask = pplx::when_all(begin(tasks), end(tasks)); // Run asynchonously joinTask.then([=](pplx::task resultTask) { // Do something when all tasks are finished try { resultTask.get(); *count += 1; } catch (const mitk::Exception &exception) { MITK_ERROR << exception.what(); return; } }).wait(); - } + CPPUNIT_ASSERT_MESSAGE("Multiple Requests", 21 == *count); } void PutRequestValidURI_ReturnsExpectedJSON() { // optional: link might get invalid or content is changed web::json::value *result = new web::json::value(); web::json::value data; - if (m_Service) - { - data[L"userId"] = web::json::value(1); - data[L"id"] = web::json::value(1); - data[L"title"] = web::json::value(U("this is a changed title")); - data[L"body"] = web::json::value(U("and the body is changed as well")); - m_Service - ->SendRequest(L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::Put, &data) - .then([=](pplx::task resultTask) { - try - { - *result = resultTask.get(); - } - catch (const mitk::Exception &exception) - { - MITK_ERROR << exception.what(); - return; - } - }) - .wait(); - } + + data[L"userId"] = web::json::value(1); + data[L"id"] = web::json::value(1); + data[L"title"] = web::json::value(U("this is a changed title")); + data[L"body"] = web::json::value(U("and the body is changed as well")); + m_Service + ->SendRequest(L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::Put, &data) + .then([=](pplx::task resultTask) { + try + { + *result = resultTask.get(); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }) + .wait(); + CPPUNIT_ASSERT_MESSAGE( "Result is the expected JSON value, check if the link is still valid since this is an optional test", *result == data); } void PostRequestValidURI_ReturnsExpectedJSON() { // optional: link might get invalid or content is changed web::json::value *result = new web::json::value(); web::json::value data; - if (m_Service) - { + data[L"userId"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a new title")); data[L"body"] = web::json::value(U("this is a new body")); - m_Service->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::Post, &data) - .then([=](pplx::task resultTask) { - try - { - *result = resultTask.get(); - } - catch (const mitk::Exception &exception) - { - MITK_ERROR << exception.what(); - return; - } - }) - .wait(); - } + + m_Service->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::Post, &data) + .then([=](pplx::task resultTask) { + try + { + *result = resultTask.get(); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }) + .wait(); + data[L"id"] = web::json::value(101); CPPUNIT_ASSERT_MESSAGE( "Result is the expected JSON value, check if the link is still valid since this is an optional test", *result == data); } void GetException() { //Method which makes a get request to an invalid uri web::json::value *result = new web::json::value(); - if (m_Service) - { - m_Service->SendRequest(L"http://localhost:1234/invalid") - .then([=](pplx::task resultTask) { *result = resultTask.get(); }) - .wait(); - } + + m_Service->SendRequest(L"http://localhost:1234/invalid") + .then([=](pplx::task resultTask) { *result = resultTask.get(); }) + .wait(); } void GetRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(GetException(), mitk::Exception); } void PutException() { //Method which makes a put request to an invalid uri web::json::value *result = new web::json::value(); web::json::value data; - if (m_Service) - { - data[L"userId"] = web::json::value(1); - data[L"id"] = web::json::value(1); - data[L"title"] = web::json::value(U("this is a changed title")); - data[L"body"] = web::json::value(U("and the body is changed as well")); - m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::Put, &data) - .then([=](pplx::task resultTask) { - *result = resultTask.get();}) - .wait(); - } + + data[L"userId"] = web::json::value(1); + data[L"id"] = web::json::value(1); + data[L"title"] = web::json::value(U("this is a changed title")); + data[L"body"] = web::json::value(U("and the body is changed as well")); + m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::Put, &data) + .then([=](pplx::task resultTask) { + *result = resultTask.get();}) + .wait(); } void PutRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PutException(), mitk::Exception); } void PostException() { //Method which makes a post request to an invalid uri web::json::value *result = new web::json::value(); web::json::value data; - if (m_Service) - { - data[L"userId"] = web::json::value(1); - data[L"title"] = web::json::value(U("this is a new title")); - data[L"body"] = web::json::value(U("this is a new body")); - m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::Post, &data) - .then([=](pplx::task resultTask) { - *result = resultTask.get(); - }) - .wait(); - } + + data[L"userId"] = web::json::value(1); + data[L"title"] = web::json::value(U("this is a new title")); + data[L"body"] = web::json::value(U("this is a new body")); + m_Service->SendRequest(L"http://localhost:1234/invalid", mitk::IRESTManager::RequestType::Post, &data) + .then([=](pplx::task resultTask) { + *result = resultTask.get(); + }) + .wait(); } void PostRequestInvalidURI_ThrowsException() { CPPUNIT_ASSERT_THROW(PostException(), mitk::Exception); } }; MITK_TEST_SUITE_REGISTRATION(mitkRESTClient) \ No newline at end of file diff --git a/Modules/CppRestSdk/test/mitkRESTServerTest.cpp b/Modules/CppRestSdk/test/mitkRESTServerTest.cpp index a610119865..ffe4136e61 100644 --- a/Modules/CppRestSdk/test/mitkRESTServerTest.cpp +++ b/Modules/CppRestSdk/test/mitkRESTServerTest.cpp @@ -1,260 +1,232 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Testing #include "mitkTestFixture.h" #include "mitkTestingMacros.h" // MITK includes #include "mitkRESTServer.h" // VTK includes #include class mitkRESTServerTestSuite : public mitk::TestFixture, mitk::IRESTObserver { CPPUNIT_TEST_SUITE(mitkRESTServerTestSuite); MITK_TEST(OpenListener_Succeed); MITK_TEST(TwoListenerSameHostSamePort_OnlyOneOpened); MITK_TEST(CloseListener_Succeed); MITK_TEST(OpenMultipleListenerCloseOne_Succeed); MITK_TEST(OpenMultipleListenerCloseAll_Succeed); MITK_TEST(OpenListenerGetRequestSamePath_ReturnExpectedJSON); MITK_TEST(CloseListener_NoRequestPossible); MITK_TEST(OpenListenerGetRequestDifferentPath_ReturnNotFound); MITK_TEST(OpenListenerCloseAndReopen_Succeed); CPPUNIT_TEST_SUITE_END(); public: mitk::IRESTManager *m_Service; web::json::value m_Data; web::json::value Notify(const web::uri &uri, const web::json::value &data) override { web::json::value returnData = data; returnData[L"userId"] = web::json::value(1); returnData[L"id"] = web::json::value(1); returnData[L"title"] = web::json::value(U("this is a title")); returnData[L"body"] = web::json::value(U("this is a body")); return returnData; } /** * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used * members for a new test case. (If the members are not used in a test, the method does not need to be called). */ void setUp() override { m_Data[L"userId"] = web::json::value(1); m_Data[L"id"] = web::json::value(1); m_Data[L"title"] = web::json::value(U("this is a title")); m_Data[L"body"] = web::json::value(U("this is a body")); us::ServiceReference serviceRef = us::GetModuleContext()->GetServiceReference(); if (serviceRef) { m_Service = us::GetModuleContext()->GetService(serviceRef); } - //TODO: if(m_Service) überprüfen, exception wenn nicht + if (!m_Service) + { + CPPUNIT_FAIL("Getting Service in setUp() failed"); + } } void tearDown() override { m_Service->HandleDeleteObserver(this); } void OpenListener_Succeed() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size()); } void TwoListenerSameHostSamePort_OnlyOneOpened() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - m_Service->ReceiveRequest(L"http://localhost:8080/example", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + m_Service->ReceiveRequest(L"http://localhost:8080/example", this); + CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, observer map size is two", 2 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open two listener with a different path,same host, same port, server map size is one", 1 == m_Service->GetServerMap().size()); } void CloseListener_Succeed() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->HandleDeleteObserver(this); - } + + m_Service->HandleDeleteObserver(this); + CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size()); } void OpenMultipleListenerCloseOne_Succeed() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - m_Service->ReceiveRequest(L"http://localhost:8090/example", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + m_Service->ReceiveRequest(L"http://localhost:8090/example", this); + CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", 2 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", 2 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->HandleDeleteObserver(this, L"http://localhost:8080/test"); - } + + m_Service->HandleDeleteObserver(this, L"http://localhost:8080/test"); + CPPUNIT_ASSERT_MESSAGE("Closed one of two listeners, observer map is size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Closed one of two listener, server map size is one", 1 == m_Service->GetServerMap().size()); } void OpenMultipleListenerCloseAll_Succeed() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - m_Service->ReceiveRequest(L"http://localhost:8090/example", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + m_Service->ReceiveRequest(L"http://localhost:8090/example", this); + CPPUNIT_ASSERT_MESSAGE("Open two listener, observer map size is two", 2 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open two listener, server map size is two", 2 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->HandleDeleteObserver(this); - } + + m_Service->HandleDeleteObserver(this); + CPPUNIT_ASSERT_MESSAGE("Closed all listeners, observer map is empty", 0 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Closed all listeners, server map is empty", 0 == m_Service->GetServerMap().size()); } void OpenListenerGetRequestSamePath_ReturnExpectedJSON() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + web::json::value *result = new web::json::value(); web::json::value data; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); - if (m_Service) - { - m_Service->SendRequest(L"http://localhost:8080/test") - .then([=](pplx::task resultTask) { - try - { - *result = resultTask.get(); - } - catch (const mitk::Exception &exception) - { - MITK_ERROR << exception.what(); - return; - } - }) - .wait(); - } + m_Service->SendRequest(L"http://localhost:8080/test") + .then([=](pplx::task resultTask) { + try + { + *result = resultTask.get(); + } + catch (const mitk::Exception &exception) + { + MITK_ERROR << exception.what(); + return; + } + }) + .wait(); + CPPUNIT_ASSERT_MESSAGE("Opened listener and send request to same uri, returned expected JSON", *result == data); } void RequestToClosedListener() { web::json::value *result = new web::json::value(); - if (m_Service) - { - m_Service->SendRequest(L"http://localhost:8080/test") - .then([=](pplx::task resultTask) { *result = resultTask.get(); }) - .wait(); - } + + m_Service->SendRequest(L"http://localhost:8080/test") + .then([=](pplx::task resultTask) { *result = resultTask.get(); }) + .wait(); } void CloseListener_NoRequestPossible() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->HandleDeleteObserver(this); - } + + m_Service->HandleDeleteObserver(this); + CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size()); CPPUNIT_ASSERT_THROW(RequestToClosedListener(), mitk::Exception); } void RequestToDifferentPathNotFound() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + web::json::value *result = new web::json::value(); web::json::value data; data[L"userId"] = web::json::value(1); data[L"id"] = web::json::value(1); data[L"title"] = web::json::value(U("this is a title")); data[L"body"] = web::json::value(U("this is a body")); - if (m_Service) - { - m_Service->SendRequest(L"http://localhost:8080/example") - .then([=](pplx::task resultTask) { *result = resultTask.get(); }) - .wait(); - } + m_Service->SendRequest(L"http://localhost:8080/example") + .then([=](pplx::task resultTask) { *result = resultTask.get(); }) + .wait(); } void OpenListenerGetRequestDifferentPath_ReturnNotFound() { CPPUNIT_ASSERT_THROW(RequestToDifferentPathNotFound(), mitk::Exception); } void OpenListenerCloseAndReopen_Succeed() { - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_ASSERT_MESSAGE("Open one listener, observer map size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Open one listener, server map size is one", 1 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->HandleDeleteObserver(this); - } + + m_Service->HandleDeleteObserver(this); + CPPUNIT_ASSERT_MESSAGE("Closed listener, observer map is empty", 0 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Closed listener, server map is empty", 0 == m_Service->GetServerMap().size()); - if (m_Service) - { - m_Service->ReceiveRequest(L"http://localhost:8080/test", this); - } + + m_Service->ReceiveRequest(L"http://localhost:8080/test", this); + CPPUNIT_ASSERT_MESSAGE("Reopened listener, observer map size is one", 1 == m_Service->GetObservers().size()); CPPUNIT_ASSERT_MESSAGE("Reopened listener, server map size is one", 1 == m_Service->GetServerMap().size()); } }; MITK_TEST_SUITE_REGISTRATION(mitkRESTServer) \ No newline at end of file