MetastreamTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/UnitTest.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzCore/Memory/OSAllocator.h>
  12. #include <AzCore/Memory/SystemAllocator.h>
  13. #include <platform.h>
  14. #include "Mocks/IConsoleMock.h"
  15. #include "Mocks/ICVarMock.h"
  16. #include "Mocks/ISystemMock.h"
  17. #include <Metastream_Traits_Platform.h>
  18. #include "MetastreamGem.h"
  19. using ::testing::NiceMock;
  20. using ::testing::Return;
  21. class MetastreamTestEnvironment
  22. : public AZ::Test::ITestEnvironment
  23. {
  24. public:
  25. virtual ~MetastreamTestEnvironment()
  26. {}
  27. protected:
  28. void SetupEnvironment() override
  29. {
  30. }
  31. void TeardownEnvironment() override
  32. {
  33. }
  34. };
  35. AZ_UNIT_TEST_HOOK(new MetastreamTestEnvironment)
  36. class MetastreamTest
  37. : public ::testing::Test
  38. {
  39. public:
  40. virtual ~MetastreamTest()
  41. {
  42. }
  43. const char* m_serverOptionsString = "document_root=Gems/Metastream/Files;listening_ports=8082";
  44. protected:
  45. void SetUp() override
  46. {
  47. using namespace ::testing;
  48. m_priorEnv = gEnv;
  49. m_data.reset(new DataMembers);
  50. ON_CALL(m_data->m_console, GetCVar(_))
  51. .WillByDefault(::testing::Return(&m_data->m_cvarMock));
  52. ON_CALL(m_data->m_console, RegisterString(_, _, _, _, _))
  53. .WillByDefault(::testing::Return(&m_data->m_cvarMock));
  54. ON_CALL(m_data->m_cvarMock, GetString())
  55. .WillByDefault(::testing::Return(m_serverOptionsString));
  56. memset(&m_data->m_stubEnv, 0, sizeof(SSystemGlobalEnvironment));
  57. m_data->m_stubEnv.pConsole = &m_data->m_console;
  58. m_data->m_stubEnv.pSystem = &m_data->m_system;
  59. gEnv = &m_data->m_stubEnv;
  60. }
  61. void TearDown() override
  62. {
  63. gEnv = m_priorEnv;
  64. m_data.reset();
  65. }
  66. struct DataMembers
  67. {
  68. ::testing::NiceMock<SystemMock> m_system;
  69. ::testing::NiceMock<ConsoleMock> m_console;
  70. ::testing::NiceMock<CVarMock> m_cvarMock;
  71. SSystemGlobalEnvironment m_stubEnv;
  72. };
  73. AZStd::unique_ptr<DataMembers> m_data;
  74. SSystemGlobalEnvironment* m_priorEnv = nullptr;
  75. };
  76. // allow punch through to get at protected methods so they do not need to be made PUBLIC
  77. class MetastreamTestAccessor
  78. : public Metastream::MetastreamGem
  79. {
  80. public:
  81. bool IsServerEnabled() const
  82. {
  83. return MetastreamGem::IsServerEnabled();
  84. }
  85. std::string GetDatabasesJSON() const
  86. {
  87. return MetastreamGem::GetDatabasesJSON();
  88. }
  89. };
  90. TEST_F(MetastreamTest, ServerStartupShutdownTest_FT)
  91. {
  92. MetastreamTestAccessor server;
  93. EXPECT_FALSE(server.IsServerEnabled());
  94. #if AZ_TRAIT_METASTREAM_USE_CIVET
  95. // Metastream only supported on PC
  96. EXPECT_FALSE(server.StartHTTPServer()); // fail because m_serverOptionsCVAR not declared until GAME_POST_INIT event
  97. server.OnSystemEvent(ESYSTEM_EVENT_GAME_POST_INIT, 0, 0); // fake this event to set the server options cvar
  98. EXPECT_TRUE(server.StartHTTPServer());
  99. EXPECT_TRUE(server.IsServerEnabled());
  100. EXPECT_EQ(server.GetDatabasesJSON(), "{\"tables\":[]}");
  101. server.AddBoolToCache("testtable", "testkey", true);
  102. EXPECT_EQ(server.GetDatabasesJSON(), "{\"tables\":[\"testtable\"]}");
  103. #else
  104. EXPECT_FALSE(server.StartHTTPServer());
  105. #endif
  106. // This will clear the cache
  107. server.StopHTTPServer();
  108. EXPECT_EQ(server.GetDatabasesJSON(), "{\"tables\":[]}");
  109. EXPECT_FALSE(server.IsServerEnabled());
  110. }