Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | #include "private/namespaceconfigurationsimpl.hpp" |
| 20 | |
| 21 | using namespace shareddatalayer; |
| 22 | using namespace testing; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | class NamespaceConfigurationsImplTest: public testing::Test |
| 27 | { |
| 28 | public: |
| 29 | const std::string someKnownNamespace; |
| 30 | const std::string someKnownInputSource; |
| 31 | std::unique_ptr<NamespaceConfigurationsImpl> namespaceConfigurationsImpl; |
| 32 | |
| 33 | NamespaceConfigurationsImplTest(): |
| 34 | someKnownNamespace("someKnownPrefixValue123"), |
| 35 | someKnownInputSource("someKnownInputSource") |
| 36 | { |
| 37 | InSequence dummy; |
| 38 | namespaceConfigurationsImpl.reset(new NamespaceConfigurationsImpl()); |
| 39 | } |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | TEST_F(NamespaceConfigurationsImplTest, CanMatchToLongestNamespacePrefixAndReturnItsValues) |
| 44 | { |
| 45 | namespaceConfigurationsImpl->addNamespaceConfiguration({"some", true, false, someKnownInputSource}); |
| 46 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, true, someKnownInputSource}); |
| 47 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefixs", false, false, someKnownInputSource}); |
| 48 | EXPECT_TRUE(namespaceConfigurationsImpl->isDbBackendUseEnabled(someKnownNamespace)); |
| 49 | EXPECT_TRUE(namespaceConfigurationsImpl->areNotificationsEnabled(someKnownNamespace)); |
| 50 | } |
| 51 | |
| 52 | TEST_F(NamespaceConfigurationsImplTest, CanMatchToEmptyNamespacePrefixAndReturnItsValues) |
| 53 | { |
| 54 | namespaceConfigurationsImpl->addNamespaceConfiguration({"anotherKnownPrefix", true, true, someKnownInputSource}); |
| 55 | namespaceConfigurationsImpl->addNamespaceConfiguration({"", false, false, someKnownInputSource}); |
| 56 | EXPECT_FALSE(namespaceConfigurationsImpl->isDbBackendUseEnabled(someKnownNamespace)); |
| 57 | EXPECT_FALSE(namespaceConfigurationsImpl->areNotificationsEnabled(someKnownNamespace)); |
| 58 | } |
| 59 | |
| 60 | TEST_F(NamespaceConfigurationsImplTest, CanReturnDefaultValues) |
| 61 | { |
| 62 | EXPECT_TRUE(namespaceConfigurationsImpl->isDbBackendUseEnabled(someKnownNamespace)); |
| 63 | EXPECT_FALSE(namespaceConfigurationsImpl->areNotificationsEnabled(someKnownNamespace)); |
| 64 | } |
| 65 | |
| 66 | TEST_F(NamespaceConfigurationsImplTest, CanShowReadConfigurationDescription) |
| 67 | { |
| 68 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, true, someKnownInputSource}); |
| 69 | EXPECT_EQ("someKnownInputSource prefix: someKnownPrefix, useDbBackend: true, enableNotifications: true", |
| 70 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 71 | |
| 72 | namespaceConfigurationsImpl.reset(new NamespaceConfigurationsImpl()); |
| 73 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, false, someKnownInputSource}); |
| 74 | EXPECT_EQ("someKnownInputSource prefix: someKnownPrefix, useDbBackend: true, enableNotifications: false", |
| 75 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 76 | |
| 77 | namespaceConfigurationsImpl.reset(new NamespaceConfigurationsImpl()); |
| 78 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", false, false, someKnownInputSource}); |
| 79 | EXPECT_EQ("someKnownInputSource prefix: someKnownPrefix, useDbBackend: false, enableNotifications: false", |
| 80 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 81 | } |
| 82 | |
| 83 | TEST_F(NamespaceConfigurationsImplTest, CanShowDefaultValuesDescription) |
| 84 | { |
| 85 | EXPECT_EQ("<default>, useDbBackend: true, enableNotifications: false", |
| 86 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 87 | } |
| 88 | |
| 89 | TEST_F(NamespaceConfigurationsImplTest, NamespaceIsAddedToLookupTableAfterFirstSearch) |
| 90 | { |
| 91 | EXPECT_FALSE(namespaceConfigurationsImpl->isNamespaceInLookupTable(someKnownNamespace)); |
| 92 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, true, someKnownInputSource}); |
| 93 | EXPECT_EQ("someKnownInputSource prefix: someKnownPrefix, useDbBackend: true, enableNotifications: true", |
| 94 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 95 | EXPECT_TRUE(namespaceConfigurationsImpl->isNamespaceInLookupTable(someKnownNamespace)); |
| 96 | |
| 97 | //Check that data can be correctly read also when configuration is read through lookup table |
| 98 | EXPECT_EQ("someKnownInputSource prefix: someKnownPrefix, useDbBackend: true, enableNotifications: true", |
| 99 | namespaceConfigurationsImpl->getDescription(someKnownNamespace)); |
| 100 | } |
| 101 | |
| 102 | TEST_F(NamespaceConfigurationsImplTest, DoesNotAllowConfigurationAdditionsAfterLookupTableInitialization) |
| 103 | { |
| 104 | // All configurations must be added before configurations are read. |
| 105 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, true, someKnownInputSource}); |
| 106 | EXPECT_TRUE(namespaceConfigurationsImpl->isDbBackendUseEnabled(someKnownNamespace)); |
| 107 | EXPECT_EXIT(namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix2", false, false, someKnownInputSource}), |
| 108 | KilledBySignal(SIGABRT), "ABORT.*namespaceconfigurationsimpl\\.cpp"); |
| 109 | } |
| 110 | |
| 111 | TEST_F(NamespaceConfigurationsImplTest, IsEmptyReturnsCorrectInformation) |
| 112 | { |
| 113 | EXPECT_TRUE(namespaceConfigurationsImpl->isEmpty()); |
| 114 | namespaceConfigurationsImpl->addNamespaceConfiguration({"someKnownPrefix", true, true, someKnownInputSource}); |
| 115 | EXPECT_FALSE(namespaceConfigurationsImpl->isEmpty()); |
| 116 | } |