| | | 1 | | #include <stdarg.h> |
| | | 2 | | #include <stddef.h> |
| | | 3 | | #include <setjmp.h> |
| | | 4 | | #include <cmocka.h> |
| | | 5 | | #include <string.h> |
| | | 6 | | #include <stdio.h> |
| | | 7 | | #include <stdlib.h> |
| | | 8 | | |
| | | 9 | | // PostgreSQL Definitions |
| | | 10 | | #define LIBPQ_FE_H |
| | | 11 | | #define PGconn void |
| | | 12 | | #define PGresult void |
| | | 13 | | #define PGRES_COMMAND_OK 1 |
| | | 14 | | #define PGRES_TUPLES_OK 2 |
| | | 15 | | #define PGRES_FATAL_ERROR 3 |
| | | 16 | | |
| | | 17 | | #include "alert_service.h" |
| | | 18 | | #include "db_connection.h" |
| | | 19 | | |
| | | 20 | | // Logger mock |
| | 0 | 21 | | void LOG_INFO(const char *format, ...) {} |
| | 0 | 22 | | void LOG_ERROR(const char *format, ...) {} |
| | | 23 | | |
| | | 24 | | // Mock State |
| | | 25 | | static DBConnection mock_conn; |
| | | 26 | | static int mock_acquire_success = 1; |
| | | 27 | | static int mock_result_status = PGRES_TUPLES_OK; |
| | | 28 | | static int mock_ntuples = 1; |
| | | 29 | | |
| | | 30 | | // Mock DB functions |
| | 10 | 31 | | DBConnection *db_pool_acquire(void) { |
| | 10 | 32 | | if (mock_acquire_success) { |
| | 8 | 33 | | mock_conn.pg_conn = (void *)0x1234; |
| | 8 | 34 | | return &mock_conn; |
| | | 35 | | } |
| | 2 | 36 | | return NULL; |
| | | 37 | | } |
| | | 38 | | |
| | 8 | 39 | | void db_pool_release(DBConnection *conn) {} |
| | | 40 | | |
| | 8 | 41 | | void *PQexec(void *conn, const char *query) { |
| | 8 | 42 | | return (void *)0x5678; |
| | | 43 | | } |
| | | 44 | | |
| | 8 | 45 | | int PQresultStatus(const void *res) { |
| | 8 | 46 | | return mock_result_status; |
| | | 47 | | } |
| | | 48 | | |
| | 8 | 49 | | void PQclear(void *res) {} |
| | | 50 | | |
| | 2 | 51 | | int PQntuples(const void *res) { |
| | 2 | 52 | | return mock_ntuples; |
| | | 53 | | } |
| | | 54 | | |
| | 15 | 55 | | char *PQgetvalue(const void *res, int row, int field) { |
| | 15 | 56 | | if (field == 0) return "100"; // ID |
| | 12 | 57 | | if (field == 1) return "1"; // SensorID |
| | 9 | 58 | | if (field == 2) return "CRITICAL"; |
| | 6 | 59 | | if (field == 3) return "Mock Message"; |
| | 3 | 60 | | if (field == 4) return "2026-02-18 22:40:00"; |
| | 0 | 61 | | return ""; |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | char *PQerrorMessage(const void *conn) { |
| | 1 | 65 | | return "Alert Mock Error"; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /* ==================================================================== |
| | | 69 | | * TEST CASES |
| | | 70 | | * ==================================================================== */ |
| | | 71 | | |
| | 1 | 72 | | static void test_create_alert_paths(void **state) { |
| | 1 | 73 | | mock_acquire_success = 1; |
| | 1 | 74 | | mock_result_status = PGRES_COMMAND_OK; |
| | 1 | 75 | | assert_true(create_alert(1, SEVERITY_WARNING, "Test Message")); |
| | | 76 | | |
| | 1 | 77 | | mock_result_status = PGRES_FATAL_ERROR; |
| | 1 | 78 | | assert_false(create_alert(1, SEVERITY_WARNING, "Test Message")); |
| | | 79 | | |
| | 1 | 80 | | mock_acquire_success = 0; |
| | 1 | 81 | | assert_false(create_alert(1, SEVERITY_WARNING, "Test Message")); |
| | 1 | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | static void test_check_and_trigger_logic(void **state) { |
| | 1 | 85 | | mock_acquire_success = 1; |
| | 1 | 86 | | mock_result_status = PGRES_COMMAND_OK; |
| | | 87 | | |
| | | 88 | | // Temperature paths |
| | 1 | 89 | | check_and_trigger_alerts(1, "Temperature", 95.0); // Critical |
| | 1 | 90 | | check_and_trigger_alerts(1, "Temperature", 80.0); // Warning |
| | 1 | 91 | | check_and_trigger_alerts(1, "Temperature", 50.0); // None |
| | | 92 | | |
| | | 93 | | // Vibration |
| | 1 | 94 | | check_and_trigger_alerts(2, "Vibration", 6.0); // Critical |
| | 1 | 95 | | check_and_trigger_alerts(2, "Vibration", 1.0); // None |
| | | 96 | | |
| | | 97 | | // Pressure |
| | 1 | 98 | | check_and_trigger_alerts(3, "Pressure", 15.0); // Warning |
| | 1 | 99 | | check_and_trigger_alerts(3, "Pressure", 5.0); // None |
| | | 100 | | |
| | | 101 | | // Unknown |
| | 1 | 102 | | check_and_trigger_alerts(4, "Unknown", 100.0); |
| | 1 | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | static void test_get_recent_alerts_paths(void **state) { |
| | 1 | 106 | | mock_acquire_success = 1; |
| | 1 | 107 | | mock_result_status = PGRES_TUPLES_OK; |
| | 1 | 108 | | mock_ntuples = 2; |
| | | 109 | | |
| | | 110 | | AlertInfo arr[10]; |
| | 1 | 111 | | assert_int_equal(get_recent_alerts(arr, 10), 2); |
| | | 112 | | |
| | 1 | 113 | | mock_acquire_success = 0; |
| | 1 | 114 | | assert_int_equal(get_recent_alerts(arr, 10), 0); |
| | 1 | 115 | | } |
| | | 116 | | |
| | 1 | 117 | | static void test_serialize_alerts(void **state) { |
| | 1 | 118 | | mock_acquire_success = 1; |
| | 1 | 119 | | mock_result_status = PGRES_TUPLES_OK; |
| | 1 | 120 | | mock_ntuples = 1; |
| | | 121 | | |
| | 1 | 122 | | char *json = alert_service_serialize_alerts(); |
| | 1 | 123 | | assert_non_null(json); |
| | 1 | 124 | | assert_true(strstr(json, "CRITICAL") != NULL); |
| | 1 | 125 | | free(json); |
| | 1 | 126 | | } |
| | | 127 | | |
| | 1 | 128 | | int main(void) { |
| | 1 | 129 | | const struct CMUnitTest tests[] = { |
| | | 130 | | cmocka_unit_test(test_create_alert_paths), |
| | | 131 | | cmocka_unit_test(test_check_and_trigger_logic), |
| | | 132 | | cmocka_unit_test(test_get_recent_alerts_paths), |
| | | 133 | | cmocka_unit_test(test_serialize_alerts), |
| | | 134 | | }; |
| | 1 | 135 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 136 | | } |