AWSCoreSystemComponentTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <AzCore/Jobs/JobManager.h>
  9. #include <AzCore/Jobs/JobContext.h>
  10. #include <AzCore/Jobs/JobManagerBus.h>
  11. #include <AzCore/Jobs/JobCancelGroup.h>
  12. #include <AzCore/Component/ComponentApplication.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/Serialization/EditContext.h>
  16. #include <AzCore/std/smart_ptr/unique_ptr.h>
  17. #include <AzTest/AzTest.h>
  18. #include <AWSCoreSystemComponent.h>
  19. #include <AWSNativeSDKTestManager.h>
  20. #include <Configuration/AWSCoreConfiguration.h>
  21. #include <Credential/AWSCredentialManager.h>
  22. #include <Framework/AWSApiJob.h>
  23. #include <ResourceMapping/AWSResourceMappingManager.h>
  24. #include <TestFramework/AWSCoreFixture.h>
  25. using namespace AWSCore;
  26. class AWSCoreNotificationsBusMock
  27. : protected AWSCoreNotificationsBus::Handler
  28. {
  29. public:
  30. AZ_CLASS_ALLOCATOR(AWSCoreNotificationsBusMock, AZ::SystemAllocator);
  31. AWSCoreNotificationsBusMock()
  32. : m_sdkInitialized(0)
  33. , m_sdkShutdownStarted(0)
  34. {
  35. AWSCoreNotificationsBus::Handler::BusConnect();
  36. }
  37. ~AWSCoreNotificationsBusMock() override
  38. {
  39. AWSCoreNotificationsBus::Handler::BusDisconnect();
  40. }
  41. //////////////////////////////////////////////////////////////////////////
  42. // handles AWSCoreNotificationsBus
  43. void OnSDKInitialized() override { m_sdkInitialized++; }
  44. void OnSDKShutdownStarted() override { m_sdkShutdownStarted++; }
  45. int m_sdkInitialized;
  46. int m_sdkShutdownStarted;
  47. };
  48. class AWSCoreSystemComponentTest
  49. : public AWSCoreFixture
  50. {
  51. protected:
  52. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  53. AZStd::unique_ptr<AZ::BehaviorContext> m_behaviorContext;
  54. AZStd::unique_ptr<AZ::ComponentDescriptor> m_componentDescriptor;
  55. void SetUp() override
  56. {
  57. AWSCoreFixture::SetUp();
  58. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  59. m_serializeContext->CreateEditContext();
  60. m_behaviorContext = AZStd::make_unique<AZ::BehaviorContext>();
  61. m_componentDescriptor.reset(AWSCoreSystemComponent::CreateDescriptor());
  62. m_componentDescriptor->Reflect(m_serializeContext.get());
  63. m_componentDescriptor->Reflect(m_behaviorContext.get());
  64. m_settingsRegistry->SetContext(m_serializeContext.get());
  65. m_entity = aznew AZ::Entity();
  66. m_coreSystemsComponent.reset(m_entity->CreateComponent<AWSCoreSystemComponent>());
  67. }
  68. void TearDown() override
  69. {
  70. m_entity->RemoveComponent(m_coreSystemsComponent.get());
  71. m_coreSystemsComponent.reset();
  72. delete m_entity;
  73. m_entity = nullptr;
  74. m_coreSystemsComponent.reset();
  75. m_componentDescriptor.reset();
  76. m_behaviorContext.reset();
  77. m_serializeContext.reset();
  78. AWSCoreFixture::TearDown();
  79. }
  80. public:
  81. AZStd::unique_ptr<AWSCoreSystemComponent> m_coreSystemsComponent;
  82. AZ::Entity* m_entity;
  83. AWSCoreNotificationsBusMock m_notifications;
  84. };
  85. TEST_F(AWSCoreSystemComponentTest, ComponentActivateTest)
  86. {
  87. // Shutdown SDK which is init in fixture setup step
  88. AWSNativeSDKTestLibs::AWSNativeSDKTestManager::Shutdown();
  89. EXPECT_FALSE(m_coreSystemsComponent->IsAWSApiInitialized());
  90. // activate component
  91. AZ_TEST_START_TRACE_SUPPRESSION;
  92. m_entity->Init();
  93. AZ_TEST_STOP_TRACE_SUPPRESSION(1); // expect the above have thrown an AZ_Error
  94. m_entity->Activate();
  95. EXPECT_EQ(m_notifications.m_sdkInitialized, 1);
  96. EXPECT_TRUE(m_coreSystemsComponent->IsAWSApiInitialized());
  97. // deactivate component
  98. m_entity->Deactivate();
  99. EXPECT_EQ(m_notifications.m_sdkShutdownStarted, 1);
  100. EXPECT_FALSE(m_coreSystemsComponent->IsAWSApiInitialized());
  101. }
  102. TEST_F(AWSCoreSystemComponentTest, GetDefaultJobContext_Call_JobContextIsNotNullptr)
  103. {
  104. auto actualContext = m_coreSystemsComponent->GetDefaultJobContext();
  105. EXPECT_TRUE(actualContext);
  106. }
  107. TEST_F(AWSCoreSystemComponentTest, GetDefaultConfig_Call_GetConfigWithExpectedValue)
  108. {
  109. auto actualDefaultConfig = m_coreSystemsComponent->GetDefaultConfig();
  110. EXPECT_TRUE(actualDefaultConfig->userAgent == "/O3DE_AwsApiJob");
  111. EXPECT_TRUE(actualDefaultConfig->requestTimeoutMs == 30000);
  112. EXPECT_TRUE(actualDefaultConfig->connectTimeoutMs == 30000);
  113. AWSCoreNotificationsBus::Broadcast(&AWSCoreNotifications::OnSDKShutdownStarted);
  114. }