Skip to content

Commit

Permalink
Unit test added for 3 of 4 cases in telefonicaid#1142:
Browse files Browse the repository at this point in the history
 - Existing location attribute L1, replace including L1
 - Existing location attribute L1, replace no including any location
attribute
 - Not existing location attribute, replace including location attribute
  • Loading branch information
arigliano committed Sep 3, 2018
1 parent 7ff57b2 commit 2ccf424
Showing 1 changed file with 307 additions and 0 deletions.
307 changes: 307 additions & 0 deletions test/unittests/mongoBackend/mongoUpdateContextGeo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,313 @@ TEST(mongoUpdateContextGeoRequest, updateLocAttribute)
utExit();
}

/* ****************************************************************************
*
* - replaceLocAttributeWithLocAttribute
*/
TEST(mongoUpdateContextGeoRequest, replaceLocAttributeWithLocAttribute)
{
utInit();

HttpStatusCode ms;
UpdateContextRequest req;
UpdateContextResponse res;

/* Prepare database */
prepareDatabase();

/* Forge the request (from "inside" to "outside") */
ContextElement* ceP = new ContextElement();
ceP->entityId.fill("E1", "T1", "false");
ContextAttribute* caP = new ContextAttribute("A1", "geo:point", "2, -4");
caP->typeGiven = true;
ceP->contextAttributeVector.push_back(caP);
req.contextElementVector.push_back(ceP);
req.updateActionType = ActionTypeReplace;

/* Invoke the function in mongoBackend library */
servicePathVector.clear();
ms = mongoUpdateContext(&req, &res, "", servicePathVector, uriParams, "", "", "", V2);

/* Check response is as expected */
EXPECT_EQ(SccOk, ms);

EXPECT_EQ(SccOk, res.errorCode.code);
EXPECT_EQ("OK", res.errorCode.reasonPhrase);
EXPECT_EQ("", res.errorCode.details);

ASSERT_EQ(1, res.contextElementResponseVector.size());
/* Context Element response # 1 */
EXPECT_EQ("E1", RES_CER(0).entityId.id);
EXPECT_EQ("T1", RES_CER(0).entityId.type);
EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
ASSERT_EQ(1, RES_CER(0).contextAttributeVector.size());
EXPECT_EQ("A1", RES_CER_ATTR(0, 0)->name);
EXPECT_EQ("geo:point", RES_CER_ATTR(0, 0)->type);
EXPECT_EQ(0, RES_CER_ATTR(0, 0)->stringValue.size());
ASSERT_EQ(0, RES_CER_ATTR(0, 0)->metadataVector.size());
EXPECT_EQ(SccOk, RES_CER_STATUS(0).code);
EXPECT_EQ("OK", RES_CER_STATUS(0).reasonPhrase);
EXPECT_EQ("", RES_CER_STATUS(0).details);

/* 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, attrs;
std::vector<BSONElement> attrNames;
ASSERT_EQ(2, 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("creDate"));
EXPECT_EQ(1360232700, ent.getIntField("modDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(1, attrs.nFields());
ASSERT_EQ(1, attrNames.size());
BSONObj a1 = attrs.getField("A1").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A1"));
EXPECT_STREQ("geo:point", C_STR_FIELD(a1, "type"));
EXPECT_STREQ("2, -4", C_STR_FIELD(a1, "value"));
EXPECT_TRUE(a1.hasField("creDate"));
EXPECT_EQ(1360232700, a1.getIntField("modDate"));
EXPECT_STREQ("A1", ent.getObjectField("location").getStringField("attrName"));
ASSERT_TRUE(ent.hasField("location"));
EXPECT_EQ(-4, coordX(ent));
EXPECT_EQ(2, coordY(ent));

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"));
EXPECT_FALSE(ent.hasField("creDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(1, attrs.nFields());
ASSERT_EQ(1, attrNames.size());
BSONObj a2 = attrs.getField("A2").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A2"));
EXPECT_STREQ("TA2", C_STR_FIELD(a2, "type"));
EXPECT_STREQ("Y", C_STR_FIELD(a2, "value"));
EXPECT_FALSE(a2.hasField("creDate"));
EXPECT_FALSE(a2.hasField("modDate"));
EXPECT_FALSE(ent.hasField("location"));

utExit();
}

/* ****************************************************************************
*
* - replaceLocAttributeWithNoLocAttribute
*/
TEST(mongoUpdateContextGeoRequest, replaceLocAttributeWithNoLocAttribute)
{
utInit();

HttpStatusCode ms;
UpdateContextRequest req;
UpdateContextResponse res;

/* Prepare database */
prepareDatabase();

/* Forge the request (from "inside" to "outside") */
ContextElement* ceP = new ContextElement();
ceP->entityId.fill("E1", "T1", "false");
ContextAttribute* caP = new ContextAttribute("A1", "TA1", "noLoc");
caP->typeGiven = true;
ceP->contextAttributeVector.push_back(caP);
req.contextElementVector.push_back(ceP);
req.updateActionType = ActionTypeReplace;

/* Invoke the function in mongoBackend library */
servicePathVector.clear();
ms = mongoUpdateContext(&req, &res, "", servicePathVector, uriParams, "", "", "", V2);

/* Check response is as expected */
EXPECT_EQ(SccOk, ms);

EXPECT_EQ(SccOk, res.errorCode.code);
EXPECT_EQ("OK", res.errorCode.reasonPhrase);
EXPECT_EQ("", res.errorCode.details);

ASSERT_EQ(1, res.contextElementResponseVector.size());
/* Context Element response # 1 */
EXPECT_EQ("E1", RES_CER(0).entityId.id);
EXPECT_EQ("T1", RES_CER(0).entityId.type);
EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
ASSERT_EQ(1, RES_CER(0).contextAttributeVector.size());
EXPECT_EQ("A1", RES_CER_ATTR(0, 0)->name);
EXPECT_EQ("TA1", RES_CER_ATTR(0, 0)->type);
EXPECT_EQ(0, RES_CER_ATTR(0, 0)->stringValue.size());
ASSERT_EQ(0, RES_CER_ATTR(0, 0)->metadataVector.size());
EXPECT_EQ(SccOk, RES_CER_STATUS(0).code);
EXPECT_EQ("OK", RES_CER_STATUS(0).reasonPhrase);
EXPECT_EQ("", RES_CER_STATUS(0).details);

/* 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, attrs;
std::vector<BSONElement> attrNames;
ASSERT_EQ(2, 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("creDate"));
EXPECT_EQ(1360232700, ent.getIntField("modDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(1, attrs.nFields());
ASSERT_EQ(1, attrNames.size());
BSONObj a1 = attrs.getField("A1").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A1"));
EXPECT_STREQ("TA1", C_STR_FIELD(a1, "type"));
EXPECT_STREQ("noLoc", C_STR_FIELD(a1, "value"));
EXPECT_TRUE(a1.hasField("creDate"));
EXPECT_EQ(1360232700, a1.getIntField("modDate"));
ASSERT_FALSE(ent.hasField("location"));

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"));
EXPECT_FALSE(ent.hasField("creDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(1, attrs.nFields());
ASSERT_EQ(1, attrNames.size());
BSONObj a2 = attrs.getField("A2").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A2"));
EXPECT_STREQ("TA2", C_STR_FIELD(a2, "type"));
EXPECT_STREQ("Y", C_STR_FIELD(a2, "value"));
EXPECT_FALSE(a2.hasField("creDate"));
EXPECT_FALSE(a2.hasField("modDate"));
EXPECT_FALSE(ent.hasField("location"));

utExit();
}

/* ****************************************************************************
*
* - replaceNoLocAttributeWithLocAttribute
*/
TEST(mongoUpdateContextGeoRequest, replaceNoLocAttributeWithLocAttribute)
{
utInit();

HttpStatusCode ms;
UpdateContextRequest req;
UpdateContextResponse res;

/* Prepare database */
prepareDatabase();

/* Forge the request (from "inside" to "outside") */
ContextElement* ceP = new ContextElement();
ceP->entityId.fill("E2", "T2", "false");
ContextAttribute* caP = new ContextAttribute("A2", "geo:point", "2, -4");
caP->typeGiven = true;
ceP->contextAttributeVector.push_back(caP);
req.contextElementVector.push_back(ceP);
req.updateActionType = ActionTypeReplace;

/* Invoke the function in mongoBackend library */
servicePathVector.clear();
ms = mongoUpdateContext(&req, &res, "", servicePathVector, uriParams, "", "", "", V2);

/* Check response is as expected */
EXPECT_EQ(SccOk, ms);

EXPECT_EQ(SccOk, res.errorCode.code);
EXPECT_EQ("OK", res.errorCode.reasonPhrase);
EXPECT_EQ("", res.errorCode.details);

ASSERT_EQ(1, res.contextElementResponseVector.size());
/* Context Element response # 1 */
EXPECT_EQ("E2", RES_CER(0).entityId.id);
EXPECT_EQ("T2", RES_CER(0).entityId.type);
EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
ASSERT_EQ(1, RES_CER(0).contextAttributeVector.size());
EXPECT_EQ("A2", RES_CER_ATTR(0, 0)->name);
EXPECT_EQ("geo:point", RES_CER_ATTR(0, 0)->type);
EXPECT_EQ(0, RES_CER_ATTR(0, 0)->stringValue.size());
ASSERT_EQ(0, RES_CER_ATTR(0, 0)->metadataVector.size());
EXPECT_EQ(SccOk, RES_CER_STATUS(0).code);
EXPECT_EQ("OK", RES_CER_STATUS(0).reasonPhrase);
EXPECT_EQ("", RES_CER_STATUS(0).details);

/* 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, attrs;
std::vector<BSONElement> attrNames;
ASSERT_EQ(2, 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("creDate"));
EXPECT_FALSE(ent.hasField("creDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(2, attrs.nFields());
ASSERT_EQ(2, attrNames.size());
BSONObj a1 = attrs.getField("A1").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A1"));
EXPECT_STREQ("TA1", C_STR_FIELD(a1, "type"));
EXPECT_STREQ("-5, 2", C_STR_FIELD(a1, "value"));
EXPECT_FALSE(a1.hasField("creDate"));
EXPECT_FALSE(a1.hasField("modDate"));
BSONObj a2 = attrs.getField("A2").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A2"));
EXPECT_STREQ("TA2", C_STR_FIELD(a2, "type"));
EXPECT_STREQ("noloc", C_STR_FIELD(a2, "value"));
EXPECT_FALSE(a2.hasField("creDate"));
EXPECT_FALSE(a2.hasField("modDate"));
EXPECT_STREQ("A1", ent.getObjectField("location").getStringField("attrName"));
ASSERT_TRUE(ent.hasField("location"));
EXPECT_EQ(2, coordX(ent));
EXPECT_EQ(-5, coordY(ent));

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_EQ(1360232700, ent.getIntField("modDate"));
EXPECT_FALSE(ent.hasField("creDate"));
attrs = ent.getField("attrs").embeddedObject();
attrNames = ent.getField("attrNames").Array();
ASSERT_EQ(1, attrs.nFields());
ASSERT_EQ(1, attrNames.size());
a2 = attrs.getField("A2").embeddedObject();
EXPECT_TRUE(findAttr(attrNames, "A2"));
EXPECT_STREQ("geo:point", C_STR_FIELD(a2, "type"));
EXPECT_STREQ("2, -4", C_STR_FIELD(a2, "value"));
EXPECT_TRUE(a2.hasField("creDate"));
EXPECT_EQ(1360232700, a2.getIntField("modDate"));
EXPECT_STREQ("A2", ent.getObjectField("location").getStringField("attrName"));
ASSERT_TRUE(ent.hasField("location"));
EXPECT_EQ(-4, coordX(ent));
EXPECT_EQ(2, coordY(ent));


utExit();
}

/* ****************************************************************************
*
* - deleteLocAttribute
Expand Down

0 comments on commit 2ccf424

Please sign in to comment.