< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: test_router_c
Assembly: src.backend.tests.unit
File(s): ./src/backend/tests/unit/test_router.c
Line coverage
97%
Covered lines: 253
Uncovered lines: 6
Coverable lines: 259
Total lines: 453
Line coverage: 97.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 97.6% (253/259) Total lines: 453

File(s)

./src/backend/tests/unit/test_router.c

#LineLine coverage
 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// Header dosyalarını doğru path ile include et
 10#include "../api/router.h"
 11#include "../api/handlers/machine_handler.h"
 12#include "../api/handlers/inventory_handler.h"
 13#include "../api/handlers/auth_handler.h"
 14#include "../api/handlers/maintenance_handler.h"
 15#include "../api/handlers/report_handler.h"
 16#include "../api/handlers/fault_handler.h"
 17#include "../database/alert_service.h"
 18
 19/* ====================================================================
 20 * MOCK FONKSİYONLAR VE GLOBAL DEĞİŞKENLER
 21 * ==================================================================== */
 22
 23// Handler çağrılarını takip etmek için counter'lar
 24static int mock_machine_handler_called = 0;
 25static int mock_inventory_handler_called = 0;
 26static int mock_auth_handler_called = 0;
 27static int mock_maintenance_handler_called = 0;
 28static int mock_report_handler_called = 0;
 29static int mock_fault_handler_called = 0;
 30static int mock_alert_service_called = 0;
 31
 32// Handler'lara gönderilen request/response'ları kaydetmek için
 33static HttpRequest last_request;
 34static HttpResponse last_response;
 35
 36// Mock handler fonksiyonları
 537void handle_machine_request(HttpRequest *req, HttpResponse *res) {
 538  mock_machine_handler_called++;
 39  // Handler'ın request'i değiştirdiğini simüle et
 540  res->status_code = 200;
 541  strcpy(res->content_type, "application/json");
 542  strcpy(res->body, "{\"machines\":[]}");
 543}
 44
 445void handle_inventory_request(HttpRequest *req, HttpResponse *res) {
 446  mock_inventory_handler_called++;
 447  res->status_code = 200;
 448  strcpy(res->content_type, "application/json");
 449  strcpy(res->body, "{\"inventory\":[]}");
 450}
 51
 152void handle_auth_request(HttpRequest *req, HttpResponse *res) {
 153  mock_auth_handler_called++;
 154  res->status_code = 200;
 155  strcpy(res->content_type, "application/json");
 156  strcpy(res->body, "{\"token\":\"mock_token\"}");
 157}
 58
 359void handle_maintenance_request(HttpRequest *req, HttpResponse *res) {
 360  mock_maintenance_handler_called++;
 361  res->status_code = 200;
 362  strcpy(res->content_type, "application/json");
 363  strcpy(res->body, "{\"maintenance\":[]}");
 364}
 65
 266void handle_report_request(HttpRequest *req, HttpResponse *res) {
 267  mock_report_handler_called++;
 268  res->status_code = 200;
 269  strcpy(res->content_type, "application/json");
 270  strcpy(res->body, "{\"reports\":[]}");
 271}
 72
 373void handle_fault_request(HttpRequest *req, HttpResponse *res) {
 374  mock_fault_handler_called++;
 375  res->status_code = 200;
 376  strcpy(res->content_type, "application/json");
 377  strcpy(res->body, "{\"faults\":[]}");
 378}
 79
 80// Alert service mock
 181char *alert_service_serialize_alerts(void) {
 182  mock_alert_service_called++;
 183  char *json = malloc(100);
 184  strcpy(json, "{\"alerts\":[{\"id\":1,\"message\":\"test\"}]}");
 185  return json;
 86}
 87
 88/* ====================================================================
 89 * Test Setup/Teardown
 90 * ==================================================================== */
 91
 2292static void reset_mocks(void) {
 2293  mock_machine_handler_called = 0;
 2294  mock_inventory_handler_called = 0;
 2295  mock_auth_handler_called = 0;
 2296  mock_maintenance_handler_called = 0;
 2297  mock_report_handler_called = 0;
 2298  mock_fault_handler_called = 0;
 2299  mock_alert_service_called = 0;
 22100  memset(&last_request, 0, sizeof(last_request));
 22101  memset(&last_response, 0, sizeof(last_response));
 22102}
 103
 104/* ====================================================================
 105 * Test Cases - Tüm Route'lar
 106 * ==================================================================== */
 107
 1108static void test_route_machines(void **state) {
 1109  reset_mocks();
 110  HttpRequest req;
 111  HttpResponse res;
 1112  strcpy(req.path, "/api/machines");
 1113  strcpy(req.method, "GET");
 1114  handle_route(&req, &res);
 115  // Handler çağrıldı mı?
 1116  assert_int_equal(mock_machine_handler_called, 1);
 1117  assert_int_equal(mock_inventory_handler_called, 0);
 1118  assert_int_equal(mock_auth_handler_called, 0);
 1119  assert_int_equal(mock_maintenance_handler_called, 0);
 1120  assert_int_equal(mock_report_handler_called, 0);
 1121  assert_int_equal(mock_fault_handler_called, 0);
 1122  assert_int_equal(mock_alert_service_called, 0);
 123  // Response doğru mu?
 1124  assert_int_equal(res.status_code, 200);
 1125  assert_string_equal(res.content_type, "application/json");
 1126  assert_string_equal(res.body, "{\"machines\":[]}");
 1127}
 128
 1129static void test_route_machines_with_id(void **state) {
 1130  reset_mocks();
 131  HttpRequest req;
 132  HttpResponse res;
 1133  strcpy(req.path, "/api/machines/123");
 1134  strcpy(req.method, "GET");
 1135  handle_route(&req, &res);
 1136  assert_int_equal(mock_machine_handler_called, 1);
 1137  assert_int_equal(res.status_code, 200);
 1138}
 139
 1140static void test_route_inventory(void **state) {
 1141  reset_mocks();
 142  HttpRequest req;
 143  HttpResponse res;
 1144  strcpy(req.path, "/api/inventory");
 1145  strcpy(req.method, "GET");
 1146  handle_route(&req, &res);
 1147  assert_int_equal(mock_inventory_handler_called, 1);
 1148  assert_int_equal(mock_machine_handler_called, 0);
 1149  assert_int_equal(res.status_code, 200);
 1150  assert_string_equal(res.body, "{\"inventory\":[]}");
 1151}
 152
 1153static void test_route_inventory_with_sku(void **state) {
 1154  reset_mocks();
 155  HttpRequest req;
 156  HttpResponse res;
 1157  strcpy(req.path, "/api/inventory/MO-001");
 1158  strcpy(req.method, "GET");
 1159  handle_route(&req, &res);
 1160  assert_int_equal(mock_inventory_handler_called, 1);
 1161}
 162
 1163static void test_route_login(void **state) {
 1164  reset_mocks();
 165  HttpRequest req;
 166  HttpResponse res;
 1167  strcpy(req.path, "/api/login");
 1168  strcpy(req.method, "POST");
 1169  handle_route(&req, &res);
 1170  assert_int_equal(mock_auth_handler_called, 1);
 1171  assert_int_equal(mock_inventory_handler_called, 0);
 1172  assert_int_equal(res.status_code, 200);
 1173  assert_string_equal(res.body, "{\"token\":\"mock_token\"}");
 1174}
 175
 1176static void test_route_maintenance(void **state) {
 1177  reset_mocks();
 178  HttpRequest req;
 179  HttpResponse res;
 1180  strcpy(req.path, "/api/maintenance");
 1181  strcpy(req.method, "GET");
 1182  handle_route(&req, &res);
 1183  assert_int_equal(mock_maintenance_handler_called, 1);
 1184  assert_int_equal(res.status_code, 200);
 1185  assert_string_equal(res.body, "{\"maintenance\":[]}");
 1186}
 187
 1188static void test_route_maintenance_with_id(void **state) {
 1189  reset_mocks();
 190  HttpRequest req;
 191  HttpResponse res;
 1192  strcpy(req.path, "/api/maintenance/456");
 1193  strcpy(req.method, "GET");
 1194  handle_route(&req, &res);
 1195  assert_int_equal(mock_maintenance_handler_called, 1);
 1196}
 197
 1198static void test_route_reports(void **state) {
 1199  reset_mocks();
 200  HttpRequest req;
 201  HttpResponse res;
 1202  strcpy(req.path, "/api/reports");
 1203  strcpy(req.method, "GET");
 1204  handle_route(&req, &res);
 1205  assert_int_equal(mock_report_handler_called, 1);
 1206  assert_int_equal(res.status_code, 200);
 1207  assert_string_equal(res.body, "{\"reports\":[]}");
 1208}
 209
 1210static void test_route_reports_with_type(void **state) {
 1211  reset_mocks();
 212  HttpRequest req;
 213  HttpResponse res;
 1214  strcpy(req.path, "/api/reports/daily");
 1215  strcpy(req.method, "GET");
 1216  handle_route(&req, &res);
 1217  assert_int_equal(mock_report_handler_called, 1);
 1218}
 219
 1220static void test_route_faults(void **state) {
 1221  reset_mocks();
 222  HttpRequest req;
 223  HttpResponse res;
 1224  strcpy(req.path, "/api/faults");
 1225  strcpy(req.method, "GET");
 1226  handle_route(&req, &res);
 1227  assert_int_equal(mock_fault_handler_called, 1);
 1228  assert_int_equal(res.status_code, 200);
 1229  assert_string_equal(res.body, "{\"faults\":[]}");
 1230}
 231
 1232static void test_route_faults_with_id(void **state) {
 1233  reset_mocks();
 234  HttpRequest req;
 235  HttpResponse res;
 1236  strcpy(req.path, "/api/faults/789");
 1237  strcpy(req.method, "GET");
 1238  handle_route(&req, &res);
 1239  assert_int_equal(mock_fault_handler_called, 1);
 1240}
 241
 1242static void test_route_alerts(void **state) {
 1243  reset_mocks();
 244  HttpRequest req;
 245  HttpResponse res;
 1246  strcpy(req.path, "/api/alerts");
 1247  strcpy(req.method, "GET");
 1248  handle_route(&req, &res);
 1249  assert_int_equal(mock_alert_service_called, 1);
 1250  assert_int_equal(mock_machine_handler_called, 0);
 1251  assert_int_equal(res.status_code, 200);
 1252  assert_string_equal(res.content_type, "application/json");
 1253  assert_string_equal(res.body, "{\"alerts\":[{\"id\":1,\"message\":\"test\"}]}");
 1254}
 255
 256/* ====================================================================
 257 * Test Cases - 404 Not Found
 258 * ==================================================================== */
 259
 1260static void test_route_not_found(void **state) {
 1261  reset_mocks();
 262  HttpRequest req;
 263  HttpResponse res;
 1264  strcpy(req.path, "/api/unknown");
 1265  strcpy(req.method, "GET");
 1266  handle_route(&req, &res);
 267  // Hiçbir handler çağrılmamalı
 1268  assert_int_equal(mock_machine_handler_called, 0);
 1269  assert_int_equal(mock_inventory_handler_called, 0);
 1270  assert_int_equal(mock_auth_handler_called, 0);
 1271  assert_int_equal(mock_maintenance_handler_called, 0);
 1272  assert_int_equal(mock_report_handler_called, 0);
 1273  assert_int_equal(mock_fault_handler_called, 0);
 1274  assert_int_equal(mock_alert_service_called, 0);
 275  // 404 response kontrolü
 1276  assert_int_equal(res.status_code, 404);
 1277  assert_string_equal(res.content_type, "application/json");
 1278  assert_string_equal(res.body, "{\"error\": \"Route not found\"}");
 1279}
 280
 1281static void test_route_empty_path(void **state) {
 1282  reset_mocks();
 283  HttpRequest req;
 284  HttpResponse res;
 1285  strcpy(req.path, "");
 1286  strcpy(req.method, "GET");
 1287  handle_route(&req, &res);
 1288  assert_int_equal(res.status_code, 404);
 1289  assert_string_equal(res.body, "{\"error\": \"Route not found\"}");
 1290}
 291
 1292static void test_route_root_path(void **state) {
 1293  reset_mocks();
 294  HttpRequest req;
 295  HttpResponse res;
 1296  strcpy(req.path, "/");
 1297  strcpy(req.method, "GET");
 1298  handle_route(&req, &res);
 1299  assert_int_equal(res.status_code, 404);
 1300}
 301
 302/* ====================================================================
 303 * Test Cases - Farklı HTTP Metodları
 304 * ==================================================================== */
 305
 1306static void test_route_machines_post(void **state) {
 1307  reset_mocks();
 308  HttpRequest req;
 309  HttpResponse res;
 1310  strcpy(req.path, "/api/machines");
 1311  strcpy(req.method, "POST");
 1312  strcpy(req.body, "{\"name\":\"new machine\"}");
 1313  handle_route(&req, &res);
 1314  assert_int_equal(mock_machine_handler_called, 1);
 315  // Handler metod farkına bakmamalı, sadece path'e bakıyor
 1316}
 317
 1318static void test_route_inventory_put(void **state) {
 1319  reset_mocks();
 320  HttpRequest req;
 321  HttpResponse res;
 1322  strcpy(req.path, "/api/inventory/1");
 1323  strcpy(req.method, "PUT");
 1324  handle_route(&req, &res);
 1325  assert_int_equal(mock_inventory_handler_called, 1);
 1326}
 327
 1328static void test_route_faults_delete(void **state) {
 1329  reset_mocks();
 330  HttpRequest req;
 331  HttpResponse res;
 1332  strcpy(req.path, "/api/faults/789");
 1333  strcpy(req.method, "DELETE");
 1334  handle_route(&req, &res);
 1335  assert_int_equal(mock_fault_handler_called, 1);
 1336}
 337
 338/* ====================================================================
 339 * Test Cases - Path Çakışmaları
 340 * ==================================================================== */
 341
 1342static void test_route_order_priority(void **state) {
 1343  reset_mocks();
 344  HttpRequest req;
 345  HttpResponse res;
 346  // /api/machines önce kontrol ediliyor
 1347  strcpy(req.path, "/api/machines/inventory"); // Bu path hem machines hem inventory ile eşleşebilir
 1348  strcpy(req.method, "GET");
 1349  handle_route(&req, &res);
 350  // İlk eşleşen handler çağrılmalı (machines)
 1351  assert_int_equal(mock_machine_handler_called, 1);
 1352  assert_int_equal(mock_inventory_handler_called, 0);
 1353}
 354
 1355static void test_route_similar_paths(void **state) {
 1356  reset_mocks();
 357  HttpRequest req;
 358  HttpResponse res;
 1359  strcpy(req.path, "/api/maintenance/reports"); // Hem maintenance hem reports
 1360  strcpy(req.method, "GET");
 1361  handle_route(&req, &res);
 362  // İlk eşleşen: maintenance
 1363  assert_int_equal(mock_maintenance_handler_called, 1);
 1364  assert_int_equal(mock_report_handler_called, 0);
 1365}
 366
 367/* ====================================================================
 368 * Test Cases - Edge Cases
 369 * ==================================================================== */
 370
 0371static void test_route_null_path(void **state) {
 0372  reset_mocks();
 373  HttpRequest req;
 374  HttpResponse res;
 0375  memset(&req, 0, sizeof(req));
 376  // path null olabilir, strstr NULL pointer ile çağrılır mı?
 377  // Gerçek kodda path'in null olması beklenmez ama yine de test edelim
 378  // Not: Bu test çökebilir, gerçek kodda path'in hep valid olduğunu varsayıyoruz
 379  // req.path[0] = '\0'; olarak bırakıyoruz
 0380  handle_route(&req, &res);
 381  // Muhtemelen 404 döner
 0382  assert_int_equal(res.status_code, 404);
 0383}
 384
 1385static void test_route_very_long_path(void **state) {
 1386  reset_mocks();
 387  HttpRequest req;
 388  HttpResponse res;
 389  char long_path[1000];
 1390  strcpy(long_path, "/api/machines/");
 1391  memset(long_path + strlen(long_path), 'a', 900);
 1392  long_path[950] = '\0';
 1393  strcpy(req.path, long_path);
 1394  strcpy(req.method, "GET");
 1395  handle_route(&req, &res);
 396  // Başlangıç /api/machines ile eşleşmeli
 1397  assert_int_equal(mock_machine_handler_called, 1);
 1398}
 399
 1400static void test_route_path_with_query(void **state) {
 1401  reset_mocks();
 402  HttpRequest req;
 403  HttpResponse res;
 1404  strcpy(req.path, "/api/inventory?min=10&max=100");
 1405  strcpy(req.method, "GET");
 1406  strcpy(req.query_params, "min=10&max=100");
 1407  handle_route(&req, &res);
 1408  assert_int_equal(mock_inventory_handler_called, 1);
 409  // Query params path'ten ayrılmış olmalı
 1410  assert_string_equal(req.query_params, "min=10&max=100");
 1411}
 412
 413/* ====================================================================
 414 * Main test runner
 415 * ==================================================================== */
 416
 1417int main(void) {
 1418  const struct CMUnitTest tests[] = {
 419    // Ana route'lar
 420    cmocka_unit_test(test_route_machines),
 421    cmocka_unit_test(test_route_machines_with_id),
 422    cmocka_unit_test(test_route_inventory),
 423    cmocka_unit_test(test_route_inventory_with_sku),
 424    cmocka_unit_test(test_route_login),
 425    cmocka_unit_test(test_route_maintenance),
 426    cmocka_unit_test(test_route_maintenance_with_id),
 427    cmocka_unit_test(test_route_reports),
 428    cmocka_unit_test(test_route_reports_with_type),
 429    cmocka_unit_test(test_route_faults),
 430    cmocka_unit_test(test_route_faults_with_id),
 431    cmocka_unit_test(test_route_alerts),
 432
 433    // 404 testleri
 434    cmocka_unit_test(test_route_not_found),
 435    cmocka_unit_test(test_route_empty_path),
 436    cmocka_unit_test(test_route_root_path),
 437
 438    // Farklı HTTP metodları
 439    cmocka_unit_test(test_route_machines_post),
 440    cmocka_unit_test(test_route_inventory_put),
 441    cmocka_unit_test(test_route_faults_delete),
 442
 443    // Path çakışmaları
 444    cmocka_unit_test(test_route_order_priority),
 445    cmocka_unit_test(test_route_similar_paths),
 446
 447    // Edge cases
 448    cmocka_unit_test(test_route_very_long_path),
 449    cmocka_unit_test(test_route_path_with_query),
 450    // cmocka_unit_test(test_route_null_path), // Bu test çökebilir, yoruma al
 451  };
 1452  return cmocka_run_group_tests(tests, NULL, NULL);
 453}

Methods/Properties