Skip to content

Commit

Permalink
FIX utest aligned with latests changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Sep 21, 2018
1 parent d6acf7b commit a5dcf0f
Show file tree
Hide file tree
Showing 52 changed files with 3,875 additions and 3,279 deletions.
1 change: 0 additions & 1 deletion test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ SET (SOURCES
ngsi10/UpdateContextSubscriptionResponse_test.cpp

ngsi/ContextAttribute_test.cpp
ngsi/ContextElement_test.cpp
ngsi/NotifyCondition_test.cpp
ngsi/AttributeList_test.cpp
ngsi/ConditionValueList_test.cpp
Expand Down
70 changes: 63 additions & 7 deletions test/unittests/apiTypesV2/Entity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
*
* Author: Fermin Galan
*/

#include <string>
#include <vector>

#include "apiTypesV2/Entity.h"
#include "common/errorMessages.h"
#include "unittests/unittest.h"

#include "ngsi/ContextElementVector.h"



/* ****************************************************************************
Expand All @@ -45,26 +51,76 @@ TEST(Entity, check)
ContextAttribute* caP = new ContextAttribute("A", "T", "val");
enP->attributeVector.push_back(caP);

EXPECT_EQ("OK", enP->check(EntitiesRequest));
EXPECT_EQ("OK", enP->check(V2, EntitiesRequest));

enP->id = "";
EXPECT_EQ("entity id length: 0, min length supported: 1", enP->check(EntitiesRequest));
EXPECT_EQ("entity id length: 0, min length supported: 1", enP->check(V2, EntitiesRequest));

enP->id = "E<1>";
EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID, enP->check(EntitiesRequest));
EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTID, enP->check(V2, EntitiesRequest));
enP->isPattern = "true";
EXPECT_EQ("OK", enP->check(EntitiesRequest));
EXPECT_EQ("OK", enP->check(V2, EntitiesRequest));
enP->id = "E";
enP->isPattern = "false";

enP->type = "T<1>";
EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE, enP->check(EntitiesRequest));
EXPECT_EQ(ERROR_DESC_BAD_REQUEST_INVALID_CHAR_ENTTYPE, enP->check(V2, EntitiesRequest));
enP->isTypePattern = true;
EXPECT_EQ("OK", enP->check(EntitiesRequest));
EXPECT_EQ("OK", enP->check(V2, EntitiesRequest));
enP->type = "T";

enP->isPattern = "<false>";
EXPECT_EQ("Invalid value for isPattern", enP->check(EntitiesRequest));
EXPECT_EQ("Invalid value for isPattern", enP->check(V2, EntitiesRequest));

utExit();
}


/* ****************************************************************************
*
* Check -
*
* Test ported from old Context Element class test
*/
TEST(Entity, checkV1)
{
Entity* enP = new Entity();

utInit();

enP->id = "";
EXPECT_EQ(enP->check(V1, UpdateContext), "empty entityId:id");

enP->id = "id";
EXPECT_EQ(enP->check(V1, UpdateContext), "OK");

ContextAttribute* aP = new ContextAttribute();
aP->name = "";
aP->stringValue = "V";
enP->attributeVector.push_back(aP);
EXPECT_EQ(enP->check(V1, UpdateContext), "missing attribute name");
aP->name = "name";

Entity* en2P = new Entity("id", "", "false");

ContextElementVector* ceVectorP = new ContextElementVector();

EXPECT_EQ(ceVectorP->check(V1, UpdateContext), "No context elements");

ceVectorP->push_back(enP);
ceVectorP->push_back(en2P);
EXPECT_EQ(ceVectorP->check(V1, UpdateContext), "OK");

// render
const char* outfile1 = "ngsi.contextelement.check.middle.json";
std::string out;
std::vector<std::string> emptyV;

out = en2P->toJsonV1(false, UpdateContextElement, emptyV, false, emptyV, false, false);
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
EXPECT_STREQ(expectedBuf, out.c_str());

EXPECT_EQ("OK", ceVectorP->check(V1, UpdateContext));

utExit();
}
25 changes: 12 additions & 13 deletions test/unittests/common/commonMacroSubstitute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

#include "logMsg/logMsg.h"
#include "logMsg/traceLevels.h"
#include "ngsi/ContextElement.h"
#include "ngsi/ContextAttribute.h"

#include "common/macroSubstitute.h"
Expand All @@ -39,17 +38,17 @@
*/
TEST(commonMacroSubstitute, simple)
{
ContextElement ce("E1", "T1", "false");
Entity en("E1", "T1", "false");
ContextAttribute* caP = new ContextAttribute("A1", "T1", "attr1");
bool b;

ce.contextAttributeVector.push_back(caP);
en.attributeVector.push_back(caP);

const char* s1 = "Entity ${id}/${type}, attribute '${A1}'";
const char* correct = "Entity E1/T1, attribute 'attr1'";
std::string result;

b = macroSubstitute(&result, s1, ce);
b = macroSubstitute(&result, s1, en);
EXPECT_TRUE(b);
EXPECT_STREQ(correct, result.c_str());
}
Expand All @@ -62,11 +61,11 @@ TEST(commonMacroSubstitute, simple)
*/
TEST(commonMacroSubstitute, withRealloc)
{
ContextElement ce("E1", "T1", "false");
Entity en("E1", "T1", "false");
ContextAttribute* caP = new ContextAttribute("A1", "T1", "attr1");
bool b;

ce.contextAttributeVector.push_back(caP);
en.attributeVector.push_back(caP);

const char* base =
"The test string needs to be longer than 1024 chars to test reallocs in Macro substitution"
Expand All @@ -86,7 +85,7 @@ TEST(commonMacroSubstitute, withRealloc)
std::string correct = std::string(base) + "Now, finally something to substitute: Entity E1/T1, attribute 'attr1'";
std::string result;

b = macroSubstitute(&result, s1, ce);
b = macroSubstitute(&result, s1, en);
EXPECT_TRUE(b);
EXPECT_STREQ(correct.c_str(), result.c_str());
}
Expand All @@ -102,10 +101,10 @@ TEST(commonMacroSubstitute, withRealloc)
TEST(commonMacroSubstitute, bufferTooBigInitially)
{
bool b;
ContextElement ce("EntityId000001", "EntityType000001", "false");
Entity en("EntityId000001", "EntityType000001", "false");
ContextAttribute* caP = new ContextAttribute("A1", "T1", "attr1");

ce.contextAttributeVector.push_back(caP);
en.attributeVector.push_back(caP);

char* base = (char*) malloc(MAX_DYN_MSG_SIZE + 2);

Expand All @@ -116,7 +115,7 @@ TEST(commonMacroSubstitute, bufferTooBigInitially)
// correct = std::string(base) + "EntityId000001/EntityType000001";
std::string result;

b = macroSubstitute(&result, s1, ce);
b = macroSubstitute(&result, s1, en);
EXPECT_FALSE(b);
EXPECT_STREQ("", result.c_str());

Expand All @@ -137,10 +136,10 @@ TEST(commonMacroSubstitute, bufferTooBigInitially)
TEST(commonMacroSubstitute, bufferTooBigAfterSubstitution)
{
bool b;
ContextElement ce("EntityId000001", "EntityType000001", "false");
Entity en("EntityId000001", "EntityType000001", "false");
ContextAttribute* caP = new ContextAttribute("A1", "T1", "attr1");

ce.contextAttributeVector.push_back(caP);
en.attributeVector.push_back(caP);

char* base = (char*) malloc(MAX_DYN_MSG_SIZE + 2 - 16); // -16 so that '${id}/${type}' fits inside 8MB
// but 'EntityId000001/EntityType000001' does not
Expand All @@ -152,7 +151,7 @@ TEST(commonMacroSubstitute, bufferTooBigAfterSubstitution)
// correct = std::string(base) + "EntityId000001/EntityType000001"; // > 8MB after substitutions
std::string result;

b = macroSubstitute(&result, s1, ce);
b = macroSubstitute(&result, s1, en);
EXPECT_FALSE(b);
EXPECT_STREQ("", result.c_str());

Expand Down
20 changes: 16 additions & 4 deletions test/unittests/commonMocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,14 @@ class NotifierMock : public Notifier
*/
}

MOCK_METHOD7(sendNotifyContextRequest, void(NotifyContextRequest* ncr,
MOCK_METHOD9(sendNotifyContextRequest, void(NotifyContextRequest& ncr,
const ngsiv2::HttpInfo& httpInfo,
const std::string& tenant,
const std::string& xauthToken,
const std::string& fiwareCorrelator,
RenderFormat renderFormat,
const std::vector<std::string>& attrsFilter,
bool blacklist,
const std::vector<std::string>& metadataFilter));

MOCK_METHOD5(sendNotifyContextAvailabilityRequest, void(NotifyContextAvailabilityRequest* ncar,
Expand All @@ -238,15 +240,25 @@ class NotifierMock : public Notifier
RenderFormat renderFormat));

/* Wrappers for parent methods (used in ON_CALL() defaults set in the constructor) */
void parent_sendNotifyContextRequest(NotifyContextRequest* ncr,
void parent_sendNotifyContextRequest(NotifyContextRequest& ncr,
const ngsiv2::HttpInfo& httpInfo,
const std::string& tenant,
const std::string& xauthToken,
const std::string& fiwareCorrelator,
RenderFormat renderFormat,
const std::vector<std::string>& attrsFilter,
bool blacklist,
const std::vector<std::string>& metadataFilter)
{
Notifier::sendNotifyContextRequest(ncr, httpInfo, tenant, xauthToken, fiwareCorrelator, renderFormat, metadataFilter);
Notifier::sendNotifyContextRequest(ncr,
httpInfo,
tenant,
xauthToken,
fiwareCorrelator,
renderFormat,
attrsFilter,
blacklist,
metadataFilter);
}

void parent_sendNotifyContextAvailabilityRequest(NotifyContextAvailabilityRequest* ncar,
Expand Down Expand Up @@ -296,7 +308,7 @@ class TimerMock : public Timer
* Eq() matcher. FIXME */
MATCHER_P(MatchNcr, expected, "")
{
return matchNotifyContextRequest(expected, arg);
return matchNotifyContextRequest(expected, &arg);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include "common/MimeType.h"
#include "convenience/AppendContextElementRequest.h"
#include "ngsi/ContextElement.h"
#include "ngsi/Metadata.h"
#include "rest/ConnectionInfo.h"

Expand All @@ -51,7 +50,7 @@ TEST(AppendContextElementRequest, render_json)

acer.contextAttributeVector.push_back(&ca);

out = acer.render(V1, false, UpdateContext);
out = acer.render(false, UpdateContext);

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
EXPECT_STREQ(expectedBuf, out.c_str());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ TEST(AppendContextElementResponse, render_json)
utInit();

// 1. empty acer
out = acer.render(V1, false, AppendContextElement);
out = acer.render(false, AppendContextElement);
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
EXPECT_STREQ(expectedBuf, out.c_str());

// 2. errorCode 'active'
acer.errorCode.fill(SccBadRequest, "very bad request");
out = acer.render(V1, false, AppendContextElement);
out = acer.render(false, AppendContextElement);
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
EXPECT_STREQ(expectedBuf, out.c_str());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ TEST(ContextAttributeResponseVector, render_json)

// 1. empty vector
car.statusCode.fill(SccBadRequest, "Empty Vector");
out = carV.render(V1, false, ContextEntityAttributes);
out = carV.render(false, ContextEntityAttributes);
EXPECT_STREQ("", out.c_str());

// 2. normal case
car.contextAttributeVector.push_back(&ca);
carV.push_back(&car);

out = carV.render(V1, false, ContextEntityAttributes);
out = carV.render(false, ContextEntityAttributes);
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
EXPECT_STREQ(expectedBuf, out.c_str());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST(UpdateContextElementRequest, render_json)
// Just the normal case
ucer.contextAttributeVector.push_back(&ca);

out = ucer.render(V1, false, UpdateContext);
out = ucer.render(false, UpdateContext);
EXPECT_STREQ(expectedBuf, out.c_str());

utExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST(UpdateContextElementResponse, render_json)
car.contextAttributeVector.push_back(&ca);
car.statusCode.fill(SccOk, "details");

out = ucer.render(V1, false, UpdateContext);
out = ucer.render(false, UpdateContext);
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
EXPECT_STREQ(expectedBuf, out.c_str());
}
Expand Down
Loading

0 comments on commit a5dcf0f

Please sign in to comment.