Skip to content

Commit

Permalink
Updated test with test persistent storage delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Jun 6, 2024
1 parent f0e1c75 commit fb56461
Showing 1 changed file with 11 additions and 103 deletions.
114 changes: 11 additions & 103 deletions src/app/tests/TestDefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
* limitations under the License.
*/

#include <cstdint>
#include <gmock/gmock.h>
#include <nlunit-test.h>

#include <lib/support/TestPersistentStorageDelegate.h>
#include <lib/support/UnitTestRegistration.h>

#include "app/server/DefaultTermsAndConditionsProvider.h"
Expand Down Expand Up @@ -73,97 +75,6 @@ SaveArgVoidPointeeAction<N, T> SaveArgVoidPointee(T value)
return { std::move(value) };
}

/**
* @brief A mock class for the KeyValueStoreManager interface.
*
* This mock class is used for testing purposes to mock the behavior of the KeyValueStoreManager class.
* It inherits from the KeyValueStoreManager class and provides mock implementations for its methods.
*/
class MockKeyValueStoreManager : public chip::DeviceLayer::PersistedStorage::KeyValueStoreManager
{
public:
/**
* @brief Mock method for the Get method of KeyValueStoreManager.
*
* This mock method is used to simulate the behavior of the Get method of KeyValueStoreManager.
* It takes parameters similar to the Get method and returns a CHIP_ERROR.
*
* @param key The key to retrieve from the key-value store.
* @param buffer Pointer to the buffer where the retrieved value will be stored.
* @param buffer_size Size of the buffer.
* @param read_bytes_size Pointer to a variable where the number of bytes read will be stored.
* @param offset_bytes Offset within the value from where to start reading.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
MOCK_METHOD5(GetMock, CHIP_ERROR(const char *, void *, size_t, size_t *, size_t));

/**
* @brief Mock method for the Put method of KeyValueStoreManager.
*
* This mock method is used to simulate the behavior of the Put method of KeyValueStoreManager.
* It takes parameters similar to the Put method and returns a CHIP_ERROR.
*
* @param key The key under which to store the value in the key-value store.
* @param value Pointer to the value to be stored.
* @param value_size Size of the value.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
MOCK_METHOD3(PutMock, CHIP_ERROR(const char *, const void *, size_t));

/**
* @brief Mock method for the Delete method of KeyValueStoreManager.
*
* This mock method is used to simulate the behavior of the Delete method of KeyValueStoreManager.
* It takes a key as a parameter and returns a CHIP_ERROR.
*
* @param key The key to delete from the key-value store.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
MOCK_METHOD1(DeleteMock, CHIP_ERROR(const char *));

/**
* @brief Override of the Get method of KeyValueStoreManager.
*
* This method overrides the Get method of KeyValueStoreManager.
* It delegates the call to the corresponding mock method GetMock.
*
* @param key The key to retrieve from the key-value store.
* @param buffer Pointer to the buffer where the retrieved value will be stored.
* @param buffer_size Size of the buffer.
* @param read_bytes_size Pointer to a variable where the number of bytes read will be stored.
* @param offset_bytes Offset within the value from where to start reading.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
CHIP_ERROR Get(const char * key, void * buffer, size_t buffer_size, size_t * read_bytes_size, size_t offset_bytes)
{
return GetMock(key, buffer, buffer_size, read_bytes_size, offset_bytes);
}

/**
* @brief Override of the Put method of KeyValueStoreManager.
*
* This method overrides the Put method of KeyValueStoreManager.
* It delegates the call to the corresponding mock method PutMock.
*
* @param key The key under which to store the value in the key-value store.
* @param value Pointer to the value to be stored.
* @param value_size Size of the value.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
CHIP_ERROR Put(const char * key, const void * value, size_t value_size) { return PutMock(key, value, value_size); }

/**
* @brief Override of the Delete method of KeyValueStoreManager.
*
* This method overrides the Delete method of KeyValueStoreManager.
* It delegates the call to the corresponding mock method DeleteMock.
*
* @param key The key to delete from the key-value store.
* @return CHIP_ERROR representing the success or failure of the operation.
*/
CHIP_ERROR Delete(const char * key) { return DeleteMock(key); }
};

/**
* @brief Setup function for the test suite.
*
Expand Down Expand Up @@ -228,10 +139,10 @@ static int Terminate(void * inContext)
*/
static void TestDefaultTermsAndConditionsProviderInitSuccess(struct _nlTestSuite * inSuite, void * inContext)
{
MockKeyValueStoreManager mockKeyValueStoreManager;
chip::TestPersistentStorageDelegate testPersistentStorageDelegate;

chip::app::DefaultTermsAndConditionsProvider uut;
CHIP_ERROR err = uut.Init(&mockKeyValueStoreManager);
CHIP_ERROR err = uut.Init(&testPersistentStorageDelegate);

NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == err);
}
Expand All @@ -249,21 +160,18 @@ static void TestDefaultTermsAndConditionsProviderGetAcceptanceSuccess(struct _nl
uint16_t acknowledgements;
uint16_t acknowledgementsVersion;

MockKeyValueStoreManager mockKeyValueStoreManager;
chip::TestPersistentStorageDelegate testPersistentStorageDelegate;
chip::app::DefaultTermsAndConditionsProvider uut;

err = uut.Init(&mockKeyValueStoreManager);
err = uut.Init(&testPersistentStorageDelegate);
NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == err);

EXPECT_CALL(mockKeyValueStoreManager,
GetMock(chip::app::DefaultTermsAndConditionsProvider::kAcceptedAcknowledgementsKeyName, testing::_,
sizeof(uint16_t), testing::_, testing::_))
.WillOnce(testing::DoAll(SaveArgVoidPointee<1>(static_cast<uint16_t>(1)), testing::Return(CHIP_NO_ERROR)));
const uint16_t value = 1;
testPersistentStorageDelegate.SyncSetKeyValue(chip::app::DefaultTermsAndConditionsProvider::kAcceptedAcknowledgementsKeyName,
&value, sizeof(uint16_t));

EXPECT_CALL(mockKeyValueStoreManager,
GetMock(chip::app::DefaultTermsAndConditionsProvider::kAcceptedAcknowledgementsVersionKeyName, testing::_,
sizeof(uint16_t), testing::_, testing::_))
.WillOnce(testing::DoAll(SaveArgVoidPointee<1>(static_cast<uint16_t>(1)), testing::Return(CHIP_NO_ERROR)));
testPersistentStorageDelegate.SyncSetKeyValue(
chip::app::DefaultTermsAndConditionsProvider::kAcceptedAcknowledgementsVersionKeyName, &value, sizeof(uint16_t));

err = uut.GetAcceptance(acknowledgements, acknowledgementsVersion);
NL_TEST_ASSERT(inSuite, 1 == acknowledgements);
Expand Down

0 comments on commit fb56461

Please sign in to comment.