| | | 1 | | #define _GNU_SOURCE |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <string.h> |
| | | 4 | | #include <assert.h> |
| | | 5 | | #include <stdlib.h> |
| | | 6 | | #include <stdbool.h> |
| | | 7 | | |
| | | 8 | | #include "../../api/handlers/machine_handler.h" |
| | | 9 | | |
| | | 10 | | /* ========================================================== |
| | | 11 | | MOCK STATE |
| | | 12 | | ========================================================== */ |
| | | 13 | | |
| | | 14 | | static int mock_duplicate_mode = 0; |
| | | 15 | | static int mock_add_machine_success = 1; |
| | | 16 | | |
| | | 17 | | /* ========================================================== |
| | | 18 | | MOCK STRUCTS |
| | | 19 | | ========================================================== */ |
| | | 20 | | |
| | | 21 | | typedef struct { |
| | | 22 | | int id; |
| | | 23 | | char name[100]; |
| | | 24 | | char model[100]; |
| | | 25 | | char location[100]; |
| | | 26 | | char status[50]; |
| | | 27 | | } Machine; |
| | | 28 | | |
| | | 29 | | /* ========================================================== |
| | | 30 | | MOCK FUNCTIONS |
| | | 31 | | ========================================================== */ |
| | | 32 | | |
| | 3 | 33 | | int get_all_machines(Machine *list, int max) { |
| | 3 | 34 | | if (!mock_duplicate_mode) |
| | 2 | 35 | | return 0; |
| | | 36 | | |
| | 1 | 37 | | strcpy(list[0].name, "CNC Machine"); |
| | 1 | 38 | | return 1; |
| | | 39 | | } |
| | | 40 | | |
| | 2 | 41 | | bool add_machine(const char *name, |
| | | 42 | | const char *model, |
| | | 43 | | const char *location, |
| | | 44 | | const char *status) { |
| | | 45 | | (void)name; |
| | | 46 | | (void)model; |
| | | 47 | | (void)location; |
| | | 48 | | (void)status; |
| | 2 | 49 | | return mock_add_machine_success; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /* ========================================================== |
| | | 53 | | MOCK SERIALIZER (for GET) |
| | | 54 | | ========================================================== */ |
| | | 55 | | |
| | 1 | 56 | | char *serialize_machines_to_json(void) { |
| | 1 | 57 | | char *json = malloc(128); |
| | 1 | 58 | | strcpy(json, "{\"machines\":[{\"id\":1,\"name\":\"CNC Machine\"}]}"); |
| | 1 | 59 | | return json; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /* ========================================================== |
| | | 63 | | TESTS |
| | | 64 | | ========================================================== */ |
| | | 65 | | |
| | 1 | 66 | | void test_get_request() { |
| | 1 | 67 | | HttpRequest req = {0}; |
| | 1 | 68 | | HttpResponse res = {0}; |
| | 1 | 69 | | strcpy(req.method, "GET"); |
| | 1 | 70 | | handle_machine_request(&req, &res); |
| | 1 | 71 | | assert(res.status_code == 200); |
| | 1 | 72 | | assert(strcmp(res.content_type, "application/json") == 0); |
| | 1 | 73 | | assert(strstr(res.body, "CNC Machine") != NULL); |
| | 1 | 74 | | printf("✔ test_get_request\n"); |
| | 1 | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | void test_post_success() { |
| | 1 | 78 | | HttpRequest req = {0}; |
| | 1 | 79 | | HttpResponse res = {0}; |
| | 1 | 80 | | strcpy(req.method, "POST"); |
| | 1 | 81 | | strcpy(req.body, |
| | | 82 | | "{\"name\":\"Drill\",\"model\":\"X1\",\"location\":\"Hall A\",\"status\":\"running\"}"); |
| | 1 | 83 | | mock_duplicate_mode = 0; |
| | 1 | 84 | | mock_add_machine_success = 1; |
| | 1 | 85 | | handle_machine_request(&req, &res); |
| | 1 | 86 | | assert(res.status_code == 201); |
| | 1 | 87 | | assert(strstr(res.body, "success") != NULL); |
| | 1 | 88 | | printf("✔ test_post_success\n"); |
| | 1 | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | void test_invalid_json() { |
| | 1 | 92 | | HttpRequest req = {0}; |
| | 1 | 93 | | HttpResponse res = {0}; |
| | 1 | 94 | | strcpy(req.method, "POST"); |
| | 1 | 95 | | strcpy(req.body, "INVALID_JSON"); |
| | 1 | 96 | | handle_machine_request(&req, &res); |
| | 1 | 97 | | assert(res.status_code == 400); |
| | 1 | 98 | | assert(strstr(res.body, "Invalid JSON") != NULL); |
| | 1 | 99 | | printf("✔ test_invalid_json\n"); |
| | 1 | 100 | | } |
| | | 101 | | |
| | 1 | 102 | | void test_missing_fields() { |
| | 1 | 103 | | HttpRequest req = {0}; |
| | 1 | 104 | | HttpResponse res = {0}; |
| | 1 | 105 | | strcpy(req.method, "POST"); |
| | 1 | 106 | | strcpy(req.body, "{\"name\":\"OnlyName\"}"); |
| | 1 | 107 | | handle_machine_request(&req, &res); |
| | 1 | 108 | | assert(res.status_code == 400); |
| | 1 | 109 | | assert(strstr(res.body, "Missing required fields") != NULL); |
| | 1 | 110 | | printf("✔ test_missing_fields\n"); |
| | 1 | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | void test_duplicate_machine() { |
| | 1 | 114 | | HttpRequest req = {0}; |
| | 1 | 115 | | HttpResponse res = {0}; |
| | 1 | 116 | | strcpy(req.method, "POST"); |
| | 1 | 117 | | strcpy(req.body, |
| | | 118 | | "{\"name\":\"CNC Machine\",\"status\":\"running\"}"); |
| | 1 | 119 | | mock_duplicate_mode = 1; |
| | 1 | 120 | | mock_add_machine_success = 1; |
| | 1 | 121 | | handle_machine_request(&req, &res); |
| | 1 | 122 | | assert(res.status_code == 409); |
| | 1 | 123 | | assert(strstr(res.body, "already exists") != NULL); |
| | 1 | 124 | | printf("✔ test_duplicate_machine\n"); |
| | 1 | 125 | | } |
| | | 126 | | |
| | 1 | 127 | | void test_add_machine_failure() { |
| | 1 | 128 | | HttpRequest req = {0}; |
| | 1 | 129 | | HttpResponse res = {0}; |
| | 1 | 130 | | strcpy(req.method, "POST"); |
| | 1 | 131 | | strcpy(req.body, |
| | | 132 | | "{\"name\":\"NewMachine\",\"status\":\"running\"}"); |
| | 1 | 133 | | mock_duplicate_mode = 0; |
| | 1 | 134 | | mock_add_machine_success = 0; |
| | 1 | 135 | | handle_machine_request(&req, &res); |
| | 1 | 136 | | assert(res.status_code == 500); |
| | 1 | 137 | | assert(strstr(res.body, "Database error") != NULL); |
| | 1 | 138 | | printf("✔ test_add_machine_failure\n"); |
| | 1 | 139 | | } |
| | | 140 | | |
| | | 141 | | /* ========================================================== |
| | | 142 | | MAIN |
| | | 143 | | ========================================================== */ |
| | | 144 | | |
| | 1 | 145 | | int main() { |
| | 1 | 146 | | test_get_request(); |
| | 1 | 147 | | test_post_success(); |
| | 1 | 148 | | test_invalid_json(); |
| | 1 | 149 | | test_missing_fields(); |
| | 1 | 150 | | test_duplicate_machine(); |
| | 1 | 151 | | test_add_machine_failure(); |
| | 1 | 152 | | printf("\nALL machine_handler tests passed.\n"); |
| | 1 | 153 | | return 0; |
| | | 154 | | } |