/* ****************************************************************************
*
* postUpdateContextAvailabilitySubscription - 
*/
std::string postUpdateContextAvailabilitySubscription
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  UpdateContextAvailabilitySubscriptionResponse  ucas;
  std::string                                    answer;

  ucas.subscriptionId = parseDataP->ucas.res.subscriptionId;

  // FIXME P6: by the moment, we are assuming that notification will be sent in the same format than the one
  // used to do the subscription, so we are passing ciP->inFomat. 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 = mongoUpdateContextAvailabilitySubscription(&parseDataP->ucas.res,
                                                                   &ucas,
                                                                   ciP->inFormat,
                                                                   ciP->tenant);

  answer = ucas.render(UpdateContextAvailabilitySubscription, ciP->outFormat, "", 0);

  return answer;
}
/* ****************************************************************************
*
* response -
*
*/
TEST(UpdateContextAvailabilitySubscriptionRequest, response)
{
  UpdateContextAvailabilitySubscriptionResponse  ucas;
  StatusCode                                     ec(SccBadRequest, "Detail");
  UpdateContextAvailabilitySubscriptionResponse  ucas2(ec);
  std::string                                    out;

  utInit();

  EXPECT_EQ(ucas2.errorCode.code, SccBadRequest);

  ucas.subscriptionId.set("012345678901234567890123");

  out = ucas.check("");
  EXPECT_EQ("OK", out);

  utExit();
}
/* ****************************************************************************
*
* postUpdateContextAvailabilitySubscription -
*/
std::string postUpdateContextAvailabilitySubscription
(
    ConnectionInfo*            ciP,
    int                        components,
    std::vector<std::string>&  compV,
    ParseData*                 parseDataP
)
{
    UpdateContextAvailabilitySubscriptionResponse  ucas;
    std::string                                    answer;

    ucas.subscriptionId = parseDataP->ucas.res.subscriptionId;

    TIMED_MONGO(ciP->httpStatusCode = mongoUpdateContextAvailabilitySubscription(&parseDataP->ucas.res,
                                      &ucas,
                                      ciP->httpHeaders.correlator,
                                      ciP->tenant));

    TIMED_RENDER(answer = ucas.render(UpdateContextAvailabilitySubscription, "", 0));

    return answer;
}
/* ****************************************************************************
*
* UpdateContextAvailabilitySubscriptionRequest::check - 
*/
std::string UpdateContextAvailabilitySubscriptionRequest::check(RequestType requestType, Format format, const std::string& indent, const std::string& predetectedError, int counter)
{
  std::string                                    res;
  UpdateContextAvailabilitySubscriptionResponse  response;

  response.subscriptionId = subscriptionId;

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

  return response.render(UpdateContextAvailabilitySubscription, format, indent, counter);
}
/* ****************************************************************************
*
* response -
*/
TEST(UpdateContextAvailabilitySubscriptionRequest, response)
{
    UpdateContextAvailabilitySubscriptionResponse  ucas;
    StatusCode                                     ec(SccBadRequest, "Detail");
    UpdateContextAvailabilitySubscriptionResponse  ucas2(ec);
    std::string                                    out;
    const char*                                    outfile1 = "ngsi9.updateContextAvailabilitySubscriptionResponse.response1.valid.xml";
    const char*                                    outfile2 = "ngsi9.updateContextAvailabilitySubscriptionResponse.response2.valid.xml";
    const char*                                    outfile3 = "ngsi9.updateContextAvailabilitySubscriptionResponse.response3.valid.xml";
    const char*                                    outfile4 = "ngsi9.updateContextAvailabilitySubscriptionResponse.response4.valid.xml";

    utInit();

    EXPECT_EQ(ucas2.errorCode.code, SccBadRequest);

    ucas.subscriptionId.set("012345678901234567890123");

    out = ucas.check(UpdateContextAvailabilitySubscription, XML, "", "", 0);
    EXPECT_EQ("OK", out);

    out = ucas.render(UpdateContextAvailabilitySubscription, XML, "", 0);
    EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
    EXPECT_STREQ(expectedBuf, out.c_str());

    ucas.errorCode.fill(SccBadRequest, "Detail");
    out = ucas.render(UpdateContextAvailabilitySubscription, XML, "", 0);
    EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
    EXPECT_STREQ(expectedBuf, out.c_str());

    ucas.errorCode.fill(SccNone);
    ucas.duration.set("ddd");
    out = ucas.check(UpdateContextAvailabilitySubscription, XML, "", "", 0);
    EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
    EXPECT_STREQ(expectedBuf, out.c_str());

    out = ucas.check(UpdateContextAvailabilitySubscription, XML, "", "predetected error", 0);
    EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile4)) << "Error getting test data from '" << outfile4 << "'";
    EXPECT_STREQ(expectedBuf, out.c_str());

    utExit();
}