From 0dd41f91f9c1e6fa061f3e5ddc0b168c9e1ddcce Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9dric=20Aguerre?= Date: Mon, 31 Aug 2015 10:48:59 +0200 Subject: [PATCH] AttributesService --- projects/GDE_API_CPP/api/src/Attribute.cpp | 31 +++ projects/GDE_API_CPP/api/src/Attribute.hpp | 57 ++++++ .../GDE_API_CPP/api/src/AttributeGroup.hpp | 41 ++++ .../GDE_API_CPP/api/src/AttributesService.cpp | 178 ++++++++++++++++++ .../GDE_API_CPP/api/src/AttributesService.hpp | 40 ++++ projects/GDE_API_CPP/api/src/CMakeLists.txt | 5 + projects/GDE_API_CPP/api/src/GDESession.cpp | 45 +++++ projects/GDE_API_CPP/api/src/GDESession.hpp | 14 ++ .../GDE_API_CPP/api/src/JsonFormatter.cpp | 42 +++++ .../GDE_API_CPP/api/src/JsonFormatter.hpp | 10 + .../GDE_API_CPP/api/tests/AttributesTest.cpp | 150 +++++++++++++++ .../GDE_API_CPP/api/tests/AttributesTest.hpp | 26 +++ 12 files changed, 639 insertions(+) create mode 100644 projects/GDE_API_CPP/api/src/Attribute.cpp create mode 100644 projects/GDE_API_CPP/api/src/Attribute.hpp create mode 100644 projects/GDE_API_CPP/api/src/AttributeGroup.hpp create mode 100644 projects/GDE_API_CPP/api/src/AttributesService.cpp create mode 100644 projects/GDE_API_CPP/api/src/AttributesService.hpp create mode 100644 projects/GDE_API_CPP/api/tests/AttributesTest.cpp create mode 100644 projects/GDE_API_CPP/api/tests/AttributesTest.hpp diff --git a/projects/GDE_API_CPP/api/src/Attribute.cpp b/projects/GDE_API_CPP/api/src/Attribute.cpp new file mode 100644 index 0000000..750afd7 --- /dev/null +++ b/projects/GDE_API_CPP/api/src/Attribute.cpp @@ -0,0 +1,31 @@ +#include "Attribute.hpp" +#include "JsonFormatter.hpp" + +const std::string +gde::Attribute::toJson() const +{ + std::string json = "{"; + json += JsonFormatter::format("\"id\":", getId()); + json += JsonFormatter::format(",\"name\":", getName()); + json += JsonFormatter::format(",\"type\":", getType()); + json += JsonFormatter::format(",\"value\":", getValue()); + json += JsonFormatter::format(",\"groupId\":", getGroupId()); + json += JsonFormatter::format(",\"mandatory\":", getMandatory()); + json += "}"; + return json; +} + +gde::Attribute +gde::Attribute::fromJson(const std::string& json) +{ + Poco::JSON::Object::Ptr object = JsonFormatter::parse(json); + + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + std::string type = JsonFormatter::extract(object, "type"); + std::string value = JsonFormatter::extract(object, "value"); + int groupId = JsonFormatter::extract(object, "groupId"); + bool mandatory = JsonFormatter::extract(object, "mandatory"); + + return gde::Attribute(id, name, type, value, groupId, mandatory); +} diff --git a/projects/GDE_API_CPP/api/src/Attribute.hpp b/projects/GDE_API_CPP/api/src/Attribute.hpp new file mode 100644 index 0000000..2d571d3 --- /dev/null +++ b/projects/GDE_API_CPP/api/src/Attribute.hpp @@ -0,0 +1,57 @@ +#ifndef GDE_ATTRIBUTE_HPP +#define GDE_ATTRIBUTE_HPP + +#include + +namespace gde { + + class Attribute { + friend class AttributesService; + friend class GDESession; + + public: + Attribute(const std::string& name, const std::string& type, const std::string& value, bool mandatory) + : _id(0), _name(name), _type(type), _value(value), _groupId(0), _mandatory(mandatory) + {} + ~Attribute() {} + + inline int getId() const { return _id; } + inline void setId(int id) { this->_id = id; } + + inline std::string getName() const { return _name; } + inline void setName(const std::string& name) { this->_name = name; } + + inline std::string getType() const { return _type; } + inline void setType(const std::string& type) { this->_type = type; } + + inline std::string getValue() const { return _value; } + inline void setValue(const std::string& value) { this->_value = value; } + + inline int getGroupId() const { return _groupId; } + inline void setGroupId(int groupId) { this->_groupId = groupId; } + + inline bool getMandatory() const { return _mandatory; } + inline void setMandatory(bool mandatory) { this->_mandatory = mandatory; } + + const std::string toJson() const; + static Attribute fromJson(const std::string& json); + + private: + Attribute(int id=0, const std::string& name="", const std::string& type="", const std::string& value="", int groupId=0, bool mandatory=false) + : _id(id), _name(name), _type(type), _value(value), _groupId(groupId), _mandatory(mandatory) + {} + //Attribute(const Attribute&); // non copyable + //Attribute& operator=(const Attribute&); // non copyable + + private: + int _id; + std::string _name; + std::string _type; + std::string _value; + int _groupId; + bool _mandatory; + }; + +}; + +#endif diff --git a/projects/GDE_API_CPP/api/src/AttributeGroup.hpp b/projects/GDE_API_CPP/api/src/AttributeGroup.hpp new file mode 100644 index 0000000..dac7a5a --- /dev/null +++ b/projects/GDE_API_CPP/api/src/AttributeGroup.hpp @@ -0,0 +1,41 @@ +#ifndef GDE_ATTRIBUTE_GROUP_HPP +#define GDE_ATTRIBUTE_GROUP_HPP + +#include "Attribute.hpp" + +#include +#include + +namespace gde { + + class AttributeGroup { + friend class AttributesService; + friend class GDESession; + + public: + AttributeGroup(const std::vector& attributes=std::vector()) + : _id(0), _attributes(attributes) + {} + ~AttributeGroup() {} + + inline int getId() const { return _id; } + inline void setId(int id) { this->_id = id; } + + inline const std::vector& getAttributes() const { return _attributes; } + inline void setAttributes(const std::vector& attributes) { this->_attributes = attributes; } + + private: + AttributeGroup(int id, const std::vector& attributes=std::vector()) + : _id(id), _attributes(attributes) + {} + //AttributeGroup(const AttributeGroup&); // non copyable + //AttributeGroup& operator=(const AttributeGroup&); // non copyable + + private: + int _id; + std::vector _attributes; + }; + +}; + +#endif diff --git a/projects/GDE_API_CPP/api/src/AttributesService.cpp b/projects/GDE_API_CPP/api/src/AttributesService.cpp new file mode 100644 index 0000000..882d2d8 --- /dev/null +++ b/projects/GDE_API_CPP/api/src/AttributesService.cpp @@ -0,0 +1,178 @@ +#include "AttributesService.hpp" +#include "HttpConnection.hpp" +#include "CommandTO.hpp" +#include "CommandResultTO.hpp" +#include "JsonFormatter.hpp" +#include "Credentials.hpp" + +#include +#include + +/* + * WARNING: keep enum values synchronized to WEB API (AttributesService) + */ +enum { + CREATE_ATTRIBUTE = 1, + DELETE_ATTRIBUTE, + READ_ATTRIBUTE, + CREATE_ATTRIBUTE_GROUP, + DELETE_ATTRIBUTE_GROUP, + UPDATE_ATTRIBUTE_GROUP, + READ_ATTRIBUTE_GROUP +}; + +std::string gde::AttributesService::_servletName = "AttributesService"; + +const gde::Attribute +gde::AttributesService::createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory) +{ + // build JSON string for CommandTO data + Poco::JSON::Object obj; + obj.set("name", Poco::Dynamic::Var(name)); + obj.set("type", Poco::Dynamic::Var(type)); + obj.set("value", Poco::Dynamic::Var(value)); + obj.set("groupId", Poco::Dynamic::Var(groupId)); + obj.set("mandatory", Poco::Dynamic::Var(mandatory)); + std::string data = JsonFormatter::stringify(obj); + + CommandTO cto(CREATE_ATTRIBUTE, data); + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build Attribute object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + std::string type = JsonFormatter::extract(object, "type"); + std::string value = JsonFormatter::extract(object, "value"); + int groupId = JsonFormatter::extract(object, "groupId"); + bool mandatory = JsonFormatter::extract(object, "mandatory"); + return gde::Attribute(id, name, type, value, groupId, mandatory); + } +} + +bool +gde::AttributesService::deleteAttribute(const Attribute& attribute) +{ + CommandTO cto(DELETE_ATTRIBUTE); + cto.setParameter("attributeId", attribute.getId()); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; +} + +const gde::Attribute +gde::AttributesService::readAttribute(int attributeId) +{ + CommandTO cto(READ_ATTRIBUTE); + cto.setParameter("attributeId", attributeId); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build Attribute object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + std::string type = JsonFormatter::extract(object, "type"); + std::string value = JsonFormatter::extract(object, "value"); + int groupId = JsonFormatter::extract(object, "groupId"); + bool mandatory = JsonFormatter::extract(object, "mandatory"); + return gde::Attribute(id, name, type, value, groupId, mandatory); + } +} + +std::string +_buildAttributesJsonString(int attributeGroupId, const std::vector& attributes) +{ + // Solution here is not very smart. Have tried using Poco to stringify attributes, + // but stringification is too recursive, for example it produces: + // "attributeCollection":"[ { \"id\" : 0, \"name\" : \"myAttribute\",... + // instead of: + // "attributeCollection":[{"id":0,"name":"myAttribute",... + std::string data = "{"; + data += gde::JsonFormatter::format("\"id\":", attributeGroupId); + data += ", \"attributeCollection\":["; + for (std::vector::const_iterator itr = attributes.begin(); itr != attributes.end(); ++itr) { + if (itr != attributes.begin()) + data += ","; + data += itr->toJson(); + } + data += "]}"; + return data; +} + +const gde::AttributeGroup +gde::AttributesService::createAttributeGroup(const AttributeGroup& attributeGroup) +{ + std::string data = _buildAttributesJsonString(attributeGroup.getId(), attributeGroup.getAttributes()); + CommandTO cto(CREATE_ATTRIBUTE_GROUP, data); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build AttributeGroup object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::vector attributes = JsonFormatter::extractVector(object, "attributeCollection"); + return gde::AttributeGroup(id, attributes); + } +} + +bool +gde::AttributesService::deleteAttributeGroup(const gde::AttributeGroup& attributeGroup) +{ + CommandTO cto(DELETE_ATTRIBUTE_GROUP); + cto.setParameter("attributeGroupId", attributeGroup.getId()); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + return crto.getCode() == CommandResultTO::OK; +} + +const gde::AttributeGroup +gde::AttributesService::updateAttributeGroup(const gde::AttributeGroup& attributeGroup, const std::vector& attributes) +{ + std::string data = _buildAttributesJsonString(attributeGroup.getId(), attributes); + CommandTO cto(UPDATE_ATTRIBUTE_GROUP, data); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build AttributeGroup object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::vector attributes = JsonFormatter::extractVector(object, "attributeCollection"); + return gde::AttributeGroup(id, attributes); + } +} + +const gde::AttributeGroup +gde::AttributesService::readAttributeGroup(int attributeGroupId) +{ + CommandTO cto(READ_ATTRIBUTE_GROUP); + cto.setParameter("attributeGroupId", attributeGroupId); + + const Credentials& credentials = _session.getCredentials(); + std::string uri = _session.getServiceURI(_servletName); + CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto); + + // build AttributeGroup object from CommandResultTO data (json format) + { + Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData()); + int id = JsonFormatter::extract(object, "id"); + std::vector attributes = JsonFormatter::extractVector(object, "attributeCollection"); + return gde::AttributeGroup(id, attributes); + } +} diff --git a/projects/GDE_API_CPP/api/src/AttributesService.hpp b/projects/GDE_API_CPP/api/src/AttributesService.hpp new file mode 100644 index 0000000..413142f --- /dev/null +++ b/projects/GDE_API_CPP/api/src/AttributesService.hpp @@ -0,0 +1,40 @@ +#ifndef GDE_ATTRIBUTES_SERVICE_HPP +#define GDE_ATTRIBUTES_SERVICE_HPP + +#include "Attribute.hpp" +#include "AttributeGroup.hpp" +#include "GDESession.hpp" + +#include +#include + +namespace gde { + + class AttributesService { + friend class GDESession; + + public: + const Attribute createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory); + bool deleteAttribute(const Attribute&); + const Attribute readAttribute(int); + + const AttributeGroup createAttributeGroup(const AttributeGroup&); + bool deleteAttributeGroup(const AttributeGroup&); + const AttributeGroup updateAttributeGroup(const AttributeGroup&, const std::vector&); + const AttributeGroup readAttributeGroup(int); + + private: + AttributesService(const GDESession& session) : _session(session) {} + ~AttributesService() {} + AttributesService(const AttributesService&); // non copyable + AttributesService& operator=(const AttributesService&); // non copyable + + private: + static std::string _servletName; + const GDESession& _session; + + }; + +}; + +#endif diff --git a/projects/GDE_API_CPP/api/src/CMakeLists.txt b/projects/GDE_API_CPP/api/src/CMakeLists.txt index 477a3e4..f8e038a 100644 --- a/projects/GDE_API_CPP/api/src/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/src/CMakeLists.txt @@ -15,6 +15,9 @@ SET(_link_LIBRARIES ) SET(gde_api_cpp_HEADERS + Attribute.hpp + AttributeGroup.hpp + AttributesService.hpp CommandResultTO.hpp CommandTO.hpp Credentials.hpp @@ -29,6 +32,8 @@ SET(gde_api_cpp_HEADERS ) SET(gde_api_cpp_SOURCES + Attribute.cpp + AttributesService.cpp CommandResultTO.cpp CommandTO.cpp CommandTO.tpp diff --git a/projects/GDE_API_CPP/api/src/GDESession.cpp b/projects/GDE_API_CPP/api/src/GDESession.cpp index eadcb52..d988cac 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.cpp +++ b/projects/GDE_API_CPP/api/src/GDESession.cpp @@ -1,6 +1,7 @@ #include "GDESession.hpp" #include "UserService.hpp" #include "StudyService.hpp" +#include "AttributesService.hpp" std::string gde::GDESession::getServiceURI(const std::string& serviceName) const @@ -92,3 +93,47 @@ gde::GDESession::readStudy(int studyId) { return gde::StudyService(*this).readStudy(studyId); } + +/* AttributesService */ + +const gde::Attribute +gde::GDESession::createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory) +{ + return gde::AttributesService(*this).createAttribute(name, type, value, groupId, mandatory); +} + +bool +gde::GDESession::deleteAttribute(const gde::Attribute& attribute) +{ + return gde::AttributesService(*this).deleteAttribute(attribute); +} + +const gde::Attribute +gde::GDESession::readAttribute(int attributeId) +{ + return gde::AttributesService(*this).readAttribute(attributeId); +} + +const gde::AttributeGroup +gde::GDESession::createAttributeGroup(const AttributeGroup& attributeGroup) +{ + return gde::AttributesService(*this).createAttributeGroup(attributeGroup); +} + +bool +gde::GDESession::deleteAttributeGroup(const gde::AttributeGroup& attributeGroup) +{ + return gde::AttributesService(*this).deleteAttributeGroup(attributeGroup); +} + +const gde::AttributeGroup +gde::GDESession::updateAttributeGroup(const gde::AttributeGroup& attributeGroup, const std::vector& attributes) +{ + return gde::AttributesService(*this).updateAttributeGroup(attributeGroup, attributes); +} + +const gde::AttributeGroup +gde::GDESession::readAttributeGroup(int attributeGroupId) +{ + return gde::AttributesService(*this).readAttributeGroup(attributeGroupId); +} diff --git a/projects/GDE_API_CPP/api/src/GDESession.hpp b/projects/GDE_API_CPP/api/src/GDESession.hpp index c8abbb2..3d02b1d 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.hpp +++ b/projects/GDE_API_CPP/api/src/GDESession.hpp @@ -5,8 +5,11 @@ #include "User.hpp" #include "Group.hpp" #include "Study.hpp" +#include "Attribute.hpp" +#include "AttributeGroup.hpp" #include +#include namespace gde { @@ -51,6 +54,17 @@ namespace gde { bool setStudyState(const Study&, int); const Study readStudy(int); + /* AttributesService */ + + const Attribute createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory); + bool deleteAttribute(const Attribute&); + const Attribute readAttribute(int); + + const AttributeGroup createAttributeGroup(const AttributeGroup& attributeGroup=AttributeGroup()); + bool deleteAttributeGroup(const AttributeGroup&); + const AttributeGroup updateAttributeGroup(const AttributeGroup&, const std::vector&); + const AttributeGroup readAttributeGroup(int); + private: std::string _serverAddress; Credentials _credentials; diff --git a/projects/GDE_API_CPP/api/src/JsonFormatter.cpp b/projects/GDE_API_CPP/api/src/JsonFormatter.cpp index 8e3dfd0..8f17dfa 100644 --- a/projects/GDE_API_CPP/api/src/JsonFormatter.cpp +++ b/projects/GDE_API_CPP/api/src/JsonFormatter.cpp @@ -29,6 +29,26 @@ gde::JsonFormatter::extract(Poco::JSON::Object::Ptr object, const std::string& v } } +std::vector +gde::JsonFormatter::extractVector(Poco::JSON::Object::Ptr object, const std::string& varName) +{ + try { + std::vector result; + Poco::JSON::Array::Ptr array = object->getArray(varName); + typedef Poco::JSON::Array::ConstIterator ArrayIterator; + + for (ArrayIterator itr = array->begin(); itr != array->end(); ++itr) { + std::string json = itr->toString(); // { var:value, ... } + result.push_back(Attribute::fromJson(json)); + } + + return result; + } catch (Poco::InvalidAccessException& e) { + // This exception is raised if Var v is empty + return std::vector(); + } +} + Poco::Timestamp gde::JsonFormatter::extract(Poco::JSON::Object::Ptr object, const std::string& varName) { @@ -53,6 +73,28 @@ gde::JsonFormatter::stringify(const Poco::JSON::Object& object) { return jsonStream.str(); } +std::string +gde::JsonFormatter::format(const std::string txt, int value) +{ + std::ostringstream oss; + oss << txt << value; + return oss.str(); +} +std::string +gde::JsonFormatter::format(const std::string txt, const std::string& value) +{ + std::ostringstream oss; + oss << txt << "\"" << value << "\""; + return oss.str(); +} +std::string +gde::JsonFormatter::format(const std::string txt, bool value) +{ + std::ostringstream oss; + oss << txt << (value?"true":"false"); + return oss.str(); +} + // Declare methods for the supported template type parameters template int gde::JsonFormatter::extract(Poco::JSON::Object::Ptr, const std::string&); template bool gde::JsonFormatter::extract(Poco::JSON::Object::Ptr, const std::string&); diff --git a/projects/GDE_API_CPP/api/src/JsonFormatter.hpp b/projects/GDE_API_CPP/api/src/JsonFormatter.hpp index 96856cf..fcafbec 100644 --- a/projects/GDE_API_CPP/api/src/JsonFormatter.hpp +++ b/projects/GDE_API_CPP/api/src/JsonFormatter.hpp @@ -1,7 +1,10 @@ #ifndef GDE_JSON_FORMATTER_HPP #define GDE_JSON_FORMATTER_HPP +#include "Attribute.hpp" + #include +#include #include #include @@ -16,7 +19,14 @@ namespace gde { template static T extract(Poco::JSON::Object::Ptr, const std::string&); static Poco::Timestamp extract(Poco::JSON::Object::Ptr, const std::string&); + static std::vector extractVector(Poco::JSON::Object::Ptr, const std::string&); + static std::string stringify(const Poco::JSON::Object&); + + static std::string format(const std::string txt, int value); + static std::string format(const std::string txt, const std::string& value); + static std::string format(const std::string txt, bool value); + }; }; diff --git a/projects/GDE_API_CPP/api/tests/AttributesTest.cpp b/projects/GDE_API_CPP/api/tests/AttributesTest.cpp new file mode 100644 index 0000000..146530a --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/AttributesTest.cpp @@ -0,0 +1,150 @@ +#include + +#include +#include +#include +#include +#include + +void +AttributesTest::testCreateDeleteAttribute() +{ + gde::GDESession session(serverAddress, credentials); + + gde::AttributeGroup myAttributeGroup = session.createAttributeGroup(); + CPPUNIT_ASSERT(myAttributeGroup.getId() > 0); + + gde::Attribute myAttribute = session.createAttribute("myAttribute", "float", "3.14", myAttributeGroup.getId(), true); + CPPUNIT_ASSERT(myAttribute.getId() > 0); + CPPUNIT_ASSERT(myAttribute.getName() == "myAttribute"); + CPPUNIT_ASSERT(myAttribute.getType() == "float"); + CPPUNIT_ASSERT(myAttribute.getValue() == "3.14"); + CPPUNIT_ASSERT(myAttribute.getMandatory()); + + CPPUNIT_ASSERT(session.deleteAttribute(myAttribute)); + CPPUNIT_ASSERT(session.deleteAttributeGroup(myAttributeGroup)); +} + +void +AttributesTest::testReadAttribute() +{ + gde::GDESession session(serverAddress, credentials); + + gde::AttributeGroup myAttributeGroup = session.createAttributeGroup(); + CPPUNIT_ASSERT(myAttributeGroup.getId() > 0); + + gde::Attribute myAttribute = session.createAttribute("myAttribute", "float", "3.14", myAttributeGroup.getId(), true); + CPPUNIT_ASSERT(myAttribute.getId() > 0); + + { + const gde::Attribute& attr = session.readAttribute(myAttribute.getId()); + CPPUNIT_ASSERT(attr.getId() > 0); + CPPUNIT_ASSERT(myAttribute.getName() == attr.getName()); + CPPUNIT_ASSERT(myAttribute.getType() == attr.getType()); + CPPUNIT_ASSERT(myAttribute.getValue() == attr.getValue()); + CPPUNIT_ASSERT(myAttribute.getMandatory() == attr.getMandatory()); + } + + CPPUNIT_ASSERT(session.deleteAttribute(myAttribute)); + CPPUNIT_ASSERT(session.deleteAttributeGroup(myAttributeGroup)); +} + +void +AttributesTest::testCreateDeleteAttributeGroup() +{ + gde::GDESession session(serverAddress, credentials); + + gde::Attribute attribute("myAttribute", "float", "3.14", true); + gde::Attribute attribute2("myAttribute2", "int", "42", false); + std::vector attributes; + attributes.push_back(attribute); + attributes.push_back(attribute2); + gde::AttributeGroup attributeGroup(attributes); + CPPUNIT_ASSERT(attributeGroup.getId() == 0); + + gde::AttributeGroup myAttributeGroup = session.createAttributeGroup(attributeGroup); + CPPUNIT_ASSERT(myAttributeGroup.getId() > 0); + + { + const std::vector& attrs = myAttributeGroup.getAttributes(); + CPPUNIT_ASSERT(attrs.size() == 2); + const gde::Attribute& attr = attrs[0]; + CPPUNIT_ASSERT(attr.getId() > 0); + CPPUNIT_ASSERT(attribute.getName() == attr.getName()); + CPPUNIT_ASSERT(attribute.getType() == attr.getType()); + CPPUNIT_ASSERT(attribute.getValue() == attr.getValue()); + CPPUNIT_ASSERT(attribute.getMandatory() == attr.getMandatory()); + const gde::Attribute& attr2 = attrs[1]; + CPPUNIT_ASSERT(attr2.getId() > 0); + CPPUNIT_ASSERT(attribute2.getName() == attr2.getName()); + CPPUNIT_ASSERT(attribute2.getType() == attr2.getType()); + CPPUNIT_ASSERT(attribute2.getValue() == attr2.getValue()); + CPPUNIT_ASSERT(attribute2.getMandatory() == attr2.getMandatory()); + } + + CPPUNIT_ASSERT(session.deleteAttributeGroup(myAttributeGroup)); +} + +void +AttributesTest::testUpdateAttributeGroup() +{ + gde::GDESession session(serverAddress, credentials); + + gde::AttributeGroup myAttributeGroup = session.createAttributeGroup(); + CPPUNIT_ASSERT(myAttributeGroup.getId() > 0); + + gde::Attribute attribute("myAttribute", "float", "3.14", true); + gde::Attribute attribute2("myAttribute2", "int", "42", false); + std::vector attributes; + attributes.push_back(attribute); + attributes.push_back(attribute2); + + { + const gde::AttributeGroup& attrGrp = session.updateAttributeGroup(myAttributeGroup, attributes); + CPPUNIT_ASSERT(attrGrp.getId() == myAttributeGroup.getId()); + const std::vector& attrs = attrGrp.getAttributes(); + CPPUNIT_ASSERT(attrs.size() == 2); + const gde::Attribute& attr = attrs[0]; + CPPUNIT_ASSERT(attr.getId() > 0); + CPPUNIT_ASSERT(attribute.getName() == attr.getName()); + CPPUNIT_ASSERT(attribute.getType() == attr.getType()); + CPPUNIT_ASSERT(attribute.getValue() == attr.getValue()); + CPPUNIT_ASSERT(attribute.getMandatory() == attr.getMandatory()); + const gde::Attribute& attr2 = attrs[1]; + CPPUNIT_ASSERT(attr2.getId() > 0); + CPPUNIT_ASSERT(attribute2.getName() == attr2.getName()); + CPPUNIT_ASSERT(attribute2.getType() == attr2.getType()); + CPPUNIT_ASSERT(attribute2.getValue() == attr2.getValue()); + CPPUNIT_ASSERT(attribute2.getMandatory() == attr2.getMandatory()); + } + + CPPUNIT_ASSERT(session.deleteAttributeGroup(myAttributeGroup)); +} + +void +AttributesTest::testReadAttributeGroup() +{ + gde::GDESession session(serverAddress, credentials); + + gde::AttributeGroup myAttributeGroup = session.createAttributeGroup(); + CPPUNIT_ASSERT(myAttributeGroup.getId() > 0); + + gde::Attribute myAttribute = session.createAttribute("myAttribute", "float", "3.14", myAttributeGroup.getId(), true); + CPPUNIT_ASSERT(myAttribute.getId() > 0); + + { + const gde::AttributeGroup& attrGrp = session.readAttributeGroup(myAttributeGroup.getId()); + CPPUNIT_ASSERT(attrGrp.getId() > 0); + const std::vector& attrs = attrGrp.getAttributes(); + CPPUNIT_ASSERT(attrs.size() == 1); + const gde::Attribute& attr = attrs[0]; + CPPUNIT_ASSERT(attr.getId() > 0); + CPPUNIT_ASSERT(myAttribute.getName() == attr.getName()); + CPPUNIT_ASSERT(myAttribute.getType() == attr.getType()); + CPPUNIT_ASSERT(myAttribute.getValue() == attr.getValue()); + CPPUNIT_ASSERT(myAttribute.getMandatory() == attr.getMandatory()); + } + + CPPUNIT_ASSERT(session.deleteAttribute(myAttribute)); + CPPUNIT_ASSERT(session.deleteAttributeGroup(myAttributeGroup)); +} diff --git a/projects/GDE_API_CPP/api/tests/AttributesTest.hpp b/projects/GDE_API_CPP/api/tests/AttributesTest.hpp new file mode 100644 index 0000000..6c57957 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/AttributesTest.hpp @@ -0,0 +1,26 @@ +#ifndef GDE_ATTRIBUTES_TEST_HPP +#define GDE_ATTRIBUTES_TEST_HPP + +#include + +class AttributesTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE(AttributesTest); + CPPUNIT_TEST(testCreateDeleteAttribute); + CPPUNIT_TEST(testReadAttribute); + CPPUNIT_TEST(testCreateDeleteAttributeGroup); + CPPUNIT_TEST(testUpdateAttributeGroup); + CPPUNIT_TEST(testReadAttributeGroup); + CPPUNIT_TEST_SUITE_END(); + +public: + void testCreateDeleteAttribute(); + void testReadAttribute(); + void testCreateDeleteAttributeGroup(); + void testUpdateAttributeGroup(); + void testReadAttributeGroup(); +}; + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(AttributesTest, "AttributesTest"); + +#endif -- 2.39.2