/* ****************************************************************************
*
* entityTypeWithoutFilter -
*
*/
TEST(mongoQueryContextExistEntity, entityTypeWithoutFilter)
{
    HttpStatusCode         ms;
    QueryContextRequest   req;
    QueryContextResponse  res;

    utInit();

    /* Prepare database */
    prepareDatabase();

    /* Forge the request (from "inside" to "outside") */
    EntityId en("E1", "", "false");
    req.entityIdVector.push_back(&en);

    /* Invoke the function in mongoBackend library */
    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);

    ASSERT_EQ(2, res.contextElementResponseVector.size());
    /* Context Element response # 1 */
    EXPECT_EQ("E1", RES_CER(0).entityId.id);
    EXPECT_EQ("T1", RES_CER(0).entityId.type);
    EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
    ASSERT_EQ(1, RES_CER(0).contextAttributeVector.size());
    EXPECT_EQ("A1", RES_CER_ATTR(0, 0)->name);
    EXPECT_EQ("TA1", RES_CER_ATTR(0, 0)->type);
    EXPECT_EQ("val1", RES_CER_ATTR(0, 0)->value);
    EXPECT_EQ(SccOk, RES_CER_STATUS(0).code);
    EXPECT_EQ("OK", RES_CER_STATUS(0).reasonPhrase);
    EXPECT_EQ(0, RES_CER_STATUS(0).details.size());

    /* Context Element response # 2 */
    EXPECT_EQ("E1", RES_CER(1).entityId.id);
    EXPECT_EQ("", RES_CER(1).entityId.type);
    EXPECT_EQ("false", RES_CER(1).entityId.isPattern);
    ASSERT_EQ(1, RES_CER(1).contextAttributeVector.size());
    EXPECT_EQ("A1", RES_CER_ATTR(1, 0)->name);
    EXPECT_EQ("TA1", RES_CER_ATTR(1, 0)->type);
    EXPECT_EQ("val1b", RES_CER_ATTR(1, 0)->value);
    EXPECT_EQ(SccOk, RES_CER_STATUS(1).code);
    EXPECT_EQ("OK", RES_CER_STATUS(1).reasonPhrase);
    EXPECT_EQ(0, RES_CER_STATUS(1).details.size());

    /* Release connection */
    mongoDisconnect();

    utExit();
}
/* ****************************************************************************
*
* noPatternMultiEntity -
*
* Discover:  (E1, E2) - no attrs
* Result:    (E1, E2) - (A1, A2, A3) - http://cr1.com
*            E1       - (A1, A4)     - http://cr2.com
*            E2       - (A2, A3)     - http://cr3.com
*/
TEST(mongoContextProvidersUpdateRequest, noPatternMultiEntity)
{
  HttpStatusCode         ms;
  UpdateContextRequest   req;
  UpdateContextResponse  res;

  /* Prepare database */
  utInit();
  prepareDatabase();

  /* Forge the request (from "inside" to "outside") */
  /* Note that although it is a bit weird having an updateContext without attributes to update,
     * it is legal from the point of view of OMA spec */
  ContextElement ce1, ce2;
  ce1.entityId.fill("E1", "T1", "false");
  ce2.entityId.fill("E2", "T2", "false");
  req.contextElementVector.push_back(&ce1);
  req.contextElementVector.push_back(&ce2);
  req.updateActionType.set("UPDATE");

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

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

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

  ASSERT_EQ(2,res.contextElementResponseVector.size());
  /* Context element response #1 */
  EXPECT_EQ("E1", RES_CER(0).entityId.id);
  EXPECT_EQ("T1", RES_CER(0).entityId.type);
  EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
  ASSERT_EQ(0, RES_CER(0).contextAttributeVector.size());
  EXPECT_EQ(SccFound, RES_CER_STATUS(0).code);
  EXPECT_EQ("Found", RES_CER_STATUS(0).reasonPhrase);
  EXPECT_EQ("http://cr1.com", RES_CER_STATUS(0).details);

  /* Context element response #2 */
  EXPECT_EQ("E2", RES_CER(1).entityId.id);
  EXPECT_EQ("T2", RES_CER(1).entityId.type);
  EXPECT_EQ("false", RES_CER(1).entityId.isPattern);
  ASSERT_EQ(0, RES_CER(1).contextAttributeVector.size());
  EXPECT_EQ(SccFound, RES_CER_STATUS(1).code);
  EXPECT_EQ("Found", RES_CER_STATUS(1).reasonPhrase);
  EXPECT_EQ("http://cr1.com", RES_CER_STATUS(1).details);

  /* Release connection */
  mongoDisconnect();

  utExit();

}
/* ****************************************************************************
*
* noPatternMultiAttr -
*
* Discover:  E1 - (A3, A4, A5)
* Result:    E1 - A3 - http://cr1.com
*            E1 - A4 - http://cr2.com
*/
TEST(mongoContextProvidersUpdateRequest, noPatternMultiAttr)
{
  HttpStatusCode         ms;
  UpdateContextRequest   req;
  UpdateContextResponse  res;

  /* Prepare database */
  utInit();
  prepareDatabase();

  /* Forge the request (from "inside" to "outside") */
  ContextElement ce;
  ce.entityId.fill("E1", "T1", "false");
  ContextAttribute ca1("A3", "TA3", "new_val");
  ContextAttribute ca2("A4", "TA4", "new_val");
  ContextAttribute ca3("A5", "TA5", "new_val");
  ce.contextAttributeVector.push_back(&ca1);
  ce.contextAttributeVector.push_back(&ca2);
  ce.contextAttributeVector.push_back(&ca3);
  req.contextElementVector.push_back(&ce);
  req.updateActionType.set("UPDATE");

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

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

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

  ASSERT_EQ(1, res.contextElementResponseVector.size());
  EXPECT_EQ("E1", RES_CER(0).entityId.id);
  EXPECT_EQ("T1", RES_CER(0).entityId.type);
  EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
  ASSERT_EQ(3, RES_CER(0).contextAttributeVector.size());
  EXPECT_EQ("A3", RES_CER_ATTR(0, 0)->name);
  EXPECT_EQ("TA3", RES_CER_ATTR(0, 0)->type);
  EXPECT_EQ("A4", RES_CER_ATTR(0, 1)->name);
  EXPECT_EQ("TA4", RES_CER_ATTR(0, 1)->type);
  EXPECT_EQ("A5", RES_CER_ATTR(0, 2)->name);
  EXPECT_EQ("TA5", RES_CER_ATTR(0, 2)->type);
  EXPECT_EQ(SccFound, RES_CER_STATUS(0).code);
  EXPECT_EQ("Found", RES_CER_STATUS(0).reasonPhrase);
  EXPECT_EQ("http://cr1.com", RES_CER_STATUS(0).details);

  /* Release connection */
  mongoDisconnect();

  utExit();

}
QSqlDatabase CommHistoryDatabase::open(const QString &databaseName)
{
    QDir databaseDir(CommHistoryDatabasePath::databaseDir());
    if (!databaseDir.exists())
        databaseDir.mkpath(QLatin1String("."));

    const QString databaseFile = databaseDir.absoluteFilePath(CommHistoryDatabasePath::databaseFile());
    const bool exists = QFile::exists(databaseFile);

    QSqlDatabase database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), databaseName);
    database.setDatabaseName(databaseFile);

    if (!database.open()) {
        qWarning() << "Failed to open commhistory database";
        qWarning() << database.lastError();
        return database;
    } else {
        qWarning() << "Opened commhistory database:" << databaseFile;
    }

    for (int i = 0; i < db_setup_count; i++) {
        if (!execute(database, QLatin1String(db_setup[i]))) {
            database.close();
            if (!exists)
                QFile::remove(databaseFile);
            return database;
        }
    }

    if (!exists) {
        if (!prepareDatabase(database)) {
            database.close();
            QFile::remove(databaseFile);
        }
    } else {
        if (!execute(database, "BEGIN EXCLUSIVE TRANSACTION")) {
            database.close();
            return database;
        }

        if (!upgradeDatabase(database) || !execute(database, "END TRANSACTION")) {
            execute(database, "ROLLBACK");
            qCritical() << "Database upgrade failed! Everything may break catastrophically.";
        }
    }

    return database;
}
/* ****************************************************************************
*
* notFoundThenFound - 
*/
TEST(mapGetIndividualContextEntity, notFoundThenFound)
{
  HttpStatusCode          ms;
  ContextElementResponse  response;

  prepareDatabase("E1", "Room");

  ms = mapGetIndividualContextEntity("NotFound", &response);
  EXPECT_EQ(ms, SccOk);

  ms = mapGetIndividualContextEntity("E1", &response);
  EXPECT_EQ(SccOk, ms);
  EXPECT_STREQ("E1", response.contextElement.entityId.id.c_str());

  mongoDisconnect();
}
/* ****************************************************************************
*
* entityTypeFilterNotExist -
*
*/
TEST(mongoQueryContextExistEntity, entityTypeFilterNotExist)
{
  HttpStatusCode         ms;
  QueryContextRequest   req;
  QueryContextResponse  res;

  utInit();

  /* Prepare database */
  prepareDatabase();

  /* Forge the request (from "inside" to "outside") */
  EntityId en("E1", "", "false");
  req.entityIdVector.push_back(&en);

  /* Define filter scope */
  Scope sc;
  sc.type  = SCOPE_FILTER_EXISTENCE;
  sc.oper  = SCOPE_OPERATOR_NOT;
  sc.value = SCOPE_VALUE_ENTITY_TYPE;
  req.restriction.scopeVector.push_back(&sc);

  /* Invoke the function in mongoBackend library */
  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);

  ASSERT_EQ(1, res.contextElementResponseVector.size());
  /* Context Element response # 1 */
  EXPECT_EQ("E1", RES_CER(0).entityId.id);
  EXPECT_EQ("", RES_CER(0).entityId.type);
  EXPECT_EQ("false", RES_CER(0).entityId.isPattern);
  ASSERT_EQ(1, RES_CER(0).contextAttributeVector.size());
  EXPECT_EQ("A1", RES_CER_ATTR(0, 0)->name);
  EXPECT_EQ("TA1", RES_CER_ATTR(0, 0)->type);
  EXPECT_EQ("val1b", RES_CER_ATTR(0, 0)->stringValue);
  EXPECT_EQ(SccOk, RES_CER_STATUS(0).code);
  EXPECT_EQ("OK", RES_CER_STATUS(0).reasonPhrase);
  EXPECT_EQ("", RES_CER_STATUS(0).details);

  utExit();
}
Esempio n. 7
0
void Database::initialization()
{
  prepareDatabase();

  SQLiteDriver *driver = new SQLiteDriver();
  QSqlDatabase db = QSqlDatabase::addDatabase(driver);
  if (mainApp->storeDBMemory())
    db.setDatabaseName(":memory:");
  else
    db.setDatabaseName(mainApp->dbFileName());
  if (db.open()) {
    setPragma(db);

    if (mainApp->storeDBMemory()) {
      sqliteDBMemFile(false);
    }
  }
}
/* ****************************************************************************
*
* ok - 
*/
TEST(mapGetIndividualContextEntityAttribute, okAndNotOk)
{
  HttpStatusCode            ms;
  ContextAttributeResponse  response;

  prepareDatabase("E1", "Room");

  ms = mapGetIndividualContextEntityAttribute("E1", "A1", &response);
  EXPECT_EQ(SccOk, ms);
  EXPECT_EQ(SccOk, response.statusCode.code);
  EXPECT_EQ("A1", response.contextAttributeVector.get(0)->name);

  ms = mapGetIndividualContextEntityAttribute("NotFound", "alsoNotFound", &response);
  EXPECT_EQ(SccOk, ms);
  EXPECT_EQ(SccContextElementNotFound, response.statusCode.code);

  mongoDisconnect();
}
/* ****************************************************************************
*
* found - 
*/
TEST(mapPostIndividualContextEntity, found)
{
  HttpStatusCode                ms;
  AppendContextElementRequest   request;
  AppendContextElementResponse  response;
  ConnectionInfo                ci;

  utInit();
  prepareDatabase("MPICE", "ttt");

  request.attributeDomainName.set("ad");

  ms = mapPostIndividualContextEntity("MPICE", &request, &response, &ci);
  EXPECT_EQ(SccOk, ms);
  EXPECT_EQ(200, response.errorCode.code);

  utExit();
}
/* ****************************************************************************
*
* 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);
}