diff --git a/Database/CrestApi/CMakeLists.txt b/Database/CrestApi/CMakeLists.txt index 1888e74a7fa9678aa2c7e111819505e17966d11b..c101246c75383317a775c3c3f7d5600b20e52ab6 100644 --- a/Database/CrestApi/CMakeLists.txt +++ b/Database/CrestApi/CMakeLists.txt @@ -19,5 +19,7 @@ atlas_add_library( CrestApi # Test(s) in the package. atlas_add_test( test SOURCES test/test.cxx - LINK_LIBRARIES CrestApi + LINK_LIBRARIES CrestApi nlohmann_json::nlohmann_json + INCLUDE_DIRS ${CURL_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} + LINK_LIBRARIES ${CURL_LIBRARIES} ${Boost_LIBRARIES} -lstdc++fs POST_EXEC_SCRIPT nopost.sh ) diff --git a/Database/CrestApi/CrestApi/CrestApi.h b/Database/CrestApi/CrestApi/CrestApi.h index 3d13f968bc857fbdaaa286dbac2a6424cf7fca4d..0ed501091d83ba61bd66af500774df6ee59e4798 100644 --- a/Database/CrestApi/CrestApi/CrestApi.h +++ b/Database/CrestApi/CrestApi/CrestApi.h @@ -19,7 +19,22 @@ #include <cstdint> #include "nlohmann/json.hpp" +// Boost Function Fragment (begin) + +#include <stdlib.h> /* getenv */ +#include <iostream> +#include <boost/parameter/name.hpp> +#include <boost/parameter/preprocessor.hpp> + +// +BOOST_PARAMETER_NAME(page) +BOOST_PARAMETER_NAME(size) +// + +// Boost Function Fragment (end) + namespace Crest { + // AUXILIARY CLASS to store URL request parameters class urlParameters { @@ -84,6 +99,7 @@ namespace Crest { GET = 0, POST = 1, PUT = 2, DELETE = 3 }; public: + // =================================== // CONSTRUCTORS @@ -182,11 +198,8 @@ namespace Crest { * @param since - since. * @param js - payloads in the JSON format. */ - std::string storeBatchPayloadRequest(const std::string& tag, uint64_t endtime, const std::string& js); // method to - // store - // payloads - // in batch - // mode + std::string storeBatchPayloadRequest(const std::string& tag, uint64_t endtime, const std::string& js); + // ========================================== @@ -621,10 +634,9 @@ namespace Crest { * <br> Example how to use these parameters: <br> * <pre> * std::string name58 = "test_MvG3b"; - * uint64_t endtime58 = 92233; - * std::string str58 = - *"[{\"payloadHash\":\"aaa\",\"since\":0},{\"payloadHash\":\"bbb\",\"since\":9223372036854775806}]"; - * myCrestClientM.storeBatchPayloads(name58, endtime58, str58); + * uint64_t endtime58 = 200; + * std::string str58 = "[{\"payloadHash\":\"aaa\",\"since\":100},{\"payloadHash\":\"bbb\",\"since\":150}]"; + * myCrestClient.storeBatchPayloads(name58, endtime58, str58); * </pre> */ void storeBatchPayloads(const std::string& tag_name, uint64_t endtime, const std::string& iovsetupload); @@ -639,10 +651,10 @@ namespace Crest { * <br> Example how to use these parameters: <br> * <pre> * std::string name58 = "test_MvG3a"; - * uint64_t endtime58 = 92233; - * nlohmann::json js59 = - *nlohmann::json::array({{{"payloadHash","aaa"},{"since",0}},{{"payloadHash","bbb"},{"since",9223372036854775806}}}); - * myCrestClientM.storeBatchPayloads(name58, endtime58, js59) + * uint64_t endtime58 = 200; + * std::string str58 = "[{\"payloadHash\":\"aaa\",\"since\":100},{\"payloadHash\":\"bbb\",\"since\":150}]"; + * nlohmann::json js58 = myCrestClient.getJson(str58); + * myCrestClient.storeBatchPayloads(name58, endtime58, js58) * </pre> */ void storeBatchPayloads(const std::string& tag_name, uint64_t endtime, nlohmann::json& js); @@ -873,38 +885,35 @@ namespace Crest { */ std::string getBlobInStreamFs(const std::string& hash, std::ofstream& out); + /** - * This auxillary method stores several payloads in batch mode in the file storage. - * (This method is an analogue of the store_batch_payloads method in Python) - * @param tag_name - tag name. - * @param endtime - end time. - * @param iovsetupload - iov data as a string. - * <br> Example how to use these parameters: <br> - * <pre> - * std::string name39 = "test_M"; - * uint64_t endtime = 200; - * std::string js39 = "{\"niovs\": 2,\"format\": - *\"PYLD_JSON\",\"iovsList\":[{\"since\":800,\"payload\":\"vvv\"},{\"since\":900,\"payload\":\"www\"}]}"; - * </pre> - */ - void storeBatchPayloadsFs(const std::string& tag_name, uint64_t endtime, const std::string& iovsetupload); +* This auxillary method stores several payloads in batch mode in the file storage. +* (This method is an analogue of the store_batch_payloads method in Python) +* @param tag_name - tag name. +* @param iovsetupload - iov data as a string. +* <br> Example how to use these parameters: <br> +* <pre> +* std::string name39 = "test_M"; +* std::string str39 = "[{\"payloadHash\":\"aaa\",\"since\":100},{\"payloadHash\":\"bbb\",\"since\":150}]"; +* myCrestClient.storeBatchPayloadsFs(name39, str39); +* </pre> +*/ +void storeBatchPayloadsFs(std::string tag_name, std::string& iovsetupload); /** - * This auxillary method stores several payloads in batch mode in the file storage. - * (This method is an analogue of the store_batch_payloads method in Python) - * @param tag_name - tag name. - * @param endtime - end time. - * @param iovsetupload - iov data as a JSON object. - * <br> Example how to use these parameters: <br> - * <pre> - * std::string name39 = "test_M"; - * uint64_t endtime = 200; - * std::string js39 = "{\"niovs\": 2,\"format\": - *\"PYLD_JSON\",\"iovsList\":[{\"since\":800,\"payload\":\"vvv\"},{\"since\":900,\"payload\":\"www\"}]}"; - * json js40 = myCrestClientF.getJson(str39); - * </pre> - */ - void storeBatchPayloadsFs(const std::string& tag_name, uint64_t endtime, nlohmann::json& js); +* This auxillary method stores several payloads in batch mode in the file storage. +* (This method is an analogue of the store_batch_payloads method in Python) +* @param tag_name - tag name. +* @param iovsetupload - iov data as a JSON object. +* <br> Example how to use these parameters: <br> +* <pre> +* std::string name39 = "test_M"; +* std::string str39 = "[{\"payloadHash\":\"aaa\",\"since\":100},{\"payloadHash\":\"bbb\",\"since\":150}]"; +* nlohmann::json js39 = myCrestClient.getJson(str39); +* myCrestClient.storeBatchPayloadsFs(name39, js39); +* </pre> +*/ +void storeBatchPayloadsFs(std::string tag_name, nlohmann::json& js); /** @@ -1058,5 +1067,52 @@ namespace Crest { */ void createTagMetaInfoIOVDbSvc(const std::string& tagname, nlohmann::json& js); + +// Boost Function Fragment (begin) + + +/** + * This method returns the tag list. It is a verion of this method with all parameters. + * (This method is an analogue of the list_tags method in Python) + */ + nlohmann::json listTags(int size, int page); + +/** + * This method returns the tag list. It has boost parameters, all of them are optional. + * (This method is an analogue of the list_tags method in Python) + * @param _size - page size, a number of tags per a page, optional parameter, default value is 1000, + * @param _page - page number optional parameter, default value is 0. + * If you dont use the optional parameter names, you have to use the same parameter order. + * Example: <br> + * <pre> + * nlohmann::json list1 = myCrestClient.listTagsParams(); + * nlohmann::json list2 = myCrestClient.listTagsParams(100,200); + * nlohmann::json list2 = myCrestClient.listTagsParams(_page=3,_size=5); + * </pre> + * + * nlohmann::json listTagsParams(int _size, int _page); + */ +BOOST_PARAMETER_MEMBER_FUNCTION( + (nlohmann::json), + listTagsParams, + tag, + (required + ) + (optional + (size, (int) , 1000) + (page, (int) , 0) + ) +) +{ + + return listTags(size, page); +} + + +// Boost Function Fragment (end) + }; -} // namespace + +} // namespace + + diff --git a/Database/CrestApi/doc/crest_example.cxx b/Database/CrestApi/doc/crest_example.cxx index 32474f5600b46c6696bd1fd1f465b6de696ec3d4..6f6c849c31cad48c3a0fa411dcac8420bf7881f7 100644 --- a/Database/CrestApi/doc/crest_example.cxx +++ b/Database/CrestApi/doc/crest_example.cxx @@ -1146,12 +1146,11 @@ int main() std::cout << std:: endl << "test: storeBatchPayloadsFs" << std::endl; std::string name56 = "test_MvG3a"; - uint64_t endtime56 = 200; std::string str56 = "{\"niovs\": 2,\"format\": \"PYLD_JSON\",\"iovsList\":[{\"since\":800,\"payload\":\"vv1\"},{\"since\":900,\"payload\":\"ww1\"}]}"; nlohmann::json js56 = myCrestClientL.getJson(str56); try { - myCrestClientL.storeBatchPayloadsFs(name56, endtime56, js56); + myCrestClientL.storeBatchPayloadsFs(name56, js56); std::cout << std:: endl << "test: storeBatchPayloadsFs (success) " << std::endl; } catch(const std::runtime_error& e){ diff --git a/Database/CrestApi/doc/crest_example2.cxx b/Database/CrestApi/doc/crest_example2.cxx new file mode 100644 index 0000000000000000000000000000000000000000..795d0544ce2ab4d20eeadcadfa2e2f64893a60cb --- /dev/null +++ b/Database/CrestApi/doc/crest_example2.cxx @@ -0,0 +1,788 @@ +/*! + \file + \brief Main file + + This file contains the examples for the CREST C++ Client Library. + Main part of the examples is commented. Please uncomment the code you need. + Check and correct (if it is necessary) the CREST Server parameters in CrestClient instances. + */ + +#include <iostream> +#include <fstream> +#include <iomanip> +#include <nlohmann/json.hpp> + +#include <CrestApi/CrestApi.h> +#include <filesystem> +// #include <dir.h> + +using namespace Crest; +// # define S_PATH "/crestapi" +// # define S_PORT "8080" +// # define SURL "http://mvg-pc-04.cern.ch:8090" + +using namespace std; +std::string SURL = "http://mvg-pc-04.cern.ch:8090"; + +void print_path(){ + cout << (std::string)SURL << endl; +} + + +bool createDirTree(const std::string full_path){ + size_t pos = 0; + bool ret_val = true; + + while(ret_val == true && pos != std::string::npos) + { + pos = full_path.find('/', pos + 1); + // ret_val = createDir(full_path.substr(0, pos)); + ret_val = std::filesystem::create_directory(full_path.substr(0, pos)); + } + + return ret_val; +} + + +//=================================================== +// Tag Method Tests +//=================================================== + +void testCreateTag(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + // std::string url = "http://mvg-pc-04.cern.ch:8090"; + // CrestClient myCrestClient = CrestClient(url); + + nlohmann::json js = + { + {"description", "none"}, + {"endOfValidity", 0}, + {"insertionTime", "2018-12-06T11:18:35.641+0000"}, + {"lastValidatedTime", 0}, + {"modificationTime", "2018-12-06T11:18:35.641+0000"}, + {"name", tagname}, + {"payloadSpec", "stave: Int32, eta: Int32, mag: Float, base: Float, free: Float"}, + {"synchronization", "none"}, + {"timeType", "time"} + }; + + try{ + myCrestClient.createTag(js); + std::cout << std:: endl << "test: createTag (success)" << std::endl; + } + catch (const std::runtime_error& e) { + std::cout << std:: endl << "test: createTag (failed)" << std::endl; + } +} + + +// Method to create a tag on the local storage (on disk). + +void testCreateTagF(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + + bool rewrite = true; + std::string path = "/tmp/cresttest/crest_dump"; + std::filesystem::path dir(path); + std::filesystem::create_directories(path); + CrestClient myCrestClient = CrestClient(rewrite, path); + + nlohmann::json js = + { + {"description", "none"}, + {"endOfValidity", 0}, + {"insertionTime", "2018-12-06T11:18:35.641+0000"}, + {"lastValidatedTime", 0}, + {"modificationTime", "2018-12-06T11:18:35.641+0000"}, + {"name", tagname}, + {"payloadSpec", "stave: Int32, eta: Int32, mag: Float, base: Float, free: Float"}, + {"synchronization", "none"}, + {"timeType", "time"} + }; + + try{ + myCrestClient.createTag(js); + std::cout << std:: endl << "test: createTag (success)" << std::endl; + } + catch (const std::runtime_error& e) { + std::cout << std:: endl << "test: createTag (failed)" << std::endl; + } +} + +void testListTags(){ + std::cout << std:: endl << "test: createTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: listTags" << std::endl; + + try { + nlohmann::json tag_list = myCrestClient.listTags(); + std::cout << std:: endl << "test: listTags (result)" << std::endl + << tag_list.dump(4) << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: listTags (failed)" << std::endl; + } +} + + +void testFindTag(std::string tagname){ + std::cout << std:: endl << "test: findTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: findTag" << std::endl; + // string name2 = "test_MvG3"; + + try { + nlohmann::json tag_info = myCrestClient.findTag(tagname); + std::cout << std:: endl << "test: findTag (result)" << std::endl + << tag_info.dump(4) << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: findTag (failed)" << std::endl; + } +} + + +void testRemoveTag(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: removeTag" << std::endl; + // std::string tagName = "test_MvG7"; + + try { + myCrestClient.removeTag(tagname); + std::cout << std:: endl << "test: removeTag (success)" << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: removeTag (failed)" << std::endl; + } +} + +void testUpdateTag(std::string tagname){ + std::cout << std:: endl << "test: updateTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: updateTag" << std::endl; + + nlohmann::json js = {{"objectType","json3"}}; + + try{ + myCrestClient.updateTag(tagname, js); + std::cout << std:: endl << "test: updateTag (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: updateTag (failed)" << std::endl; + } +} + +void testUpdateTagSpecification(std::string tagname){ + std::cout << std:: endl << "test: updateTagSpecification" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: updateTagSpecification" << std::endl; + + std::string newObjectType = "json4"; + + try{ + myCrestClient.updateTagSpecification(tagname, newObjectType); + std::cout << std:: endl << "test: updateTagSpecification (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: updateTagSpecification (failed)" << std::endl; + } +} + +//=================================================== +// Global Tag Method Tests +//=================================================== + +void testCreateGlobalTag(std::string tagname){ + std::cout << std:: endl << "test: createGlobalTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: createGlobalTag" << std::endl; + nlohmann::json js = + { + {"name", tagname}, + {"validity", 0}, + {"description", "test"}, + {"release", "1"}, + {"insertionTime", "2018-12-18T11:32:58.081+0000"}, + {"snapshotTime", "2018-12-18T11:32:57.952+0000"}, + {"scenario", "test"}, + {"workflow", "M"}, + {"type", "t"}, + {"snapshotTimeMilli", "null"}, + {"insertionTimeMilli", "null"} + }; + + try { + myCrestClient.createGlobalTag(js); + std::cout << std:: endl << "test: createGlobalTag (success) " << std::endl; + } + catch (const std::runtime_error& e) { + std::cout << std:: endl << "test: createGlobalTag (failed)" << std::endl; + } +} + + +void testFindGlobalTag(std::string tagname){ + std::cout << std:: endl << "test: findGlobalTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std::endl << "test: findGlobalTag" << std::endl; + // string name32 = "MVG_TEST"; + + try { + nlohmann::json tag_info = myCrestClient.findGlobalTag(tagname); + std::cout << std:: endl << "test: findGlobalTag (result) = " << std::endl + << tag_info.dump(4) << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: findGlobalTag (failed)" << std::endl; + } +} + +void testListGlobalTags(){ + std::cout << std:: endl << "test: listGlobalTags" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: listGlobalTags" << std::endl; + + try{ + nlohmann::json tag_info9 = myCrestClient.listGlobalTags(); + std::cout << std::endl << "test: listGlobalTags (result) = " + << tag_info9.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: listGlobalTags (failed)" << std::endl; + } +} + + +void testListGlobalTagsAsString(){ + std::cout << std:: endl << "test: listGlobalTagsAsString" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: listGlobalTagsAsString" << std::endl; + + try{ + nlohmann::json tag_info9 = myCrestClient.listGlobalTagsAsString(); + std::cout << std:: endl << "test: listGlobalTagsAsString (result) = " + << tag_info9.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: listGlobalTagsAsString (failed)" << std::endl; + } +} + + +void testRemoveGlobalTag(std::string tagname){ + std::cout << std:: endl << "test: removeGlobalTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: removeGlobalTag" << std::endl; + // std::string tagName6 = "MvG_TEST"; + + try{ + myCrestClient.removeGlobalTag(tagname); + std::cout << std:: endl << "test: removeGlobalTag (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: removeGlobalTag (failed)" << std::endl; + } +} + + +void testUpdateGlobalTag(std::string tagname){ + std::cout << std:: endl << "test: updateGlobalTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: updateGlobalTag" << std::endl; + + // std::string name = "MvG_TEST_01"; + nlohmann::json js = + { + {"name",tagname}, + {"validity",0}, + {"description","test"}, + {"release","1"}, + {"insertionTime","2018-12-18T11:32:58.081+0000"}, + {"snapshotTime","2018-12-18T11:32:57.952+0000"}, + {"scenario","test2"}, + {"workflow","M"}, + {"type","t"}, + {"snapshotTimeMilli","null"}, + {"insertionTimeMilli","null"} + }; + + try { + myCrestClient.updateGlobalTag(tagname, js); + std::cout << std:: endl << "test: updateGlobalTag (success) " << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: updateGlobalTag (failed)" << std::endl; + } +} + + +void testFindGlobalTagAsString(std::string tagname){ + std::cout << std:: endl << "test: findGlobalTagAsString" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: findGlobalTagAsString" << std::endl; + // string name31 = "MVG_TEST"; + + try { + nlohmann::json tag_info = myCrestClient.findGlobalTagAsString(tagname); + std::cout << std:: endl << "test: findGlobalTagAsString (result) = " << std::endl + << tag_info.dump(4) << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: findGlobalTagAsString (failed)" << std::endl; + } +} + +//=================================================== +// RunLumiInfo Method Tests +//=================================================== + +void testCreateRunLumiInfo(){ + std::cout << std:: endl << "test: createRunLumiInfo" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: createRunLumiInfo" << std::endl; + std::string str = + "{\"since\":\"10\",\"run\":\"7777771\",\"lb\":\"62\",\"starttime\":\"10\",\"endtime\":\"200\"}"; + nlohmann::json js = myCrestClient.getJson(str); + + try{ + myCrestClient.createRunLumiInfo(js); + std::cout << std:: endl << "test: createRunLumiInfo (success) = " << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: createRunLumiInfo (failed)" << std::endl; + } +} + + +void testListRunLumiInfo(){ + std::cout << std:: endl << "test: listRunLumiInfo" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: listRunLumiInfo" << std::endl; + nlohmann::json info16 = myCrestClient.listRunLumiInfo(); + std::cout << std:: endl << "test: listRunLumiInfo (result)" << std::endl; + std::cout << info16.dump(4) << std::endl; + std::cout << std:: endl << "test: listRunLumiInfo" << std::endl; +} + +//=================================================== +// Global Tag Method Tests +//=================================================== + +void testFindGlobalTagMap(std::string mapname){ + std::cout << std:: endl << "test: findGlobalTagMap" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: findGlobalTagMap" << std::endl; + // string name30 = "MVG_TEST"; + + try{ + nlohmann::json tag_info = myCrestClient.findGlobalTagMap(mapname); + std::cout << std:: endl << "test: findGlobalTagMap (result) = " + << tag_info.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: findGlobalTagMap (failed)" << std::endl; + } +} + +// global tag and tag have to exist +void testCreateGlobalTagMap(std::string globaltag, std::string tagname){ + std::cout << std:: endl << "test: createGlobalTagMap" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: createGlobalTagMap" << std::endl; + + nlohmann::json js = + { + {"globalTagName", globaltag}, + {"record", "testing2"}, + {"label", "test2"}, + {"tagName", tagname} + }; + + try{ + myCrestClient.createGlobalTagMap(js); + std::cout << std:: endl << "test: createGlobalTagMap (success) " << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: find_createGlobalTagMap (failed)" << std::endl; + } +} + +//=================================================== +// Auxillary Method Tests +//=================================================== + +void testGetJson(){ + std::cout << std:: endl << "test: getJson" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << "getJson test" << std::endl; + std::string stra = + "{\"tagName\":\"IndetBeampos-nominal\",\"since\":0,\"insertionTime\":\"2019-02-21T13:52:06.222+0000\",\"payloadHash\":\"de9afd9a5bb526d175265d1e66520ac1e7a81816cc3081925625759583e9107f\"}"; + std::string strb = "test"; + std::cout << myCrestClient.getJson(stra).dump(4) << std::endl; + std::cout << myCrestClient.getJson(strb).dump(4) << std::endl; + std::cout << "getJson test" << std::endl; +} + + +//=================================================== +// Tag + Payloads Method Tests +//=================================================== + +// Be carrful tag has to be creted before this test! +void testStorePayload(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + // payload + tag name (command line example): + // curl --form file=@./test.txt --form tag="SB_TAG-PYLD" --form since=0 + // http://mvg-test-pc-03.cern.ch:8090/crestapi/payloads/store + + std::cout << std:: endl << "test: storePayload" << std::endl; + + // std::string tagname = "test_MvG3a"; + uint64_t since = 0; + std::string payload = "aaa"; + + + try{ + myCrestClient.storePayload(tagname, since, payload); + std::cout << std:: endl << "test: storePayload (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: storePayload (failed)" << std::endl; + } +} + +// This test for the local file storage method storePayloadDump. + +void testStorePayloadDump(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + bool rewrite = true; + std::string path = "/tmp/cresttest/crest_dump"; + std::filesystem::create_directories(path); + CrestClient myCrestClient = CrestClient(rewrite, path); + + std::cout << std:: endl << "test: storePayloadDump" << std::endl; + + uint64_t since = 200; + + + std::string str = "{\"niovs\": 2,\"format\":\"PYLD_JSON\",\"iovsList\":[{\"since\":800,\"payload\":\"vv1\"},{\"since\":900,\"payload\":\"ww1\"}]}"; + + nlohmann::json js = myCrestClient.getJson(str); + + try { + myCrestClient.storePayloadDump(tagname, since, str); + std::cout << std:: endl << "test: storePayloadDump (success) " << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: storePayloadDump (failed)" << std::endl; + } +} + +void testStoreBatchPayloads(std::string tagname){ + std::cout << std:: endl << "test: storeBatchPayloads" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: storeBatchPayloads" << std::endl; + + uint64_t endtime = 200; + std::string str = "[{\"payloadHash\":\"aaa\",\"since\":100},{\"payloadHash\":\"bbb\",\"since\":150}]"; + nlohmann::json js = myCrestClient.getJson(str); + + try { + myCrestClient.storeBatchPayloads(tagname, endtime, js); + std::cout << std:: endl << "test: storeBatchPayloads (success) " << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: storeBatchPayloads (failed)" << std::endl; + } +} + + + +//=================================================== +// IOV Method Tests +//=================================================== + +void testFindAllIovs(std::string tagname){ + std::cout << std:: endl << "test: findAllIovs" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: findAllIovs" << std::endl; + + //std::string tagname = "test_MvG4"; + try{ + nlohmann::json tag_info = myCrestClient.findAllIovs(tagname); + std::cout << std:: endl << "test: findAllIovs (result)" << std::endl; + std::cout << tag_info.dump(4) << std::endl; + std::cout << std:: endl << "test: findAllIovs" << std::endl; + } + catch (const std::runtime_error& e) { + std::cout << std:: endl << "test: findAllIovs (failed)" << std::endl; + } +} + +//=================================================== +// Tag Meta Info Method Tests +//=================================================== + +void testCreateTagMetainfo(std::string tagname){ + std::cout << std:: endl << "test: createTagMetainfo" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: createTagMetainfo" << std::endl; + + //std::string tagname = "test_MvG4"; + nlohmann::json js = + { + {"tagName", tagname}, + {"description", "desc-01"}, + {"tagInfo", "taginf-01"}, + {"chansize", 0}, + {"colsize", 0}, + {"insertionTime", "2019-03-14T13:29:25.286Z"} + }; + + try{ + myCrestClient.createTagMetaInfo(tagname, js); + std::cout << std:: endl << "test: createTagMetaInfo (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: createTagMetaInfo (failed)" << std::endl; + } +} + + +void testGetTagMetaInfo(std::string tagname){ + std::cout << std:: endl << "test: getTagMetaInfo" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: getTagMetainfo" << std::endl; + + // std::string tagname61 = "test_MvG3"; + + try{ + nlohmann::json js = myCrestClient.getTagMetaInfo(tagname); + // std::cout << std:: endl << "AAA" << js << "BBB" << std::endl; + std::cout << std:: endl << "test: getTagMetaInfo (result)" << std::endl + << js.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: getTagMetaInfo (failed)" << std::endl; + } +} + + +void testUpdateTagMetainfo(std::string tagname){ + std::cout << std:: endl << "test: createTag" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: updateTagMetainfo" << std::endl; + + // std::string tagname = "test_MvG3"; + nlohmann::json js = + { + // {"tagName", tagname}, + {"description", "desc-02"}, + // {"tagInfo", "taginf-01"}, + // {"chansize", 0}, + // {"colsize", 0}, + // {"insertionTime", "2019-03-14T13:29:25.286Z"} + }; + + try{ + myCrestClient.updateTagMetaInfo(tagname, js); + std::cout << std:: endl << "test: updateTagMetaInfo (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: updateTagMetaInfo (failed)" << std::endl; + } +} + + +//=================================================== +// Tag Meta Info Method Tests in IOVDbSvc format +//=================================================== + +void testCreateTagMetainfoIOVDbSvc(std::string tagname){ + std::cout << std:: endl << "test: createTagMetainfoIOVDbSvc" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: createTagMetainfoIOVDbSvc" << std::endl; + + // std::string tagname = "test_MvG2"; + std::string str = "[{\"node_description\":\" string of the folder description \"},{\"channel_list\":[0,10,20]},{\"channel_names\": [\"first\",\"second\",\"third\"]},{\"payload_specification\":\"stave: Int32, eta: Int32, mag: Float, base: Float, free: Float\"}]"; + + nlohmann::json js = myCrestClient.getJson(str); + + try{ + myCrestClient.createTagMetaInfoIOVDbSvc(tagname, js); + std::cout << std:: endl << "test: createTagMetaInfoIOVDbSvc (success)" << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: createTagMetaInfoIOVDbSvc (failed)" << std::endl; + } +} + + +void testGetTagMetaInfoIOVDbSvc(std::string tagname){ + std::cout << std:: endl << "test: getTagMetaInfoIOVDbSvc" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: getTagMetaInfoIOVDbSvc" << std::endl; + + // std::string tagname = "test_MvG3"; + + try{ + nlohmann::json js68 = myCrestClient.getTagMetaInfoIOVDbSvc(tagname); + std::cout << std:: endl << "test: getTagMetaInfo (result)" << std::endl + << js68.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: getTagMetaInfo (failed)" << std::endl; + } +} + + +void testConvertTagMetaInfo2CREST(){ + std::cout << std:: endl << "test: convertTagMetaInfo2CREST" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + std::cout << std:: endl << "test: convertTagMetaInfo2CREST" << std::endl; + + std::string tagname = "test_MvG4"; + + std::string str = "[{\"node_description\":\" string of the folder description \"},{\"channel_list\":[0,10,20]},{\"channel_names\": [\"first\",\"second\",\"third\"]},{\"payload_specification\":\"stave: Int32, eta: Int32, mag: Float, base: Float, free: Float\"}]"; + + nlohmann::json js = myCrestClient.getJson(str); + + try{ + nlohmann::json res = myCrestClient.convertTagMetaInfo2CREST(js); + std::cout << std:: endl << "test: convertTagMetaInfo2CREST (success)" + << "result = " << res.dump(4) << std::endl; + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: convertTagMetaInfo2CREST (failed)" << std::endl; + } +} + +//---------------------- + +void testTagListParams(){ + std::cout << std:: endl << "test: tagListParams" << std::endl; + CrestClient myCrestClient = CrestClient((std::string)SURL); + + try{ + + //myCrestClient.listTagsParams(); + + // nlohmann::json res = myCrestClient.listTagsParams(100,200); + nlohmann::json res = myCrestClient.listTagsParams(_page=3,_size=5); + + // + std::cout << std:: endl << "test: tagListParams (success)" + << "result = " << res.dump(4) << std::endl; + // + } + catch (const std::runtime_error& e){ + std::cout << std:: endl << "test: tagListParams (failed)" << std::endl; + } +} + + +//---------------------- + +// --------------------- + +int main(int argc, char* argv[]) +{ + if (argc == 2){ + // print_path(); + // cout << "argv[1] = "<< argv[1] << endl; + SURL = argv[1]; + print_path(); + + testCreateTag("test_MvG3b"); + // testListTags(); + // testFindTag("test_MvG3b"); + // testRemoveTag("test_MvG3b"); + // testUpdateTag("test_MvG3b"); + // testUpdateTagSpecification("test_MvG3b"); + + // testCreateGlobalTag("MvG_TEST_01"); + // testFindGlobalTag("MvG_TEST_01"); + // testFindGlobalTagAsString("MvG_TEST_01"); + // testListGlobalTags(); + // testListGlobalTagsAsString(); + // testRemoveGlobalTag("MvG_TEST_01"); + // testUpdateGlobalTag("MvG_TEST_01"); + + // testCreateRunLumiInfo(); // it works on crest-01.cern.ch:8080 + // testListRunLumiInfo(); // it works on crest-01.cern.ch:8080 + + // testCreateGlobalTagMap("MvG_TEST_01","test_MvG3b"); // global tag and tag have to exist + // testFindGlobalTagMap("MvG_TEST_01"); + + // testStorePayload("test_MvG3b"); + + // testCreateTagF("test_MvG3b"); // Create a tag on a file storage + // testStorePayloadDump("test_MvG3b"); + testStoreBatchPayloads("test_MvG3b"); + + // testGetJson(); + + // iovs tests (begin) + // testFindAllIovs("test_MvG3b"); + // iovs tests (end) + + testCreateTagMetainfo("test_MvG3b"); + // testUpdateTagMetainfo("test_MvG3b"); + testGetTagMetaInfo("test_MvG3b"); + + + // testCreateTagMetainfoIOVDbSvc("test_MvG3b"); // it works on crest-01.cern.ch:8080 + // testGetTagMetaInfoIOVDbSvc("test_MvG3b"); // problem on crest-01.cern.ch:8080 + // testConvertTagMetaInfo2CREST(); + + // tmp instructions + // testCreateTag("test_MvG3c"); + // testCreateTagMetainfoIOVDbSvc("test_MvG3c"); // it works on crest-01.cern.ch:8080 + testGetTagMetaInfoIOVDbSvc("test_MvG3c"); // problem on crest-01.cern.ch:8080 // uncomment + // testGetTagMetaInfo("test_MvG3c"); + // tmp instructions + + // new + // testTagListParams(); + // testListTags(); // added for test + + } + else { + cout << "CREST Server path not found" << endl; + cout << "Please, run this program with a server path:" << endl; + cout << "crest_example2 http://mvg-pc-04.cern.ch:8090" << endl; + } + // system("pause"); + cout << "Test ended" << endl; + return 0; +} diff --git a/Database/CrestApi/src/CrestApi.cxx b/Database/CrestApi/src/CrestApi.cxx index eccf735f387c71c39b29f6abe8645ec9a19e5f5f..b92b04c129667bbcbfa4f87d1d50dac36d0c2ef2 100644 --- a/Database/CrestApi/src/CrestApi.cxx +++ b/Database/CrestApi/src/CrestApi.cxx @@ -7,18 +7,18 @@ */ #include <CrestApi/CrestApi.h> -#include <stdlib.h> /* getenv */ #include <boost/uuid/uuid.hpp> // uuid class #include <boost/uuid/uuid_generators.hpp> // generators #include <boost/uuid/uuid_io.hpp> #include <boost/asio.hpp> #include <curl/curl.h> -#include <iostream> + #include <fstream> #include <filesystem> namespace Crest { + CrestClient::CrestClient(bool rewriteIfExists, const std::string& root_folder) : m_mode(FILESYSTEM_MODE), m_root_folder(root_folder), m_isRewrite(rewriteIfExists) { if (!std::filesystem::exists(std::filesystem::path(m_root_folder))) { @@ -83,9 +83,61 @@ namespace Crest { nlohmann::json respond = getJson(retv); - return respond; + checkErrors(respond); + return getResources(respond); } + // + + nlohmann::json CrestClient::listTags(int size, int page) { + std::string method_name = "CrestClient::listTags"; + if (m_mode == FILESYSTEM_MODE) { + method_name.append(" This methods is unsupported for FILESYSTEM mode"); + throw std::runtime_error(method_name.c_str()); + } + + int size_default = 1000; + int page_default = 0; + std::string size_param = ""; + std::string page_param = ""; + std::string params = ""; + + if (size == size_default && page == page_default){ + nlohmann::json respond = listTags(); + checkErrors(respond); + return getResources(respond); + } + + if (size != size_default) { + size_param = "size=" + std::to_string(size); + params = params + size_param; + } + if (page != page_default) { + page_param = "page=" + std::to_string(page); + if (page_param == "") { + params = params + page_param; + } + else{ + params = params + "&" + page_param; + } + } + + std::string current_path = s_PATH + s_TAG_PATH + "?" + params; + std::string getTxt = "GET"; + + std::string retv; + + nlohmann::json js = nullptr; + retv = performRequest(current_path, GET, js, method_name); + + nlohmann::json respond = getJson(retv); + + checkErrors(respond); + return getResources(respond); + } + + // + void CrestClient::removeTag(const std::string& tagName) { std::string method_name = "removeTag"; if (m_mode == FILESYSTEM_MODE) { @@ -120,7 +172,8 @@ namespace Crest { nlohmann::json respond = getJson(retv); - return respond; + checkErrors(respond); + return getResources(respond); } void CrestClient::createTag(const std::string& name, const std::string& desc, const std::string& timeType) { @@ -287,7 +340,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::getSizeByTag(const std::string& tagname) { @@ -307,7 +362,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } int CrestClient::getSize(const std::string& tagname) { @@ -327,7 +384,7 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); int respond = atoi(retv.c_str()); - ; + return respond; } @@ -348,7 +405,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::selectIovs(const std::string& tagname, long snapshot) { @@ -369,7 +428,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::selectGroups(const std::string& tagname) { @@ -389,7 +450,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::selectGroups(const std::string& tagname, long snapshot) { @@ -409,7 +472,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::selectSnapshot(const std::string& tagname) { @@ -429,7 +494,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::selectSnapshot(const std::string& tagname, long snapshot) { @@ -450,7 +517,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } // GLOBALTAGS @@ -504,6 +573,7 @@ namespace Crest { nlohmann::json js = nullptr; retv = performRequest(current_path, GET, js); + return retv; } @@ -516,7 +586,9 @@ namespace Crest { std::string retv = findGlobalTagAsString(name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } void CrestClient::removeGlobalTag(const std::string& name) { @@ -544,7 +616,9 @@ namespace Crest { nlohmann::json js = nullptr; retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } std::string CrestClient::listGlobalTagsAsString() { @@ -584,7 +658,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } void CrestClient::createGlobalTagMap(nlohmann::json& js) { @@ -607,12 +683,10 @@ namespace Crest { std::string CrestClient::getBlob(const std::string& hash) { std::string method_name = "CrestClient::getBlob"; if (m_mode == FILESYSTEM_MODE) { - // method_name.append(" This methods is unsupported for FILESYSTEM mode"); // METHOD REALISED - // throw std::runtime_error(method_name.c_str()); return getBlobFs(hash); } - std::string current_path = s_PATH + s_PAYLOAD_PATH + "/" + hash + s_DATA_PATH; + std::string current_path = s_PATH + s_PAYLOAD_PATH + "/" + hash; std::string retv; @@ -629,7 +703,7 @@ namespace Crest { return getBlobInStreamFs(hash, out); } - std::string current_path = s_PATH + s_PAYLOAD_PATH + "/" + hash + s_DATA_PATH; + std::string current_path = s_PATH + s_PAYLOAD_PATH + "/" + hash; std::string retv; @@ -657,7 +731,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } // @@ -697,7 +773,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } nlohmann::json CrestClient::getPayloadAsJson(const std::string& hash) { @@ -758,8 +836,8 @@ namespace Crest { void CrestClient::storeBatchPayloads(const std::string& tag_name, uint64_t endtime, nlohmann::json& js) { std::string method_name = "CrestClient::storeBatchPayloads"; - if (m_mode == FILESYSTEM_MODE) { - storeBatchPayloadsFs(tag_name, endtime, js); + if (m_mode == FILESYSTEM_MODE) { + storeBatchPayloadsFs(tag_name, js); return; } if (!js.is_array()) { @@ -795,7 +873,10 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); + } std::string CrestClient::createFolder(nlohmann::json& body) { @@ -833,7 +914,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } void CrestClient::createRunLumiInfo(nlohmann::json& body) { @@ -866,7 +949,9 @@ namespace Crest { retv = performRequest(current_path, GET, js, method_name); nlohmann::json respond = getJson(retv); - return respond; + + checkErrors(respond); + return getResources(respond); } // REQUEST METHODS @@ -1408,35 +1493,42 @@ namespace Crest { in.close(); } - void CrestClient::storeBatchPayloadsFs(const std::string& tag_name, uint64_t endtime, - const std::string& iovsetupload) { + // + // storeBatchPayloadsFs + +void CrestClient::storeBatchPayloadsFs(std::string tag_name, std::string& iovsetupload){ + nlohmann::json js = getJson(iovsetupload); - storeBatchPayloadsFs(tag_name, endtime, js); - } + if(!js.is_array()){ + throw std::runtime_error("CrestClient::storeBatchPayloadsFs: JSON has wrong type (must be array"); + } - void CrestClient::storeBatchPayloadsFs(const std::string& tag_name, uint64_t /*endtime*/, nlohmann::json& js) { - std::string method_name = "CrestClient::storeBatchPayloadsFs"; + storeBatchPayloadsFs(tag_name, js); - //try { +} - std::string strS = js.dump(); - nlohmann::json j_null = nlohmann::json::array(); - nlohmann::json iovList = js.value("iovsList", j_null); - std::string iovs = iovList.dump(); - try { - for (auto& kvp : iovList) { - std::cout << kvp << "\n"; - std::string payload = kvp.value("payload", ""); - int since = kvp.value("since", 0); - storePayload(tag_name, since, payload); - } + void CrestClient::storeBatchPayloadsFs(std::string tag_name, nlohmann::json& js){ + std::string method_name = "CrestClient::storeBatchPayloadsFs"; + + try { + + for (auto& kvp : js) { + // std::string payload = kvp.value("payload", ""); + std::string payload = kvp.value("payloadHash", ""); + int since = kvp.value("since", 0); + storePayload(tag_name, since, payload); + } } // end of try - catch (...) { - method_name.append(" cannot store the data in a file"); - throw std::runtime_error(method_name.c_str()); + catch(...) { + method_name.append(" cannot store the data in a file"); + throw std::runtime_error(method_name.c_str()); } // end of catch - } + +} + + // storeBatchPayloadsFs (end) + // std::string CrestClient::getEnvA(const std::string& varname) { std::string respond = ""; @@ -1456,20 +1548,19 @@ namespace Crest { int CrestClient::checkErrors(nlohmann::json& js) { - std::cout << std:: endl << "checkErrors: js = " << std::endl - << js.dump(4) << std::endl; + int result = 0; auto res = js.find("type"); if (res != js.end()) { std::cout << std:: endl << "Error Parsing: error " << std::endl; - std::string type = js.value("type", "oops"); + std::string type = js.value("type", " unknown type "); std::cout << std:: endl << "Error Parsing, type = " << type << std::endl; - std::string message = js.value("message", "oops"); + std::string message = js.value("message", " unknown message "); std::cout << std:: endl << "Error Parsing, message = " << message << std::endl; result = 1; - throw std::logic_error("Error: MvG"); - } else { - std::cout << std:: endl << "Error Parsing: no error " << std::endl; + throw std::logic_error("Error: " + message); + } + else { result = 0; } return result; @@ -1509,7 +1600,8 @@ namespace Crest { nlohmann::json CrestClient::convertTagMetaInfo2IOVDbSvc(nlohmann::json& js) { std::string jsName = "tagInfo"; - return getJson(getTagMetaInfoElement(jsName, js)); + nlohmann::json res = getJson(js[jsName]); + return res; } nlohmann::json CrestClient::convertTagMetaInfo2CREST(nlohmann::json& js) { @@ -1600,7 +1692,8 @@ namespace Crest { nlohmann::json respond = getJson(retv); - return respond; + checkErrors(respond); + return getResources(respond); } void CrestClient::updateTagMetaInfo(const std::string& tagname, nlohmann::json& js) { @@ -1626,7 +1719,8 @@ namespace Crest { throw std::runtime_error(method_name.c_str()); } nlohmann::json js = getTagMetaInfo(tagname); - nlohmann::json resource = getResFirst(js); + nlohmann::json resource = js.at(0); + nlohmann::json result = convertTagMetaInfo2IOVDbSvc(resource); return result; } @@ -1643,4 +1737,6 @@ namespace Crest { return; } -} // namespace + + } // namespace + diff --git a/Database/CrestApi/test/test.cxx b/Database/CrestApi/test/test.cxx index 8e2989b5b27678621e7d219a4b46082583adc4ee..7eee75d37a3672cabf3b4bdb98c758ecb2cd6d1d 100644 --- a/Database/CrestApi/test/test.cxx +++ b/Database/CrestApi/test/test.cxx @@ -1,8 +1,70 @@ #include <cassert> #include <iostream> +#include <nlohmann/json.hpp> + +#include <CrestApi/CrestApi.h> +#include <filesystem> + +using namespace Crest; int main() { - std::cout << "CREST Client Libtary test\n"; - return 0; + std::cout << "CREST Client Libtary test 2\n"; + + int retv = 0; + + // CrestApi Library innitialization for local data storage: + + std::string work_dir = "/tmp/crest_dump"; + bool rewrite = true; + CrestClient testCrestClient = CrestClient(rewrite,work_dir); // CrestClient(); + + //============================================== + // Tag Creation + + std::cout << std:: endl << "test: createTag" << std::endl; + + // Tag description as a JSON: + + nlohmann::json js= + { + {"description", "none"}, + {"endOfValidity", 0}, + {"insertionTime", "2018-12-06T11:18:35.641+0000"}, + {"lastValidatedTime", 0}, + {"modificationTime", "2018-12-06T11:18:35.641+0000"}, + {"name", "test_MvG3a"}, + {"payloadSpec", "stave: Int32, eta: Int32, mag: Float, base: Float, free: Float"}, + {"synchronization", "none"}, + {"timeType", "time"} + }; + + try{ + testCrestClient.createTag(js); + std::cout << std:: endl << "test: createTag (success)" << std::endl; + } + catch (const std::runtime_error& e) { + std::cout << std:: endl << "test: createTag (failed)" << std::endl; + retv = 1; + } + + + //============================================== + // Tag Reading + + std::cout << std:: endl << "test: findTag" << std::endl; + std::string tagname = "test_MvG3a"; + + try { + nlohmann::json tag_info = testCrestClient.findTag(tagname); + std::cout << std:: endl << "test: findTag (result)" << std::endl + << tag_info.dump(4) << std::endl; + } + catch(const std::runtime_error& e){ + std::cout << std:: endl << "test: findTag (failed)" << std::endl; + retv = 1; + } + // + + return retv; }