2026-03-27 14:12:49 +01:00
/********************************************************************************
* Copyright ( c ) 2025 - 2026 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https : //www.apache.org/licenses/LICENSE-2.0
*
* SPDX - License - Identifier : Apache - 2.0
*
* Contributors :
* Erik Verhoeven - initial API and implementation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2025-11-04 13:28:06 +01:00
# include <gtest/gtest.h>
# include <fstream>
# include "mock.h"
# include "generated/IComponent.h"
# include <interfaces/repository.h>
# include "../../../global/exec_dir_helper.h"
const uint32_t LoopCount = 100 ; //< amount of loops used for concurrency tests. more loops means more thorough deadlock check at the cost of increased runtime
// used to supply external objects for testing pruposes. Implements IObjectControl to make sure this is NOT called for externally supplied objects
class TestExternalObject : public sdv : : IInterfaceAccess , public sdv : : IObjectControl
{
public :
BEGIN_SDV_INTERFACE_MAP ( )
SDV_INTERFACE_ENTRY ( sdv : : IObjectControl )
END_SDV_INTERFACE_MAP ( )
2026-03-27 14:12:49 +01:00
virtual void Initialize ( [[maybe_unused]] const sdv : : u8string & ssObjectConfig ) override
2025-11-04 13:28:06 +01:00
{
FAIL ( ) < < " Error: Initialize should not be called by Repo Service! " ;
2026-03-27 14:12:49 +01:00
//m_eObjectState = sdv::EObjectState::initialization_failure;
2025-11-04 13:28:06 +01:00
}
2026-03-27 14:12:49 +01:00
virtual sdv : : EObjectState GetObjectState ( ) const override
2025-11-04 13:28:06 +01:00
{
2026-03-27 14:12:49 +01:00
return m_eObjectState ;
2025-11-04 13:28:06 +01:00
}
/**
* @ brief Set the component operation mode . Overload of sdv : : IObjectControl : : SetOperationMode .
* @ param [ in ] eMode The operation mode , the component should run in .
*/
2026-03-27 14:12:49 +01:00
virtual void SetOperationMode ( sdv : : EOperationMode eMode ) override
2025-11-04 13:28:06 +01:00
{
switch ( eMode )
{
case sdv : : EOperationMode : : configuring :
2026-03-27 14:12:49 +01:00
if ( m_eObjectState = = sdv : : EObjectState : : running | | m_eObjectState = = sdv : : EObjectState : : initialized )
m_eObjectState = sdv : : EObjectState : : configuring ;
2025-11-04 13:28:06 +01:00
break ;
case sdv : : EOperationMode : : running :
2026-03-27 14:12:49 +01:00
if ( m_eObjectState = = sdv : : EObjectState : : configuring | | m_eObjectState = = sdv : : EObjectState : : initialized )
m_eObjectState = sdv : : EObjectState : : running ;
2025-11-04 13:28:06 +01:00
break ;
default :
break ;
}
}
2026-03-27 14:12:49 +01:00
virtual sdv : : u8string GetObjectConfig ( ) const override
{
return { } ;
}
virtual void Shutdown ( ) override
2025-11-04 13:28:06 +01:00
{
2026-03-27 14:12:49 +01:00
m_eObjectState = sdv : : EObjectState : : shutdown_in_progress ;
2025-11-04 13:28:06 +01:00
FAIL ( ) < < " Error: Shutdown should not be called by Repo Service! " ;
2026-03-27 14:12:49 +01:00
//m_eObjectState = sdv::EObjectState::destruction_pending;
2025-11-04 13:28:06 +01:00
}
2026-03-27 14:12:49 +01:00
sdv : : EObjectState m_eObjectState = sdv : : EObjectState : : initialization_pending ;
2025-11-04 13:28:06 +01:00
} ;
TEST ( RepositoryTest , LoadNonexistentModule )
{
CRepository repository ; // Must be created first
CModuleControl modulectrl ;
CHelper helper ( modulectrl , repository ) ;
EXPECT_FALSE ( modulectrl . Load ( ( GetExecDirectory ( ) / " TestFooBar.sdv " ) . generic_u8string ( ) ) ) ;
}
TEST ( RepositoryTest , CreateNonexistentClass )
{
CRepository repository ; // Must be created first
CModuleControl modulectrl ;
CHelper helper ( modulectrl , repository ) ;
ASSERT_TRUE ( modulectrl . Load ( ( GetExecDirectory ( ) / " UnitTest_Repository_test_module.sdv " ) . generic_u8string ( ) ) ) ;
EXPECT_FALSE ( repository . CreateObject2 ( " TestFooBar " , nullptr , nullptr ) ) ;
repository . DestroyObject2 ( " TestFooBar " ) ;
}
TEST ( RepositoryTest , GetNonexistentObject )
{
CRepository repository ; // Must be created first
CModuleControl modulectrl ;
CHelper helper ( modulectrl , repository ) ;
ASSERT_TRUE ( modulectrl . Load ( ( GetExecDirectory ( ) / " UnitTest_Repository_test_module.sdv " ) . generic_u8string ( ) ) ) ;
bool bRes = repository . CreateObject2 ( " Example_Object " , nullptr , nullptr ) ;
EXPECT_TRUE ( bRes ) ;
EXPECT_EQ ( nullptr , repository . GetObject ( " TestFooBar " ) ) ;
repository . DestroyObject2 ( " Example_Object " ) ;
}
TEST ( RepositoryTest , InstantiateAndGet )
{
CRepository repository ; // Must be created first
CModuleControl modulectrl ;
CHelper helper ( modulectrl , repository ) ;
ASSERT_TRUE ( modulectrl . Load ( ( GetExecDirectory ( ) / " UnitTest_Repository_test_module.sdv " ) . generic_u8string ( ) ) ) ;
bool bRes = repository . CreateObject2 ( " Example_Object " , nullptr , nullptr ) ;
EXPECT_TRUE ( bRes ) ;
EXPECT_NE ( nullptr , repository . GetObject ( " Example_Object " ) ) ;
repository . DestroyObject2 ( " Example_Object " ) ;
EXPECT_EQ ( nullptr , repository . GetObject ( " Example_Object " ) ) ;
}
TEST ( RepositoryTest , InstantiateInitFail )
{
CRepository repository ; // Must be created first
CModuleControl modulectrl ;
CHelper helper ( modulectrl , repository ) ;
ASSERT_TRUE ( modulectrl . Load ( ( GetExecDirectory ( ) / " UnitTest_Repository_test_module.sdv " ) . generic_u8string ( ) ) ) ;
bool bRes = repository . CreateObject2 ( " TestObject_IObjectControlFail " , nullptr , nullptr ) ;
EXPECT_FALSE ( bRes ) ;
EXPECT_EQ ( nullptr , repository . GetObject ( " TestObject_IObjectControlFail " ) ) ;
repository . DestroyObject2 ( " TestObject_IObjectControlFail " ) ;
}