RIC:1060: Change in PTL
[ric-plt/sdl.git] / tst / databaseconfigurationimpl_test.cpp
index d29e663..df6dd0b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-   Copyright (c) 2018-2019 Nokia.
+   Copyright (c) 2018-2022 Nokia.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    limitations under the License.
 */
 
+/*
+ * This source code is part of the near-RT RIC (RAN Intelligent Controller)
+ * platform project (RICP).
+*/
+
 #include <gtest/gtest.h>
 #include <gmock/gmock.h>
 #include <arpa/inet.h>
@@ -71,6 +76,27 @@ TEST_F(DatabaseConfigurationImplTest, CanApplyRedisClusterDbTypeStringAndReturnT
     EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_CLUSTER, retDbType);
 }
 
+TEST_F(DatabaseConfigurationImplTest, CanApplyRedisSentinelDbTypeStringAndReturnType)
+{
+    databaseConfigurationImpl->checkAndApplyDbType("redis-sentinel");
+    const auto retDbType(databaseConfigurationImpl->getDbType());
+    EXPECT_EQ(DatabaseConfiguration::DbType::REDIS_SENTINEL, retDbType);
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplySdlStandaloneClusterDbTypeStringAndReturnType)
+{
+    databaseConfigurationImpl->checkAndApplyDbType("sdl-standalone-cluster");
+    const auto retDbType(databaseConfigurationImpl->getDbType());
+    EXPECT_EQ(DatabaseConfiguration::DbType::SDL_STANDALONE_CLUSTER, retDbType);
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplySdlSentinelClusterDbTypeStringAndReturnType)
+{
+    databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster");
+    const auto retDbType(databaseConfigurationImpl->getDbType());
+    EXPECT_EQ(DatabaseConfiguration::DbType::SDL_SENTINEL_CLUSTER, retDbType);
+}
+
 TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAddresses)
 {
     databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local");
@@ -83,6 +109,22 @@ TEST_F(DatabaseConfigurationImplTest, CanApplyNewAddressesOneByOneAndReturnAllAd
     EXPECT_EQ(65535U, ntohs(retAddresses.at(1).getPort()));
 }
 
+TEST_F(DatabaseConfigurationImplTest, CanGetAddressesOneByOneWithAddressIndex)
+{
+    databaseConfigurationImpl->checkAndApplyServerAddress("server0.local");
+    databaseConfigurationImpl->checkAndApplyServerAddress("10.20.30.40:65535");
+    const auto addresses(databaseConfigurationImpl->getServerAddresses(boost::none));
+    const auto addresses0(databaseConfigurationImpl->getServerAddresses(0));
+    const auto addresses1(databaseConfigurationImpl->getServerAddresses(1));
+    EXPECT_EQ(2U, addresses.size());
+    EXPECT_EQ(1U, addresses0.size());
+    EXPECT_EQ(1U, addresses1.size());
+    EXPECT_EQ("server0.local", addresses0.at(0).getHost());
+    EXPECT_EQ(6379U, ntohs(addresses0.at(0).getPort()));
+    EXPECT_EQ("10.20.30.40", addresses1.at(0).getHost());
+    EXPECT_EQ(65535U, ntohs(addresses1.at(0).getPort()));
+}
+
 TEST_F(DatabaseConfigurationImplTest, CanThrowIfIllegalDbTypeIsApplied)
 {
     EXPECT_THROW(databaseConfigurationImpl->checkAndApplyDbType("bad_db_type"), DatabaseConfiguration::InvalidDbType);
@@ -103,3 +145,114 @@ TEST_F(DatabaseConfigurationImplTest, IsEmptyReturnsCorrectInformation)
     databaseConfigurationImpl->checkAndApplyServerAddress("[2001::123]:12345");
     EXPECT_FALSE(databaseConfigurationImpl->isEmpty());
 }
+
+TEST_F(DatabaseConfigurationImplTest, DefaultSentinelAddressIsNone)
+{
+    EXPECT_EQ(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddress)
+{
+    databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local:1234");
+    databaseConfigurationImpl->checkAndApplySentinelPorts("51234");
+    const auto serverAddresses(databaseConfigurationImpl->getServerAddresses());
+    const auto address = databaseConfigurationImpl->getSentinelAddress(boost::none);
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
+    EXPECT_EQ("dummydatabaseaddress.local", address->getHost());
+    EXPECT_EQ(51234, ntohs(address->getPort()));
+    EXPECT_EQ(1U, serverAddresses.size());
+    EXPECT_EQ("dummydatabaseaddress.local", serverAddresses.at(0).getHost());
+    EXPECT_EQ(1234U, ntohs(serverAddresses.at(0).getPort()));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelAddressDefaultSentinelPort)
+{
+    databaseConfigurationImpl->checkAndApplyServerAddress("dummydatabaseaddress.local:1234");
+    databaseConfigurationImpl->checkAndApplySentinelPorts("");
+    const auto serverAddresses(databaseConfigurationImpl->getServerAddresses());
+    const auto address = databaseConfigurationImpl->getSentinelAddress(boost::none);
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
+    EXPECT_EQ("dummydatabaseaddress.local", address->getHost());
+    EXPECT_EQ(26379, ntohs(address->getPort()));
+    EXPECT_EQ(1U, serverAddresses.size());
+    EXPECT_EQ("dummydatabaseaddress.local", serverAddresses.at(0).getHost());
+    EXPECT_EQ(1234U, ntohs(serverAddresses.at(0).getPort()));
+}
+
+TEST_F(DatabaseConfigurationImplTest, DefaultSentinelMasterNameIsEmpty)
+{
+    EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(boost::none));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterName)
+{
+    databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster");
+    EXPECT_EQ("mymaster", databaseConfigurationImpl->getSentinelMasterName(boost::none));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnSentinelMasterNames)
+{
+    databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster-0,mymaster-1,mymaster-2");
+    EXPECT_EQ("mymaster-0", databaseConfigurationImpl->getSentinelMasterName(0));
+    EXPECT_EQ("mymaster-1", databaseConfigurationImpl->getSentinelMasterName(1));
+    EXPECT_EQ("mymaster-2", databaseConfigurationImpl->getSentinelMasterName(2));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanApplyAndReturnDefaultSentinelMasterNamesWhenNotAllMasterNamesAreSet)
+{
+    databaseConfigurationImpl->checkAndApplySentinelMasterNames("mymaster-0");
+    EXPECT_EQ("mymaster-0", databaseConfigurationImpl->getSentinelMasterName(0));
+    EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(1));
+    EXPECT_EQ("dbaasmaster", databaseConfigurationImpl->getSentinelMasterName(2));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanReturnSDLSentinelClusterAddresses)
+{
+    databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local");
+    databaseConfigurationImpl->checkAndApplySentinelPorts("54321,54322,54323");
+    auto address0 = databaseConfigurationImpl->getSentinelAddress(0);
+    auto address1 = databaseConfigurationImpl->getSentinelAddress(1);
+    auto address2 = databaseConfigurationImpl->getSentinelAddress(2);
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0));
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(1));
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(2));
+    EXPECT_EQ("cluster-0.local", address0->getHost());
+    EXPECT_EQ("cluster-1.local", address1->getHost());
+    EXPECT_EQ("cluster-2.local", address2->getHost());
+    EXPECT_EQ(54321, ntohs(address0->getPort()));
+    EXPECT_EQ(54322, ntohs(address1->getPort()));
+    EXPECT_EQ(54323, ntohs(address2->getPort()));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanReturnSDLSentinelPorts)
+{
+    databaseConfigurationImpl->checkAndApplyDbType("sdl-sentinel-cluster");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-1.local");
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-2.local");
+    databaseConfigurationImpl->checkAndApplySentinelPorts("54321");
+    auto address0 = databaseConfigurationImpl->getSentinelAddress(0);
+    auto address1 = databaseConfigurationImpl->getSentinelAddress(1);
+    auto address2 = databaseConfigurationImpl->getSentinelAddress(2);
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(0));
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(1));
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(2));
+    EXPECT_EQ("cluster-0.local", address0->getHost());
+    EXPECT_EQ("cluster-1.local", address1->getHost());
+    EXPECT_EQ("cluster-2.local", address2->getHost());
+    EXPECT_EQ(54321, ntohs(address0->getPort()));
+    EXPECT_EQ(26379, ntohs(address1->getPort()));
+    EXPECT_EQ(26379, ntohs(address2->getPort()));
+}
+
+TEST_F(DatabaseConfigurationImplTest, CanReturnDefaultSentinelPortForSDLClusterAddress)
+{
+    databaseConfigurationImpl->checkAndApplyServerAddress("cluster-0.local");
+    auto address = databaseConfigurationImpl->getSentinelAddress(boost::none);
+    EXPECT_NE(boost::none, databaseConfigurationImpl->getSentinelAddress(boost::none));
+    EXPECT_EQ("cluster-0.local", address->getHost());
+    EXPECT_EQ(26379, ntohs(address->getPort()));
+}