Skip to content

Commit

Permalink
Fixes Issue telefonicaid#2948 - Wrong "max one service-path..." Response
Browse files Browse the repository at this point in the history
Implemented toJson() method in SubscribeContextResponse, SubscribeError
and SubscriptionId
  • Loading branch information
arigliano committed Oct 31, 2017
1 parent a2acb52 commit fd41125
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/lib/ngsi/SubscribeError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,45 @@ SubscribeError::SubscribeError()



/* ****************************************************************************
*
* SubscribeError::toJson -
*/
std::string SubscribeError::toJson(RequestType requestType, bool comma)
{
std::string out;

out += JSON_VALUE("error", errorCode.reasonPhrase.c_str());
out += ",";
out += JSON_VALUE("description", errorCode.details.c_str());


if (requestType == UpdateContextSubscription)
{
//
// NOTE: the subscriptionId must have come from the request.
// If the field is empty, we are in unit tests and I here set it to all zeroes
//
if (subscriptionId.get() == "")
{
subscriptionId.set("000000000000000000000000");
}
out += ",";
out += JSON_PROP("affectedItems") + "[" + JSON_STR(subscriptionId.toJson(requestType, true)) + "]";
}
else if ((requestType == SubscribeContext) &&
(subscriptionId.get() != "000000000000000000000000") &&
(subscriptionId.get() != ""))
{
out += ",";
out += JSON_PROP("affectedItems") + "[" + JSON_STR(subscriptionId.toJson(requestType, true)) + "]";
}

return out;
}



/* ****************************************************************************
*
* SubscribeError::render -
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ngsi/SubscribeError.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct SubscribeError

SubscribeError();
std::string render(RequestType requestType, bool comma);

std::string toJson(RequestType requestType, bool comma);
std::string check(void);
} SubscribeError;

Expand Down
34 changes: 34 additions & 0 deletions src/lib/ngsi/SubscriptionId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,40 @@ void SubscriptionId::present(const std::string& indent)



/* ****************************************************************************
*
* SubscriptionId::toJson -
*/
std::string SubscriptionId::toJson(RequestType container, bool comma)
{
std::string xString = string;

if (xString == "")
{
if ((container == RtSubscribeContextAvailabilityResponse) ||
(container == RtUpdateContextAvailabilitySubscriptionResponse) ||
(container == RtUnsubscribeContextAvailabilityResponse) ||
(container == NotifyContextAvailability) ||
(container == UpdateContextSubscription) ||
(container == UnsubscribeContext) ||
(container == RtUnsubscribeContextResponse) ||
(container == NotifyContext) ||
(container == RtSubscribeResponse) ||
(container == RtSubscribeError))
{
// subscriptionId is Mandatory
xString = "000000000000000000000000";
}
else
{
return ""; // subscriptionId is Optional
}
}

return xString;
}


/* ****************************************************************************
*
* SubscriptionId::render -
Expand Down
1 change: 1 addition & 0 deletions src/lib/ngsi/SubscriptionId.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct SubscriptionId
const char* c_str(void) const;
bool isEmpty(void);
std::string render(RequestType container,bool comma);
std::string toJson(RequestType container,bool comma);
void present(const std::string& indent);
void release(void);
bool rendered(RequestType container);
Expand Down
24 changes: 24 additions & 0 deletions src/lib/ngsi10/SubscribeContextResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ SubscribeContextResponse::SubscribeContextResponse(StatusCode& errorCode)
subscribeError.errorCode.keyNameSet("errorCode");
}

/* ****************************************************************************
*
* SubscribeContextResponse::toJson -
*/
std::string SubscribeContextResponse::toJson(void)
{
std::string out = "";

out += "{" ;

if (subscribeError.errorCode.code == SccNone)
{
out += subscribeResponse.render(false);
}
else
{
out += subscribeError.toJson(SubscribeContext, false);
}

out += "}";

return out;
}

/* ****************************************************************************
*
* SubscribeContextResponse::render -
Expand Down
1 change: 1 addition & 0 deletions src/lib/ngsi10/SubscribeContextResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct SubscribeContextResponse
~SubscribeContextResponse();

std::string render(void);
std::string toJson(void);
} SubscribeContextResponse;

#endif // SRC_LIB_NGSI10_SUBSCRIBECONTEXTRESPONSE_H_
2 changes: 1 addition & 1 deletion src/lib/serviceRoutinesV2/postSubscriptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern std::string postSubscriptions
alarmMgr.badInput(clientIp, errMsg);
scr.subscribeError.errorCode.fill(SccBadRequest, "max one service-path allowed for subscriptions");

TIMED_RENDER(answer = scr.render());
TIMED_RENDER(answer = scr.toJson());
return answer;
}

Expand Down

0 comments on commit fd41125

Please sign in to comment.