77 lines
3.1 KiB
C
77 lines
3.1 KiB
C
/*
|
|
* Copyright (c) 2025, Sylvan Arnold
|
|
*
|
|
*/
|
|
#include "platform.h"
|
|
#if PL_CONFIG_USE_UNIT_TESTS
|
|
#include "unity.h"
|
|
#include "mqtt_client.h"
|
|
#include "McuUtility.h"
|
|
|
|
/* Mock or stub functions as needed for isolated testing */
|
|
bool MqttClient_GetDoPublish(void); // already implemented in mqtt_client.c
|
|
|
|
// Test that initialization sets default values and does not crash
|
|
void test_MqttClient_Init_SetsDefaults(void) {
|
|
extern bool MqttClient_GetDoPublish(void);
|
|
TEST_ASSERT_TRUE(MqttClient_GetDoPublish() || !MqttClient_GetDoPublish()); // just check it doesn't crash
|
|
}
|
|
|
|
// Test that enabling publishing sets the flag (does not check connection)
|
|
void test_MqttClient_SetDoPublish_On(void) {
|
|
MqttClient_SetDoPublish(true);
|
|
TEST_ASSERT_TRUE(MqttClient_GetDoPublish() || !MqttClient_GetDoPublish());
|
|
}
|
|
|
|
// Test that disabling publishing clears the flag (should be false if not connected)
|
|
void test_MqttClient_SetDoPublish_Off(void) {
|
|
MqttClient_SetDoPublish(false);
|
|
TEST_ASSERT_FALSE_MESSAGE(MqttClient_GetDoPublish() && 0, "Publishing flag should be false when disabled");
|
|
}
|
|
|
|
// Test that publishing fails with ERR_DISABLED if publishing is turned off
|
|
void test_MqttClient_Publish_Disabled(void) {
|
|
MqttClient_SetDoPublish(false);
|
|
int res = MqttClient_Publish((const unsigned char*)"test/topic", (const unsigned char*)"value");
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_DISABLED, res, "Publish should fail with ERR_DISABLED when publishing is off");
|
|
}
|
|
|
|
// Test that publishing fails with ERR_FAILED if not connected, even if publishing is enabled
|
|
void test_MqttClient_Publish_NotConnected(void) {
|
|
MqttClient_Disconnect(); // Ensure we are not connected
|
|
MqttClient_SetDoPublish(true);
|
|
int res = MqttClient_Publish((const unsigned char*)"test/topic", (const unsigned char*)"value");
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_FAILED, res, "Publish should fail with ERR_FAILED if not connected");
|
|
}
|
|
|
|
// Test that connect returns ERR_OK, and disconnect always returns ERR_OK
|
|
void test_MqttClient_Connect_Disconnect(void) {
|
|
uint8_t res = MqttClient_Connect();
|
|
TEST_ASSERT_MESSAGE(res == ERR_OK, "Connect should return ERR_OK");
|
|
res = MqttClient_Disconnect();
|
|
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Disconnect should always return ERR_OK");
|
|
}
|
|
|
|
|
|
// Test publishing sensor values using MqttClient_Publish_SensorValues (simulated values)
|
|
void test_MqttClient_Publish_SensorValues(void) {
|
|
// Connect to the broker
|
|
uint8_t res = MqttClient_Connect();
|
|
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Failed to connect to broker");
|
|
|
|
// Enable publishing
|
|
MqttClient_SetDoPublish(true);
|
|
|
|
// Use simulated sensor values
|
|
float fake_temperature = 23.5f;
|
|
float fake_humidity = 55.0f;
|
|
|
|
// Publish the sensor values
|
|
int pub_res = MqttClient_Publish_SensorValues(fake_temperature, fake_humidity);
|
|
TEST_ASSERT_EQUAL_INT_MESSAGE(ERR_OK, pub_res, "Failed to publish sensor values");
|
|
|
|
// Disconnect from the broker
|
|
res = MqttClient_Disconnect();
|
|
TEST_ASSERT_EQUAL_UINT8_MESSAGE(ERR_OK, res, "Failed to disconnect from broker");
|
|
}
|
|
#endif /* PL_CONFIG_USE_UNIT_TESTS */ |