/* ****************************************************************************
*
* postSubscribeContextAvailability - 
*/
std::string postSubscribeContextAvailability(ConnectionInfo* ciP, int components, std::vector<std::string> compV, ParseData* parseDataP)
{
  SubscribeContextAvailabilityResponse  scar;
  std::string                           answer;

  // FIXME P6: at the moment, we assume that notifications are sent in the same format that the one
  // used to do the subscription, so we are passing ciP->inFormat. This is just an heuristic, the client could want
  // for example to use XML in the subscription message but wants notifications in JSON. We need a more
  // flexible approach, to be implemented
  ciP->httpStatusCode = mongoSubscribeContextAvailability(&parseDataP->scar.res, &scar, ciP->inFormat);
  answer = scar.render(SubscribeContextAvailability, ciP->outFormat, "");

  return answer;
}
/* ****************************************************************************
*
* jsonRender -
*
* subscriptionId: MANDATORY
* duration:       Optional
* errorCode:      Optional
*/
TEST(SubscribeContextAvailabilityResponse, jsonRender)
{
  const char*                            filename1  = "ngsi9.subscribeContextAvailabilityResponse.jsonRender1.valid.json";
  const char*                            filename2  = "ngsi9.subscribeContextAvailabilityResponse.jsonRender2.valid.json";
  const char*                            filename3  = "ngsi9.subscribeContextAvailabilityResponse.jsonRender3.valid.json";
  const char*                            filename4  = "ngsi9.subscribeContextAvailabilityResponse.jsonRender4.valid.json";
  SubscribeContextAvailabilityResponse*  scarP;
  std::string                            rendered;
  utInit();

  // Preparations
  scarP = new SubscribeContextAvailabilityResponse();

  // 1. +subscriptionId -duration -errorCode
  scarP->subscriptionId.set("012345678901234567890123");

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename1)) << "Error getting test data from '" << filename1 << "'";
  rendered = scarP->toJsonV1();
  EXPECT_STREQ(expectedBuf, rendered.c_str());


  // 2. +subscriptionId -duration +errorCode
  scarP->errorCode.fill(SccBadRequest);

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename2)) << "Error getting test data from '" << filename2 << "'";
  rendered = scarP->toJsonV1();
  EXPECT_STREQ(expectedBuf, rendered.c_str());


  // 3. +subscriptionId +duration -errorCode
  scarP->errorCode.fill(SccNone);
  scarP->duration.set("PT1H");

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename3)) << "Error getting test data from '" << filename3 << "'";
  rendered = scarP->toJsonV1();
  EXPECT_STREQ(expectedBuf, rendered.c_str());


  // 4. +subscriptionId +duration +errorCode
  scarP->errorCode.fill(SccBadRequest, "no details");

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename4)) << "Error getting test data from '" << filename4 << "'";
  rendered = scarP->toJsonV1();
  EXPECT_STREQ(expectedBuf, rendered.c_str());


  free(scarP);

  utExit();
}
/* ****************************************************************************
*
* SubscribeContextAvailabilityRequest::check - 
*/
std::string SubscribeContextAvailabilityRequest::check(RequestType requestType, Format format, const std::string& indent, const std::string& predetectedError, int counter)
{
  SubscribeContextAvailabilityResponse response;
  std::string                          res;

  if (predetectedError != "")
  {
    response.errorCode.fill(SccBadRequest, predetectedError);
  }
  else if (((res = entityIdVector.check(SubscribeContextAvailability, format, indent, predetectedError, counter))   != "OK") ||
           ((res = attributeList.check(SubscribeContextAvailability, format, indent, predetectedError, counter))    != "OK") ||
           ((res = reference.check(SubscribeContextAvailability, format, indent, predetectedError, counter))        != "OK") ||
           ((res = duration.check(SubscribeContextAvailability, format, indent, predetectedError, counter))         != "OK") ||
           ((res = restriction.check(SubscribeContextAvailability, format, indent, predetectedError, restrictions)) != "OK"))
  {
    response.errorCode.fill(SccBadRequest, res);
  }
  else
    return "OK";

  return response.render(SubscribeContextAvailability, format, indent);
}