PageRenderTime 36ms CodeModel.GetById 6ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

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