int ConfigServer::dbConfigVersion( DBClientBase& conn ){ auto_ptr<DBClientCursor> c = conn.query( "config.version" , BSONObj() ); int version = 0; if ( c->more() ){ BSONObj o = c->next(); version = o["version"].numberInt(); uassert( 10189 , "should only have 1 thing in config.version" , ! c->more() ); } else { if ( conn.count( ShardNS::shard ) || conn.count( ShardNS::database ) ){ version = 1; } } return version; }
/* **************************************************************************** * * MongoDbFindOneFail - * */ TEST(mongoUnsubscribeContext, MongoDbFindOneFail) { HttpStatusCode ms; UnsubscribeContextRequest req; UnsubscribeContextResponse res; /* Prepare mocks */ const DBException e = DBException("boom!!", 33); DBClientConnectionMock* connectionMock = new DBClientConnectionMock(); ON_CALL(*connectionMock, findOne("unittest.csubs",_,_,_)) .WillByDefault(Throw(e)); NotifierMock* notifierMock = new NotifierMock(); EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads(_)) .Times(0); EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_,_)) .Times(0); EXPECT_CALL(*notifierMock, createIntervalThread(_,_,_)) .Times(0); setNotifier(notifierMock); /* Forge the request (from "inside" to "outside") */ req.subscriptionId.set("51307b66f481db11bf860001"); /* Prepare database */ prepareDatabase(); mongoConnect(connectionMock); /* Invoke the function in mongoBackend library */ ms = mongoUnsubscribeContext(&req, &res); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get()); EXPECT_EQ(SccReceiverInternalError, res.statusCode.code); EXPECT_EQ("Internal Server Error", res.statusCode.reasonPhrase); EXPECT_EQ("collection: unittest.csubs " "- findOne() _id: 51307b66f481db11bf860001 " "- exception: boom!!", res.statusCode.details); /* Check database (untouched) */ mongoDisconnect(); // Sleeping a little to "give mongod time to process its input". // Without this sleep, this tests fails around 10% of the times (in Ubuntu 13.04) usleep(1000); mongoConnect("localhost"); DBClientBase* connection = getMongoConnection(); int count = connection->count(SUBSCRIBECONTEXT_COLL, BSONObj()); ASSERT_EQ(2, count); /* Release mocks */ delete notifierMock; }
/* **************************************************************************** * * collectionCount - */ bool collectionCount ( const std::string& col, const BSONObj& q, unsigned long long* c, std::string* err ) { TIME_STAT_MONGO_READ_WAIT_START(); DBClientBase* connection = getMongoConnection(); if (connection == NULL) { TIME_STAT_MONGO_READ_WAIT_STOP(); LM_E(("Fatal Error (null DB connection)")); *err = "null DB connection"; return false; } LM_T(LmtMongo, ("count() in '%s' collection: '%s'", col.c_str(), q.toString().c_str())); try { *c = connection->count(col.c_str(), q); releaseMongoConnection(connection); TIME_STAT_MONGO_READ_WAIT_STOP(); LM_I(("Database Operation Successful (count: %s)", q.toString().c_str())); } catch (const std::exception& e) { releaseMongoConnection(connection); TIME_STAT_MONGO_READ_WAIT_STOP(); std::string msg = std::string("collection: ") + col.c_str() + " - count(): " + q.toString() + " - exception: " + e.what(); *err = "Database Error (" + msg + ")"; LM_E((err->c_str())); return false; } catch (...) { releaseMongoConnection(connection); TIME_STAT_MONGO_READ_WAIT_STOP(); std::string msg = std::string("collection: ") + col.c_str() + " - query(): " + q.toString() + " - exception: generic"; *err = "Database Error (" + msg + ")"; LM_E((err->c_str())); return false; } return true; }
/* **************************************************************************** * * unsubscribe - */ TEST(mongoUnsubscribeContext, unsubscribe) { HttpStatusCode ms; UnsubscribeContextRequest req; UnsubscribeContextResponse res; /* Prepare mock */ NotifierMock* notifierMock = new NotifierMock(); EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads("51307b66f481db11bf860001")) .Times(1); EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_,_)) .Times(0); EXPECT_CALL(*notifierMock, createIntervalThread(_,_,_)) .Times(0); setNotifier(notifierMock); /* Forge the request (from "inside" to "outside") */ req.subscriptionId.set("51307b66f481db11bf860001"); /* Prepare database */ prepareDatabase(); /* Invoke the function in mongoBackend library */ ms = mongoUnsubscribeContext(&req, &res); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get()); EXPECT_EQ(SccOk, res.statusCode.code); EXPECT_EQ("OK", res.statusCode.reasonPhrase); EXPECT_EQ(0, res.statusCode.details.size()); /* Check database (one document, but not the deleted one) */ DBClientBase* connection = getMongoConnection(); ASSERT_EQ(1, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj())); BSONObj sub = connection->findOne(SUBSCRIBECONTEXT_COLL, BSON("_id" << OID("51307b66f481db11bf860002"))); EXPECT_EQ("51307b66f481db11bf860002", sub.getField("_id").OID().str()); /* Release connection */ mongoDisconnect(); /* Release mock */ delete notifierMock; }
/* **************************************************************************** * * subscriptionNotFound - */ TEST(mongoUnsubscribeContext, subscriptionNotFound) { HttpStatusCode ms; UnsubscribeContextRequest req; UnsubscribeContextResponse res; /* Prepare mock */ NotifierMock* notifierMock = new NotifierMock(); EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads(_)) .Times(0); EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_,_)) .Times(0); EXPECT_CALL(*notifierMock, createIntervalThread(_,_,_)) .Times(0); setNotifier(notifierMock); /* Forge the request (from "inside" to "outside") */ req.subscriptionId.set("51307b66f481db11bf869999"); /* Prepare database */ prepareDatabase(); /* Invoke the function in mongoBackend library */ ms = mongoUnsubscribeContext(&req, &res); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ("51307b66f481db11bf869999", res.subscriptionId.get()); EXPECT_EQ(SccContextElementNotFound, res.statusCode.code); EXPECT_EQ("No context element found", res.statusCode.reasonPhrase); EXPECT_EQ("subscriptionId: '51307b66f481db11bf869999'", res.statusCode.details); /* Check database (untouched) */ DBClientBase* connection = getMongoConnection(); ASSERT_EQ(2, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj())); /* Release connection */ mongoDisconnect(); /* Release mock */ delete notifierMock; }
/* **************************************************************************** * * countEntities - * */ static long long countEntities(const std::string& tenant, const std::vector<std::string>& servicePathV,std::string entityType) { DBClientBase* connection = NULL; std::string idType = std::string("_id.") + ENT_ENTITY_TYPE; std::string idServicePath = std::string("_id.") + ENT_SERVICE_PATH; BSONObj query = BSON(idType << entityType << idServicePath << fillQueryServicePath(servicePathV)); LM_T(LmtMongo, ("count() in '%s' collection: '%s'", getEntitiesCollectionName(tenant).c_str(), query.toString().c_str())); try { connection = getMongoConnection(); long long c = connection->count(getEntitiesCollectionName(tenant).c_str(), query); releaseMongoConnection(connection); LM_I(("Database Operation Successful (%s)", query.toString().c_str())); return c; } catch (const DBException& e) { releaseMongoConnection(connection); LM_E(("Database Error ('%s', '%s')", query.toString().c_str(), e.what())); } catch (...) { releaseMongoConnection(connection); LM_E(("Database Error ('%s', '%s')", query.toString().c_str(), "generic exception")); } return -1; }
/* **************************************************************************** * * createEntity - */ TEST(mongoNotifyContextRequest, createEntity) { HttpStatusCode ms; NotifyContextRequest req; NotifyContextResponse res; /* Prepare database */ prepareDatabase(); /* Forge the request */ ContextElementResponse cer; req.subscriptionId.set("51307b66f481db11bf860001"); req.originator.set("localhost"); cer.contextElement.entityId.fill("E10", "T10", "false"); ContextAttribute ca("A1", "TA1", "new_val"); cer.contextElement.contextAttributeVector.push_back(&ca); cer.statusCode.fill(SccOk); req.contextElementResponseVector.push_back(&cer); /* Prepare mock */ TimerMock* timerMock = new TimerMock(); ON_CALL(*timerMock, getCurrentTime()) .WillByDefault(Return(1360232700)); setTimer(timerMock); /* Invoke the function in mongoBackend library */ ms = mongoNotifyContext(&req, &res); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ(SccOk, res.responseCode.code); EXPECT_EQ("OK", res.responseCode.reasonPhrase); EXPECT_EQ(0, res.responseCode.details.size()); /* Check that every involved collection at MongoDB is as expected */ /* Note we are using EXPECT_STREQ() for some cases, as Mongo Driver returns const char*, not string * objects (see http://code.google.com/p/googletest/wiki/Primer#String_Comparison) */ DBClientBase* connection = getMongoConnection(); /* entities collection */ BSONObj ent; std::vector<BSONElement> attrs; ASSERT_EQ(6, connection->count(ENTITIES_COLL, BSONObj())); ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E1" << "_id.type" << "T1")); EXPECT_STREQ("E1", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent.getObjectField("_id"), "type")); EXPECT_FALSE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(2, attrs.size()); BSONObj a1 = getAttr(attrs, "A1", "TA1"); BSONObj a2 = getAttr(attrs, "A2", "TA2"); EXPECT_STREQ("A1", C_STR_FIELD(a1, "name")); EXPECT_STREQ("TA1",C_STR_FIELD(a1, "type")); EXPECT_STREQ("val1", C_STR_FIELD(a1, "value")); EXPECT_FALSE(a1.hasField("modDate")); EXPECT_STREQ("A2", C_STR_FIELD(a2, "name")); EXPECT_STREQ("TA2", C_STR_FIELD(a2, "type")); EXPECT_FALSE(a2.hasField("value")); EXPECT_FALSE(a2.hasField("modDate")); ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E2" << "_id.type" << "T2")); EXPECT_STREQ("E2", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_STREQ("T2", C_STR_FIELD(ent.getObjectField("_id"), "type")); EXPECT_FALSE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(2, attrs.size()); BSONObj a3 = getAttr(attrs, "A3", "TA3"); BSONObj a4 = getAttr(attrs, "A4", "TA4"); EXPECT_STREQ("A3", C_STR_FIELD(a3, "name")); EXPECT_STREQ("TA3", C_STR_FIELD(a3, "type")); EXPECT_STREQ("val3", C_STR_FIELD(a3, "value")); EXPECT_FALSE(a3.hasField("modDate")); EXPECT_STREQ("A4", C_STR_FIELD(a4, "name")); EXPECT_STREQ("TA4", C_STR_FIELD(a4, "type")); EXPECT_FALSE(a4.hasField("value")); EXPECT_FALSE(a4.hasField("modDate")); ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E3" << "_id.type" << "T3")); EXPECT_STREQ("E3", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_STREQ("T3", C_STR_FIELD(ent.getObjectField("_id"), "type")); EXPECT_FALSE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(2, attrs.size()); BSONObj a5 = getAttr(attrs, "A5", "TA5"); BSONObj a6 = getAttr(attrs, "A6", "TA6"); EXPECT_STREQ("A5", C_STR_FIELD(a5, "name")); EXPECT_STREQ("TA5", C_STR_FIELD(a5, "type")); EXPECT_STREQ("val5", C_STR_FIELD(a5, "value")); EXPECT_FALSE(a5.hasField("modDate")); EXPECT_STREQ("A6", C_STR_FIELD(a6, "name")); EXPECT_STREQ("TA6", C_STR_FIELD(a6, "type")); EXPECT_FALSE(a6.hasField("value")); EXPECT_FALSE(a6.hasField("modDate")); ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E1" << "_id.type" << "T1bis")); EXPECT_STREQ("E1", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_STREQ("T1bis", C_STR_FIELD(ent.getObjectField("_id"), "type")); EXPECT_FALSE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); a1 = getAttr(attrs, "A1", "TA1"); EXPECT_STREQ("A1", C_STR_FIELD(a1, "name")); EXPECT_STREQ("TA1",C_STR_FIELD(a1, "type")); EXPECT_STREQ("val1bis2", C_STR_FIELD(a1, "value")); EXPECT_FALSE(a1.hasField("modDate")); ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E10" << "_id.type" << "T10")); EXPECT_STREQ("E10", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_STREQ("T10", C_STR_FIELD(ent.getObjectField("_id"), "type")); EXPECT_TRUE(ent.hasField("creDate")); EXPECT_TRUE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); a1 = getAttr(attrs, "A1", "TA1"); EXPECT_STREQ("A1", C_STR_FIELD(a1, "name")); EXPECT_STREQ("TA1",C_STR_FIELD(a1, "type")); EXPECT_STREQ("new_val", C_STR_FIELD(a1, "value")); EXPECT_TRUE(a1.hasField("creDate")); EXPECT_TRUE(a1.hasField("modDate")); /* Note "_id.type: {$exists: false}" is a way for querying for entities without type */ ent = connection->findOne(ENTITIES_COLL, BSON("_id.id" << "E1" << "_id.type" << BSON("$exists" << false))); EXPECT_STREQ("E1", C_STR_FIELD(ent.getObjectField("_id"), "id")); EXPECT_FALSE(ent.getObjectField("_id").hasField("type")); EXPECT_FALSE(ent.hasField("modDate")); attrs = ent.getField("attrs").Array(); ASSERT_EQ(2, attrs.size()); a1 = getAttr(attrs, "A1", "TA1"); a2 = getAttr(attrs, "A2", "TA2"); EXPECT_STREQ("A1", C_STR_FIELD(a1, "name")); EXPECT_STREQ("TA1",C_STR_FIELD(a1, "type")); EXPECT_STREQ("val1-nt", C_STR_FIELD(a1, "value")); EXPECT_FALSE(a1.hasField("modDate")); EXPECT_STREQ("A2", C_STR_FIELD(a2, "name")); EXPECT_STREQ("TA2", C_STR_FIELD(a2, "type")); EXPECT_FALSE(a2.hasField("value")); EXPECT_FALSE(a2.hasField("modDate")); /* Release connection */ mongoDisconnect(); /* Release mock */ delete timerMock; }
/* **************************************************************************** * * MongoDbFindOneFail - */ TEST(mongoRegisterContext_update, MongoDbFindOneFail) { HttpStatusCode ms; RegisterContextRequest req; RegisterContextResponse res; utInit(); /* Prepare mock */ const DBException e = DBException("boom!!", 33); DBClientConnectionMock* connectionMock = new DBClientConnectionMock(); ON_CALL(*connectionMock, findOne("utest.registrations",_,_,_)) .WillByDefault(Throw(e)); /* Forge the request (from "inside" to "outside") */ EntityId en("E1", "T1"); ContextRegistrationAttribute cra("A1", "TA1", "true"); ContextRegistration cr; cr.entityIdVector.push_back(&en); cr.contextRegistrationAttributeVector.push_back(&cra); cr.providingApplication.set("http://newurl.com"); req.contextRegistrationVector.push_back(&cr); req.registrationId.set("51307b66f481db11bf860001"); req.duration.set("PT1M"); /* Prepare database */ prepareDatabase(); /* Set MongoDB connection mock (preserving "actual" connection for later use) */ DBClientBase* connectionDb = getMongoConnection(); setMongoConnectionForUnitTest(connectionMock); /* Invoke the function in mongoBackend library */ ms = mongoRegisterContext(&req, &res, uriParams); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_TRUE(res.duration.isEmpty()); EXPECT_TRUE(res.registrationId.isEmpty()); EXPECT_EQ(SccReceiverInternalError, res.errorCode.code); EXPECT_EQ("Internal Server Error", res.errorCode.reasonPhrase); EXPECT_EQ("Database Error (collection: utest.registrations " "- findOne(): { _id: ObjectId('51307b66f481db11bf860001'), servicePath: \"/\" } " "- exception: boom!!)", res.errorCode.details); /* Restore real DB connection */ setMongoConnectionForUnitTest(connectionDb); /* Release mock */ delete connectionMock; /* Check that every involved collection at MongoDB is as expected */ /* Note we are using EXPECT_STREQ() for some cases, as Mongo Driver returns const char*, not string * objects (see http://code.google.com/p/googletest/wiki/Primer#String_Comparison) */ /* registrations collection: */ ASSERT_EQ(2, connectionDb->count(REGISTRATIONS_COLL, BSONObj())); BSONObj reg, contextRegistration, ent0, attr0, attr1, attr2; std::vector<BSONElement> contextRegistrationV, entities, attrs; /* reg #1 (untouched) */ reg = connectionDb->findOne(REGISTRATIONS_COLL, BSON("_id" << OID("51307b66f481db11bf860001"))); EXPECT_EQ("51307b66f481db11bf860001", reg.getField("_id").OID().toString()); EXPECT_EQ(10000000, reg.getIntField("expiration")); contextRegistrationV = reg.getField("contextRegistration").Array(); ASSERT_EQ(1, contextRegistrationV.size()); contextRegistration = contextRegistrationV[0].embeddedObject(); EXPECT_STREQ("http://cr1.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(3, attrs.size()); attr0 = attrs[0].embeddedObject(); attr1 = attrs[1].embeddedObject(); attr2 = attrs[2].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); EXPECT_STREQ("A2", C_STR_FIELD(attr1, "name")); EXPECT_STREQ("TA2", C_STR_FIELD(attr1, "type")); EXPECT_STREQ("false", C_STR_FIELD(attr1, "isDomain")); EXPECT_STREQ("A3", C_STR_FIELD(attr2, "name")); EXPECT_STREQ("TA3", C_STR_FIELD(attr2, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr2, "isDomain")); /* reg #2 (untouched) */ reg = connectionDb->findOne(REGISTRATIONS_COLL, BSON("_id" << OID("51307b66f481db11bf860002"))); EXPECT_EQ("51307b66f481db11bf860002", reg.getField("_id").OID().toString()); EXPECT_EQ(20000000, reg.getIntField("expiration")); contextRegistrationV = reg.getField("contextRegistration").Array(); ASSERT_EQ(2, contextRegistrationV.size()); contextRegistration = contextRegistrationV[0].embeddedObject(); EXPECT_STREQ("http://cr1.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); attr0 = attrs[0].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); contextRegistration = contextRegistrationV[1].embeddedObject(); EXPECT_STREQ("http://cr2.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); attr0 = attrs[0].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); utExit(); }
/* **************************************************************************** * * updateWrongIdNoHex - * * FIXME P3: check that we cover this case in the proper place, e.g. check() in the pre-mongoBackend layers, before permanent removal */ TEST(DISABLED_mongoRegisterContext_update, updateWrongIdNoHex) { HttpStatusCode ms; RegisterContextRequest req; RegisterContextResponse res; utInit(); /* Forge the request (from "inside" to "outside") */ EntityId en("E1", "T1"); ContextRegistrationAttribute cra("A1", "TA1", "true"); ContextRegistration cr; cr.entityIdVector.push_back(&en); cr.contextRegistrationAttributeVector.push_back(&cra); cr.providingApplication.set("http://newurl.com"); req.contextRegistrationVector.push_back(&cr); req.registrationId.set("51307b66f481db11bf8600XX"); req.duration.set("PT1M"); /* Prepare database */ prepareDatabase(); /* Invoke the function in mongoBackend library */ ms = mongoRegisterContext(&req, &res, uriParams); /* Check that every involved collection at MongoDB is as expected */ /* Note we are using EXPECT_STREQ() for some cases, as Mongo Driver returns const char*, not string * objects (see http://code.google.com/p/googletest/wiki/Primer#String_Comparison) */ DBClientBase* connection = getMongoConnection(); /* registrations collection: */ ASSERT_EQ(2, connection->count(REGISTRATIONS_COLL, BSONObj())); BSONObj reg, contextRegistration, ent0, attr0, attr1, attr2; std::vector<BSONElement> contextRegistrationV, entities, attrs; /* reg #1 (untouched) */ reg = connection->findOne(REGISTRATIONS_COLL, BSON("_id" << OID("51307b66f481db11bf860001"))); EXPECT_EQ("51307b66f481db11bf860001", reg.getField("_id").OID().toString()); EXPECT_EQ(10000000, reg.getIntField("expiration")); contextRegistrationV = reg.getField("contextRegistration").Array(); ASSERT_EQ(1, contextRegistrationV.size()); contextRegistration = contextRegistrationV[0].embeddedObject(); EXPECT_STREQ("http://cr1.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(3, attrs.size()); attr0 = attrs[0].embeddedObject(); attr1 = attrs[1].embeddedObject(); attr2 = attrs[2].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); EXPECT_STREQ("A2", C_STR_FIELD(attr1, "name")); EXPECT_STREQ("TA2", C_STR_FIELD(attr1, "type")); EXPECT_STREQ("false", C_STR_FIELD(attr1, "isDomain")); EXPECT_STREQ("A3", C_STR_FIELD(attr2, "name")); EXPECT_STREQ("TA3", C_STR_FIELD(attr2, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr2, "isDomain")); /* reg #2 (untouched) */ reg = connection->findOne(REGISTRATIONS_COLL, BSON("_id" << OID("51307b66f481db11bf860002"))); EXPECT_EQ("51307b66f481db11bf860002", reg.getField("_id").OID().toString()); EXPECT_EQ(20000000, reg.getIntField("expiration")); contextRegistrationV = reg.getField("contextRegistration").Array(); ASSERT_EQ(2, contextRegistrationV.size()); contextRegistration = contextRegistrationV[0].embeddedObject(); EXPECT_STREQ("http://cr1.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); attr0 = attrs[0].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); contextRegistration = contextRegistrationV[1].embeddedObject(); EXPECT_STREQ("http://cr2.com", C_STR_FIELD(contextRegistration, "providingApplication")); entities = contextRegistration.getField("entities").Array(); ASSERT_EQ(1, entities.size()); ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); attrs = contextRegistration.getField("attrs").Array(); ASSERT_EQ(1, attrs.size()); attr0 = attrs[0].embeddedObject(); EXPECT_STREQ("A1", C_STR_FIELD(attr0, "name")); EXPECT_STREQ("TA1", C_STR_FIELD(attr0, "type")); EXPECT_STREQ("true", C_STR_FIELD(attr0, "isDomain")); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ(0, res.duration.get().size()); EXPECT_EQ("51307b66f481db11bf8600XX", res.registrationId.get()); EXPECT_EQ(SccContextElementNotFound, res.errorCode.code); EXPECT_EQ("Registration Not Found", res.errorCode.reasonPhrase); EXPECT_EQ(0, res.errorCode.details.size()); utExit(); }
/* **************************************************************************** * * MongoDbRemoveFail - * */ TEST(mongoUnsubscribeContext, MongoDbRemoveFail) { HttpStatusCode ms; UnsubscribeContextRequest req; UnsubscribeContextResponse res; /* Prepare mocks */ const DBException e = DBException("boom!!", 33); BSONObj fakeSub = BSON("_id" << OID("51307b66f481db11bf860001") << "expiration" << 10000000 << "lastNotification" << 15000000 << "reference" << "http://notify1.me" << "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) << "attrs" << BSONArray() << "conditions" << BSONArray()); DBClientConnectionMock* connectionMock = new DBClientConnectionMock(); ON_CALL(*connectionMock, findOne("unittest.csubs",_,_,_)) .WillByDefault(Return(fakeSub)); ON_CALL(*connectionMock, remove("unittest.csubs",_,_)) .WillByDefault(Throw(e)); NotifierMock* notifierMock = new NotifierMock(); EXPECT_CALL(*notifierMock, destroyOntimeIntervalThreads(_)) .Times(0); EXPECT_CALL(*notifierMock, sendNotifyContextRequest(_,_,_,_)) .Times(0); EXPECT_CALL(*notifierMock, createIntervalThread(_,_,_)) .Times(0); setNotifier(notifierMock); /* Forge the request (from "inside" to "outside") */ req.subscriptionId.set("51307b66f481db11bf860001"); /* Prepare database */ prepareDatabase(); mongoConnect(connectionMock); /* Invoke the function in mongoBackend library */ ms = mongoUnsubscribeContext(&req, &res); /* Check response is as expected */ EXPECT_EQ(SccOk, ms); EXPECT_EQ("51307b66f481db11bf860001", res.subscriptionId.get()); EXPECT_EQ(SccReceiverInternalError, res.statusCode.code); EXPECT_EQ("Internal Server Error", res.statusCode.reasonPhrase); EXPECT_EQ("collection: unittest.csubs " "- remove() _id: 51307b66f481db11bf860001 " "- exception: boom!!", res.statusCode.details); /* Check database (untouched) */ mongoDisconnect(); // Sleeping a little to "give mongod time to process its input". usleep(1000); mongoConnect("localhost"); DBClientBase* connection = getMongoConnection(); ASSERT_EQ(2, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj())); /* Release mocks */ delete notifierMock; }
/* **************************************************************************** * * createSubscriptionNotCustomOK - */ TEST(mongoCreateSubscriptions, createSubscriptionNotCustomOK) { OrionError oe; utInit(); /* Forge input subscription */ Subscription sub; sub.description = "this is the sub"; sub.expires = 1360236300; sub.status = "active"; sub.throttling = 5; sub.attrsFormat = NGSI_V2_NORMALIZED; EntID en1("E1", "", "T1"); EntID en2("", "E.*", "T2"); sub.subject.entities.push_back(en1); sub.subject.entities.push_back(en2); sub.subject.condition.attributes.push_back("A"); sub.subject.condition.attributes.push_back("B"); sub.subject.condition.expression.q = "temperature<=20"; sub.subject.condition.expression.coords = "-40.4,-3.5;0,0"; sub.subject.condition.expression.georel = "coveredBy"; sub.subject.condition.expression.geometry = "box"; sub.notification.attributes.push_back("C"); sub.notification.attributes.push_back("D"); sub.notification.httpInfo.url = "http://foo.bar"; sub.notification.httpInfo.custom = false; /* Invoke the function in mongoBackend library */ std::string result = mongoCreateSubscription(sub, &oe, "", servicePathVector, "", ""); /* Check response is as expected */ EXPECT_EQ(SccNone, oe.code); EXPECT_EQ("", oe.reasonPhrase); EXPECT_EQ("", oe.details); DBClientBase* connection = getMongoConnection(); ASSERT_EQ(1, connection->count(SUBSCRIBECONTEXT_COLL, BSONObj())); BSONObj doc = connection->findOne(SUBSCRIBECONTEXT_COLL, BSONObj()); EXPECT_EQ(result, doc.getField("_id").OID().toString()); EXPECT_EQ(1360236300, doc.getIntField("expiration")); EXPECT_FALSE(doc.hasField("lastNotification")); EXPECT_EQ(5, doc.getIntField("throttling")); EXPECT_STREQ("http://foo.bar", C_STR_FIELD(doc, "reference")); EXPECT_STREQ("normalized", C_STR_FIELD(doc, "format")); EXPECT_STREQ("this is the sub", C_STR_FIELD(doc, "description")); EXPECT_STREQ("active", C_STR_FIELD(doc, "status")); EXPECT_FALSE(doc.getBoolField("custom")); BSONObj expression = doc.getField("expression").embeddedObject(); EXPECT_STREQ("temperature<=20", C_STR_FIELD(expression, "q")); EXPECT_STREQ("-40.4,-3.5;0,0", C_STR_FIELD(expression, "coords")); EXPECT_STREQ("coveredBy", C_STR_FIELD(expression, "georel")); EXPECT_STREQ("box", C_STR_FIELD(expression, "geometry")); std::vector<BSONElement> entities = doc.getField("entities").Array(); ASSERT_EQ(2, entities.size()); BSONObj ent0 = entities[0].embeddedObject(); EXPECT_STREQ("E1", C_STR_FIELD(ent0, "id")); EXPECT_STREQ("T1", C_STR_FIELD(ent0, "type")); EXPECT_STREQ("false", C_STR_FIELD(ent0, "isPattern")); BSONObj ent1 = entities[1].embeddedObject(); EXPECT_STREQ("E.*", C_STR_FIELD(ent1, "id")); EXPECT_STREQ("T2", C_STR_FIELD(ent1, "type")); EXPECT_STREQ("true", C_STR_FIELD(ent1, "isPattern")); std::vector<BSONElement> attrs = doc.getField("attrs").Array(); EXPECT_EQ(2, attrs.size()); EXPECT_EQ("C", attrs[0].String()); EXPECT_EQ("D", attrs[1].String()); std::vector<BSONElement> conds = doc.getField("conditions").Array(); ASSERT_EQ(1, conds.size()); BSONObj cond0 = conds[0].embeddedObject(); EXPECT_STREQ("ONCHANGE", C_STR_FIELD(cond0, "type")); std::vector<BSONElement> condValues = cond0.getField("value").Array(); ASSERT_EQ(2, condValues.size()); EXPECT_EQ("A", condValues[0].String()); EXPECT_EQ("B", condValues[1].String()); utExit(); }