< Summary - Backend C Tests - Coverage Report

Information
Class: machine_handler_c
Assembly: src.backend.api.handlers
File(s): C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\src\backend\api\handlers\machine_handler.c
Line coverage
100%
Covered lines: 43
Uncovered lines: 0
Coverable lines: 43
Total lines: 76
Line coverage: 100%
Branch coverage
86%
Covered branches: 19
Total branches: 22
Branch coverage: 86.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
handle_machine_request86.36%00100%

File(s)

C:\Users\yagiz\Desktop\Project\smart-maintenance-suite\src\backend\api\handlers\machine_handler.c

#LineLine coverage
 1#include "machine_handler.h"
 2#include "../../database/api_handlers.h"
 3#include "../../database/machine_service.h"
 4#include "../../database/cJSON.h"
 5#include <string.h>
 6#include <stdlib.h>
 7#include <stdio.h>
 8
 9
 10
 11// Serialization is handled by api_handlers.c void serialize_machines_to_json is already defined there.
 12
 613void handle_machine_request(HttpRequest *req, HttpResponse *res) {
 614  if (strcmp(req->method, "GET") == 0) {
 115    char *json = serialize_machines_to_json();
 116    res->status_code = 200;
 117    strcpy(res->content_type, "application/json");
 118    strncpy(res->body, json, 8191);
 119    res->body[8191] = '\0';
 120    free(json);
 521  } else if (strcmp(req->method, "POST") == 0) {
 522    cJSON *root = cJSON_Parse(req->body);
 23
 524    if (root) {
 425      cJSON *name_item = cJSON_GetObjectItem(root, "name");
 426      cJSON *model_item = cJSON_GetObjectItem(root, "model");
 427      cJSON *location_item = cJSON_GetObjectItem(root, "location");
 428      cJSON *status_item = cJSON_GetObjectItem(root, "status");
 29
 630      if (name_item && status_item) {
 331        const char *name = name_item->valuestring;
 332        const char *model = model_item ? model_item->valuestring : "Standard";
 333        const char *location = location_item ? location_item->valuestring : "Main Hall";
 334        const char *status = status_item->valuestring;
 35        // --- DUPLICATE CHECK ---
 36        Machine list[50];
 337        int count = get_all_machines(list, 50);
 338        bool duplicate = false;
 39
 340        for (int i = 0; i < count; i++) {
 141          if (strcmp(list[i].name, name) == 0) {
 142            duplicate = true;
 143            break;
 44          }
 45        }
 46
 347        if (duplicate) {
 148          res->status_code = 409;
 149          strcpy(res->content_type, "application/json");
 150          strcpy(res->body, "{\"error\": \"A machine with this name already exists in the registry.\"}");
 151          cJSON_Delete(root);
 152          return;
 53        }
 54
 55        // ------------------------
 56
 257        if (add_machine(name, model, location, status)) {
 158          res->status_code = 201;
 159          strcpy(res->content_type, "application/json");
 160          strcpy(res->body, "{\"success\": true, \"message\": \"Machine onboarded successfully\"}");
 61        } else {
 162          res->status_code = 500;
 163          strcpy(res->body, "{\"error\": \"Database error while onboarding machine\"}");
 64        }
 65      } else {
 166        res->status_code = 400;
 167        strcpy(res->body, "{\"error\": \"Missing required fields: name, status\"}");
 68      }
 69
 370      cJSON_Delete(root);
 71    } else {
 172      res->status_code = 400;
 173      strcpy(res->body, "{\"error\": \"Invalid JSON\"}");
 74    }
 75  }
 76}

Methods/Properties

handle_machine_request