mirror of
https://github.com/OpenNebula/one.git
synced 2024-12-22 13:33:52 +03:00
feature #407: Added tests for from_xml methods
This commit is contained in:
parent
d3828b30da
commit
13b93bcf83
@ -66,8 +66,10 @@ public:
|
||||
* @param value to set
|
||||
* @param xpath_expr of the xml element
|
||||
* @param def default value if the element is not found
|
||||
*
|
||||
* @return -1 if default was set
|
||||
*/
|
||||
void xpath(string& value, const char * xpath_expr, const char * def);
|
||||
int xpath(string& value, const char * xpath_expr, const char * def);
|
||||
|
||||
/**
|
||||
* Gets and sets a xpath attribute, if the attribute is not found a default
|
||||
@ -75,8 +77,10 @@ public:
|
||||
* @param value to set
|
||||
* @param xpath_expr of the xml element
|
||||
* @param def default value if the element is not found
|
||||
*
|
||||
* @return -1 if default was set
|
||||
*/
|
||||
void xpath(int& value, const char * xpath_expr, const int& def);
|
||||
int xpath(int& value, const char * xpath_expr, const int& def);
|
||||
|
||||
/**
|
||||
* Gets and sets a xpath attribute, if the attribute is not found a default
|
||||
@ -84,8 +88,10 @@ public:
|
||||
* @param value to set
|
||||
* @param xpath_expr of the xml element
|
||||
* @param def default value if the element is not found
|
||||
*
|
||||
* @return -1 if default was set
|
||||
*/
|
||||
void xpath(time_t& value, const char * xpath_expr, const time_t& def);
|
||||
int xpath(time_t& value, const char * xpath_expr, const time_t& def);
|
||||
|
||||
/**
|
||||
* Gets the value of an element from an xml string
|
||||
|
@ -224,39 +224,51 @@ string& Host::to_xml(string& xml) const
|
||||
int Host::from_xml(const string& xml)
|
||||
{
|
||||
vector<xmlNodePtr> content;
|
||||
|
||||
int int_state;
|
||||
int rc = 0;
|
||||
|
||||
// Initialize the internal XML object
|
||||
update_from_str(xml);
|
||||
|
||||
xpath(oid, "/HOST/ID", -1);
|
||||
xpath(hostname, "/HOST/NAME", "not_found");
|
||||
xpath(int_state, "/HOST/STATE", 0);
|
||||
// Get class base attributes
|
||||
rc += xpath(oid, "/HOST/ID", -1);
|
||||
rc += xpath(hostname, "/HOST/NAME", "not_found");
|
||||
rc += xpath(int_state, "/HOST/STATE", 0);
|
||||
|
||||
rc += xpath(im_mad_name, "/HOST/IM_MAD", "not_found");
|
||||
rc += xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found");
|
||||
rc += xpath(tm_mad_name, "/HOST/TM_MAD", "not_found");
|
||||
|
||||
rc += xpath(last_monitored, "/HOST/LAST_MON_TIME", 0);
|
||||
rc += xpath(cluster, "/HOST/CLUSTER", "not_found");
|
||||
|
||||
state = static_cast<HostState>( int_state );
|
||||
|
||||
xpath(im_mad_name, "/HOST/IM_MAD", "not_found");
|
||||
xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found");
|
||||
xpath(tm_mad_name, "/HOST/TM_MAD", "not_found");
|
||||
|
||||
xpath(last_monitored, "/HOST/LAST_MON_TIME", 0);
|
||||
|
||||
xpath(cluster, "/HOST/CLUSTER", "not_found");
|
||||
if (rc != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get associated classes
|
||||
ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
|
||||
|
||||
if( content.size() < 1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
host_share.from_xml_node( content[0] );
|
||||
|
||||
content.clear();
|
||||
|
||||
ObjectXML::get_nodes("/HOST/TEMPLATE", content);
|
||||
|
||||
if( content.size() < 1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
host_template.from_xml_node( content[0] );
|
||||
|
||||
return 0;
|
||||
|
@ -324,35 +324,44 @@ int Image::from_xml(const string& xml)
|
||||
int int_state;
|
||||
int int_type;
|
||||
|
||||
int rc = 0;
|
||||
|
||||
// Initialize the internal XML object
|
||||
update_from_str(xml);
|
||||
|
||||
// Get class base attributes
|
||||
rc += xpath(oid, "/IMAGE/ID", -1);
|
||||
rc += xpath(uid, "/IMAGE/UID", -1);
|
||||
rc += xpath(user_name, "/IMAGE/USERNAME", "not_found");
|
||||
rc += xpath(name, "/IMAGE/NAME", "not_found");
|
||||
|
||||
xpath(oid, "/IMAGE/ID", -1);
|
||||
xpath(uid, "/IMAGE/UID", -1);
|
||||
xpath(user_name,"/IMAGE/USERNAME", "not_found");
|
||||
xpath(name, "/IMAGE/NAME", "not_found");
|
||||
rc += xpath(int_type, "/IMAGE/TYPE", 0);
|
||||
rc += xpath(public_img, "/IMAGE/PUBLIC", 0);
|
||||
rc += xpath(persistent_img, "/IMAGE/PERSISTENT", 0);
|
||||
rc += xpath(regtime, "/IMAGE/REGTIME", 0);
|
||||
|
||||
xpath(int_type, "/IMAGE/TYPE", 0);
|
||||
type = static_cast<ImageType>(int_type);
|
||||
rc += xpath(source, "/IMAGE/SOURCE", "not_found");
|
||||
rc += xpath(int_state, "/IMAGE/STATE", 0);
|
||||
rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1);
|
||||
|
||||
xpath(public_img, "/IMAGE/PUBLIC", 0);
|
||||
xpath(persistent_img, "/IMAGE/PERSISTENT", 0);
|
||||
xpath(regtime, "/IMAGE/REGTIME", 0);
|
||||
xpath(source, "/IMAGE/SOURCE", "not_found");
|
||||
if (rc != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
xpath(int_state, "/IMAGE/STATE", 0);
|
||||
type = static_cast<ImageType>(int_type);
|
||||
state = static_cast<ImageState>(int_state);
|
||||
|
||||
xpath(running_vms, "/IMAGE/RUNNING_VMS", -1);
|
||||
|
||||
// Get associated classes
|
||||
ObjectXML::get_nodes("/IMAGE/TEMPLATE", content);
|
||||
if( content.size() < 1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return image_template->from_xml_node(content[0]);
|
||||
image_template->from_xml_node(content[0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
@ -132,34 +132,40 @@ vector<string> ObjectXML::operator[] (const char * xpath_expr)
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void ObjectXML::xpath(string& value, const char * xpath_expr, const char * def)
|
||||
int ObjectXML::xpath(string& value, const char * xpath_expr, const char * def)
|
||||
{
|
||||
vector<string> values;
|
||||
int rc = 0;
|
||||
|
||||
values = (*this)[xpath_expr];
|
||||
|
||||
if ( values.empty() == true )
|
||||
{
|
||||
value = def;
|
||||
rc = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
value = values[0];
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void ObjectXML::xpath(int& value, const char * xpath_expr, const int& def)
|
||||
int ObjectXML::xpath(int& value, const char * xpath_expr, const int& def)
|
||||
{
|
||||
vector<string> values;
|
||||
int rc = 0;
|
||||
|
||||
values = (*this)[xpath_expr];
|
||||
|
||||
if (values.empty() == true)
|
||||
{
|
||||
value = def;
|
||||
rc = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -172,20 +178,26 @@ void ObjectXML::xpath(int& value, const char * xpath_expr, const int& def)
|
||||
if (iss.fail() == true)
|
||||
{
|
||||
value = def;
|
||||
rc = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void ObjectXML::xpath(time_t& value, const char * xpath_expr, const time_t& def)
|
||||
int ObjectXML::xpath(time_t& value, const char * xpath_expr, const time_t& def)
|
||||
{
|
||||
int int_val;
|
||||
int int_def = static_cast<time_t>(def);
|
||||
int rc;
|
||||
|
||||
xpath(int_val, xpath_expr, int_def);
|
||||
rc = xpath(int_val, xpath_expr, int_def);
|
||||
value = static_cast<time_t>(int_val);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
@ -104,22 +104,27 @@ public:
|
||||
int int_exists;
|
||||
int int_no_exists;
|
||||
int int_malformed;
|
||||
int rc;
|
||||
|
||||
obj.xpath(str_exists,"/VM_POOL/VM/HISTORY/HOSTNAME","default_host");
|
||||
rc = obj.xpath(str_exists,"/VM_POOL/VM/HISTORY/HOSTNAME","default_host");
|
||||
CPPUNIT_ASSERT(str_exists == "A_hostname");
|
||||
CPPUNIT_ASSERT(rc == 0);
|
||||
|
||||
|
||||
obj.xpath(str_no_exists,"/VM_POOL/NOT_AN_ELEMENT","default_host");
|
||||
rc = obj.xpath(str_no_exists,"/VM_POOL/NOT_AN_ELEMENT","default_host");
|
||||
CPPUNIT_ASSERT(str_no_exists == "default_host");
|
||||
CPPUNIT_ASSERT(rc == -1);
|
||||
|
||||
obj.xpath(int_exists,"/VM_POOL/VM/STATE",35);
|
||||
rc = obj.xpath(int_exists,"/VM_POOL/VM/STATE",35);
|
||||
CPPUNIT_ASSERT(int_exists == 1);
|
||||
CPPUNIT_ASSERT(rc == 0);
|
||||
|
||||
obj.xpath(int_no_exists,"/VM_POOL/NOT_AN_ELEMENT",35);
|
||||
rc = obj.xpath(int_no_exists,"/VM_POOL/NOT_AN_ELEMENT",35);
|
||||
CPPUNIT_ASSERT(int_no_exists == 35);
|
||||
CPPUNIT_ASSERT(rc == -1);
|
||||
|
||||
obj.xpath(int_malformed,"/VM_POOL/VM/USERNAME",33);
|
||||
rc = obj.xpath(int_malformed,"/VM_POOL/VM/USERNAME",33);
|
||||
CPPUNIT_ASSERT(int_malformed == 33);
|
||||
CPPUNIT_ASSERT(rc == -1);
|
||||
}
|
||||
catch(runtime_error& re)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user