Skip to content

Commit

Permalink
Merge pull request #426 from telefonicaid/bug/pagination_details_on_n…
Browse files Browse the repository at this point in the history
…ot_rendering

Fix bug related with pagination
  • Loading branch information
LeandroGuillen committed Jun 23, 2014
2 parents c76b3b1 + f57819b commit cd972a5
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 42 deletions.
22 changes: 22 additions & 0 deletions src/lib/ngsi10/QueryContextResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ std::string QueryContextResponse::render(RequestType requestType, Format format,

out += startTag(indent, tag, format, false);

if (contextElementResponseVector.size() > 0)
{
bool commaNeeded = (errorCode.code != SccNone);
out += contextElementResponseVector.render(QueryContext, format, indent + " ", commaNeeded);
}

if (errorCode.code != SccNone)
{
out += errorCode.render(format, indent + " ");
}

/* Safety check: neither errorCode nor CER vector was filled by mongoBackend */
if (errorCode.code == SccNone && contextElementResponseVector.size() == 0)
{
errorCode.fill(SccReceiverInternalError, "Both the error-code structure and the response vector were empty");
out += errorCode.render(format, indent + " ");
}

#if 0
// I needed to adjust rednder function for details=on to work. Ken, please review that this code can be safely removed, after the
// above re-factoring
if ((errorCode.code == SccNone) || (errorCode.code == SccOk))
{
if (contextElementResponseVector.size() == 0)
Expand All @@ -89,6 +110,7 @@ std::string QueryContextResponse::render(RequestType requestType, Format format,
}
else
out += errorCode.render(format, indent + " ");
#endif

out += endTag(indent, tag, format);

Expand Down
24 changes: 23 additions & 1 deletion src/lib/ngsi9/DiscoverContextAvailabilityResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,33 @@ std::string DiscoverContextAvailabilityResponse::render(RequestType requestType,
// no JSON commas necessary
//
out += startTag(indent, tag, format, false);


if (responseVector.size() > 0)
{
bool commaNeeded = (errorCode.code != SccNone);
out += responseVector.render(format, indent + " ", commaNeeded);
}

if (errorCode.code != SccNone)
{
out += errorCode.render(format, indent + " ", false);
}

/* Safety check: neither errorCode nor CER vector was filled by mongoBackend */
if (errorCode.code == SccNone && responseVector.size() == 0)
{
errorCode.fill(SccReceiverInternalError, "Both the error-code structure and the response vector were empty");
out += errorCode.render(format, indent + " ");
}

#if 0
// I needed to adjust rednder function for details=on to work. Ken, please review that this code can be safely removed, after the
// above re-factoring
if (errorCode.code == SccNone)
out += responseVector.render(format, indent + " ", false);
else
out += errorCode.render(format, indent + " ", false);
#endif

out += endTag(indent, tag, format);

Expand Down
13 changes: 5 additions & 8 deletions test/unittests/ngsi10/QueryContextResponse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@ TEST(QueryContextResponse, ok_xml)
{
StatusCode* ecP = new StatusCode(SccOk, "Detail");
StatusCode ec(SccOk, "Detail2");
QueryContextResponse qcr1;
QueryContextResponse qcr2(ec);
QueryContextResponse qcr(ec);
std::string out;
const char* outfile = "ngsi10.queryContextResponse.notFound.valid.xml";
const char* outfile = "ngsi10.queryContextResponse.ok.valid.xml";

utInit();

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
out = qcr1.render(QueryContext, XML, "");
EXPECT_STREQ(expectedBuf, out.c_str());

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
out = qcr2.render(QueryContext, XML, "");
out = qcr.render(QueryContext, XML, "");
EXPECT_STREQ(expectedBuf, out.c_str());

delete ecP;
Expand Down Expand Up @@ -212,6 +207,7 @@ TEST(QueryContextResponse, json_render)
qcrP->errorCode.fill(SccOk);

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename12)) << "Error getting test data from '" << filename12 << "'";
qcrP->errorCode.code = SccNone;
out = qcrP->render(QueryContext, JSON, "");
EXPECT_STREQ(expectedBuf, out.c_str());

Expand All @@ -220,6 +216,7 @@ TEST(QueryContextResponse, json_render)
qcrP->errorCode.fill(SccBadRequest, "no details");

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename13)) << "Error getting test data from '" << filename13 << "'";
qcrP->contextElementResponseVector.release();
out = qcrP->render(QueryContext, JSON, "");
EXPECT_STREQ(expectedBuf, out.c_str());

Expand Down
62 changes: 39 additions & 23 deletions test/unittests/ngsi9/DiscoverContextAvailabilityResponse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST(DiscoverContextAvailabilityResponse, render)
{
DiscoverContextAvailabilityResponse dcar1;
std::string out;
const char* outfile1 = "ngsi9.discoverContextAvailabilityResponse.empty.invalid.xml";
const char* outfile1 = "ngsi9.discoverContextAvailabilityResponse.empty.valid.xml";
const char* outfile2 = "ngsi9.discoverContextAvailabilityResponse.error.valid.xml";
StatusCode ec(SccBadRequest, "Detail");
DiscoverContextAvailabilityResponse dcar2(ec);
Expand All @@ -58,7 +58,7 @@ TEST(DiscoverContextAvailabilityResponse, render)
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
out = dcar1.render(DiscoverContextAvailability, XML, "");
EXPECT_STREQ(expectedBuf, out.c_str());
EXPECT_EQ(SccNone, dcar1.errorCode.code);
EXPECT_EQ(SccReceiverInternalError, dcar1.errorCode.code);

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
out = dcar2.render(DiscoverContextAvailability, XML, "");
Expand Down Expand Up @@ -99,6 +99,7 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
const char* filename18 = "ngsi9.discoverContextAvailabilityResponse.jsonRender18.valid.json";
const char* filename19 = "ngsi9.discoverContextAvailabilityResponse.jsonRender19.valid.json";
const char* filename20 = "ngsi9.discoverContextAvailabilityResponse.jsonRender20.valid.json";
const char* emptyFilename = "ngsi9.discoverContextAvailabilityResponse.jsonRender.empty.valid.json";
std::string rendered;
DiscoverContextAvailabilityResponse* dcarP = new DiscoverContextAvailabilityResponse();
ContextRegistrationResponse* crrP;
Expand All @@ -121,8 +122,11 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());
EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests
free(dcarP);


Expand Down Expand Up @@ -153,9 +157,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 4. ContextRegistration: One entityId inside entityIdVector
Expand Down Expand Up @@ -183,9 +188,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 6. ContextRegistration: one attribute in contextRegistrationAttributeVector
Expand Down Expand Up @@ -214,9 +220,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 8. ContextRegistration: one metadata in registrationMetadataVector
Expand Down Expand Up @@ -245,9 +252,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 10. !entityIdVector !contextRegistrationAttributeVector !registrationMetadataVector +providingApplication
Expand All @@ -274,9 +282,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 12. !entityIdVector +contextRegistrationAttributeVector !registrationMetadataVector +providingApplication
Expand Down Expand Up @@ -304,9 +313,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 14. +entityIdVector !contextRegistrationAttributeVector !registrationMetadataVector +providingApplication
Expand Down Expand Up @@ -347,9 +357,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 16. +entityIdVector +contextRegistrationAttributeVector !registrationMetadataVector +providingApplication
Expand All @@ -367,9 +378,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 18. StatusCode
Expand All @@ -381,8 +393,9 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)

dcarP->release();
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());

EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 19. StatusCode
Expand All @@ -393,8 +406,10 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());
EXPECT_STREQ(expectedBuf, rendered.c_str());
dcarP->release(); // ... otherwise the 500 remains and "pollutes" next tests


// 20. Two ContextRegistrationResponses
Expand All @@ -417,8 +432,9 @@ TEST(DiscoverContextAvailabilityResponse, jsonRender)
EXPECT_STREQ(expectedBuf, rendered.c_str());

dcarP->release();
EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), emptyFilename)) << "Error getting test data from '" << emptyFilename << "'";
rendered = dcarP->render(DiscoverContextAvailability, JSON, "");
EXPECT_STREQ(EMPTY_JSON, rendered.c_str());
EXPECT_STREQ(expectedBuf, rendered.c_str());

free(dcarP);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<queryContextResponse>
<errorCode>
<code>200</code>
<reasonPhrase>OK</reasonPhrase>
<details>Detail2</details>
</errorCode>
</queryContextResponse>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<queryContextResponse>
<errorCode>
<code>404</code>
<reasonPhrase>No context element found</reasonPhrase>
<code>200</code>
<reasonPhrase>OK</reasonPhrase>
<details>detail</details>
</errorCode>
</queryContextResponse>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<discoverContextAvailabilityResponse>
<errorCode>
<code>500</code>
<reasonPhrase>Internal Server Error</reasonPhrase>
<details>Both the error-code structure and the response vector were empty</details>
</errorCode>
</discoverContextAvailabilityResponse>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"errorCode" : {
"code" : "500",
"reasonPhrase" : "Internal Server Error",
"details" : "Both the error-code structure and the response vector were empty"
}
}

0 comments on commit cd972a5

Please sign in to comment.