diff options
author | Pavel Zhdanov (GitHub) <39907184+ZhdanovP@users.noreply.github.com> | 2020-05-23 00:21:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-22 17:21:54 -0400 |
commit | f5cfd151d82a8c9af2e71dba6d91bd2f4ef22652 (patch) | |
tree | 720c77b4f78d8adc759ed8b0fcc85490c3e6c2d9 /src/components | |
parent | bad8f0a361a2d30911a92727a997820bfda10daa (diff) | |
download | sdl_core-f5cfd151d82a8c9af2e71dba6d91bd2f4ef22652.tar.gz |
Increase policy unit-tests speed (#2315)
* Increase speed for Policy unit-tests: External policy
Increase speed for unit-tests:
- External policy tests
- Regular policy tests
* Increase speed for Policy unit-tests: Regular policy
Co-authored-by: Collin <iCollin@users.noreply.github.com>
Diffstat (limited to 'src/components')
8 files changed, 755 insertions, 271 deletions
diff --git a/src/components/policy/policy_external/test/sql_pt_ext_representation_storage_test.cc b/src/components/policy/policy_external/test/sql_pt_ext_representation_storage_test.cc new file mode 100644 index 0000000000..9ec8235c67 --- /dev/null +++ b/src/components/policy/policy_external/test/sql_pt_ext_representation_storage_test.cc @@ -0,0 +1,124 @@ +/* Copyright (c) 2018, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtest/gtest.h" +#include "policy/sql_pt_ext_representation.h" +#include "policy/mock_policy_settings.h" +#include "utils/shared_ptr.h" +#include "utils/make_shared.h" +#include "utils/file_system.h" +#include "sqlite_wrapper/sql_query.h" + +#include <string> + +using namespace ::policy; + +using testing::ReturnRef; +using testing::NiceMock; + +namespace test { +namespace components { +namespace policy_test { + +class SQLPTExtRepresentationStorageTest : public ::testing::Test { + public: + SQLPTExtRepresentationStorageTest() : reps_(NULL) {} + + protected: + const std::string kAppStorageFolder = + "storage_SQLPTExtRepresentationStorageTest"; + + std::shared_ptr<SQLPTExtRepresentation> reps_; + std::shared_ptr<NiceMock<policy_handler_test::MockPolicySettings> > + policy_settings_; + std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; + + void SetUp() OVERRIDE { + file_system::CreateDirectory(kAppStorageFolder); + + reps_ = std::make_shared<SQLPTExtRepresentation>(); + ASSERT_TRUE(reps_ != NULL); + + policy_settings_ = + std::make_shared<NiceMock<policy_handler_test::MockPolicySettings> >(); + ON_CALL(*policy_settings_, app_storage_folder()) + .WillByDefault(ReturnRef(kAppStorageFolder)); + ASSERT_EQ(SUCCESS, reps_->Init(policy_settings_.get())); + + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps_->db()); + ASSERT_TRUE(query_wrapper_ != NULL); + } + + void TearDown() OVERRIDE { + EXPECT_TRUE(reps_->Drop()); + EXPECT_TRUE(reps_->Close()); + + file_system::remove_directory_content(kAppStorageFolder); + file_system::RemoveDirectory(kAppStorageFolder, true); + } +}; + +TEST_F( + SQLPTExtRepresentationStorageTest, + CanAppKeepContext_InsertKeepContext_ExpectValuesThatSetInKeepContextParams) { + // Arrange + const std::string query_delete = "DELETE FROM `application`; "; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_delete)); + + // Act + const std::string value_12345 = "12345"; + const std::string query_insert_12345 = + "INSERT INTO `application` (`id`, `memory_kb`," + " `heart_beat_timeout_ms`, `keep_context`) VALUES ('" + + value_12345 + "', 5, 10, 1)"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_12345)); + EXPECT_FALSE(reps_->CanAppKeepContext("0")); + EXPECT_TRUE(reps_->CanAppKeepContext(value_12345)); + + // Act + const std::string value_123 = "123"; + const std::string query_insert_123 = + "INSERT INTO `application` (`id`, `memory_kb`," + " `heart_beat_timeout_ms`, `keep_context`) VALUES ('" + + value_123 + "', 10, 7, 0)"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_123)); + EXPECT_FALSE(reps_->CanAppKeepContext(value_123)); +} + +} // namespace policy_test +} // namespace components +} // namespace test diff --git a/src/components/policy/policy_external/test/sql_pt_ext_representation_test.cc b/src/components/policy/policy_external/test/sql_pt_ext_representation_test.cc index 7d40210ba4..ce51682f3c 100644 --- a/src/components/policy/policy_external/test/sql_pt_ext_representation_test.cc +++ b/src/components/policy/policy_external/test/sql_pt_ext_representation_test.cc @@ -65,32 +65,24 @@ class SQLPTExtRepresentationTest : public ::testing::Test { SQLPTExtRepresentationTest() : reps_(NULL) {} protected: - SQLPTExtRepresentation* reps_; + std::shared_ptr<SQLPTExtRepresentation> reps_; policy_handler_test::MockPolicySettings policy_settings_; - static const string kDatabaseName; PermissionConsent perm_consent; FunctionalGroupPermission group1_perm; FunctionalGroupPermission group2_perm; - utils::dbms::SQLQuery* query_wrapper_; - static const bool in_memory_; - const std::string kAppStorageFolder = "storage_SQLPTExtRepresentationTest"; + std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; void SetUp() OVERRIDE { - file_system::DeleteFile(kDatabaseName); - reps_ = new SQLPTExtRepresentation(in_memory_); + reps_ = std::make_shared<SQLPTExtRepresentation>(true); ASSERT_TRUE(reps_ != NULL); - ON_CALL(policy_settings_, app_storage_folder()) - .WillByDefault(ReturnRef(kAppStorageFolder)); ASSERT_EQ(SUCCESS, reps_->Init(&policy_settings_)); - query_wrapper_ = new utils::dbms::SQLQuery(reps_->db()); + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps_->db()); ASSERT_TRUE(query_wrapper_ != NULL); } void TearDown() OVERRIDE { - delete query_wrapper_; EXPECT_TRUE(reps_->Drop()); EXPECT_TRUE(reps_->Close()); - delete reps_; } void FillGroupPermission( @@ -261,9 +253,6 @@ SQLPTExtRepresentationTest::GetDataInternal( return table.policy_table.functional_groupings; } -const string SQLPTExtRepresentationTest::kDatabaseName = ":memory:"; -const bool SQLPTExtRepresentationTest::in_memory_ = true; - ::testing::AssertionResult IsValid(const policy_table::Table& table) { if (table.is_valid()) { return ::testing::AssertionSuccess(); diff --git a/src/components/policy/policy_external/test/sql_pt_representation_storage_test.cc b/src/components/policy/policy_external/test/sql_pt_representation_storage_test.cc new file mode 100644 index 0000000000..6fa6bf204a --- /dev/null +++ b/src/components/policy/policy_external/test/sql_pt_representation_storage_test.cc @@ -0,0 +1,127 @@ +/* Copyright (c) 2018, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gtest/gtest.h" +#include "policy/sql_pt_representation.h" +#include "utils/file_system.h" +#include "policy/sql_wrapper.h" +#include "policy/mock_policy_settings.h" + +#include <string> +#include <memory> + +using testing::ReturnRef; +using testing::NiceMock; + +namespace test { +namespace components { +namespace policy_test { + +class SQLPTRepresentationStorageTest : public ::testing::Test { + protected: + const std::string kAppStorageFolder = + "storage_SQLPTRepresentationStorageTest"; + + std::shared_ptr<policy::SQLPTRepresentation> reps; + std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; + std::shared_ptr<policy_handler_test::MockPolicySettings> policy_settings_; + + void SetUp() { + file_system::CreateDirectory(kAppStorageFolder); + + reps = std::make_shared<policy::SQLPTRepresentation>(); + ASSERT_TRUE(reps != NULL); + + policy_settings_ = + std::make_shared<NiceMock<policy_handler_test::MockPolicySettings> >(); + ON_CALL(*policy_settings_, app_storage_folder()) + .WillByDefault(ReturnRef(kAppStorageFolder)); + EXPECT_EQ(::policy::SUCCESS, reps->Init(policy_settings_.get())); + + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps->db()); + ASSERT_TRUE(query_wrapper_ != NULL); + } + + void TearDown() OVERRIDE { + EXPECT_TRUE(reps->Drop()); + EXPECT_TRUE(reps->Close()); + reps->RemoveDB(); + + file_system::remove_directory_content(kAppStorageFolder); + file_system::RemoveDirectory(kAppStorageFolder, true); + } +}; + +TEST_F( + SQLPTRepresentationStorageTest, + CheckPermissionsAllowed_SetValuesInAppGroupRpcFunctionalGroup_GetEqualParamsInCheckPermissionResult) { + // Arrange + const std::string value_12345 = "12345"; + const std::string gps = "gps"; + const std::string speed = "speed"; + const std::string query = + "INSERT OR REPLACE INTO `application` (`id`, `memory_kb`," + " `heart_beat_timeout_ms`) VALUES ('" + + value_12345 + + "', 5, 10); " + "INSERT OR REPLACE INTO functional_group (`id`, `name`)" + " VALUES (1, 'Base-4'); " + "INSERT OR REPLACE INTO `app_group` (`application_id`," + " `functional_group_id`) VALUES ('" + + value_12345 + + "', 1); " + "INSERT OR REPLACE INTO `rpc` (`name`, `parameter`, `hmi_level_value`," + " `functional_group_id`) VALUES ('Update', '" + + gps + + "', 'FULL', 1); " + "INSERT OR REPLACE INTO `rpc` (`name`, `parameter`, `hmi_level_value`," + " `functional_group_id`) VALUES ('Update', '" + + speed + "', 'FULL', 1);"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query)); + + // Act + policy::CheckPermissionResult ret; + reps->CheckPermissions(value_12345, "FULL", "Update", ret); + + // Assert + EXPECT_TRUE(ret.hmi_level_permitted == ::policy::kRpcAllowed); + ASSERT_EQ(2u, ret.list_of_allowed_params.size()); + EXPECT_TRUE(ret.list_of_allowed_params.end() != + ret.list_of_allowed_params.find(gps)); + EXPECT_TRUE(ret.list_of_allowed_params.end() != + ret.list_of_allowed_params.find(speed)); +} + +} // namespace policy_test +} // namespace components +} // namespace test diff --git a/src/components/policy/policy_external/test/sql_pt_representation_test.cc b/src/components/policy/policy_external/test/sql_pt_representation_test.cc index 2f85f8dc0a..2cb5ec1aaf 100644 --- a/src/components/policy/policy_external/test/sql_pt_representation_test.cc +++ b/src/components/policy/policy_external/test/sql_pt_representation_test.cc @@ -66,33 +66,22 @@ namespace test { namespace components { namespace policy_test { -namespace { -const int32_t kPolicyTablesNumber = 27; -} - class SQLPTRepresentationTest : public SQLPTRepresentation, public ::testing::Test { - public: - static const bool in_memory_; - protected: - static SQLPTRepresentation* reps; - static const std::string kDatabaseName; - static utils::dbms::SQLQuery* query_wrapper_; + static std::shared_ptr<SQLPTRepresentation> reps; + static std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; // Gtest can show message that this object doesn't destroyed - static std::unique_ptr<policy_handler_test::MockPolicySettings> + static std::shared_ptr<policy_handler_test::MockPolicySettings> policy_settings_; static void SetUpTestCase() { - const std::string kAppStorageFolder = "storage_SQLPTRepresentationTest"; - reps = new SQLPTRepresentation(in_memory_); + reps = std::make_shared<SQLPTRepresentation>(true); ASSERT_TRUE(reps != NULL); - policy_settings_ = std::unique_ptr<policy_handler_test::MockPolicySettings>( - new policy_handler_test::MockPolicySettings()); - ON_CALL(*policy_settings_, app_storage_folder()) - .WillByDefault(ReturnRef(kAppStorageFolder)); + policy_settings_ = + std::make_shared<policy_handler_test::MockPolicySettings>(); EXPECT_EQ(::policy::SUCCESS, reps->Init(policy_settings_.get())); - query_wrapper_ = new utils::dbms::SQLQuery(reps->db()); + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps->db()); ASSERT_TRUE(query_wrapper_ != NULL); } @@ -101,12 +90,9 @@ class SQLPTRepresentationTest : public SQLPTRepresentation, } static void TearDownTestCase() { - delete query_wrapper_; EXPECT_TRUE(reps->Drop()); EXPECT_TRUE(reps->Close()); reps->RemoveDB(); - delete reps; - policy_settings_.reset(); } virtual utils::dbms::SQLDatabase* db() const { @@ -343,16 +329,15 @@ class SQLPTRepresentationTest : public SQLPTRepresentation, } }; -SQLPTRepresentation* SQLPTRepresentationTest::reps = 0; -utils::dbms::SQLQuery* SQLPTRepresentationTest::query_wrapper_ = 0; -const std::string SQLPTRepresentationTest::kDatabaseName = ":memory:"; -const bool SQLPTRepresentationTest::in_memory_ = true; -std::unique_ptr<policy_handler_test::MockPolicySettings> - SQLPTRepresentationTest::policy_settings_; +std::shared_ptr<SQLPTRepresentation> SQLPTRepresentationTest::reps = NULL; +std::shared_ptr<utils::dbms::SQLQuery> SQLPTRepresentationTest::query_wrapper_ = + NULL; +std::shared_ptr<policy_handler_test::MockPolicySettings> + SQLPTRepresentationTest::policy_settings_ = NULL; class SQLPTRepresentationTest2 : public ::testing::Test { protected: - SQLPTRepresentation* reps; + std::shared_ptr<SQLPTRepresentation> reps; NiceMock<policy_handler_test::MockPolicySettings> policy_settings_; virtual void SetUp() OVERRIDE { file_system::CreateDirectory(kAppStorageFolder); @@ -363,13 +348,14 @@ class SQLPTRepresentationTest2 : public ::testing::Test { .WillByDefault(Return(kOpenAttemptTimeoutMs)); ON_CALL(policy_settings_, attempts_to_open_policy_db()) .WillByDefault(Return(kAttemptsToOpenPolicyDB)); - reps = new SQLPTRepresentation; + reps = std::make_shared<SQLPTRepresentation>(); ASSERT_TRUE(reps != NULL); } virtual void TearDown() OVERRIDE { + chmod(kAppStorageFolder.c_str(), 755); file_system::RemoveDirectory(kAppStorageFolder, true); - delete reps; + reps.reset(); } const std::string kAppStorageFolder = "storage123"; const uint16_t kOpenAttemptTimeoutMs = 70u; @@ -1153,17 +1139,11 @@ TEST_F(SQLPTRepresentationTest, EXPECT_EQ("EMERGENCY", priority); } -namespace { -const std::string kAppStorageFolder = "storage"; -} TEST(SQLPTRepresentationTest3, Init_InitNewDataBase_ExpectResultSuccess) { // Arrange - const bool in_memory_ = true; NiceMock<policy_handler_test::MockPolicySettings> policy_settings_; - SQLPTRepresentation reps(in_memory_); + SQLPTRepresentation reps(true); // Checks - ON_CALL(policy_settings_, app_storage_folder()) - .WillByDefault(ReturnRef(kAppStorageFolder)); EXPECT_EQ(::policy::SUCCESS, reps.Init(&policy_settings_)); EXPECT_EQ(::policy::EXISTS, reps.Init(&policy_settings_)); reps.RemoveDB(); @@ -1185,10 +1165,7 @@ TEST(SQLPTRepresentationTest3, Close_InitNewDataBaseThenClose_ExpectResultSuccess) { // Arrange NiceMock<policy_handler_test::MockPolicySettings> policy_settings_; - ON_CALL(policy_settings_, app_storage_folder()) - .WillByDefault(ReturnRef(kAppStorageFolder)); - const bool in_memory_ = true; - SQLPTRepresentation reps(in_memory_); + SQLPTRepresentation reps(true); EXPECT_EQ(::policy::SUCCESS, reps.Init(&policy_settings_)); EXPECT_TRUE(reps.Close()); utils::dbms::SQLError error(utils::dbms::Error::OK); @@ -1630,9 +1607,8 @@ TEST_F(SQLPTRepresentationTest, TEST(SQLPTRepresentationTest3, RemoveDB_RemoveDB_ExpectFileDeleted) { // Arrange - const bool in_memory_ = true; policy_handler_test::MockPolicySettings policy_settings_; - SQLPTRepresentation reps(in_memory_); + SQLPTRepresentation reps(true); EXPECT_EQ(::policy::SUCCESS, reps.Init(&policy_settings_)); EXPECT_EQ(::policy::EXISTS, reps.Init(&policy_settings_)); std::string path = (reps.db())->get_path(); diff --git a/src/components/policy/policy_regular/include/policy/sql_pt_representation.h b/src/components/policy/policy_regular/include/policy/sql_pt_representation.h index 5bc49ee973..1bb74e028c 100644 --- a/src/components/policy/policy_regular/include/policy/sql_pt_representation.h +++ b/src/components/policy/policy_regular/include/policy/sql_pt_representation.h @@ -53,6 +53,7 @@ namespace policy { class SQLPTRepresentation : public virtual PTRepresentation { public: SQLPTRepresentation(); + explicit SQLPTRepresentation(bool in_memory); ~SQLPTRepresentation(); virtual void CheckPermissions(const PTString& app_id, const PTString& hmi_level, diff --git a/src/components/policy/policy_regular/src/sql_pt_representation.cc b/src/components/policy/policy_regular/src/sql_pt_representation.cc index c6d0b60f89..ed5bf37b5b 100644 --- a/src/components/policy/policy_regular/src/sql_pt_representation.cc +++ b/src/components/policy/policy_regular/src/sql_pt_representation.cc @@ -67,7 +67,18 @@ void InsertUnique(K value, T* array) { const std::string SQLPTRepresentation::kDatabaseName = "policy"; SQLPTRepresentation::SQLPTRepresentation() - : db_(new utils::dbms::SQLDatabase(kDatabaseName)) {} + : db_(new utils::dbms::SQLDatabase(kDatabaseName)) { + is_in_memory = false; +} + +SQLPTRepresentation::SQLPTRepresentation(bool in_memory) { + is_in_memory = in_memory; + if (in_memory) { + db_ = new utils::dbms::SQLDatabase(); + } else { + db_ = new utils::dbms::SQLDatabase(kDatabaseName); + } +} SQLPTRepresentation::~SQLPTRepresentation() { db_->Close(); @@ -299,10 +310,14 @@ InitResult SQLPTRepresentation::Init(const PolicySettings* settings) { #ifdef BUILD_TESTS open_counter_ = 0; #endif // BUILD_TESTS - std::string path = get_settings().app_storage_folder(); - if (!path.empty()) { - db_->set_path(path + "/"); + + if (!is_in_memory) { + const std::string& path = get_settings().app_storage_folder(); + if (!path.empty()) { + db_->set_path(path + "/"); + } } + if (!db_->Open()) { LOG4CXX_ERROR(logger_, "Failed opening database."); LOG4CXX_INFO(logger_, "Starting opening retries."); @@ -335,13 +350,12 @@ InitResult SQLPTRepresentation::Init(const PolicySettings* settings) { return InitResult::FAIL; } } -#ifndef __QNX__ + if (!db_->IsReadWrite()) { LOG4CXX_ERROR(logger_, "There are no read/write permissions for database"); return InitResult::FAIL; } -#endif // __QNX__ utils::dbms::SQLQuery check_pages(db()); if (!check_pages.Prepare(sql_pt::kCheckPgNumber) || !check_pages.Next()) { LOG4CXX_WARN(logger_, "Incorrect pragma for page counting."); @@ -2400,13 +2414,7 @@ const int32_t SQLPTRepresentation::GetDBVersion() const { } utils::dbms::SQLDatabase* SQLPTRepresentation::db() const { -#ifdef __QNX__ - utils::dbms::SQLDatabase* db = new utils::dbms::SQLDatabase(kDatabaseName); - db->Open(); - return db; -#else return db_; -#endif } bool SQLPTRepresentation::CopyApplication(const std::string& source, diff --git a/src/components/policy/policy_regular/test/sql_pt_representation_storage_test.cc b/src/components/policy/policy_regular/test/sql_pt_representation_storage_test.cc new file mode 100644 index 0000000000..ef713fd6fe --- /dev/null +++ b/src/components/policy/policy_regular/test/sql_pt_representation_storage_test.cc @@ -0,0 +1,130 @@ +/* Copyright (c) 2018, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <vector> +#include <string> +#include <algorithm> +#include <fstream> +#include <memory> +#include <stdio.h> +#include <sys/stat.h> + +#include "gtest/gtest.h" +#include "policy/driver_dbms.h" +#include "policy/sql_pt_representation.h" +#include "policy/policy_types.h" +#include "policy/mock_policy_settings.h" +#include "policy/policy_table/types.h" +#include "policy/policy_table/enums.h" +#include "json/writer.h" +#include "json/reader.h" +#include "rpc_base/rpc_base.h" +#include "utils/file_system.h" +#include "utils/sqlite_wrapper/sql_database.h" +#include "sqlite_wrapper/sql_query.h" + +using testing::ReturnRef; +using testing::NiceMock; + +namespace test { +namespace components { +namespace policy_test { + +class SQLPTRepresentationStorageTest : public ::testing::Test { + protected: + const std::string kAppStorageFolder = + "storage_SQLPTRepresentationStorageTest"; + + std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; + std::shared_ptr<policy::SQLPTRepresentation> reps; + std::shared_ptr<NiceMock<policy_handler_test::MockPolicySettings> > + policy_settings_; + + void SetUp() OVERRIDE { + file_system::CreateDirectory(kAppStorageFolder); + + reps = std::make_shared<policy::SQLPTRepresentation>(); + ASSERT_TRUE(reps != NULL); + + policy_settings_ = + std::make_shared<NiceMock<policy_handler_test::MockPolicySettings> >(); + ON_CALL(*policy_settings_, app_storage_folder()) + .WillByDefault(ReturnRef(kAppStorageFolder)); + ASSERT_EQ(policy::SUCCESS, reps->Init(policy_settings_.get())); + + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps->db()); + ASSERT_TRUE(query_wrapper_ != NULL); + } + + void TearDown() OVERRIDE { + EXPECT_TRUE(reps->Drop()); + EXPECT_TRUE(reps->Close()); + + file_system::remove_directory_content(kAppStorageFolder); + file_system::RemoveDirectory(kAppStorageFolder, true); + } +}; + +TEST_F( + SQLPTRepresentationStorageTest, + CheckPermissionsAllowedWithoutParameters_SetLimitedPermissions_ExpectEmptyListOfAllowedParams) { + // Arrange + const std::string value_12345 = "12345"; + const std::string query = + "INSERT OR REPLACE INTO `application` (`id`, `memory_kb`," + " `heart_beat_timeout_ms`) VALUES ('" + + value_12345 + + "', 5, 10); " + "INSERT OR REPLACE INTO functional_group (`id`, `name`)" + " VALUES (1, 'Base-4'); " + "INSERT OR REPLACE INTO `app_group` (`application_id`," + " `functional_group_id`) VALUES ('" + + value_12345 + + "', 1); " + "DELETE FROM `rpc`; " + "INSERT OR REPLACE INTO `rpc` (`name`, `hmi_level_value`," + " `functional_group_id`) VALUES ('Update', 'LIMITED', 1);"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query)); + + // Act + policy::CheckPermissionResult ret; + reps->CheckPermissions(value_12345, "LIMITED", "Update", ret); + + // Assert + EXPECT_TRUE(ret.hmi_level_permitted == ::policy::kRpcAllowed); + EXPECT_TRUE(ret.list_of_allowed_params.empty()); +} + +} // namespace policy_test +} // namespace components +} // namespace test diff --git a/src/components/policy/policy_regular/test/sql_pt_representation_test.cc b/src/components/policy/policy_regular/test/sql_pt_representation_test.cc index 30536bbaa4..367df2798c 100644 --- a/src/components/policy/policy_regular/test/sql_pt_representation_test.cc +++ b/src/components/policy/policy_regular/test/sql_pt_representation_test.cc @@ -50,6 +50,7 @@ #include "utils/file_system.h" #include "utils/sqlite_wrapper/sql_database.h" +#include "sqlite_wrapper/sql_query.h" namespace policy_table = rpc::policy_table_interface_base; using policy::CheckPermissionResult; @@ -72,35 +73,22 @@ using policy_handler_test::MockPolicySettings; class SQLPTRepresentationTest : protected SQLPTRepresentation, public ::testing::Test { protected: - DBMS* dbms; - SQLPTRepresentation* reps; - static const std::string kDatabaseName; - static const std::string kAppStorageFolder; - // Gtest can show message that this object doesn't destroyed - std::unique_ptr<NiceMock<MockPolicySettings> > policy_settings_; + std::shared_ptr<utils::dbms::SQLQuery> query_wrapper_; + std::shared_ptr<SQLPTRepresentation> reps; + policy_handler_test::MockPolicySettings policy_settings_; void SetUp() OVERRIDE { - file_system::CreateDirectory(kAppStorageFolder); - reps = new SQLPTRepresentation; - policy_settings_ = std::unique_ptr<NiceMock<MockPolicySettings> >( - new NiceMock<MockPolicySettings>()); - ON_CALL(*policy_settings_, app_storage_folder()) - .WillByDefault(ReturnRef(kAppStorageFolder)); - EXPECT_EQ(::policy::SUCCESS, reps->Init(policy_settings_.get())); - dbms = new DBMS(kAppStorageFolder + "/" + kDatabaseName); - EXPECT_TRUE(dbms->Open()); + reps = std::make_shared<SQLPTRepresentation>(true); + ASSERT_TRUE(reps != NULL); + ASSERT_EQ(policy::SUCCESS, reps->Init(&policy_settings_)); + + query_wrapper_ = std::make_shared<utils::dbms::SQLQuery>(reps->db()); + ASSERT_TRUE(query_wrapper_ != NULL); } void TearDown() OVERRIDE { - EXPECT_TRUE(reps->Clear()); EXPECT_TRUE(reps->Drop()); EXPECT_TRUE(reps->Close()); - reps->RemoveDB(); - delete reps; - dbms->Close(); - file_system::remove_directory_content(kAppStorageFolder); - file_system::RemoveDirectory(kAppStorageFolder, true); - policy_settings_.reset(); } virtual utils::dbms::SQLDatabase* db() const { @@ -160,6 +148,14 @@ class SQLPTRepresentationTest : protected SQLPTRepresentation, return ::SQLPTRepresentation::GatherNickName(app_id, nicknames); } + int FetchOneInt(const std::string& query) { + query_wrapper_->Prepare(query); + query_wrapper_->Exec(); + const int ret = query_wrapper_->GetInteger(0); + query_wrapper_->Finalize(); + return ret; + } + void CheckAppPoliciesSection( policy_table::ApplicationPoliciesSection& policies, uint16_t apps_size, @@ -374,10 +370,6 @@ class SQLPTRepresentationTest : protected SQLPTRepresentation, } }; -const std::string SQLPTRepresentationTest::kDatabaseName = "policy.sqlite"; -const std::string SQLPTRepresentationTest::kAppStorageFolder = - "storage_SQLPTRepresentationTest"; - class SQLPTRepresentationTest2 : public ::testing::Test { protected: SQLPTRepresentationTest2() @@ -415,15 +407,15 @@ class SQLPTRepresentationTest3 : public ::testing::Test { void SetUp() OVERRIDE { file_system::CreateDirectory(kAppStorageFolder); - reps = new SQLPTRepresentation; + reps = std::make_shared<SQLPTRepresentation>(); } void TearDown() OVERRIDE { file_system::RemoveDirectory(kAppStorageFolder, true); - delete reps; + reps.reset(); } - SQLPTRepresentation* reps; + std::shared_ptr<SQLPTRepresentation> reps; NiceMock<MockPolicySettings> policy_settings_; const std::string kAppStorageFolder; }; @@ -574,8 +566,10 @@ TEST_F(SQLPTRepresentationTest, VehicleDataItem_Select_NonParameterizedVDI) { TEST_F(SQLPTRepresentationTest2, DISABLED_OpenAttemptTimeOut_ExpectCorrectNumber) { EXPECT_EQ(::policy::FAIL, reps->Init(&policy_settings_)); + // Check Actual attempts number made to try to open DB EXPECT_EQ(kAttemptsToOpenPolicyDB, reps->open_counter()); + // Check timeot value correctly read from config file. EXPECT_EQ(700u, kOpenAttemptTimeoutMs); } @@ -583,74 +577,89 @@ TEST_F(SQLPTRepresentationTest2, TEST_F(SQLPTRepresentationTest, RefreshDB_DropExistedPTThenRefreshDB_ExpectTablesWithInitialData) { // Check - const char* query_select = + const std::string query_select = "SELECT COUNT(*) FROM sqlite_master WHERE `type` = 'table'"; // In normally created PT there are more than 0 tables - ASSERT_GT(dbms->FetchOneInt(query_select), 0); + ASSERT_GT(FetchOneInt(query_select), 0); ASSERT_TRUE(reps->Drop()); - ASSERT_EQ(0, dbms->FetchOneInt(query_select)); + + ASSERT_EQ(0, FetchOneInt(query_select)); ASSERT_TRUE(reps->RefreshDB()); // Check PT structure destroyed and tables number is 0 - // There are 35 tables in the database, now. + // There are 37 tables in the database, now. const int32_t total_tables_number = 37; - ASSERT_EQ(total_tables_number, dbms->FetchOneInt(query_select)); - const char* query_select_count_of_iap_buffer_full = + ASSERT_EQ(total_tables_number, FetchOneInt(query_select)); + + const std::string query_select_count_of_iap_buffer_full = "SELECT `count_of_iap_buffer_full` FROM `usage_and_error_count`"; - const char* query_select_count_sync_out_of_memory = + ASSERT_EQ(0, FetchOneInt(query_select_count_of_iap_buffer_full)); + + const std::string query_select_count_sync_out_of_memory = "SELECT `count_sync_out_of_memory` FROM `usage_and_error_count`"; - const char* query_select_count_of_sync_reboots = + ASSERT_EQ(0, FetchOneInt(query_select_count_sync_out_of_memory)); + + const std::string query_select_count_of_sync_reboots = "SELECT `count_of_sync_reboots` FROM `usage_and_error_count`"; - const char* query_select_pt_exchanged_at_odometer_x = + ASSERT_EQ(0, FetchOneInt(query_select_count_of_sync_reboots)); + + const std::string query_select_pt_exchanged_at_odometer_x = "SELECT `pt_exchanged_at_odometer_x` FROM `module_meta`"; - const char* query_select_pt_exchanged_x_days_after_epoch = + ASSERT_EQ(0, FetchOneInt(query_select_pt_exchanged_at_odometer_x)); + + const std::string query_select_pt_exchanged_x_days_after_epoch = "SELECT `pt_exchanged_x_days_after_epoch` FROM `module_meta`"; - const char* query_select_flag_update_required = + ASSERT_EQ(0, FetchOneInt(query_select_pt_exchanged_x_days_after_epoch)); + + const std::string query_select_flag_update_required = "SELECT `flag_update_required` FROM `module_meta`"; - const char* query_select_ignition_cycles_since_last_exchange = + ASSERT_EQ(0, FetchOneInt(query_select_flag_update_required)); + + const std::string query_select_ignition_cycles_since_last_exchange = "SELECT `ignition_cycles_since_last_exchange` FROM `module_meta`"; - const char* query_select_preloaded_pt = + ASSERT_EQ(0, FetchOneInt(query_select_ignition_cycles_since_last_exchange)); + + const std::string query_select_preloaded_pt = "SELECT `preloaded_pt` FROM `module_config`"; - const char* query_select_is_first_run = + ASSERT_EQ(1, FetchOneInt(query_select_preloaded_pt)); + + const std::string query_select_is_first_run = "SELECT `is_first_run` FROM `module_config`"; - const char* query_select_exchange_after_x_ignition_cycles = + ASSERT_EQ(0, FetchOneInt(query_select_is_first_run)); + + const std::string query_select_exchange_after_x_ignition_cycles = "SELECT `exchange_after_x_ignition_cycles` FROM `module_config`"; - const char* query_select_exchange_after_x_kilometers = + ASSERT_EQ(0, FetchOneInt(query_select_exchange_after_x_ignition_cycles)); + + const std::string query_select_exchange_after_x_kilometers = "SELECT `exchange_after_x_kilometers` FROM `module_config`"; - const char* query_select_exchange_after_x_days = + ASSERT_EQ(0, FetchOneInt(query_select_exchange_after_x_kilometers)); + + const std::string query_select_exchange_after_x_days = "SELECT `exchange_after_x_days` FROM `module_config`"; - const char* query_select_timeout_after_x_seconds = + ASSERT_EQ(0, FetchOneInt(query_select_exchange_after_x_days)); + + const std::string query_select_timeout_after_x_seconds = "SELECT `timeout_after_x_seconds` FROM `module_config`"; - const char* query_select_priorities = "SELECT COUNT(`value`) FROM `priority`"; - const char* query_select_hmi_levels = + ASSERT_EQ(0, FetchOneInt(query_select_timeout_after_x_seconds)); + + const std::string query_select_priorities = + "SELECT COUNT(`value`) FROM `priority`"; + ASSERT_EQ(6, FetchOneInt(query_select_priorities)); + + const std::string query_select_hmi_levels = "SELECT COUNT(`value`) FROM `hmi_level`"; - const char* query_select_version = "SELECT `number` FROM `version`"; - - ASSERT_EQ(0, dbms->FetchOneInt(query_select_count_of_iap_buffer_full)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_count_sync_out_of_memory)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_count_of_sync_reboots)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_pt_exchanged_at_odometer_x)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_pt_exchanged_x_days_after_epoch)); - ASSERT_EQ( - 0, dbms->FetchOneInt(query_select_ignition_cycles_since_last_exchange)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_flag_update_required)); - ASSERT_EQ(1, dbms->FetchOneInt(query_select_preloaded_pt)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_is_first_run)); - ASSERT_EQ(0, - dbms->FetchOneInt(query_select_exchange_after_x_ignition_cycles)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_exchange_after_x_kilometers)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_exchange_after_x_days)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_timeout_after_x_seconds)); - ASSERT_EQ(6, dbms->FetchOneInt(query_select_priorities)); - ASSERT_EQ(4, dbms->FetchOneInt(query_select_hmi_levels)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_version)); + ASSERT_EQ(4, FetchOneInt(query_select_hmi_levels)); + + const std::string query_select_version = "SELECT `number` FROM `version`"; + ASSERT_EQ(0, FetchOneInt(query_select_version)); } TEST_F( SQLPTRepresentationTest, CheckPermissionsAllowed_SetValuesInAppGroupRpcFunctionalGroup_GetEqualParamsInCheckPermissionResult) { // Arrange - const char* query = + const std::string query = "INSERT OR REPLACE INTO `application` (`id`, `memory_kb`," " `heart_beat_timeout_ms`) VALUES ('12345', 5, 10); " "INSERT OR REPLACE INTO functional_group (`id`, `name`)" @@ -663,7 +672,7 @@ TEST_F( " `functional_group_id`) VALUES ('Update', 'speed', 'FULL', 1);"; // Assert - ASSERT_TRUE(dbms->Exec(query)); + ASSERT_TRUE(query_wrapper_->Exec(query)); // Act CheckPermissionResult ret; @@ -681,7 +690,7 @@ TEST_F( SQLPTRepresentationTest, CheckPermissionsAllowedWithoutParameters_SetLimitedPermissions_ExpectEmptyListOfAllowedParams) { // Arrange - const char* query = + const std::string query = "INSERT OR REPLACE INTO `application` (`id`, `memory_kb`," " `heart_beat_timeout_ms`) VALUES ('12345', 5, 10); " "INSERT OR REPLACE INTO functional_group (`id`, `name`)" @@ -693,7 +702,7 @@ TEST_F( " `functional_group_id`) VALUES ('Update', 'LIMITED', 1);"; // Assert - ASSERT_TRUE(dbms->Exec(query)); + ASSERT_TRUE(query_wrapper_->Exec(query)); // Act CheckPermissionResult ret; @@ -708,10 +717,10 @@ TEST_F( SQLPTRepresentationTest, CheckPermissionsDisallowedWithoutParameters_DeletedAppGroupAndSetFULLLevel_ExpectHmiLevelIsDissalowed) { // Arrange - const char* query = "DELETE FROM `app_group`"; + const std::string query = "DELETE FROM `app_group`"; // Assert - ASSERT_TRUE(dbms->Exec(query)); + ASSERT_TRUE(query_wrapper_->Exec(query)); // Act CheckPermissionResult ret; @@ -725,20 +734,20 @@ TEST_F( TEST_F(SQLPTRepresentationTest, PTPReloaded_UpdateModuleConfig_ReturnIsPTPreloadedTRUE) { // Arrange - const char* query = "UPDATE `module_config` SET `preloaded_pt` = 1"; + const std::string query = "UPDATE `module_config` SET `preloaded_pt` = 1"; // Assert - ASSERT_TRUE(dbms->Exec(query)); + ASSERT_TRUE(query_wrapper_->Exec(query)); EXPECT_TRUE(reps->IsPTPreloaded()); } TEST_F(SQLPTRepresentationTest, GetUpdateUrls_DeleteAndInsertEndpoints_ExpectUpdateUrls) { // Arrange - const char* query_delete = "DELETE FROM `endpoint`; "; + const std::string query_delete = "DELETE FROM `endpoint`; "; // Assert - ASSERT_TRUE(dbms->Exec(query_delete)); + ASSERT_TRUE(query_wrapper_->Exec(query_delete)); // Act EndpointUrls ret = reps->GetUpdateUrls(7); @@ -747,14 +756,15 @@ TEST_F(SQLPTRepresentationTest, EXPECT_TRUE(ret.empty()); // Act - const char* query_insert = + const std::string query_insert = "INSERT INTO `endpoint` (`application_id`, `url`, `service`) " " VALUES ('12345', 'http://ford.com/cloud/1', 7);" "INSERT INTO `endpoint` (`application_id`, `url`, `service`) " " VALUES ('12345', 'http://ford.com/cloud/2', 7);"; // Assert - ASSERT_TRUE(dbms->Exec(query_insert)); + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + // Act ret = reps->GetUpdateUrls(7); @@ -773,13 +783,13 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithParametersOfQueryEqualZero) { // Arrange - const char* query_zeros = + const std::string query_zeros = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = 0; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = 0"; // Assert - ASSERT_TRUE(dbms->Exec(query_zeros)); + ASSERT_TRUE(query_wrapper_->Exec(query_zeros)); EXPECT_EQ(0, reps->IgnitionCyclesBeforeExchange()); // Act @@ -792,13 +802,13 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithParametersOfQueryAreLessLimit) { // Arrange - const char* query_less_limit = + const std::string query_less_limit = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = 5; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = 10"; // Assert - ASSERT_TRUE(dbms->Exec(query_less_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_less_limit)); EXPECT_EQ(5, reps->IgnitionCyclesBeforeExchange()); // Act @@ -811,16 +821,18 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithLimitCountOfParametersOfQuery) { // Arrange - const char* query_limit = + const std::string query_limit = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = 9; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = 10"; // Assert - ASSERT_TRUE(dbms->Exec(query_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_limit)); EXPECT_EQ(1, reps->IgnitionCyclesBeforeExchange()); + // Act reps->IncrementIgnitionCycles(); + // Assert EXPECT_EQ(0, reps->IgnitionCyclesBeforeExchange()); } @@ -828,13 +840,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithMoreLimitCountOfParametersOfQuery) { // Arrange - const char* query_more_limit = + const std::string query_more_limit = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = 12; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = 10"; // Assert - ASSERT_TRUE(dbms->Exec(query_more_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_more_limit)); + // Chceck EXPECT_EQ(0, reps->IgnitionCyclesBeforeExchange()); } @@ -842,13 +855,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithNegativeLimitOfParametersOfQuery) { // Arrange - const char* query_negative_limit = + const std::string query_negative_limit = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = 3; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = -1"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_limit)); + // Check EXPECT_EQ(0, reps->IgnitionCyclesBeforeExchange()); } @@ -857,13 +871,14 @@ TEST_F( SQLPTRepresentationTest, IgnitionCyclesBeforeExchange_WithNegativeLimitOfCurrentParameterOfQuery) { // Arrange - const char* query_negative_current = + const std::string query_negative_current = "UPDATE `module_meta` SET " " `ignition_cycles_since_last_exchange` = -1; " " UPDATE `module_config` SET `exchange_after_x_ignition_cycles` = 2"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_current)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_current)); + // Check EXPECT_EQ(0, reps->IgnitionCyclesBeforeExchange()); } @@ -871,13 +886,14 @@ TEST_F( TEST_F(SQLPTRepresentationTest, KilometersBeforeExchange_WithParametersOfQueryEqualZero) { // Arrange - const char* query_zeros = + const std::string query_zeros = "UPDATE `module_meta` SET " " `pt_exchanged_at_odometer_x` = 0; " " UPDATE `module_config` SET `exchange_after_x_kilometers` = 0"; // Assert - ASSERT_TRUE(dbms->Exec(query_zeros)); + ASSERT_TRUE(query_wrapper_->Exec(query_zeros)); + // Checks EXPECT_EQ(0, reps->KilometersBeforeExchange(0)); EXPECT_EQ(0, reps->KilometersBeforeExchange(-10)); @@ -887,13 +903,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, KilometersBeforeExchange_QueryWithNegativeLimit) { // Arrange - const char* query_negative_limit = + const std::string query_negative_limit = "UPDATE `module_meta` SET " " `pt_exchanged_at_odometer_x` = 10; " " UPDATE `module_config` SET `exchange_after_x_kilometers` = -10"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_limit)); + // Checks EXPECT_EQ(0, reps->KilometersBeforeExchange(0)); EXPECT_EQ(0, reps->KilometersBeforeExchange(10)); @@ -902,13 +919,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, KilometersBeforeExchange_QueryWithNegativeCurrentLimit) { // Arrange - const char* query_negative_last = + const std::string query_negative_last = "UPDATE `module_meta` SET " " `pt_exchanged_at_odometer_x` = -10; " " UPDATE `module_config` SET `exchange_after_x_kilometers` = 20"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_last)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_last)); + // Checks EXPECT_EQ(0, reps->KilometersBeforeExchange(0)); EXPECT_EQ(0, reps->KilometersBeforeExchange(10)); @@ -917,13 +935,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, KilometersBeforeExchange_QueryWithLimitParameters) { // Arrange - const char* query_limit = + const std::string query_limit = "UPDATE `module_meta` SET " " `pt_exchanged_at_odometer_x` = 10; " " UPDATE `module_config` SET `exchange_after_x_kilometers` = 100"; // Assert - ASSERT_TRUE(dbms->Exec(query_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_limit)); + // Checks EXPECT_EQ(0, reps->KilometersBeforeExchange(120)); EXPECT_EQ(60, reps->KilometersBeforeExchange(50)); @@ -933,13 +952,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, DaysBeforeExchange_WithParametersOfQueryEqualZero) { // Arrange - const char* query_zeros = + const std::string query_zeros = "UPDATE `module_meta` SET " " `pt_exchanged_x_days_after_epoch` = 0; " " UPDATE `module_config` SET `exchange_after_x_days` = 0"; // Assert - ASSERT_TRUE(dbms->Exec(query_zeros)); + ASSERT_TRUE(query_wrapper_->Exec(query_zeros)); + // Checks EXPECT_EQ(0, reps->DaysBeforeExchange(0)); EXPECT_EQ(0, reps->DaysBeforeExchange(-10)); @@ -948,13 +968,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, DaysBeforeExchange_QueryWithNegativeLimit) { // Arrange - const char* query_negative_limit = + const std::string query_negative_limit = "UPDATE `module_meta` SET " " `pt_exchanged_x_days_after_epoch` = 10; " " UPDATE `module_config` SET `exchange_after_x_days` = -10"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_limit)); + // Checks EXPECT_EQ(0, reps->DaysBeforeExchange(0)); EXPECT_EQ(0, reps->DaysBeforeExchange(10)); @@ -963,13 +984,14 @@ TEST_F(SQLPTRepresentationTest, DaysBeforeExchange_QueryWithNegativeLimit) { TEST_F(SQLPTRepresentationTest, DaysBeforeExchange_QueryWithNegativeCurrentLimit) { // Arrange - const char* query_negative_last = + const std::string query_negative_last = "UPDATE `module_meta` SET " " `pt_exchanged_x_days_after_epoch` = -10; " " UPDATE `module_config` SET `exchange_after_x_days` = 20"; // Assert - ASSERT_TRUE(dbms->Exec(query_negative_last)); + ASSERT_TRUE(query_wrapper_->Exec(query_negative_last)); + // Checks EXPECT_EQ(0, reps->DaysBeforeExchange(0)); EXPECT_EQ(0, reps->DaysBeforeExchange(10)); @@ -977,13 +999,14 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, DaysBeforeExchange_QueryWithLimitParameters) { // Arrange - const char* query_limit = + const std::string query_limit = "UPDATE `module_meta` SET " " `pt_exchanged_x_days_after_epoch` = 10; " " UPDATE `module_config` SET `exchange_after_x_days` = 100"; // Assert - ASSERT_TRUE(dbms->Exec(query_limit)); + ASSERT_TRUE(query_wrapper_->Exec(query_limit)); + // Checks EXPECT_EQ(0, reps->DaysBeforeExchange(120)); EXPECT_EQ(60, reps->DaysBeforeExchange(50)); @@ -995,23 +1018,24 @@ TEST_F( SecondsBetweenRetries_DeletedAndInsertedSecondsBetweenRetry_ExpectCountOfSecondsEqualInserted) { // Arrange std::vector<int> seconds; - const char* query_delete = "DELETE FROM `seconds_between_retry`; "; + const std::string query_delete = "DELETE FROM `seconds_between_retry`; "; // Assert - ASSERT_TRUE(dbms->Exec(query_delete)); + ASSERT_TRUE(query_wrapper_->Exec(query_delete)); ASSERT_TRUE(reps->SecondsBetweenRetries(&seconds)); EXPECT_EQ(0u, seconds.size()); // Arrange - const char* query_insert = + const std::string query_insert = "INSERT INTO `seconds_between_retry` (`index`, `value`) " " VALUES (0, 10); " "INSERT INTO `seconds_between_retry` (`index`, `value`) " " VALUES (1, 20); "; // Assert - ASSERT_TRUE(dbms->Exec(query_insert)); + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); ASSERT_TRUE(reps->SecondsBetweenRetries(&seconds)); + // Checks ASSERT_EQ(2u, seconds.size()); EXPECT_EQ(10, seconds[0]); @@ -1020,11 +1044,12 @@ TEST_F( TEST_F(SQLPTRepresentationTest, TimeoutResponse_Set60Seconds_GetEqualTimeout) { // Arrange - const char* query = + const std::string query = "UPDATE `module_config` SET `timeout_after_x_seconds` = 60"; // Assert - ASSERT_TRUE(dbms->Exec(query)); + ASSERT_TRUE(query_wrapper_->Exec(query)); + // Check EXPECT_EQ(60000, reps->TimeoutResponse()); } @@ -1032,8 +1057,12 @@ TEST_F(SQLPTRepresentationTest, TimeoutResponse_Set60Seconds_GetEqualTimeout) { TEST_F(SQLPTRepresentationTest, IsPTPreloaded_SetPTPreloadedThenCheck_ExpectCorrectValue) { // Arrange - const char* query_insert = "UPDATE `module_config` SET `preloaded_pt` = 1"; - ASSERT_TRUE(dbms->Exec(query_insert)); + const std::string query_insert = + "UPDATE `module_config` SET `preloaded_pt` = 1"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + // Check ASSERT_TRUE(reps->IsPTPreloaded()); } @@ -1042,65 +1071,81 @@ TEST_F( SQLPTRepresentationTest, SetCountersPassedForSuccessfulUpdate_SetCounters_ExpectValueChangedInPT) { // Arrange - const char* query_select_odometer = + const std::string query_select_odometer = "SELECT `pt_exchanged_at_odometer_x` FROM`module_meta`"; - const char* query_select_days_after_epoch = + const std::string query_select_days_after_epoch = "SELECT `pt_exchanged_x_days_after_epoch` FROM`module_meta`"; - ASSERT_EQ(0, dbms->FetchOneInt(query_select_odometer)); - ASSERT_EQ(0, dbms->FetchOneInt(query_select_days_after_epoch)); + ASSERT_EQ(0, FetchOneInt(query_select_odometer)); + ASSERT_EQ(0, FetchOneInt(query_select_days_after_epoch)); // Act ASSERT_TRUE(reps->SetCountersPassedForSuccessfulUpdate(100, 10000)); - ASSERT_EQ(100, dbms->FetchOneInt(query_select_odometer)); - ASSERT_EQ(10000, dbms->FetchOneInt(query_select_days_after_epoch)); + ASSERT_EQ(100, FetchOneInt(query_select_odometer)); + ASSERT_EQ(10000, FetchOneInt(query_select_days_after_epoch)); } TEST_F( SQLPTRepresentationTest, IncrementIgnitionCycles_SetIgnitionCyclesValueThenIncrement_ExpectValueIncrementedInPT) { // Arrange - const char* query_insert = + const std::string query_insert = "UPDATE `module_meta` SET `ignition_cycles_since_last_exchange` = 54"; - const char* query_select = + const std::string query_select = "SELECT `ignition_cycles_since_last_exchange`FROM `module_meta`"; - ASSERT_TRUE(dbms->Exec(query_insert)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + // Act reps->IncrementIgnitionCycles(); + // Check - ASSERT_EQ(55, dbms->FetchOneInt(query_select)); + ASSERT_EQ(55, FetchOneInt(query_select)); } TEST_F( SQLPTRepresentationTest, ResetIgnitionCycles_SetIgnitionCyclesValueThenReset_ExpectZeroValueInPT) { // Arrange - const char* query_insert = + const std::string query_insert = "UPDATE `module_meta` SET `ignition_cycles_since_last_exchange` = 55"; - const char* query_select = + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + + const std::string query_select = "SELECT `ignition_cycles_since_last_exchange` FROM `module_meta`"; - ASSERT_TRUE(dbms->Exec(query_insert)); + // Act reps->ResetIgnitionCycles(); + // Check - ASSERT_EQ(0, dbms->FetchOneInt(query_select)); + ASSERT_EQ(0, FetchOneInt(query_select)); } TEST_F(SQLPTRepresentationTest, GetUserFriendlyMsg_SetMsg_ExpectReceivedMsgSetInParams) { // Arrange - const char* query_insert = + const std::string query_insert_language_code = "INSERT INTO `message` (`language_code`, `message_type_name`) VALUES " "('en-en', 'AppPermissions')"; - ASSERT_TRUE(dbms->Exec(query_insert)); - query_insert = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_language_code)); + + const std::string query_insert_name = "INSERT INTO `message_type` (`name`) VALUES ('AppPermissions')"; - ASSERT_TRUE(dbms->Exec(query_insert)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_name)); + std::vector<std::string> msg_code; msg_code.push_back("AppPermissions"); + // Act std::vector<UserFriendlyMessage> result = reps->GetUserFriendlyMsg(msg_code, std::string("en-en")); + // Checks ASSERT_EQ(1u, result.size()); EXPECT_EQ(result[0].message_code, "AppPermissions"); @@ -1110,13 +1155,14 @@ TEST_F( SQLPTRepresentationTest, GetNotificationNumber_SetNotificationsPriorities_ExpectReceivedValuesCorrect) { // Arrange - const char* query_insert = + const std::string query_insert = "INSERT INTO `notifications_by_priority` (`priority_value`, `value`) " "VALUES ('NAVIGATION', 15) , " "('COMMUNICATION', 6), ('EMERGENCY', 60), ('NONE', 0), ('NORMAL', 4), " "('VOICECOMMUNICATION', 20)"; - ASSERT_TRUE(dbms->Exec(query_insert)); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); EXPECT_EQ(6, reps->GetNotificationsNumber("COMMUNICATION")); EXPECT_EQ(60, reps->GetNotificationsNumber("EMERGENCY")); EXPECT_EQ(15, reps->GetNotificationsNumber("NAVIGATION")); @@ -1128,7 +1174,7 @@ TEST_F( TEST_F(SQLPTRepresentationTest, GetPriority_SetAppsPrioritiesThenGet_ExpectReceivedValuesCorrect) { // Arrange - const char* query_insert_app = + const std::string query_insert_app_default = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1136,9 +1182,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'default', 0, 0, 'NONE', 'NONE', 0, " "0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_default)); + + const std::string query_insert_app_predataconsent = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1146,9 +1194,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'pre_DataConsent', 0, 0, 'NONE', " "'NONE', 0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_predataconsent)); + + const std::string query_insert_app_device = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1156,9 +1206,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'device', 0, 0, 'NONE', " "'COMMUNICATION', 0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_device)); + + const std::string query_insert_app_12345 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1166,7 +1218,9 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '12345', 0, 0, 'NONE', 'EMERGENCY', " "0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_12345)); std::string priority; // Checks @@ -1216,7 +1270,7 @@ TEST_F(SQLPTRepresentationTest3, TEST_F(SQLPTRepresentationTest, Clear_InitNewDataBaseThenClear_ExpectResultSuccess) { // Arrange - const char* query_insert_app = + const std::string query_insert_app_default = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1224,9 +1278,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'default', 0, 0, 'NONE', 'NONE', 0, " "0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_default)); + + const std::string query_insert_app_predataconsent = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1234,9 +1290,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'pre_DataConsent', 0, 0, 'NONE', " "'NONE', 0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_predataconsent)); + + const std::string query_insert_app_device = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1244,9 +1302,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'device', 0, 0, 'NONE', " "'COMMUNICATION', 0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_device)); + + const std::string query_insert_app_12345 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1254,15 +1314,18 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '12345', 0, 0, 'NONE', 'EMERGENCY', " "0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - const char* query_insert = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_12345)); + + const std::string query_insert = "INSERT INTO `notifications_by_priority` (`priority_value`, `value`) " "VALUES ('NAVIGATION', 15) , " "('COMMUNICATION', 6), ('EMERGENCY', 60), ('NONE', 0), ('NORMAL', 4), " "('VOICECOMMUNICATION', 20)"; - ASSERT_TRUE(dbms->Exec(query_insert)); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); EXPECT_TRUE(reps->Clear()); utils::dbms::SQLError error(utils::dbms::Error::OK); EXPECT_EQ(error.number(), (reps->db()->LastError().number())); @@ -1271,17 +1334,22 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, GetInitialAppData_SetData_ExpectCorrectValuesReceived) { // Arrange - const char* query_insert = + const std::string query_insert_nickname = "INSERT INTO `nickname` (`application_id`, `name`) " "VALUES ('1111', 'first_app') , " "('2222', 'second_app'), ('3333', 'third_app')"; - ASSERT_TRUE(dbms->Exec(query_insert)); - query_insert = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_nickname)); + + const std::string query_insert_app_type = "INSERT INTO `app_type` (`application_id`, `name`)" "VALUES ('1111', 'NAVIGATION') , " "('1111', 'MEDIA'), ('3333', 'COMMUNICATION')"; - ASSERT_TRUE(dbms->Exec(query_insert)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_type)); + ::policy::StringArray nicknames; ::policy::StringArray app_types; ASSERT_TRUE(reps->GetInitialAppData("1111", &nicknames, &app_types)); @@ -1315,22 +1383,28 @@ TEST_F( SQLPTRepresentationTest, GetFunctionalGroupings_SetFunctionalGroupings_ExpectCorrectValuesReceived) { // Arrange - const char* query_insert = + const std::string query_insert_functional_group = "INSERT INTO `functional_group` (`id`, `user_consent_prompt`, `name`) " "VALUES (73072936, null, 'SendLocation'), (1533011474, null, " "'OnKeyboardInputOnlyGroup')"; - ASSERT_TRUE(dbms->Exec(query_insert)); - query_insert = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_functional_group)); + + const std::string query_insert_rpc_sendlocation = "INSERT INTO `rpc` (`name`, `hmi_level_value`, `functional_group_id`) " "VALUES ('SendLocation', 'BACKGROUND', 73072936), ('SendLocation', " "'FULL', 73072936), ('SendLocation', 'LIMITED', 73072936)"; - ASSERT_TRUE(dbms->Exec(query_insert)); - query_insert = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_rpc_sendlocation)); + + const std::string query_insert_rpc_onkeyboard_input = "INSERT INTO `rpc` (`name`, `hmi_level_value`, `functional_group_id`) " "VALUES ('OnKeyboardInput', 'FULL', 1533011474)"; - ASSERT_TRUE(dbms->Exec(query_insert)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_rpc_onkeyboard_input)); policy_table::FunctionalGroupings func_groups; ASSERT_TRUE(reps->GetFunctionalGroupings(func_groups)); @@ -1383,10 +1457,12 @@ TEST_F( TEST_F(SQLPTRepresentationTest, UpdateRequired_SetUpdateRequiredFlagThenCheck_ExpectUpdateRequired) { // Arrange - const char* query_insert = + const std::string query_insert = "UPDATE `module_meta` SET `flag_update_required` = 1"; + // Assert - ASSERT_TRUE(dbms->Exec(query_insert)); + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + // Check EXPECT_TRUE(reps->UpdateRequired()); } @@ -1395,10 +1471,13 @@ TEST_F(SQLPTRepresentationTest, SaveUpdateRequired_SaveUpdateRequired_ExpectCorrectValues) { // Arrange reps->SaveUpdateRequired(true); + // Check EXPECT_TRUE(reps->UpdateRequired()); + // Act reps->SaveUpdateRequired(false); + // Check EXPECT_FALSE(reps->UpdateRequired()); } @@ -1406,7 +1485,7 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IsApplicationRepresented_Check_ExpectCorrectResult) { // Arrange - const char* query_insert_app = + const std::string query_insert_app_default = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1414,18 +1493,22 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'default', 0, 0, 'NONE', 'NONE', 0, " "0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_default)); + + const std::string query_insert_app_device = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " "`is_predata`, `memory_kb`, " " `heart_beat_timeout_ms`) VALUES( 'device', 0, 0, 'NONE', " "'COMMUNICATION', 0, 0, 0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_device)); + + const std::string query_insert_app_12345 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1433,7 +1516,10 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '12345', 0, 0, 'NONE', 'EMERGENCY', " "0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_12345)); + // Checks EXPECT_TRUE(reps->IsApplicationRepresented("default")); EXPECT_TRUE(reps->IsApplicationRepresented("device")); @@ -1444,7 +1530,7 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IsApplicationRevoked_CheckApps_ExpectCorrectResult) { // Arrange - const char* query_insert_app = + const std::string query_insert_app_7777 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1452,9 +1538,11 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '7777', 0, 0, 'NONE', 'NONE', 1, " "0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_7777)); + + const std::string query_insert_app_12345 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1462,7 +1550,10 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '12345', 0, 0, 'NONE', 'EMERGENCY', " "0, 0, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_12345)); + // Checks EXPECT_TRUE(reps->IsApplicationRevoked("7777")); EXPECT_FALSE(reps->IsApplicationRevoked("12345")); @@ -1471,7 +1562,7 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, CopyApplication_CopyApplication_ExpectAppCopiedSuccesfully) { // Arrange - const char* query_insert_app = + const std::string query_insert_app_default = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1479,21 +1570,28 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( 'default', 0, 0, 'NONE', 'NONE', 0, " "1, " "0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - query_insert_app = + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_default)); + + const std::string query_insert_app_123 = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " "`is_predata`, `memory_kb`, " " `heart_beat_timeout_ms`) VALUES( '123', 1, 0, 'FULL', " "'COMMUNICATION', 1, 1, 0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app_123)); + EXPECT_FALSE(reps->IsApplicationRepresented("7777")); EXPECT_FALSE(reps->IsApplicationRepresented("9999")); + // Act EXPECT_TRUE(reps->CopyApplication("default", "7777")); EXPECT_TRUE(reps->CopyApplication("123", "9999")); + // Checks EXPECT_TRUE(reps->IsApplicationRepresented("7777")); EXPECT_TRUE(reps->IsApplicationRepresented("9999")); @@ -1513,7 +1611,7 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IsDefaultPolicy_SetAppPreDataThenCheck_ExpectNotDefaultPolicySet) { // Arrange - const char* query_insert_app = + const std::string query_insert_app = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, " " `default_hmi`, `priority_value`, `is_revoked`, `is_default`, " @@ -1521,7 +1619,10 @@ TEST_F(SQLPTRepresentationTest, " `heart_beat_timeout_ms`) VALUES( '12345', 0, 0, 'NONE', 'NONE', 0, " "0, " "1, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app)); + // Check EXPECT_FALSE(reps->IsDefaultPolicy("12345")); } @@ -1529,28 +1630,35 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, IsDefaultPolicy_SetAppDefaultThenCheck_ExpectNotDefaultPolicySet) { // Arrange - const char* query_insert_app = + const std::string query_insert_app = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, `default_hmi`, `priority_value`, `is_revoked`, " "`is_default`, `is_predata`, `memory_kb`, `heart_beat_timeout_ms`) " "VALUES( '1234567', 0, 0, 'NONE', 'NONE', 0, 1, 0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app)); + // Check EXPECT_TRUE(reps->IsDefaultPolicy("1234567")); } TEST_F(SQLPTRepresentationTest, Drop_DropExistedPT_ExpectZeroTables) { // Check - const char* query_select = + const std::string query_select = "SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = 'table'"; // In normally created PT there are more than 0 tables - ASSERT_TRUE(dbms->Exec(query_select)); - ASSERT_GT(dbms->FetchOneInt(query_select), 0); + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_select)); + ASSERT_GT(FetchOneInt(query_select), 0); + // Destroy schema ASSERT_TRUE(reps->Drop()); + // Check PT structure destroyed and tables number is 0 - ASSERT_EQ(0, dbms->FetchOneInt(query_select)); + ASSERT_EQ(0, FetchOneInt(query_select)); + // Restore schema ASSERT_TRUE(reps->RefreshDB()); } @@ -1572,21 +1680,24 @@ TEST_F(SQLPTRepresentationTest, "VALUES( '" + kDefaultId + "', 0, 0, 'NONE', 'NONE', 0, 0, 0, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_default_app.c_str())); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_default_app)); const std::string query_insert_default_app_request_types = "INSERT INTO `request_type` (`application_id`, `request_type`) " "VALUES ('" + kDefaultId + "', '" + kRequestType + "')"; - ASSERT_TRUE(dbms->Exec(query_insert_default_app_request_types.c_str())); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_default_app_request_types)); const std::string query_insert_default_app_hmi_types = "INSERT INTO `app_type` (`application_id`, `name`) " "VALUES ('" + kDefaultId + "', '" + kHmiType + "')"; - ASSERT_TRUE(dbms->Exec(query_insert_default_app_hmi_types.c_str())); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_default_app_hmi_types)); const std::string query_insert_new_app = "INSERT INTO `application`(`id`, `keep_context`, " @@ -1595,11 +1706,14 @@ TEST_F(SQLPTRepresentationTest, "VALUES('" + kAppId + "', 0, 0, 'NONE', 'NONE', 0, 0, 1, 64, 10)"; - ASSERT_TRUE(dbms->Exec(query_insert_new_app.c_str())); + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_new_app)); EXPECT_FALSE(reps->IsDefaultPolicy(kAppId)); + // Act ASSERT_TRUE(reps->SetDefaultPolicy(kAppId)); + // Check EXPECT_TRUE(reps->IsDefaultPolicy(kAppId)); @@ -1617,16 +1731,24 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, SetPreloaded_SetPreloaded_ExpectPTSetToPreloaded) { // Arrange - const char* query_insert = "UPDATE `module_config` SET `preloaded_pt` = 0"; - ASSERT_TRUE(dbms->Exec(query_insert)); + const std::string query_insert = + "UPDATE `module_config` SET `preloaded_pt` = 0"; + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert)); + // Check ASSERT_FALSE(reps->IsPTPreloaded()); + // Act reps->SetPreloaded(true); + // Check ASSERT_TRUE(reps->IsPTPreloaded()); + // Act reps->SetPreloaded(false); + // Check ASSERT_FALSE(reps->IsPTPreloaded()); } @@ -1634,23 +1756,30 @@ TEST_F(SQLPTRepresentationTest, TEST_F(SQLPTRepresentationTest, SetIsDefault_SetIsDefault_ExpectDefaultFlagSet) { // Arrange - const char* query_insert_app = + const std::string query_insert_app = "INSERT OR IGNORE INTO `application`(`id`, `keep_context`, " "`steal_focus`, `default_hmi`, `priority_value`, `is_revoked`, " "`is_default`, `is_predata`, `memory_kb`, `heart_beat_timeout_ms`) " "VALUES( '1234567', 0, 0, 'NONE', 'NONE', 0, 0, 1, 64, 10) "; - ASSERT_TRUE(dbms->Exec(query_insert_app)); - const char* query_select = + + // Assert + ASSERT_TRUE(query_wrapper_->Exec(query_insert_app)); + + const std::string query_select = "SELECT `is_default` FROM `application`WHERE`id`= '1234567' "; - EXPECT_EQ(0, dbms->FetchOneInt(query_select)); + EXPECT_EQ(0, FetchOneInt(query_select)); + // Act EXPECT_TRUE(reps->SetIsDefault("1234567", true)); + // Check - EXPECT_EQ(1, dbms->FetchOneInt(query_select)); + EXPECT_EQ(1, FetchOneInt(query_select)); + // Act EXPECT_TRUE(reps->SetIsDefault("1234567", false)); + // Check - EXPECT_EQ(0, dbms->FetchOneInt(query_select)); + EXPECT_EQ(0, FetchOneInt(query_select)); } TEST_F(SQLPTRepresentationTest3, RemoveDB_RemoveDB_ExpectFileDeleted) { |