/* ****************************************************************************
*
* postUpdateContextSubscription - 
*/
std::string postUpdateContextSubscription(ConnectionInfo* ciP, int components, std::vector<std::string> compV, ParseData* parseDataP)
{
  UpdateContextSubscriptionResponse  ucsr;
  std::string                        answer;

  ucsr.subscribeError.subscriptionId = parseDataP->ucsr.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 = mongoUpdateContextSubscription(&parseDataP->ucsr.res, &ucsr, ciP->inFormat);
  answer = ucsr.render(UpdateContextSubscription, ciP->outFormat, "");
  return answer;
}
/* ****************************************************************************
*
* postUpdateContextSubscription - 
*/
std::string postUpdateContextSubscription
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  UpdateContextSubscriptionResponse  ucsr;
  std::string                        answer;

  ucsr.subscribeError.subscriptionId = parseDataP->ucsr.res.subscriptionId;  

  TIMED_MONGO(ciP->httpStatusCode = mongoUpdateContextSubscription(&parseDataP->ucsr.res,
                                                                   &ucsr,                                                                   
                                                                   ciP->tenant,
                                                                   ciP->httpHeaders.xauthToken,
                                                                   ciP->servicePathV));

  TIMED_RENDER(answer = ucsr.render(UpdateContextSubscription, ""));

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

  if (predetectedError != "")
  {
    response.subscribeError.subscriptionId = subscriptionId;
    response.subscribeError.errorCode.fill(SccBadRequest, predetectedError);
  }
  else if (((res = duration.check(UpdateContextSubscription, format, indent, predetectedError, counter))              != "OK") ||
           ((res = restriction.check(UpdateContextSubscription, format, indent, predetectedError, restrictions))      != "OK") ||
           ((res = subscriptionId.check(UpdateContextSubscription, format, indent, predetectedError, counter))        != "OK") ||
           ((res = notifyConditionVector.check(UpdateContextSubscription, format, indent, predetectedError, counter)) != "OK") ||
           ((res = throttling.check(UpdateContextSubscription, format, indent, predetectedError, counter))            != "OK"))
  {
    response.subscribeError.subscriptionId = subscriptionId;
    response.subscribeError.errorCode.fill(SccBadRequest, res);
  }
  else
    return "OK";

  return response.render(UpdateContextSubscription, format, indent);
}
/* ****************************************************************************
*
* jsonRender - 
*/
TEST(UpdateContextSubscriptionResponse, json_render)
{
  const char*                         filename1  = "ngsi10.updateContextSubscriptionResponse.jsonRender1.valid.json";
  const char*                         filename2  = "ngsi10.updateContextSubscriptionResponse.jsonRender2.valid.json";
  const char*                         filename3  = "ngsi10.updateContextSubscriptionResponse.jsonRender3.valid.json";
  const char*                         filename4  = "ngsi10.updateContextSubscriptionResponse.jsonRender4.valid.json";
  const char*                         filename5  = "ngsi10.updateContextSubscriptionResponse.jsonRender5.valid.json";
  const char*                         filename6  = "ngsi10.updateContextSubscriptionResponse.jsonRender6.valid.json";
  UpdateContextSubscriptionResponse*  ucsrP;
  std::string                         out;

  utInit();

  // Preparations
  ucsrP = new UpdateContextSubscriptionResponse();

  // 1. subscribeError, -subscriptionId, with details
  // 2. subscribeError, +subscriptionId, no details
  // 3. subscribeResponse: +subscription -duration -throttling
  // 4. subscribeResponse: +subscription -duration +throttling
  // 5. subscribeResponse: +subscription +duration -throttling
  // 6. subscribeResponse: +subscription +duration +throttling

  // 1.
  ucsrP->subscribeError.errorCode.fill(SccBadRequest, "details");

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



  // 2.
  ucsrP->subscribeError.errorCode.fill(SccBadRequest);
  ucsrP->subscribeError.subscriptionId.set("012345678901234567890123");

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

  ucsrP->subscribeError.errorCode.fill(SccNone);



  // 3.
  ucsrP->subscribeResponse.subscriptionId.set("012345678901234567890123");

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



  // 4.
  ucsrP->subscribeResponse.throttling.set("PT1M");

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



  // 5.
  ucsrP->subscribeResponse.throttling.set("");
  ucsrP->subscribeResponse.duration.set("PT1H");
  
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename5)) << "Error getting test data from '" << filename5 << "'";
  out = ucsrP->render(QueryContext, JSON, "");
  EXPECT_STREQ(expectedBuf, out.c_str());



  // 6.
  ucsrP->subscribeResponse.throttling.set("PT1M");

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

  utExit();
}