| | | 1 | | /* test_machine_handler_suite.c */ |
| | | 2 | | #include <stdarg.h> |
| | | 3 | | #include <stddef.h> |
| | | 4 | | #include <setjmp.h> |
| | | 5 | | #include <cmocka.h> |
| | | 6 | | #include <string.h> |
| | | 7 | | #include "api/handlers/machine_handler.h" |
| | | 8 | | |
| | | 9 | | // Mock logs to avoid linking errors |
| | 0 | 10 | | void LOG_INFO(const char *f, ...) { |
| | | 11 | | (void)f; |
| | 0 | 12 | | } |
| | | 13 | | |
| | 0 | 14 | | void LOG_ERROR(const char *f, ...) { |
| | | 15 | | (void)f; |
| | 0 | 16 | | } |
| | | 17 | | |
| | 1 | 18 | | static void test_get_machines_success(void **state) { |
| | | 19 | | (void)state; |
| | 1 | 20 | | HttpRequest req = { .method = "GET" }; |
| | 1 | 21 | | HttpResponse res = { 0 }; |
| | 1 | 22 | | handle_machine_request(&req, &res); |
| | 1 | 23 | | assert_int_equal(res.status_code, 200); |
| | 1 | 24 | | assert_non_null(strstr(res.body, "CNC Machine")); |
| | 1 | 25 | | } |
| | | 26 | | |
| | 1 | 27 | | static void test_post_machines_success(void **state) { |
| | | 28 | | (void)state; |
| | 1 | 29 | | HttpRequest req = { .method = "POST" }; |
| | 1 | 30 | | HttpResponse res = { 0 }; |
| | 1 | 31 | | handle_machine_request(&req, &res); |
| | 1 | 32 | | assert_int_equal(res.status_code, 201); |
| | 1 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | static void test_unsupported_method_branch(void **state) { |
| | | 36 | | (void)state; |
| | 1 | 37 | | HttpRequest req = { .method = "DELETE" }; // Triggers the final implicit 'else' |
| | 1 | 38 | | HttpResponse res = { .status_code = -1 }; |
| | 1 | 39 | | handle_machine_request(&req, &res); |
| | | 40 | | // Since code has no 'else' for other methods, status remains unchanged |
| | 1 | 41 | | assert_int_equal(res.status_code, -1); |
| | 1 | 42 | | } |
| | | 43 | | |
| | 1 | 44 | | int main(void) { |
| | 1 | 45 | | const struct CMUnitTest tests[] = { |
| | | 46 | | cmocka_unit_test(test_get_machines_success), |
| | | 47 | | cmocka_unit_test(test_post_machines_success), |
| | | 48 | | cmocka_unit_test(test_unsupported_method_branch), |
| | | 49 | | }; |
| | 1 | 50 | | return cmocka_run_group_tests(tests, NULL, NULL); |
| | | 51 | | } |