Commit fd9b2b9d authored by Pavel Vainerman's avatar Pavel Vainerman

(REST): накидал скелет теста для проверки REST API (через RPC)

parent 6f8dfd03
......@@ -68,23 +68,27 @@ namespace uniset
bool waitSMready( int msec, int pause = 5000 );
bool waitSMworking( uniset::ObjectId, int msec, int pause = 3000 );
inline bool isLocalwork()
inline bool isLocalwork() const noexcept
{
return (ic == NULL);
}
inline uniset::ObjectId ID()
inline uniset::ObjectId ID() const noexcept
{
return myid;
}
inline const std::shared_ptr<IONotifyController> SM()
inline const std::shared_ptr<IONotifyController> SM() noexcept
{
return ic;
}
inline uniset::ObjectId getSMID()
inline uniset::ObjectId getSMID() const noexcept
{
return shmID;
}
#ifndef DISABLE_REST_API
std::string apiRequest( const std::string& query );
#endif
protected:
const std::shared_ptr<IONotifyController> ic;
const std::shared_ptr<UInterface> ui;
......
......@@ -314,3 +314,21 @@ using namespace uniset;
return sm_ready;
}
// --------------------------------------------------------------------------
#ifndef DISABLE_REST_API
std::string SMInterface::apiRequest( const std::string& query )
{
if( ic )
{
BEG_FUNC1(SMInterface::apiRequest)
SimpleInfo_var i = ic->apiRequest(query.c_str());
return std::string(i->info);
END_FUNC(SMInterface::apiRequest)
}
BEG_FUNC(SMInterface::apiRequest)
SimpleInfo_var i = shm->apiRequest(query.c_str());
return std::string(i->info);
END_FUNC(SMInterface::apiRequest)
}
#endif
// --------------------------------------------------------------------------
......@@ -11,7 +11,7 @@ tests_with_conf_SOURCES = tests_with_conf.cc test_calibration.cc test_iobase.c
tests_with_conf_LDADD = $(top_builddir)/lib/libUniSet2.la $(top_builddir)/extensions/lib/libUniSet2Extensions.la
tests_with_conf_CPPFLAGS = -I$(top_builddir)/include -I$(top_builddir)/extensions/include
tests_with_sm_SOURCES = tests_with_sm.cc test_ui.cc test_iobase_with_sm.cc
tests_with_sm_SOURCES = tests_with_sm.cc test_ui.cc test_iobase_with_sm.cc test_restapi_uniset.cc
tests_with_sm_LDADD = $(top_builddir)/lib/libUniSet2.la $(top_builddir)/extensions/lib/libUniSet2Extensions.la \
$(top_builddir)/extensions/SharedMemory/libUniSet2SharedMemory.la $(SIGC_LIBS) $(POCO_LIBS)
tests_with_sm_CPPFLAGS = -I$(top_builddir)/include -I$(top_builddir)/extensions/include \
......
#ifndef DISABLE_REST_API
// -----------------------------------------------------------------------------
#include <catch.hpp>
// -----------------------------------------------------------------------------
#include <memory>
#include <Poco/JSON/Parser.h>
#include "Exceptions.h"
#include "Extensions.h"
#include "tests_with_sm.h"
// -----------------------------------------------------------------------------
using namespace std;
using namespace uniset;
using namespace uniset::extensions;
// -----------------------------------------------------------------------------
static std::shared_ptr<SMInterface> shm;
static void init_test()
{
shm = smiInstance();
REQUIRE( shm != nullptr );
REQUIRE( uniset_conf() != nullptr );
}
// -----------------------------------------------------------------------------
TEST_CASE("[RESTAPI]: ", "[restapi]")
{
init_test();
auto conf = uniset_conf();
std::string s = shm->apiRequest("/conf/get?2,Input5_S&params=iotype");
Poco::JSON::Parser parser;
auto result = parser.parse(s);
// Ожидаемый формат ответа:
// {"conf": [
// {"id":"2","iotype":"DI","mbaddr":"0x01","mbfunc":"0x06","mbreg":"0x02","mbtype":"rtu","name":"Input2_S","nbit":"11","priority":"Medium","rs":"4","textname":"Команда 2"}
// ],
// "object":
// {"id":5003,"isActive":true,"lostMessages":0,"maxSizeOfMessageQueue":1000,"msgCount":0,"name":"SharedMemory","objectType":"IONotifyController"}
// }
//
Poco::JSON::Object::Ptr json = result.extract<Poco::JSON::Object::Ptr>();
REQUIRE(json);
auto jconf = json->get("conf").extract<Poco::JSON::Array::Ptr>();
REQUIRE(jconf);
Poco::JSON::Object::Ptr jret = jconf->getObject(0);
REQUIRE(jret);
REQUIRE( jret->get("iotype").convert<std::string>() == "DI" );
REQUIRE( jret->get("id").convert<ObjectId>() == 2 );
Poco::JSON::Object::Ptr jret2 = jconf->getObject(1);
REQUIRE(jret2);
REQUIRE( jret2->get("iotype").convert<std::string>() == "DI" );
REQUIRE( jret2->get("name").convert<std::string>() == "Input5_S" );
REQUIRE( jret2->get("id").convert<ObjectId>() == 5 );
}
// -----------------------------------------------------------------------------
#endif // ifndef DISABLE_REST_API
// -----------------------------------------------------------------------------
......@@ -989,7 +989,7 @@ namespace uniset
}
else if( query == "conf" )
{
// запрос вида: /conf/qconf?params
// запрос вида: /conf/query?params
string qconf = ( seg.size() > (qind+1) ) ? seg[qind+1] : "";
auto reply = request_conf(qconf, uri.getQueryParameters());
reply->stringify(out);
......
......@@ -212,6 +212,7 @@ extensions/tests/test_iobase.cc
extensions/tests/test_iobase_with_sm.cc
extensions/tests/test_ui.cc
extensions/tests/test_vtypes.cc
extensions/tests/test_restapi_uniset.cc
extensions/tests/tests.cc
extensions/tests/tests_with_conf.cc
extensions/tests/tests_with_conf.xml
......
......@@ -26,3 +26,4 @@ Utilities/MBTester
Utilities/NullController
/usr/include/sigc++-2.0
/usr/include/omniORB4
/usr/include/Poco
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment