/* ****************************************************************************
*
* withoutType -
*
*/
TEST(mongoQueryContextRequest_filters, withEntityType)
{
  HttpStatusCode         ms;
  QueryContextRequest   req;
  QueryContextResponse  res;

  /* Prepare database */
  prepareDatabase(true);

  /* Forge the request (from "inside" to "outside") */
  EntityId en(".*", "", "true");
  Scope sc(SCOPE_TYPE_SIMPLE_QUERY, "+type");
  req.entityIdVector.push_back(&en);
  req.restriction.scopeVector.push_back(&sc);

  /* Invoke the function in mongoBackend library */
  servicePathVector.clear();
  ms = mongoQueryContext(&req, &res, "", servicePathVector, uriParams);

  /* Check response is as expected */
  EXPECT_EQ(SccOk, ms);

  EXPECT_EQ(SccNone, res.errorCode.code);
  EXPECT_EQ("", res.errorCode.reasonPhrase);
  EXPECT_EQ("", res.errorCode.details);

  /* Only entitie IDs are check (we have a bunch of tests in other places to check the query response itself */
  ASSERT_EQ(9, res.contextElementResponseVector.size());
  EXPECT_EQ("E1", RES_CER(0).entityId.id);
  EXPECT_EQ("E2", RES_CER(1).entityId.id);
  EXPECT_EQ("E3", RES_CER(2).entityId.id);
  EXPECT_EQ("E4", RES_CER(3).entityId.id);
  EXPECT_EQ("E5", RES_CER(4).entityId.id);
  EXPECT_EQ("C1", RES_CER(5).entityId.id);
  EXPECT_EQ("C2", RES_CER(6).entityId.id);
  EXPECT_EQ("C3", RES_CER(7).entityId.id);
  EXPECT_EQ("E6", RES_CER(8).entityId.id);

  /* Release dynamic memory used by response (mongoBackend allocates it) */
  res.contextElementResponseVector.release();

  /* Release connection */
  setMongoConnectionForUnitTest(NULL);
}
/* ****************************************************************************
*
* mongoConnectionPoolInit - 
*/
int mongoConnectionPoolInit
(
  const char*  host,
  const char*  db,
  const char*  rplSet,
  const char*  username,
  const char*  passwd,
  bool         multitenant,
  double       timeout,
  int          writeConcern,
  int          poolSize,
  bool         semTimeStat
)
{
#ifdef UNIT_TEST
  /* Basically, we are mocking all the DB pool with a single connection. The getMongoConnection() and mongoReleaseConnection() methods
   * are mocked in similar way to ensure a coherent behaviour */
  setMongoConnectionForUnitTest(mongoConnect(host, db, rplSet, username, passwd, multitenant, writeConcern, timeout));
  return 0;
#else
  //
  // Create the pool
  //
  connectionPool  = (MongoConnection*) calloc(sizeof(MongoConnection), poolSize);
  if (connectionPool == NULL)
  {
    LM_E(("Runtime Error (insufficient memory to create connection pool of %d connections)", poolSize));
    return -1;
  }
  connectionPoolSize = poolSize;

  //
  // Initialize (connect) the pool
  //
  for (int ix = 0; ix < connectionPoolSize; ++ix)
  {
    connectionPool[ix].free       = true;
    connectionPool[ix].connection =
        mongoConnect(host, db, rplSet, username, passwd, multitenant, writeConcern, timeout);
  }

  //
  // Set up the semaphore protecting the pool itself (connectionPoolSem)
  //
  int r = sem_init(&connectionPoolSem, 0, 1);

  if (r != 0)
  {
    LM_E(("Runtime Error (cannot create connection pool semaphore)"));
    return -1;
  }

  //
  // Set up the semaphore protecting the set of connections of the pool (connectionSem)
  // Note that this is a counting semaphore, initialized to connectionPoolSize.
  //
  r = sem_init(&connectionSem, 0, connectionPoolSize);
  if (r != 0)
  {
    LM_E(("Runtime Error (cannot create connection semaphore-set)"));
    return -1;
  }

  // Measure accumulated semaphore waiting time?
  semStatistics = semTimeStat;

  return 0;
#endif
}