/test/factory/topology/testPhysicalDataCenterFactory.cpp

https://github.com/deltaforge/nebu-common-cpp · C++ · 162 lines · 131 code · 28 blank · 3 comment · 0 complexity · 1fc4163c42e388f56cedcfb8dce88dfc MD5 · raw file

  1. #include "gmock/gmock.h"
  2. #include "gtest/gtest.h"
  3. #include "tinyxml2.h"
  4. #include "nebu/topology/physicalDataCenterFactory.h"
  5. #include "mocks/mockPhysicalRackFactory.h"
  6. #include "mocks/mockPhysicalRack.h"
  7. #include "nebu/util/exceptions.h"
  8. #include "nebu/util/xmlUtil.h"
  9. // Using declarations - tinyxml2
  10. using tinyxml2::XMLDocument;
  11. using tinyxml2::XMLElement;
  12. using std::string;
  13. using std::shared_ptr;
  14. using std::static_pointer_cast;
  15. using std::dynamic_pointer_cast;
  16. // Using declarations - nebu-common
  17. using nebu::common::PhysicalDataCenter;
  18. using nebu::common::PhysicalDataCenterFactory;
  19. using nebu::common::PhysicalRackFactory;
  20. using nebu::common::XMLParseException;
  21. using nebu::common::XMLUtil;
  22. using nebu::common::Traits;
  23. using nebu::test::MockPhysicalRack;
  24. using nebu::test::MockPhysicalRackFactory;
  25. // Using declarations - gtest/gmock
  26. using ::testing::Eq;
  27. using ::testing::NotNull;
  28. using ::testing::Contains;
  29. using ::testing::Return;
  30. using ::testing::_;
  31. TEST(PhysicalDataCenterFactoryTest, testGetInstance) {
  32. shared_ptr<PhysicalDataCenterFactory> factory = PhysicalDataCenterFactory::getInstance();
  33. EXPECT_THAT(factory.get(), NotNull());
  34. }
  35. TEST(PhysicalDataCenterFactoryTest, testParseXMLNull) {
  36. PhysicalDataCenterFactory factory;
  37. try {
  38. factory.parseXML(NULL);
  39. FAIL() << "Expected XMLParseException";
  40. } catch(XMLParseException &ex) {
  41. SUCCEED();
  42. }
  43. }
  44. TEST(PhysicalDataCenterFactoryTest, testParseXMLEmptyXML) {
  45. PhysicalDataCenterFactory factory;
  46. XMLDocument doc;
  47. XMLElement *elem = doc.NewElement("");
  48. try {
  49. factory.parseXML(elem);
  50. FAIL() << "Expected XMLParseException";
  51. } catch(XMLParseException &ex) {
  52. SUCCEED();
  53. }
  54. }
  55. TEST(PhysicalDataCenterFactoryTest, testParseXMLWrongRoot) {
  56. PhysicalDataCenterFactory factory;
  57. XMLDocument doc;
  58. XMLElement *elem = doc.NewElement("thisisnotthecorrectrootname");
  59. try {
  60. factory.parseXML(elem);
  61. FAIL() << "Expected XMLParseException";
  62. } catch(XMLParseException &ex) {
  63. SUCCEED();
  64. }
  65. }
  66. TEST(PhysicalDataCenterFactoryTest, testParseXMLNoIDAttribute) {
  67. PhysicalDataCenterFactory factory;
  68. XMLDocument doc;
  69. XMLElement *elem = doc.NewElement(PhysicalDataCenterFactory::ROOT_NAME.c_str());
  70. try {
  71. factory.parseXML(elem);
  72. FAIL() << "Expected XMLParseException";
  73. } catch(XMLParseException &ex) {
  74. SUCCEED();
  75. }
  76. }
  77. TEST(PhysicalDataCenterFactoryTest, testParseXMLCheckID) {
  78. PhysicalDataCenterFactory factory;
  79. XMLDocument doc;
  80. XMLElement *elem = doc.NewElement(PhysicalDataCenterFactory::ROOT_NAME.c_str());
  81. elem->SetAttribute(PhysicalDataCenterFactory::ATTR_ID.c_str(), "id");
  82. Traits<PhysicalDataCenter>::Ptr object = factory.parseXML(elem);
  83. EXPECT_THAT(object->getUUID(), Eq("id"));
  84. }
  85. TEST(PhysicalDataCenterFactoryTest, testParseXMLCheckNoRacks) {
  86. PhysicalDataCenterFactory factory;
  87. XMLDocument doc;
  88. XMLElement *elem = doc.NewElement(PhysicalDataCenterFactory::ROOT_NAME.c_str());
  89. elem->SetAttribute(PhysicalDataCenterFactory::ATTR_ID.c_str(), "id");
  90. Traits<PhysicalDataCenter>::Ptr object = factory.parseXML(elem);
  91. EXPECT_THAT(object->getRacks().size(), Eq(0));
  92. }
  93. TEST(PhysicalDataCenterFactoryTest, testParseXMLCheckSingleDisk) {
  94. PhysicalDataCenterFactory factory;
  95. XMLDocument doc;
  96. XMLElement *elem = doc.NewElement(PhysicalDataCenterFactory::ROOT_NAME.c_str());
  97. elem->SetAttribute(PhysicalDataCenterFactory::ATTR_ID.c_str(), "id");
  98. XMLElement *disk = doc.NewElement(PhysicalRackFactory::ROOT_NAME.c_str());
  99. elem->InsertEndChild(disk);
  100. Traits<MockPhysicalRackFactory>::Ptr mock = Traits<MockPhysicalRackFactory>::MakePtr();
  101. Traits<MockPhysicalRack>::Ptr mockedDisk = Traits<MockPhysicalRack>::MakePtr();
  102. PhysicalRackFactory::setInstance(mock);
  103. EXPECT_CALL(*mock, parseXML(disk)).WillOnce(Return(mockedDisk));
  104. EXPECT_CALL(*mockedDisk, setParent(_));
  105. EXPECT_CALL(*mockedDisk, getUUID()).WillOnce(Return("disk"));
  106. Traits<PhysicalDataCenter>::Ptr object = factory.parseXML(elem);
  107. EXPECT_THAT(object->getRacks().size(), Eq(1));
  108. PhysicalRackFactory::setInstance(Traits<MockPhysicalRackFactory>::Ptr());
  109. }
  110. TEST(PhysicalDataCenterFactoryTest, testParseXMLCheckTripleRacks) {
  111. PhysicalDataCenterFactory factory;
  112. XMLDocument doc;
  113. XMLElement *elem = doc.NewElement(PhysicalDataCenterFactory::ROOT_NAME.c_str());
  114. elem->SetAttribute(PhysicalDataCenterFactory::ATTR_ID.c_str(), "id");
  115. XMLElement *disk = doc.NewElement(PhysicalRackFactory::ROOT_NAME.c_str());
  116. elem->InsertEndChild(disk);
  117. XMLElement *disk1 = doc.NewElement(PhysicalRackFactory::ROOT_NAME.c_str());
  118. elem->InsertEndChild(disk1);
  119. XMLElement *disk2 = doc.NewElement(PhysicalRackFactory::ROOT_NAME.c_str());
  120. elem->InsertEndChild(disk2);
  121. Traits<MockPhysicalRackFactory>::Ptr mock = Traits<MockPhysicalRackFactory>::MakePtr();
  122. Traits<MockPhysicalRack>::Ptr mockedDisk = Traits<MockPhysicalRack>::MakePtr();
  123. PhysicalRackFactory::setInstance(mock);
  124. EXPECT_CALL(*mock, parseXML(_)).WillRepeatedly(Return(mockedDisk));
  125. EXPECT_CALL(*mockedDisk, setParent(_)).Times(3);
  126. EXPECT_CALL(*mockedDisk, getUUID()).WillOnce(Return("disk")).WillOnce(Return("disk1")).WillOnce(Return("disk2"));
  127. Traits<PhysicalDataCenter>::Ptr object = factory.parseXML(elem);
  128. EXPECT_THAT(object->getRacks().size(), Eq(3));
  129. PhysicalRackFactory::setInstance(Traits<MockPhysicalRackFactory>::Ptr());
  130. }
  131. int main(int argc, char **argv) {
  132. testing::InitGoogleMock(&argc, argv);
  133. return RUN_ALL_TESTS();
  134. }