| | | 1 | | #include "../../api/handlers/fault_handler.h" |
| | | 2 | | #include "../../api/http_server.h" |
| | | 3 | | #include <assert.h> |
| | | 4 | | #include <string.h> |
| | | 5 | | #include <stdio.h> |
| | | 6 | | |
| | 1 | 7 | | void test_fault_get_all() { |
| | 1 | 8 | | HttpRequest req = {0}; |
| | 1 | 9 | | HttpResponse res = {0}; |
| | 1 | 10 | | strcpy(req.method, "GET"); |
| | 1 | 11 | | strcpy(req.path, "/api/faults"); |
| | 1 | 12 | | handle_fault_request(&req, &res); |
| | 1 | 13 | | assert(res.status_code == 200); |
| | 1 | 14 | | assert(strstr(res.body, "faults") != NULL); |
| | 1 | 15 | | } |
| | | 16 | | |
| | 1 | 17 | | void test_fault_get_single() { |
| | 1 | 18 | | HttpRequest req = {0}; |
| | 1 | 19 | | HttpResponse res = {0}; |
| | 1 | 20 | | strcpy(req.method, "GET"); |
| | 1 | 21 | | strcpy(req.path, "/api/faults/1"); |
| | 1 | 22 | | handle_fault_request(&req, &res); |
| | 1 | 23 | | assert(res.status_code == 200); |
| | 1 | 24 | | assert(strstr(res.body, "CNC Machine") != NULL); |
| | 1 | 25 | | } |
| | | 26 | | |
| | 1 | 27 | | void test_fault_post() { |
| | 1 | 28 | | HttpRequest req = {0}; |
| | 1 | 29 | | HttpResponse res = {0}; |
| | 1 | 30 | | strcpy(req.method, "POST"); |
| | 1 | 31 | | handle_fault_request(&req, &res); |
| | 1 | 32 | | assert(res.status_code == 201); |
| | 1 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | void test_fault_put_ack() { |
| | 1 | 36 | | HttpRequest req = {0}; |
| | 1 | 37 | | HttpResponse res = {0}; |
| | 1 | 38 | | strcpy(req.method, "PUT"); |
| | 1 | 39 | | strcpy(req.path, "/api/faults/1/acknowledge"); |
| | 1 | 40 | | handle_fault_request(&req, &res); |
| | 1 | 41 | | assert(res.status_code == 200); |
| | 1 | 42 | | assert(strstr(res.body, "acknowledged") != NULL); |
| | 1 | 43 | | } |
| | | 44 | | |
| | 1 | 45 | | void test_fault_put_resolve() { |
| | 1 | 46 | | HttpRequest req = {0}; |
| | 1 | 47 | | HttpResponse res = {0}; |
| | 1 | 48 | | strcpy(req.method, "PUT"); |
| | 1 | 49 | | strcpy(req.path, "/api/faults/1/resolve"); |
| | 1 | 50 | | handle_fault_request(&req, &res); |
| | 1 | 51 | | assert(res.status_code == 200); |
| | 1 | 52 | | assert(strstr(res.body, "resolved") != NULL); |
| | 1 | 53 | | } |
| | | 54 | | |
| | 1 | 55 | | void test_fault_invalid_method() { |
| | 1 | 56 | | HttpRequest req = {0}; |
| | 1 | 57 | | HttpResponse res = {0}; |
| | 1 | 58 | | strcpy(req.method, "DELETE"); |
| | 1 | 59 | | handle_fault_request(&req, &res); |
| | 1 | 60 | | assert(res.status_code == 405); |
| | 1 | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | int main() { |
| | 1 | 64 | | test_fault_get_all(); |
| | 1 | 65 | | test_fault_get_single(); |
| | 1 | 66 | | test_fault_post(); |
| | 1 | 67 | | test_fault_put_ack(); |
| | 1 | 68 | | test_fault_put_resolve(); |
| | 1 | 69 | | test_fault_invalid_method(); |
| | 1 | 70 | | printf("[PASS] Fault handler\n"); |
| | 1 | 71 | | return 0; |
| | | 72 | | } |