/* ****************************************************************************
*
* prepareDatabaseV1Subs -
*/
static void prepareDatabaseV1Subs(void) {

    /* Set database */
    setupDatabase();

    // 10000000 -> Sun, 26 Apr 1970 17:46:40 GMT -> "1970-04-26T17:46:40.00Z"
    // 20000000 -> Thu, 20 Aug 1970 11:33:20 GMT -> "1970-08-20T11:33:20.00Z"
    // 25000000 -> Sat, 17 Oct 1970 08:26:40 GMT -> "1970-19-17T08:26:40.00Z"
    DBClientBase* connection = getMongoConnection();

    BSONObj sub1 = BSON("_id" << OID(SUB_OID1) <<
                        "expiration" << 10000000 <<                        
                        "reference" << "http://notify1.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSONArray() <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX1" << "AY1")
                                                       ))
                        );

    BSONObj sub2 = BSON("_id" << OID(SUB_OID2) <<
                        "expiration" << 25000000 <<
                        "lastNotification" << 20000000 <<
                        "count" << 24 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E.*" << "type" << "T2" << "isPattern" << "true")) <<
                        "attrs" << BSON_ARRAY("A1" << "A2") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX2" << "AY2")
                                                       )) <<
                        "throttling" << 5
                        );

    BSONObj sub3 = BSON("_id" << OID(SUB_OID3) <<
                        "expiration" << 20000000 <<
                        "lastNotification" << 25000000 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E.*" << "type" << "T2" << "isPattern" << "true")) <<
                        "attrs" << BSON_ARRAY("A1" << "A2") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                     "type" << "ONTIMEINTERVAL" <<
                                                     "value" << 100
                                                     ))
                        );

    connection->insert(SUBSCRIBECONTEXT_COLL, sub1);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub2);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub3);
}
/* ****************************************************************************
*
* prepareDatabase -
*
* This function is called before every test, to populate some information in the
* entities collection.
*/
static void prepareDatabase(void) {

  /* Set database */
  setupDatabase();

  DBClientBase* connection = getMongoConnection();

  BSONObj en1 = BSON("_id" << BSON("id" << "E1" << "type" << "T1") <<
                     "attrNames" << BSON_ARRAY("A1") <<
                     "attrs" << BSON(
                        "A1" << BSON("type" << "TA1" << "value" << "val1")
                        )
                    );

  BSONObj en2 = BSON("_id" << BSON("id" << "E1") <<
                     "attrNames" << BSON_ARRAY("A1") <<
                     "attrs" << BSON(
                        "A1" << BSON("type" << "TA1" << "value" << "val1b")
                        )
                    );


  connection->insert(ENTITIES_COLL, en1);
  connection->insert(ENTITIES_COLL, en2);


}
/* ****************************************************************************
*
* collectionInsert -
*/
bool collectionInsert
(
  const std::string&  col,
  const BSONObj&      doc,
  std::string*        err
)
{
  TIME_STAT_MONGO_WRITE_WAIT_START();
  DBClientBase* connection = getMongoConnection();

  if (connection == NULL)
  {
    TIME_STAT_MONGO_WRITE_WAIT_STOP();

    LM_E(("Fatal Error (null DB connection)"));
    *err = "null DB connection";
    return false;
  }

  LM_T(LmtMongo, ("insert() in '%s' collection: '%s'", col.c_str(), doc.toString().c_str()));

  try
  {
    connection->insert(col.c_str(), doc);
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_WRITE_WAIT_STOP();
    LM_I(("Database Operation Successful (insert: %s)", doc.toString().c_str()));
  }
  catch (const std::exception &e)
  {
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_WRITE_WAIT_STOP();

    std::string msg = std::string("collection: ") + col.c_str() +
      " - insert(): " + doc.toString() +
      " - exception: " + e.what();
    *err = "Database Error (" + msg + ")";
    LM_E((err->c_str()));
    return false;
  }
  catch (...)
  {
    releaseMongoConnection(connection);
    TIME_STAT_MONGO_WRITE_WAIT_STOP();

    std::string msg = std::string("collection: ") + col.c_str() +
      " - insert(): " + doc.toString() +
      " - exception: generic";
    *err = "Database Error (" + msg + ")";
    LM_E((err->c_str()));
    return false;
  }

  return true;
}
/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(void) {

    /* Set database */
    setupDatabase();

    DBClientBase* connection = getMongoConnection();

    BSONObj sub1 = BSON("_id" << OID("51307b66f481db11bf860001") <<
                        "expiration" << 10000000 <<
                        "lastNotification" << 15000000 <<
                        "reference" << "http://notify1.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSONArray() <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX1" << "AY1")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONTIMEINTERVAL" <<
                                                       "value" << 100
                                                       ))
                        );

    BSONObj sub2 = BSON("_id" << OID("51307b66f481db11bf860002") <<
                        "expiration" << 20000000 <<
                        "lastNotification" << 25000000 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T1" << "isPattern" << "false")) <<
                        "attrs" << BSON_ARRAY("A1" << "A2") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("AX2" << "AY2")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONTIMEINTERVAL" <<
                                                       "value" << 200
                                                       ))
                        );

    connection->insert(SUBSCRIBECONTEXT_COLL, sub1);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub2);

}
Beispiel #5
0
int main() {
    try {
        DBClientBase *conn = NULL;
        string err_msg;
        ConnectionString cs = ConnectionString::parse("localhost", err_msg);
        
        if (!cs.isValid()) {
            throw "bad: " + err_msg;
        }
        
        try {
            conn = cs.connect(err_msg);
        } catch (DBException &e) {
            cout << "caught " << err_msg << endl;
            return 1;
        }
        
        if (!conn){
            cout<<"Unable to connect to DB"<<endl;
            return 1;
        }
        
//        BSONObjBuilder b;
//        b.append("name", "Joe");
//        b.append("age", 33);
//        BSONObj p = b.obj();
//        
//        conn->insert("db.coll",p,0);
        
        
        GridFS * fs;
       
        fs = new GridFS(*conn,"test");
        BSONObj p = fs->storeFile("pic.JPG");
        conn->insert("db.coll",p,0);
    }
    catch( DBException &e ) {
        cout << "caught " << e.what() << endl;
    }
    return 0;
}
Beispiel #6
0
    JSBool mongo_insert(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval){    
        uassert( "mongo_insert needs 2 args" , argc == 2 );
        uassert( "2nd param to insert has to be an object" , JSVAL_IS_OBJECT( argv[1] ) );
        
        DBClientBase * conn = getConnection( cx, obj );
        uassert( "no connection!" , conn );
        
        Convertor c( cx );
        
        string ns = c.toString( argv[0] );
        BSONObj o = c.toObject( argv[1] );

        // TODO: add _id
        
        try {
            conn->insert( ns , o );
            return JS_TRUE;
        }
        catch ( ... ){
            JS_ReportError( cx , "error doing insert" );
            return JS_FALSE;
        }
    }
/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(void) {

    /* Set database */
    setupDatabase();

    DBClientBase* connection = getMongoConnection();

    /* We create the following entities:
     *
     * - E1:
     *     A1:  X
     *     A2:  Z
     *     A3:  W
     *     A7:  R
     * - E2
     *     A1:  S
     *     A4:  T
     *     A6:  U
     * - E3
     *     A1:  noise
     *     A1*: noise
     *     A2:  noise
     *     A3:  noise
     *     A4:  noise
     *     A6:  noise
     *     A7:  noise
     *
     * We create the following subscriptions:
     *
     * - Sub1:
     *     Entities: E1, E2
     *     Attribute: A1, A3, A4
     *     NotifyCond: ONCHANGE on [A1, A2, A4]
     *
     * - Sub2:
     *     Entities: E2
     *     Attribute: A1, A3, A4
     *     NotifyCond: ONCHANGE on A1
     *                 ONCHANGE on A2
     *                 ONCHANGE on A4
     *
     */

    BSONObj en1 = BSON("_id" << BSON("id" << "E1" << "type" << "T") <<
                       "attrNames" << BSON_ARRAY("A1" << "A2" << "A3" << "A7") <<
                       "attrs" << BSON(
                          "A1" << BSON("type" << "TA1" << "value" << "X") <<
                          "A2" << BSON("type" << "TA2" << "value" << "Z") <<
                          "A3" << BSON("type" << "TA3" << "value" << "W") <<
                          "A7" << BSON("type" << "TA7" << "value" << "R")
                          )
                      );

    BSONObj en2 = BSON("_id" << BSON("id" << "E2" << "type" << "T") <<
                       "attrNames" << BSON_ARRAY("A1" << "A4" << "A6") <<
                       "attrs" << BSON(
                          "A1" << BSON("type" << "TA1" << "value" << "S") <<
                          "A4" << BSON("type" << "TA4" << "value" << "T") <<
                          "A6" << BSON("type" << "TA6" << "value" << "U")
                          )
                      );

    BSONObj en3 = BSON("_id" << BSON("id" << "E3" << "type" << "T") <<
                       "attrNames" << BSON_ARRAY("A1" << "A2" << "A3" << "A4" << "A6" << "A7") <<
                       "attrs" << BSON(
                           "A1" << BSON("type" << "TA1" << "value" << "noise") <<
                           "A2" << BSON("type" << "TA2" << "value" << "noise") <<
                           "A3" << BSON("type" << "TA3" << "value" << "noise") <<
                           "A4" << BSON("type" << "TA4" << "value" << "noise") <<
                           "A6" << BSON("type" << "TA6" << "value" << "noise") <<
                           "A7" << BSON("type" << "TA7" << "value" << "noise")
                          )
                      );

    BSONObj sub1 = BSON("_id" << OID("51307b66f481db11bf860001") <<
                        "expiration" << 10000000 <<
                        "lastNotification" << 20000000 <<
                        "throttling" << 10 <<
                        "reference" << "http://notify1.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E1" << "type" << "T" << "isPattern" << "false") <<
                                                 BSON("id" << "E2" << "type" << "T" << "isPattern" << "false")
                                                 ) <<
                        "attrs" << BSON_ARRAY("A1" << "A3" << "A4") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("A1" << "A2" << "A4" << "A5")
                                                       )) <<
                        "count" << 20 <<
                        "format" << "XML"
                        );

    BSONObj sub2 = BSON("_id" << OID("51307b66f481db11bf860002") <<
                        "expiration" << 20000000 <<
                        "lastNotification" << 30000000 <<
                        "throttling" << 20 <<
                        "reference" << "http://notify2.me" <<
                        "entities" << BSON_ARRAY(BSON("id" << "E2" << "type" << "T" << "isPattern" << "false")) <<
                        "attrs" << BSON_ARRAY("A1" << "A3" << "A4") <<
                        "conditions" << BSON_ARRAY(BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("A1")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("A2")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("A4")
                                                       ) <<
                                                   BSON(
                                                       "type" << "ONCHANGE" <<
                                                       "value" << BSON_ARRAY("A5")
                                                       )) <<
                        "count" << 30
                        );

    connection->insert(ENTITIES_COLL, en1);
    connection->insert(ENTITIES_COLL, en2);
    connection->insert(ENTITIES_COLL, en3);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub1);
    connection->insert(SUBSCRIBECONTEXT_COLL, sub2);


}
Beispiel #8
0
void DocumentSourceOut::initialize() {
    DBClientBase* conn = pExpCtx->mongoProcessInterface->directClient();

    // Save the original collection options and index specs so we can check they didn't change
    // during computation.
    _originalOutOptions = pExpCtx->mongoProcessInterface->getCollectionOptions(_outputNs);
    _originalIndexes = conn->getIndexSpecs(_outputNs.ns());

    // Check if it's sharded or capped to make sure we have a chance of succeeding before we do all
    // the work. If the collection becomes capped during processing, the collection options will
    // have changed, and the $out will fail. If it becomes sharded during processing, the final
    // rename will fail.
    uassert(17017,
            str::stream() << "namespace '" << _outputNs.ns()
                          << "' is sharded so it can't be used for $out'",
            !pExpCtx->mongoProcessInterface->isSharded(pExpCtx->opCtx, _outputNs));
    uassert(17152,
            str::stream() << "namespace '" << _outputNs.ns()
                          << "' is capped so it can't be used for $out",
            _originalOutOptions["capped"].eoo());

    // We will write all results into a temporary collection, then rename the temporary collection
    // to be the target collection once we are done.
    _tempNs = NamespaceString(str::stream() << _outputNs.db() << ".tmp.agg_out."
                                            << aggOutCounter.addAndFetch(1));

    // Create output collection, copying options from existing collection if any.
    {
        BSONObjBuilder cmd;
        cmd << "create" << _tempNs.coll();
        cmd << "temp" << true;
        cmd.appendElementsUnique(_originalOutOptions);

        BSONObj info;
        bool ok = conn->runCommand(_outputNs.db().toString(), cmd.done(), info);
        uassert(16994,
                str::stream() << "failed to create temporary $out collection '" << _tempNs.ns()
                              << "': "
                              << info.toString(),
                ok);
    }

    // copy indexes to _tempNs
    for (std::list<BSONObj>::const_iterator it = _originalIndexes.begin();
         it != _originalIndexes.end();
         ++it) {
        MutableDocument index((Document(*it)));
        index.remove("_id");  // indexes shouldn't have _ids but some existing ones do
        index["ns"] = Value(_tempNs.ns());

        BSONObj indexBson = index.freeze().toBson();
        conn->insert(_tempNs.getSystemIndexesCollection(), indexBson);
        BSONObj err = conn->getLastErrorDetailed();
        uassert(16995,
                str::stream() << "copying index for $out failed."
                              << " index: "
                              << indexBson
                              << " error: "
                              << err,
                DBClientBase::getLastErrorString(err).empty());
    }
    _initialized = true;
}
/* ****************************************************************************
*
* prepareDatabase -
*
* This function is called before every test, to populate some information in the
* entities collection.
*/
static void prepareDatabase(void) {

  /* Set database */
  setupDatabase();

  DBClientBase* connection = getMongoConnection();

  /* We create the following entities:
   *
   * - E1:
   *     A1: val1
   *     A2: (no value)
   * - E2
   *     A3: val3
   *     A4: (no value)
   * - E3
   *     A5: val5
   *     A6: (no value)
   * - E1*:
   *     A1: val1bis2
   * - E1**:
   *     A1: val1
   *     A2: (no value)
   *
   * (*) Means that entity/type is using same name but different type. This is included to check that type is
   *     taken into account.
   *
   * (**)same name but without type
   *
   */

  BSONObj en1 = BSON("_id" << BSON("id" << "E1" << "type" << "T1") <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A1" << "type" << "TA1" << "value" << "val1") <<
                        BSON("name" << "A2" << "type" << "TA2")
                        )
                    );

  BSONObj en2 = BSON("_id" << BSON("id" << "E2" << "type" << "T2") <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A3" << "type" << "TA3" << "value" << "val3") <<
                        BSON("name" << "A4" << "type" << "TA4")
                        )
                    );

  BSONObj en3 = BSON("_id" << BSON("id" << "E3" << "type" << "T3") <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A5" << "type" << "TA5" << "value" << "val5") <<
                        BSON("name" << "A6" << "type" << "TA6")
                        )
                    );

  BSONObj en4 = BSON("_id" << BSON("id" << "E1" << "type" << "T1bis") <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A1" << "type" << "TA1" << "value" << "val1bis2")
                        )
                    );

  BSONObj en1nt = BSON("_id" << BSON("id" << "E1") <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A1" << "type" << "TA1" << "value" << "val1-nt") <<
                        BSON("name" << "A2" << "type" << "TA2")
                        )
                    );

  connection->insert(ENTITIES_COLL, en1);
  connection->insert(ENTITIES_COLL, en2);
  connection->insert(ENTITIES_COLL, en3);
  connection->insert(ENTITIES_COLL, en4);
  connection->insert(ENTITIES_COLL, en1nt);

}
/* ****************************************************************************
*
* prepareDatabase -
*
* This function is called before every test, to populate some information in the
* registrations collection.
*/
static void prepareDatabase(void)
{

  /* Set database */
  setupDatabase();

  DBClientBase* connection = getMongoConnection();

  /* We create the following registrations:
   *
   * - Reg1: CR: (E1,E2,E3) (A1,A2,A3)
   *         CR: (E1)       (A1,A4)
   * - Reg2: CR: (E2)       (A2, A3)
   * - Reg3: CR: (E1*)      (A1*)
   * - Reg4: CR: (E1**)     (A1)
   *
   * (*) same name but different types. This is included to check that type is taken into account,
   *     so Reg3 is not returned never (except noPatternNoType). You can try to change types in Reg3
   *     to make them equal to the ones in Reg1 and Reg2 and check that some tests are failing.
   * (**)same name but without type
   */

  BSONObj cr1 = BSON("providingApplication" << "http://cr1.com" <<
                     "entities" << BSON_ARRAY(
                       BSON("id" << "E1" << "type" << "T1") <<
                       BSON("id" << "E2" << "type" << "T2") <<
                       BSON("id" << "E3" << "type" << "T3")
                       ) <<
                     "attrs" << BSON_ARRAY(
                       BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true") <<
                       BSON("name" << "A2" << "type" << "TA2" << "isDomain" << "false") <<
                       BSON("name" << "A3" << "type" << "TA3" << "isDomain" << "true")
                       )
                     );
  BSONObj cr2 = BSON("providingApplication" << "http://cr2.com" <<
                     "entities" << BSON_ARRAY(
                       BSON("id" << "E1" << "type" << "T1")
                       ) <<
                     "attrs" << BSON_ARRAY(
                       BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true") <<
                       BSON("name" << "A4" << "type" << "TA4" << "isDomain" << "false")
                       )
                     );
  BSONObj cr3 = BSON("providingApplication" << "http://cr3.com" <<
                     "entities" << BSON_ARRAY(
                       BSON("id" << "E2" << "type" << "T2")
                       ) <<
                     "attrs" << BSON_ARRAY(
                       BSON("name" << "A2" << "type" << "TA2" << "isDomain" << "false") <<
                       BSON("name" << "A3" << "type" << "TA3" << "isDomain" << "true")
                       )
                     );

  BSONObj cr4 = BSON("providingApplication" << "http://cr4.com" <<
                     "entities" << BSON_ARRAY(
                       BSON("id" << "E1" << "type" << "T1bis")
                       ) <<
                     "attrs" << BSON_ARRAY(
                       BSON("name" << "A1" << "type" << "TA1bis" << "isDomain" << "false")
                       )
                     );

  BSONObj cr5 = BSON("providingApplication" << "http://cr5.com" <<
                     "entities" << BSON_ARRAY(
                       BSON("id" << "E1")
                       ) <<
                     "attrs" << BSON_ARRAY(
                       BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true")
                       )
                     );

  /* 1879048191 corresponds to year 2029 so we avoid any expiration problem in the next 16 years :) */
  BSONObj reg1 = BSON(
        "_id" << OID("51307b66f481db11bf860001") <<
        "expiration" << 1879048191 <<
        "contextRegistration" << BSON_ARRAY(cr1 << cr2)
        );

  BSONObj reg2 = BSON(
        "_id" << OID("51307b66f481db11bf860002") <<
        "expiration" << 1879048191 <<
        "contextRegistration" << BSON_ARRAY(cr3)
        );

  BSONObj reg3 = BSON(
        "_id" << OID("51307b66f481db11bf860003") <<
        "expiration" << 1879048191 <<
        "contextRegistration" << BSON_ARRAY(cr4)
        );

  BSONObj reg4 = BSON(
        "_id" << OID("51307b66f481db11bf860004") <<
        "expiration" << 1879048191 <<
        "contextRegistration" << BSON_ARRAY(cr5)
        );

  connection->insert(REGISTRATIONS_COLL, reg1);
  connection->insert(REGISTRATIONS_COLL, reg2);
  connection->insert(REGISTRATIONS_COLL, reg3);
  connection->insert(REGISTRATIONS_COLL, reg4);


}
/* ****************************************************************************
*
* prepareDatabase -
*
* This function is called before every test, to populate some information in the
* registrations collection.
*
*/
static void prepareDatabase(void) {

  /* Set database */
  setupDatabase();

  DBClientBase* connection = getMongoConnection();

  /* We create the following registrations:
   *
   * - Reg1: CR: E1 - (A1,A2,A3) - http://cr1.com
   * - Reg2: CR: E1 - A1 - http://cr1.com
   *         CR: E1 - A1 - http://cr3.com
   */

  BSONObj cr1 = BSON("providingApplication" << "http://cr1.com" <<
                     "entities" << BSON_ARRAY(
                         BSON("id" << "E1" << "type" << "T1")
                         ) <<
                     "attrs" << BSON_ARRAY(
                         BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true") <<
                         BSON("name" << "A2" << "type" << "TA2" << "isDomain" << "false") <<
                         BSON("name" << "A3" << "type" << "TA3" << "isDomain" << "true")
                         )
                     );
  BSONObj cr2 = BSON("providingApplication" << "http://cr1.com" <<
                     "entities" << BSON_ARRAY(
                         BSON("id" << "E1" << "type" << "T1")
                         ) <<
                     "attrs" << BSON_ARRAY(
                         BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true")
                         )
                     );
  BSONObj cr3 = BSON("providingApplication" << "http://cr2.com" <<
                     "entities" << BSON_ARRAY(
                         BSON("id" << "E1" << "type" << "T1")
                         ) <<
                     "attrs" << BSON_ARRAY(
                         BSON("name" << "A1" << "type" << "TA1" << "isDomain" << "true")
                         )
                     );

  BSONObjBuilder reg1;
  reg1.appendElements(BSON(
                        "_id" << OID("51307b66f481db11bf860001") <<
                        "expiration" << 10000000 <<
                        "contextRegistration" << BSON_ARRAY(cr1) <<
                        "servicePath" << "/"
                        ));

  BSONObjBuilder reg2;
  reg2.appendElements(BSON(
                        "_id" << OID("51307b66f481db11bf860002") <<
                        "expiration" << 20000000 <<
                        "contextRegistration" << BSON_ARRAY(cr2 << cr3) <<
                        "servicePath" << "/"
                        ));

  /* 1879048191 corresponds to year 2029 so we avoid any expiration problem in the next 16 years :) */
  BSONObj sub1 = BSON("_id" << OID("51307b66f481db11bf860010") <<
                      "expiration" << 1879048191 <<
                      "reference" << "http://notify1.me" <<
                      "entities" << BSON_ARRAY(BSON("id" << "E5" << "type" << "T5" << "isPattern" << "false")) <<
                      "attrs" << BSONArray());

  BSONObj sub2 = BSON("_id" << OID("51307b66f481db11bf860020") <<
                      "expiration" << 1879048191 <<
                      "reference" << "http://notify2.me" <<
                      "entities" << BSON_ARRAY(BSON("id" << "E5" << "type" << "T5" << "isPattern" << "false")) <<
                      "attrs" << BSON_ARRAY("A1"));

  connection->insert(REGISTRATIONS_COLL, reg1.obj());
  connection->insert(REGISTRATIONS_COLL, reg2.obj());

  connection->insert(SUBSCRIBECONTEXTAVAIL_COLL, sub1);
  connection->insert(SUBSCRIBECONTEXTAVAIL_COLL, sub2);

}
/* ****************************************************************************
*
* prepareDatabase -
*
* This function is called before every test, to populate some information in the
* entities collection.
*/
static void prepareDatabase(bool extraEntities = false) {

  /* Set database */
  setupDatabase();

  DBClientBase* connection = getMongoConnection();

  /* We create the following entities:
   *
   * - E1
   *     S: "running"
   *     N: 26.5
   *     D: ISODate("2017-06-17T07:12:25.823Z")
   * - E2
   *     S: "running"
   *     N: 27
   *     D: ISODate("2017-06-17T07:21:24.238Z")
   * - E3
   *     S: "shutdown"
   *     N: 31
   *     D: ISODate("2017-06-17T08:19:12.231Z")
   * - E4
   *     S: "error"
   *     N: 17.8
   *     D: ISODate("2017-06-17T07:22:43.112Z")
   * - E5
   *     S: "shutdown"
   *     N: 24
   *     D: ISODate("2017-06-17T07:10:12.328Z")
   * - C1
   *     colour=black,white
   * - C2
   *     colour=red,blue
   * - C3
   *     colour=red, blue
   */

  // FIXME: D will be set with dates once https://github.com/telefonicaid/fiware-orion/issues/1039 implementation
  // gets addressed

  BSONObj en1 = BSON("_id" << BSON("id" << "E1" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("S" << "N" << "D") <<
                     "attrs" << BSON(
                        "S" << BSON("type" << "T" << "value" << "running") <<
                        "N" << BSON("type" << "T" << "value" << 26.5) <<
                        "D" << BSON("type" << "T" << "value" << "")
                        )
                    );

  BSONObj en2 = BSON("_id" << BSON("id" << "E2" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("S" << "N" << "D") <<
                     "attrs" << BSON(
                        "S" << BSON("type" << "T" << "value" << "running") <<
                        "N" << BSON("type" << "T" << "value" << 27) <<
                        "D" << BSON("type" << "T" << "value" << "")
                        )
                    );

  BSONObj en3 = BSON("_id" << BSON("id" << "E3" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("S" << "N" << "D") <<
                     "attrs" << BSON(
                        "S" << BSON("type" << "T" << "value" << "shutdown") <<
                        "N" << BSON("type" << "T" << "value" << 31) <<
                        "D" << BSON("type" << "T" << "value" << "")
                        )
                    );

  BSONObj en4 = BSON("_id" << BSON("id" << "E4" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("S" << "N" << "D") <<
                     "attrs" << BSON(
                        "S" << BSON("type" << "T" << "value" << "error") <<
                        "N" << BSON("type" << "T" << "value" << 17.8) <<
                        "D" << BSON("type" << "T" << "value" << "")
                        )
                    );

  BSONObj en5 = BSON("_id" << BSON("id" << "E5" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("S" << "N" << "D") <<
                     "attrs" << BSON(
                        "S" << BSON("type" << "T" << "value" << "shutdown") <<
                        "N" << BSON("type" << "T" << "value" << 24) <<
                        "D" << BSON("type" << "T" << "value" << "")
                        )
                    );

  BSONObj c1 = BSON("_id" << BSON("id" << "C1" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("colour") <<
                     "attrs" << BSON(
                        "colour" << BSON("type" << "T" << "value" << "black,white")
                        )
                    );

  BSONObj c2 = BSON("_id" << BSON("id" << "C2" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("colour") <<
                     "attrs" << BSON(
                        "colour" << BSON("type" << "T" << "value" << "red,blue")
                        )
                    );

  BSONObj c3 = BSON("_id" << BSON("id" << "C3" << "type" << "T") <<
                     "attrNames" << BSON_ARRAY("colour") <<
                     "attrs" << BSON(
                        "colour" << BSON("type" << "T" << "value" << "black, white")
                        )
                    );

  connection->insert(ENTITIES_COLL, en1);
  connection->insert(ENTITIES_COLL, en2);
  connection->insert(ENTITIES_COLL, en3);
  connection->insert(ENTITIES_COLL, en4);
  connection->insert(ENTITIES_COLL, en5);
  connection->insert(ENTITIES_COLL, c1);
  connection->insert(ENTITIES_COLL, c2);
  connection->insert(ENTITIES_COLL, c3);

  if (extraEntities)
  {
    /* Adding additional entities, used in some tests
     * - E6
     *     N: 26.5
     * - E7 (no type, "")
     *     N: 27
     * - E8 (no type, <none>)
     *     N: 27
     */

    // FIXME: D will be set with dates once https://github.com/telefonicaid/fiware-orion/issues/1039 implementation
    // gets addressed

    BSONObj en6 = BSON("_id" << BSON("id" << "E6" << "type" << "T") <<
                       "attrNames" << BSON_ARRAY("S" << "N") <<
                       "attrs" << BSON(
                          "N" << BSON("type" << "T" << "value" << 26.5)
                          )
                      );

    BSONObj en7 = BSON("_id" << BSON("id" << "E7" << "type" << "") <<
                       "attrNames" << BSON_ARRAY("S" << "N") <<
                       "attrs" << BSON(
                          "N" << BSON("type" << "T" << "value" << 27)
                          )
                      );

    BSONObj en8 = BSON("_id" << BSON("id" << "E8") <<
                       "attrNames" << BSON_ARRAY("S" << "N") <<
                       "attrs" << BSON(
                          "N" << BSON("type" << "T" << "value" << 27)
                          )
                      );

    connection->insert(ENTITIES_COLL, en6);
    connection->insert(ENTITIES_COLL, en7);
    connection->insert(ENTITIES_COLL, en8);

  }

}
/*****************************************************************************
*
* processAssociations -
*
*/
static bool processAssociations(MetadataVector mdV, std::string* err, std::string tenant)
{
    // FIXME: first version of associations doesn't support association update

    DBClientBase* connection = NULL;

    for (unsigned int ix = 0; ix < mdV.size(); ++ix) {

        Metadata* md = mdV.get(ix);
        if (md->type != "Association") {
            continue;
        }

        LM_T(LmtMongo, ("Processing association metadata"));

        std::string name      = md->name;
        std::string srcEnId   = md->association.entityAssociation.source.id;
        std::string srcEnType = md->association.entityAssociation.source.type;
        std::string tgtEnId   = md->association.entityAssociation.target.id;
        std::string tgtEnType = md->association.entityAssociation.target.type;

        BSONObj srcEn;
        if (srcEnType == "") {
            srcEn = BSON(ASSOC_ENT_ID << srcEnId);
        }
        else {
            srcEn = BSON(ASSOC_ENT_ID << srcEnId << ASSOC_ENT_TYPE << srcEnType);
        }

        BSONObj tgtEn;
        if (tgtEnType == "") {
            tgtEn = BSON(ASSOC_ENT_ID << tgtEnId);
        }
        else {
            tgtEn = BSON(ASSOC_ENT_ID << tgtEnId << ASSOC_ENT_TYPE << tgtEnType);
        }

        BSONArrayBuilder attrs;
        for (unsigned int jx = 0; jx < md->association.attributeAssociationList.size(); ++jx) {
            std::string srcAtt = md->association.attributeAssociationList.get(jx)->source;
            std::string tgtAtt = md->association.attributeAssociationList.get(jx)->target;
            attrs.append(BSON(ASSOC_ATTRS_SOURCE << srcAtt << ASSOC_ATTRS_TARGET << tgtAtt));
        }

        BSONObj doc = BSON("_id" << name << ASSOC_SOURCE_ENT << srcEn << ASSOC_TARGET_ENT << tgtEn << ASSOC_ATTRS << attrs.arr());
        LM_T(LmtMongo, ("insert() in '%s' collection: '%s'", getAssociationsCollectionName(tenant).c_str(), doc.toString().c_str()));
        try
        {
            connection = getMongoConnection();
            connection->insert(getAssociationsCollectionName(tenant).c_str(), doc);
            releaseMongoConnection(connection);

            LM_I(("Database Operation Successful (%s)", doc.toString().c_str()));
        }
        catch (const DBException &e)
        {
            releaseMongoConnection(connection);

            *err = e.what();
            LM_E(("Database Error ('insert \"%s\" in %s', '%s')", doc.toString().c_str(), getAssociationsCollectionName(tenant).c_str(), e.what()));

            return false;
        }
        catch (...)
        {
            releaseMongoConnection(connection);

            *err = "Generic Exception from mongo";
            LM_E(("Database Error ('insert \"%s\" in %s', '%s')", doc.toString().c_str(), getAssociationsCollectionName(tenant).c_str(), "generic error"));

            return false;
        }
    }

    return true;
}