/* ****************************************************************************
*
* error - 
*/
TEST(exitTreat, error)
{
  ConnectionInfo ci1("/exit",  "GET", "1.1");
  ConnectionInfo ci2("/exit/nadadenada",  "GET", "1.1");
  ConnectionInfo ci3("/exit/harakiri",  "GET", "1.1");
  const char*    outfile1 = "orion.exit.error1.valid.xml";
  const char*    outfile2 = "orion.exit.error2.valid.xml";
  const char*    outfile3 = "orion.exit.error3.valid.xml";
  std::string    out;

  utInit();

  harakiri = true;

  out = restService(&ci1, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  out = restService(&ci2, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  harakiri = false;
  out = restService(&ci3, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  harakiri = true;
  out = restService(&ci3, rs);
  EXPECT_STREQ("DIE", out.c_str());
  harakiri = false;

  utExit();
}
/* ****************************************************************************
*
* found - 
*/
TEST(getAttributeValueInstance, found)
{
  ConnectionInfo ci1("/ngsi10/contextEntities/E1/attributes",          "POST", "1.1");
  ConnectionInfo ci2("/ngsi10/contextEntities/E1/attributes/A1/left",  "GET", "1.1");
  const char*    outfile1  = "ngsi10.appendContextElementResponse.ok1.valid.xml";
  const char*    outfile2  = "ngsi10.appendContextElementResponse.ok2.valid.xml";
  const char*    infile    = "ngsi10.IndividualContextEntityAttributes.A1-left.postponed.xml";
  std::string    out;

  utInit();

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";

  ci1.outFormat    = XML;
  ci1.inFormat     = XML;
  ci1.payload      = testBuf;
  ci1.payloadSize  = strlen(testBuf);
  out              = restService(&ci1, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  ci2.outFormat    = XML;
  out              = restService(&ci2, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* put - 
*/
TEST(logTraceTreat, put)
{
  ConnectionInfo  ci1("/log/traceLevel/0-19,21-200",  "PUT", "1.1");
  ConnectionInfo  ci2("/log/traceLevel/aaa",  "PUT", "1.1");
  ConnectionInfo  ci3("/log/traceLevel",  "GET", "1.1");
  const char*     outfile1 = "orion.logTrace.spanOfLevels.valid.xml";
  const char*     outfile2 = "orion.logTrace.invalidLevels.valid.xml";
  const char*     outfile3 = "orion.logTrace.get.valid.xml";
  std::string     out;

  utInit();

  ci1.outFormat = XML;
  out          = restService(&ci1, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci2.outFormat = XML;
  out          = restService(&ci2, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci3.outFormat = XML;
  out          = restService(&ci3, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* shortPath - 
*/
TEST(Convenience, shortPath)
{
  ConnectionInfo  ci1("ngsi9", "GET", "1.1");
  ConnectionInfo  ci2("ngsi10", "GET", "1.1");
  ConnectionInfo  ci3("ngsi8", "GET", "1.1");
  ConnectionInfo  ci4("ngsi10/nada", "GET", "1.1");
  std::string     out;
  const char*     outfile1 = "ngsi.convenience.shortPath.postponed.xml";
  const char*     outfile2 = "ngsi.convenience.shortPath2.postponed.xml";
  const char*     outfile3 = "ngsi.convenience.shortPath3.postponed.xml";
  const char*     outfile4 = "ngsi.convenience.shortPath4.postponed.xml";

  utInit();

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  out = restService(&ci1, restServiceV);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  out = restService(&ci2, restServiceV);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  out = restService(&ci3, restServiceV);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile4)) << "Error getting test data from '" << outfile4 << "'";
  out = restService(&ci4, restServiceV);
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* put - 
*/
TEST(putSubscriptionConvOp, put)
{
  ConnectionInfo ci1("/ngsi10/contextSubscriptions/012345678901234567890123",  "DELETE", "1.1");
  ConnectionInfo ci2("/ngsi10/contextSubscriptions/111222333444555666777888",  "PUT",    "1.1");
  ConnectionInfo ci3("/ngsi10/contextSubscriptions/111222333444555666777881",  "PUT",    "1.1");
  ConnectionInfo ci4("/ngsi10/contextSubscriptions/012345678901234567890123",  "XVERB",  "1.1");
  const char*    infile       = "ngsi10.updateContextSubscriptionRequest.subscriptionNotFound.valid.xml";
  const char*    outfile1     = "ngsi10.unsubscribeContextResponse.putSubscriptionConvOp.notFound.valid.xml";
  const char*    outfile2     = "ngsi10.updateContextSubscriptionResponse.putSubscriptionConvOp.notFound.valid.xml";
  const char*    outfile3     = "ngsi10.updateContextSubscriptionResponse.putSubscriptionConvOp.unmatchingSubscriptionId.valid.xml";
  std::string    out;

  utInit();

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  ci1.outFormat    = XML;
  ci1.inFormat     = XML;
  ci1.payload      = NULL;
  ci1.payloadSize  = 0;
  out              = restService(&ci1, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());
  

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  ci2.outFormat    = XML;
  ci2.inFormat     = XML;
  ci2.payload      = testBuf;
  ci2.payloadSize  = strlen(testBuf);
  out              = restService(&ci2, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());


  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  ci3.outFormat    = XML;
  ci3.inFormat     = XML;
  ci3.payload      = testBuf;
  ci3.payloadSize  = strlen(testBuf);
  out              = restService(&ci3, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());


  ci4.outFormat    = XML;
  ci4.inFormat     = XML;
  ci4.payload      = NULL;
  ci4.payloadSize  = 0;
  out              = restService(&ci4, rs);
  EXPECT_EQ("", out);
  EXPECT_EQ("Allow",       ci4.httpHeader[0]);
  EXPECT_EQ("PUT, DELETE", ci4.httpHeaderValue[0]);

  utExit();
}
/* ****************************************************************************
*
* somethingFound - 
*/
TEST(getContextEntityTypeAttribute, somethingFound)
{
  ConnectionInfo ci1("/ngsi9/registerContext",                                 "POST", "1.1");
  ConnectionInfo ci2("/ngsi9/contextEntityTypes/Room/attributes/temperature",  "GET",  "1.1");
  const char*    registerXmlFile = "ngsi9.registerContextRequest.ok.valid.xml";
  const char*    outfile1        = "ngsi9.registerContextResponse.ok.middle.xml";
  const char*    outfile2        = "ngsi9.discoverContextAvailabilityResponse.ok.valid.xml";
  std::string    out;

  utInit();
  // Avoid forwarding of messages
  extern int fwdPort;
  int saved = fwdPort;

  //
  // 1. Register entities so we have something to find:
  //    - entityId type="Room" isPattern="false", ConferenceRoom
  //    - entityId type="Room" isPattern="false", OfficeRoom
  //
  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), registerXmlFile)) << "Error getting test data from '" << registerXmlFile << "'";

  ci1.outFormat    = XML;
  ci1.inFormat     = XML;
  ci1.payload      = testBuf;
  ci1.payloadSize  = strlen(testBuf);
  out              = restService(&ci1, rs);

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";

  char* outStart  = (char*) out.c_str();

  // Remove last char in expectedBuf
  expectedBuf[strlen(expectedBuf) - 1] = 0;

  // Shorten 'out' to be of same length as expectedBuf
  outStart[strlen(expectedBuf)]    = 0;
  EXPECT_STREQ(expectedBuf, out.c_str());


  //
  // Now discover
  //
  ci2.outFormat = XML;
  out           = restService(&ci2, rs);

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  // Putting old value back
  fwdPort = saved;

  utExit();
}
/* ****************************************************************************
*
* ok - 
*/
TEST(postRegisterContext, ok)
{
  ConnectionInfo ci("/ngsi9/registerContext",  "POST", "1.1");
  ConnectionInfo ci2("/ngsi9/registerContext",  "POST", "1.1");
  const char*    infile1   = "ngsi9.registerContextRequest.ok.valid.xml";
  const char*    infile2   = "ngsi9.registerContextRequest.update.valid.xml";
  const char*    outfile1  = "ngsi9.registerContextResponse.postRegisterContext1.middle.xml";
  const char*    outfile2  = "ngsi9.registerContextResponse.postRegisterContext2.valid.xml";
  std::string    out;

  // Avoid forwarding of messages
  extern int     fwdPort;
  int            saved = fwdPort;
  fwdPort = 0;

  utInit();

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile1)) << "Error getting test data from '" << infile1 << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  ci.outFormat    = XML;
  ci.inFormat     = XML;
  ci.payload      = testBuf;
  ci.payloadSize  = strlen(testBuf);
  out             = restService(&ci, rs);

  char* outStart  = (char*) out.c_str();

  // Remove last char in expectedBuf
  expectedBuf[strlen(expectedBuf) - 1] = 0;

  // Shorten'out' to be of same length as expectedBuf
  outStart[strlen(expectedBuf)]    = 0;
  EXPECT_STREQ(expectedBuf, out.c_str());


  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile2)) << "Error getting test data from '" << infile2 << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  ci2.outFormat    = XML;
  ci2.inFormat     = XML;
  ci2.payload      = testBuf;
  ci2.payloadSize  = strlen(testBuf);
  out              = restService(&ci2, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  // Putting old value back
  fwdPort = saved;

  utExit();
}
/* ****************************************************************************
*
* ok - 
*
* FIXME P5 #1862: _json countepart?
*/
TEST(postSubscribeContextAvailability, DISABLED_ok)
{
  ConnectionInfo ci("/ngsi9/subscribeContextAvailability",  "POST", "1.1");
  const char*    infile   = "ngsi9.subscribeContextAvailabilityRequest.ok2.valid.xml";
  const char*    outfile  = "ngsi9.subscribeContextAvailabilityResponse.ok2.middle.xml";
  std::string    out;

  utInit();

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";

  ci.outFormat    = JSON;
  ci.inFormat     = JSON;
  ci.payload      = testBuf;
  ci.payloadSize  = strlen(testBuf);
  out             = restService(&ci, rs);

  char* outStart  = (char*) out.c_str();

  // Remove last char in expectedBuf
  expectedBuf[strlen(expectedBuf) - 1] = 0;

  // Shorten'out' to be of same length as expectedBuf
  outStart[strlen(expectedBuf)]    = 0;
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* ok - 
*
* FIXME P5 #1862: _json counterpart?
*/
TEST(postContextEntityTypeAttribute, DISABLED_ok)
{
  utInit();

  // Avoid forwarding of messages
  extern int fwdPort;
  int saved = fwdPort;
  fwdPort = 0;

  ConnectionInfo ci("/ngsi9/contextEntityTypes/TYPE_123/attributes/temperature",  "POST", "1.1");
  const char*    infile     = "ngsi9.registerProviderRequest.noRegistrationId.postponed.xml";
  const char*    outfile    = "ngsi9.registerContextResponse.noRegistrationId.middle.xml";
  std::string    out;

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";

  ci.outFormat    = JSON;
  ci.inFormat     = JSON;
  ci.payload      = testBuf;
  ci.payloadSize  = strlen(testBuf);
  out             = restService(&ci, rs);

  char* outStart  = (char*) out.c_str();

  // Remove last char in expectedBuf
  expectedBuf[strlen(expectedBuf) - 1] = 0;

  // Shorten 'out' to be of same length as expectedBuf
  outStart[strlen(expectedBuf)]    = 0;
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
  fwdPort = saved;
}
/* ****************************************************************************
*
* delete_verbose - 
*/
TEST(versionTreat, delete_verbose)
{
  ConnectionInfo ci("/log/verbose",  "DELETE", "1.1");
  std::string out;

  out = restService(&ci, rs);
  EXPECT_STREQ("OK", out.c_str());
}
/* ****************************************************************************
*
* post_verbose - 
*/
TEST(versionTreat, post_verbose)
{
  ConnectionInfo ci("/log/verbose/3",  "POST", "1.1");
  std::string out;

  out = restService(&ci, rs);
  EXPECT_STREQ("OK", out.c_str());
}
/* ****************************************************************************
*
* ok - 
*/
TEST(versionTreat, ok)
{
  ConnectionInfo  ci("/version",  "GET", "1.1");
  std::string     out;

  out = restService(&ci, rs);

  // FIXME P2: Some day we'll do this ...
  //
  // char*    expected =
  //   "<orion>\n"
  //   "  <version>" ORION_VERSION "</version>\n"
  //   "  <uptime>*</uptime>"
  //   "  <git_hash>*</git_hash>\n"
  //   "  <compile_time>*</compile_time>\n"
  //   "  <compiled_by>*</compiled_by>\n"
  //   "  <compiled_in>*</compiled_in>\n"
  //   "</orion>\n";  
  // bool            match;
  // match = std::regex_match(expected, out.c_str());

  EXPECT_TRUE(strstr(out.c_str(), "<orion>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "<version>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</version>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "<uptime>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</uptime>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "<git_hash>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</git_hash>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "<compile_time>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</compile_time>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "<compiled_in>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</compiled_in>") != NULL);
  EXPECT_TRUE(strstr(out.c_str(), "</orion>") != NULL);

  extern const char*  orionUnitTestVersion;
  std::string         expected = std::string("<version>") + orionUnitTestVersion + "</version>";
  EXPECT_TRUE(strstr(out.c_str(), expected.c_str()) != NULL);

  versionSet("1.2.3");
  out       = restService(&ci, rs);
  EXPECT_TRUE(strstr(out.c_str(), "<version>1.2.3</version>") != NULL);

  versionSet("1.2.3");
  std::string version = versionGet();
  EXPECT_EQ("1.2.3", version);
}
/* ****************************************************************************
*
* notFound - 
*/
TEST(putAttributeValueInstance, notFound)
{
  ConnectionInfo ci1("/ngsi10/contextEntities/E1/attributes/A1/left",  "PUT", "1.1");
  ConnectionInfo ci2("/ngsi10/contextEntities/E1/attributes/A1/right",  "PUT", "1.1");
  ConnectionInfo ci3("/ngsi10/contextEntities/E1/attributes/A1/left",  "PUT", "1.1");
  const char*    infile1   = "ngsi10.putAttributeValueInstance.A1-left.ok.postponed.xml";
  const char*    infile2   = "ngsi10.putAttributeValueInstance.noIdMetadata.postponed.xml";
  const char*    outfile1  = "ngsi10.statusCode.putAttributeValueInstance1.ok.valid.xml";
  const char*    outfile2  = "ngsi10.statusCode.putAttributeValueInstance2.ok.valid.xml";
  const char*    outfile3  = "ngsi10.statusCode.putAttributeValueInstance3.ok.valid.xml";
  std::string    out;

  utInit();
  
  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile1)) << "Error getting test data from '" << infile1 << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  ci1.outFormat    = XML;
  ci1.inFormat     = XML;
  ci1.payload      = testBuf;
  ci1.payloadSize  = strlen(testBuf);
  out              = restService(&ci1, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile1)) << "Error getting test data from '" << infile1 << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  ci2.outFormat    = XML;
  ci2.inFormat     = XML;
  ci2.payload      = testBuf;
  ci2.payloadSize  = strlen(testBuf);
  out              = restService(&ci2, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile2)) << "Error getting test data from '" << infile2 << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  ci3.outFormat    = XML;
  ci3.inFormat     = XML;
  ci3.payload      = testBuf;
  ci3.payloadSize  = strlen(testBuf);
  out              = restService(&ci3, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* ok - 
*/
TEST(badVerbGetPutDeleteOnly, ok)
{
  ConnectionInfo  ci("/ngsi10/contextEntities/entityId01/attributes/temperature/14",  "POST", "1.1");
  std::string     expected = ""; // Bad verb gives no payload, only HTTP headers
  std::string     out      = restService(&ci, rs);

  EXPECT_EQ(expected, out);
  EXPECT_EQ("Allow", ci.httpHeader[0]);
  EXPECT_EQ("GET, PUT, DELETE",  ci.httpHeaderValue[0]);
}
/* ****************************************************************************
*
* ok - 
*/
TEST(badVerbGetOnly, ok)
{
  ConnectionInfo  ci("/version",  "PUT", "1.1");
  std::string     expected = ""; // Bad verb gives no payload, only HTTP headers
  std::string     out      = restService(&ci, rs);

  EXPECT_EQ(expected, out);
  EXPECT_EQ("Allow", ci.httpHeader[0]);
  EXPECT_EQ("GET",   ci.httpHeaderValue[0]);
}
/* ****************************************************************************
*
* ok - 
*/
TEST(badVerbGetPostOnly, ok)
{
  ConnectionInfo ci("/ngsi9/contextEntities/aaa",  "PUT", "1.1");
  std::string     expected = "";  // no payload for bad verb, only http headers to indicate the error
  std::string     out;

  out = restService(&ci, rs);

  EXPECT_EQ(expected, out);
  EXPECT_EQ("Allow",      ci.httpHeader[0]);
  EXPECT_EQ("POST, GET",  ci.httpHeaderValue[0]);
}
/* ****************************************************************************
*
* deleteIndividual - 
*/
TEST(logTraceTreat, deleteIndividual)
{
  ConnectionInfo  ci0("/log/traceLevel/0-255",  "PUT",  "1.1");
  ConnectionInfo  ci1("/log/traceLevel/161",  "DELETE", "1.1");
  ConnectionInfo  ci2("/log/traceLevel/aaa",  "DELETE", "1.1");
  ConnectionInfo  ci3("/log/traceLevel",  "GET", "1.1");
  const char*     outfile0 = "orion.logTrace.allLevelsSet.valid.xml";
  const char*     outfile1 = "orion.logTrace.deleteOk.valid.xml";
  const char*     outfile2 = "orion.logTrace.invalidDelete.valid.xml";
  const char*     outfile3 = "orion.logTrace.deleteIndividual4.valid.xml";
  std::string     out;

  utInit();

  ci0.outFormat  = XML;
  ci0.apiVersion = "v1";
  out            = restService(&ci0, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile0)) << "Error getting test data from '" << outfile0 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci1.outFormat  = XML;
  ci1.apiVersion = "v1";
  out            = restService(&ci1, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci2.outFormat  = XML;
  ci2.apiVersion = "v1";
  out            = restService(&ci2, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci3.outFormat  = XML;
  ci3.apiVersion = "v1";
  out            = restService(&ci3, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile3)) << "Error getting test data from '" << outfile3 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* deleteAll - 
*/
TEST(logTraceTreat, deleteAll)
{
  ConnectionInfo ci1("/log/traceLevel",  "DELETE", "1.1");
  ConnectionInfo ci2("/log/traceLevel",  "GET", "1.1");
  const char*    outfile1 = "orion.logTrace.allLevelsOff.valid.xml";
  const char*    outfile2 = "orion.logTrace.empty.valid.xml";
  std::string    out;

  utInit();

  ci1.outFormat = XML;
  out           = restService(&ci1, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  ci2.outFormat = XML;
  out           = restService(&ci2, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* ok -
*/
TEST(deleteIndividualContextEntityAttribute, ok)
{
    ConnectionInfo  ci("/ngsi10/contextEntities/entity901/attributes/aa",  "DELETE", "1.1");
    const char*     outfile = "ngsi10.deleteIndividualContextEntityAttribute.valid.xml";

    utInit();

    EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
    std::string out = restService(&ci, rs);
    EXPECT_STREQ(expectedBuf, out.c_str());

    utExit();
}
/* ****************************************************************************
*
* emptyPath - 
*/
TEST(Convenience, emptyPath)
{
  ConnectionInfo            ci("", "GET", "1.1");
  std::string               response;
  std::string               expected = "Empty URL";

  utInit();

  response = restService(&ci, restServiceV);
  EXPECT_STREQ(expected.c_str(), response.c_str());

  utExit();
}
/* ****************************************************************************
*
* ok - 
*/
TEST(badRequest, ok)
{
  ConnectionInfo ci("/xxx/badbadbad",  "GET", "1.1");
  std::string    out;
  const char*    outfile = "orion.badRequest.valid.xml";

  utInit();

  out = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* badPathNgsi10 - 
*/
TEST(Convenience, badPathNgsi10)
{
  ConnectionInfo            ci("ngsi10/badpathcomponent", "GET", "1.1");
  std::string               out;
  const char*               outfile = "ngsi10.convenience.badPathNgsi10.postponed.xml";

  utInit();

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  out = restService(&ci, restServiceV);
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* error - 
*/
TEST(badVerbAllFour, error)
{
  ConnectionInfo ci1("/ngsi10/contextEntities/123",  "PUST", "1.1");
  ConnectionInfo ci2("/ngsi10/contextEntities",      "PUST", "1.1");
  ConnectionInfo ci3("/ngsi10/",                     "PUST", "1.1");
  ConnectionInfo ci4("/ngsi10/1/2/3/4",              "PUST", "1.1");
  const char*    outfile1 = "orion.serviceNotRecognized.valid.xml";
  const char*    outfile2 = "orion.serviceNotRecognized2.valid.xml";
  std::string    out;

  utInit();

  ci1.apiVersion = "v1";
  out = restService(&ci1, rs);
  EXPECT_EQ("", out);
  EXPECT_EQ("Allow", ci1.httpHeader[0]);
  EXPECT_EQ("POST, GET, PUT, DELETE", ci1.httpHeaderValue[0]);

  ci2.apiVersion = "v1";
  out = restService(&ci2, rs);
  EXPECT_EQ("", out);
  EXPECT_EQ("Allow", ci2.httpHeader[0]);
  EXPECT_EQ("POST, GET, PUT, DELETE", ci2.httpHeaderValue[0]);

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile1)) << "Error getting test data from '" << outfile1 << "'";
  ci3.apiVersion = "v1";
  out = restService(&ci3, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile2)) << "Error getting test data from '" << outfile2 << "'";
  ci4.apiVersion = "v1";
  out = restService(&ci4, rs);
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* notFound - 
*/
TEST(getNgsi10ContextEntityTypesAttribute, notFound)
{
  ConnectionInfo ci("/ngsi10/contextEntityTypes/entity11/attributes/Attribute11",  "GET", "1.1");
  const char*    outfile = "ngsi10.queryContextResponse.getNgsi10ContextEntityTypesAttribute.valid.xml";
  std::string    out;

  utInit();

  ci.outFormat = XML;
  out          = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* notFound - 
*/
TEST(getAttributeValueInstance, notFound)
{
  ConnectionInfo ci("/ngsi10/contextEntities/E1/attributes/A1/left",  "GET", "1.1");
  const char*    outfile = "ngsi10.contextAttributeResponse.notFound.valid.xml";
  std::string    out;

  utInit();
    
  ci.outFormat = XML;
  out          = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* nothingFound - 
*/
TEST(getContextEntityTypeAttribute, nothingFound)
{
  ConnectionInfo ci("/ngsi9/contextEntityTypes/TYPE_123/attributes/temperature",  "GET", "1.1");
  const char*    outfile = "ngsi9.discoverContextAvailabilityResponse.notFound.valid.xml";
  std::string    out;

  utInit();

  ci.outFormat = XML;
  out          = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* notFound - 
*/
TEST(getIndividualContextEntityAttributes, notFound)
{
  ConnectionInfo ci("/ngsi10/contextEntities/entity11/attributes",  "GET", "1.1");
  const char*    outfile = "ngsi10.contextElementResponse.getIndividualContextEntityAttributes.notFound.valid.xml";
  std::string    out;

  utInit();

  ci.outFormat = JSON;
  out          = restService(&ci, rs);

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

  utExit();
}
/* ****************************************************************************
*
* post - 
*/
TEST(logTraceTreat, post)
{
  ConnectionInfo  ci("/log/traceLevel/20",  "POST", "1.1");
  const char*     outfile = "orion.logTrace.post20.valid.xml";
  std::string     out;

  utInit();

  ci.outFormat  = XML;
  ci.apiVersion = "v1";
  out          = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
*
* get - 
*/
TEST(logTraceTreat, get)
{
  ConnectionInfo ci("/log/traceLevel",  "GET", "1.1");
  const char*    outfile = "orion.logTrace.empty.valid.xml";
  std::string    out;

  utInit();

  lmTraceSet(NULL);

  ci.outFormat = XML;
  out          = restService(&ci, rs);
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  EXPECT_STREQ(expectedBuf, out.c_str());

  utExit();
}
/* ****************************************************************************
 * 
 * json - 
 */
TEST(putIndividualContextEntityAttribute, json)
{
  ConnectionInfo ci("/ngsi10/contextEntities/entity11/attributes/temperature",  "PUT", "1.1");
  ci.servicePathV.push_back("");

  const char*    infile      = "ngsi10.updateContextAttributeRequest.putAttribute.valid.json";
  const char*    outfile     = "ngsi10.updateContextAttributeResponse.notFound.valid.json";
  std::string    out;
  
  EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'";
  EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'";
  
  ci.outMimeType    = JSON;
  ci.inMimeType     = JSON;
  ci.payload        = testBuf;
  ci.payloadSize    = strlen(testBuf);
  out               = restService(&ci, rs);
  
  EXPECT_STREQ(expectedBuf, out.c_str());
  
  utExit();
}