AWSGameLiftServerMocks.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #pragma once
  9. #include <AWSGameLiftPlayer.h>
  10. #include <AWSGameLiftServerSystemComponent.h>
  11. #include <AWSGameLiftServerManager.h>
  12. #include <GameLiftServerSDKWrapper.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <AzCore/std/smart_ptr/make_shared.h>
  15. using namespace Aws::GameLift;
  16. using namespace AWSGameLift;
  17. using testing::_;
  18. using testing::Invoke;
  19. using testing::Return;
  20. using testing::NiceMock;
  21. using testing::Eq;
  22. namespace UnitTest
  23. {
  24. class GameLiftServerSDKWrapperMock
  25. : public GameLiftServerSDKWrapper
  26. {
  27. public:
  28. GameLiftServerSDKWrapperMock()
  29. {
  30. GenericOutcome successOutcome(nullptr);
  31. Server::InitSDKOutcome sdkOutcome(nullptr);
  32. ON_CALL(*this, InitSDK(_)).WillByDefault(Return(sdkOutcome));
  33. ON_CALL(*this, ProcessReady(_)).WillByDefault(Invoke(this, &GameLiftServerSDKWrapperMock::ProcessReadyMock));
  34. ON_CALL(*this, ProcessEnding()).WillByDefault(Return(successOutcome));
  35. }
  36. MOCK_METHOD1(AcceptPlayerSession, GenericOutcome(const std::string&));
  37. MOCK_METHOD0(ActivateGameSession, GenericOutcome());
  38. MOCK_METHOD1(DescribePlayerSessions, DescribePlayerSessionsOutcome(
  39. const Aws::GameLift::Server::Model::DescribePlayerSessionsRequest&));
  40. MOCK_METHOD1(InitSDK, Server::InitSDKOutcome(Server::Model::ServerParameters));
  41. MOCK_METHOD1(ProcessReady, GenericOutcome(const Server::ProcessParameters& processParameters));
  42. MOCK_METHOD0(ProcessEnding, GenericOutcome());
  43. MOCK_METHOD1(RemovePlayerSession, GenericOutcome(const AZStd::string& playerSessionId));
  44. MOCK_METHOD0(GetComputeCertificate, Aws::GameLift::GetComputeCertificateOutcome());
  45. MOCK_METHOD0(GetTerminationTime, AZStd::string());
  46. MOCK_METHOD1(StartMatchBackfill, StartMatchBackfillOutcome(
  47. const Aws::GameLift::Server::Model::StartMatchBackfillRequest&));
  48. MOCK_METHOD1(StopMatchBackfill, GenericOutcome(
  49. const Aws::GameLift::Server::Model::StopMatchBackfillRequest&));
  50. GenericOutcome ProcessReadyMock(const Server::ProcessParameters& processParameters)
  51. {
  52. m_healthCheckFunc = processParameters.getOnHealthCheck();
  53. m_onStartGameSessionFunc = processParameters.getOnStartGameSession();
  54. m_onProcessTerminateFunc = processParameters.getOnProcessTerminate();
  55. m_onUpdateGameSessionFunc = processParameters.getOnUpdateGameSession();
  56. GenericOutcome successOutcome(nullptr);
  57. return successOutcome;
  58. }
  59. AZStd::function<bool()> m_healthCheckFunc;
  60. AZStd::function<void()> m_onProcessTerminateFunc;
  61. AZStd::function<void(Aws::GameLift::Server::Model::GameSession)> m_onStartGameSessionFunc;
  62. AZStd::function<void(Aws::GameLift::Server::Model::UpdateGameSession)> m_onUpdateGameSessionFunc;
  63. };
  64. class AWSGameLiftServerManagerMock
  65. : public AWSGameLiftServerManager
  66. {
  67. public:
  68. AWSGameLiftServerManagerMock()
  69. {
  70. AZStd::unique_ptr<NiceMock<GameLiftServerSDKWrapperMock>> gameLiftServerSDKWrapper =
  71. AZStd::make_unique<NiceMock<GameLiftServerSDKWrapperMock>>();
  72. m_gameLiftServerSDKWrapperMockPtr = gameLiftServerSDKWrapper.get();
  73. SetGameLiftServerSDKWrapper(AZStd::move(gameLiftServerSDKWrapper));
  74. }
  75. ~AWSGameLiftServerManagerMock()
  76. {
  77. m_gameLiftServerSDKWrapperMockPtr = nullptr;
  78. }
  79. void SetupTestMatchmakingData(const AZStd::string& matchmakingData, int maxPlayer = 10)
  80. {
  81. m_testGameSession.SetMatchmakerData(matchmakingData.c_str());
  82. m_testGameSession.SetMaximumPlayerSessionCount(maxPlayer);
  83. UpdateGameSessionData(m_testGameSession);
  84. }
  85. bool AddConnectedTestPlayer(const Multiplayer::PlayerConnectionConfig& playerConnectionConfig)
  86. {
  87. return AddConnectedPlayer(playerConnectionConfig);
  88. }
  89. AZStd::vector<AWSGameLiftPlayer> GetTestServerMatchBackfillPlayers()
  90. {
  91. return GetActiveServerMatchBackfillPlayers();
  92. }
  93. NiceMock<GameLiftServerSDKWrapperMock>* m_gameLiftServerSDKWrapperMockPtr;
  94. Aws::GameLift::Server::Model::GameSession m_testGameSession;
  95. };
  96. class AWSGameLiftServerSystemComponentMock
  97. : public AWSGameLift::AWSGameLiftServerSystemComponent
  98. {
  99. public:
  100. AZ_CLASS_ALLOCATOR(AWSGameLiftServerSystemComponentMock, AZ::SystemAllocator)
  101. AWSGameLiftServerSystemComponentMock()
  102. {
  103. SetGameLiftServerManager(AZStd::make_unique<NiceMock<AWSGameLiftServerManagerMock>>());
  104. ON_CALL(*this, Init()).WillByDefault(testing::Invoke(this, &AWSGameLiftServerSystemComponentMock::InitMock));
  105. ON_CALL(*this, Activate()).WillByDefault(testing::Invoke(this, &AWSGameLiftServerSystemComponentMock::ActivateMock));
  106. ON_CALL(*this, Deactivate()).WillByDefault(testing::Invoke(this, &AWSGameLiftServerSystemComponentMock::DeactivateMock));
  107. }
  108. void InitMock()
  109. {
  110. AWSGameLift::AWSGameLiftServerSystemComponent::Init();
  111. }
  112. void ActivateMock()
  113. {
  114. AWSGameLift::AWSGameLiftServerSystemComponent::Activate();
  115. }
  116. void DeactivateMock()
  117. {
  118. AWSGameLift::AWSGameLiftServerSystemComponent::Deactivate();
  119. }
  120. MOCK_METHOD0(Init, void());
  121. MOCK_METHOD0(Activate, void());
  122. MOCK_METHOD0(Deactivate, void());
  123. GameLiftServerProcessDesc m_serverProcessDesc;
  124. };
  125. };