diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 52d2760..a6b9d9c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -81,6 +81,7 @@ target_link_libraries (test_device add_custom_command(TARGET test_device POST_BUILD COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/tests/index.html ${CMAKE_BINARY_DIR}/bin + COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/tests/devices.json ${CMAKE_BINARY_DIR}/bin COMMENT "Install test Files" ) diff --git a/src/rest/rest_devices_handlers.c b/src/rest/rest_devices_handlers.c index 8280198..38e984d 100644 --- a/src/rest/rest_devices_handlers.c +++ b/src/rest/rest_devices_handlers.c @@ -25,95 +25,82 @@ /*------------------------------- INCLUDES ----------------------------------*/ -#include - #include #include "devices/devices_manager.h" + +#include "rest_devices_handlers.h" + #include "domo.h" -//#include "rest_devices_handlers.h" // Code Description // 204 Success. No content. // 400 Bad request /*--------------------------------------------------------------------------*/ -#if 0 -int outlet_create_handler(short event, restd_conn_t *conn, void *userdata) + +int outlet_create_handler(restd_resp_t *response, void *arg) { + struct json_object *root_node = NULL; + char *data; + devices_manager_t *dm = (devices_manager_t *)arg; + printf("outlet_create_handler\n\n"); - if (event & RESTD_EVENT_WRITE) + + data = restd_http_get_body(response); + + if (data == NULL) { - off_t data_size; - - data_size = restd_http_get_content_length(conn); - printf("1/data size: %ld\n", data_size); - if (data_size > 0) - { - char *data; - struct json_object *root_node = NULL; - - data = restd_http_get_content(conn, 0, &data_size); - printf("2/data size: %ld\n", data_size); - printf("3/data size: %s\n", data); - -#if 0 - root_node = json_tokener_parse(data); - free(data); - - if (root_node != NULL) - { - int ret; - devices_manager_t *dm; - - ret = devices_manager_create(dm, kOutletEntry, root_node); - json_object_put(root_node); - if (ret == 0) - { - restd_http_response(conn, 204, "application/json", "{}", 2); - return RESTD_CLOSE; - } - } -#endif - restd_http_response(conn, 204, "application/json", "{}", 2); - return RESTD_CLOSE; - - } - - restd_http_response(conn, 400, "application/json", "{\"status\":\"error\"}", 18); + restd_http_response(response, 400, "application/json", kerror_body); return RESTD_OK; } + root_node = json_tokener_parse(data); + free(data); + + if (root_node != NULL) + { + int ret; + + ret = devices_manager_create(dm, kOutletEntry, root_node); + json_object_put(root_node); + if (ret == 0) + { + restd_http_response(response, 204, "application/json", kempty_body); + return RESTD_OK; + } + } + + restd_http_response(response, 400, "application/json", kerror_body); return RESTD_OK; } /*--------------------------------------------------------------------------*/ -int outlet_list_handler(short event, restd_conn_t *conn, void *userdata) +int outlet_list_handler(restd_resp_t *response, void *arg) { printf("outlet_list_handler\n\n"); - restd_http_response(conn, 200, "application/json", "{}", 2); - return RESTD_CLOSE; + restd_http_response(response, 200, "application/json", kempty_body); + return RESTD_OK; } /*--------------------------------------------------------------------------*/ -int outlet_get_handler(short event, restd_conn_t *conn, void *userdata) +int outlet_get_handler(restd_resp_t *response, void *arg) { return 0; } /*--------------------------------------------------------------------------*/ -int outlet_update_handler(short event, restd_conn_t *conn, void *userdata) +int outlet_update_handler(restd_resp_t *response, void *arg) { return 0; } /*--------------------------------------------------------------------------*/ -int outlet_remove_handler(short event, restd_conn_t *conn, void *userdata) +int outlet_remove_handler(restd_resp_t *response, void *arg) { return 0; } -#endif diff --git a/src/rest/rest_devices_handlers.h b/src/rest/rest_devices_handlers.h index fbaa3e9..026ffe3 100644 --- a/src/rest/rest_devices_handlers.h +++ b/src/rest/rest_devices_handlers.h @@ -30,11 +30,15 @@ #include + +#define kerror_body "{ \"status\": \"error\" }" +#define kempty_body "{}" + // Outlets -extern int outlet_create_handler(short event, restd_conn_t *conn, void *userdata); -extern int outlet_list_handler(short event, restd_conn_t *conn, void *userdata); -extern int outlet_get_handler(short event, restd_conn_t *conn, void *userdata); -extern int outlet_update_handler(short event, restd_conn_t *conn, void *userdata); -extern int outlet_remove_handler(short event, restd_conn_t *conn, void *userdata); +extern int outlet_create_handler(restd_resp_t *response, void *arg); +extern int outlet_list_handler(restd_resp_t *response, void *arg); +extern int outlet_get_handler(restd_resp_t *response, void *arg); +extern int outlet_update_handler(restd_resp_t *response, void *arg); +extern int outlet_remove_handler(restd_resp_t *response, void *arg); #endif /*_REST_DEVICES_HANDLERS_H */ diff --git a/src/rest/rest_server.c b/src/rest/rest_server.c index 5ec3356..185cb45 100644 --- a/src/rest/rest_server.c +++ b/src/rest/rest_server.c @@ -28,57 +28,52 @@ /*------------------------------- INCLUDES ----------------------------------*/ -//#include "rest_devices_handlers.h" +#include "rest_devices_handlers.h" #include "rest_server.h" -#if 0 /*--------------------------------------------------------------------------*/ -int my_error_handler(short event, restd_conn_t *conn, void *userdata) +int my_error_handler(restd_resp_t *response, int reason, void *arg) { int http_code = 500; - if (event & RESTD_ERROR_METHOD_NOT_ALLOWED) + if (reason == RESTD_ERROR_METHOD_NOT_ALLOWED) { http_code = 405; } - else if (event & RESTD_ERROR_PATH_NOT_FOUND) + else if (reason == RESTD_ERROR_PATH_NOT_FOUND) { http_code = 404; } - restd_http_response(conn, http_code, "application/json", "{\"status\":\"error\"}", 18); - return RESTD_CLOSE; // Close connection. + restd_http_response(response, http_code, "application/json", kerror_body); } /*--------------------------------------------------------------------------*/ -#endif + int setup_rest_server(restd_server_t *rest_server, const char *port, const char *root_path, void *dm) { -#if 0 + restd_server_set_option(rest_server, "server.port", port); restd_server_set_option(rest_server, "server.root_path", root_path); printf (">>>>>>>>>>\n"); - //restd_server_register_request_handler(rest_server, restd_rest_handler); - restd_server_register_error_handler(rest_server, my_error_handler); + restd_server_register_error_handler(rest_server, my_error_handler, NULL); - restd_server_register_call_hooks_handler(rest_server, restd_rest_handler); - // restd_server_register_hook(rest_server, restd_http_handler, NULL); - // Capabilities. - restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/capabilities", NULL, dm); + // Capabilities. + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/capabilities", NULL, dm); // Outlets - restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/outlets", outlet_create_handler, dm); - restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/outlets", outlet_list_handler, dm); - restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/outlets/:id", outlet_get_handler, dm); - restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/outlets/:id", outlet_update_handler, dm); - restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/outlets/:id", outlet_remove_handler, dm); + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_POST, "/api/v1/outlets", outlet_create_handler, dm); + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/outlets", outlet_list_handler, dm); + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/outlets/:id", outlet_get_handler, dm); + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, "/api/v1/outlets/:id", outlet_update_handler, dm); + restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_DELETE, "/api/v1/outlets/:id", outlet_remove_handler, dm); - // Outlets + // Sprinkler // Shutters -#endif + return 0; } diff --git a/src/tests/devices.json b/src/tests/devices.json new file mode 100644 index 0000000..75e998b --- /dev/null +++ b/src/tests/devices.json @@ -0,0 +1,5 @@ +{ + "outlets": [], + "shutters": [], + "sprinklers": [] +} \ No newline at end of file diff --git a/src/tests/test_domo.c b/src/tests/test_domo.c new file mode 100644 index 0000000..12bb9e8 --- /dev/null +++ b/src/tests/test_domo.c @@ -0,0 +1,75 @@ +/*! + * test_domo.c + * + * Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 21/02/2020 + * + */ + +/*--------------------------------------------------------------------------*/ + + + +/*--------------------------------------------------------------------------*/ + +TEST("Domo - Server - Create Free\t") +{ + restd_server_t *rest_server; + + rest_server = restd_server_new(); + ASSERT_NOT_NULL(rest_server); + + restd_server_free(rest_server); +} + +/*--------------------------------------------------------------------------*/ + +TEST("Domo - Test API correct access\t") +{ + struct event_base *ev_base; + restd_server_t *rest_server; + devices_manager_t *dm; + char *config_path; + int ret; + + dm = devices_manager_new(); + ASSERT_NOT_NULL(dm); + + ev_base = event_base_new(); + ASSERT_NOT_NULL(ev_base); + + rest_server = restd_server_new(); + ASSERT_NOT_NULL(rest_server); + + ret = restd_server_attach_event_loop(rest_server, ev_base); + ASSERT_EQUAL_INT(ret, 0); + + config_path = get_config_path(); + ASSERT_NOT_NULL(config_path); + + ret = setup_rest_server(rest_server, kserver_port, config_path, dm); + ASSERT_EQUAL_INT(ret, 0); + + // TODO + + restd_server_free(rest_server); + devices_manager_free(dm); + free(config_path); +} diff --git a/src/tests/test_main.c b/src/tests/test_main.c index 10dce2f..5c6981d 100644 --- a/src/tests/test_main.c +++ b/src/tests/test_main.c @@ -1,5 +1,5 @@ /*! - * test_device.c + * test_main.c * * Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved. * @@ -32,8 +32,9 @@ #include #include +#include + #include -#include #include #include @@ -44,8 +45,60 @@ #include "domo.h" #include "devices/devices_manager.h" +#include "rest/rest_server.h" -QUNIT_START("Test Data Model"); +/*--------------------------------------------------------------------------*/ + +#define k_max_path_len 200 /* make this larger if you need to. */ + +/*--------------------------------------------------------------------------*/ + +char *get_config_path(void) +{ + int length; + char full_path[k_max_path_len]; + char *working_path; + char *last_slash; + + length = readlink("/proc/self/exe", full_path, sizeof(full_path)); + + /* Catch some errors: */ + if (length < 0) + { + fprintf(stderr, "Error resolving symlink /proc/self/exe.\n"); + return NULL; + } + if (length >= k_max_path_len) + { + fprintf(stderr, "Path too long. Truncated.\n"); + return NULL; + } + + /* I don't know why, but the string this readlink() function + * returns is appended with a '@'. + */ + full_path[length] = '\0'; /* Strip '@' off the end. */ + + working_path = dirname(full_path); + if (working_path == NULL) + return NULL; + + return strdup(working_path); +} + +/*--------------------------------------------------------------------------*/ + +QUNIT_START("Test Domo"); + +/*--------------------------------------------------------------------------*/ + +TEST("Get config path\t") +{ + char *config_path; + config_path = get_config_path(); + ASSERT_NOT_NULL(config_path); + free(config_path); +} /*--------------------------------------------------------------------------*/ @@ -55,6 +108,7 @@ TEST("devices_manager create and free\t") dm = devices_manager_new(); ASSERT_NOT_NULL(dm); + devices_manager_free(dm); } @@ -63,5 +117,6 @@ TEST("devices_manager create and free\t") #include "test_shutter.c" #include "test_outlet.c" #include "test_rest.c" +#include "test_domo.c" QUNIT_END(); diff --git a/src/tests/test_rest.c b/src/tests/test_rest.c index 05daea2..1e45e26 100644 --- a/src/tests/test_rest.c +++ b/src/tests/test_rest.c @@ -36,7 +36,6 @@ #define ksuccess_put_param1 "{ \"id\": 1 }" #define ksuccess_put_param2 "{ \"year\": 1977, \"action\": \"todo\" }" -#define kserver_port "7777" #define kapi_test_get "/api/v1/test_get" #define kapi_test_put_id1 "/api/v1/test_put/:id" @@ -49,7 +48,7 @@ #define kput_method "PUT" #define kdelete_method "DELETE" -#define k_max_path_len 200 /* make this larger if you need to. */ +#define kserver_port "7777" /*--------------------------------------------------------------------------*/ @@ -350,39 +349,6 @@ bool found_special_route(restd_server_t *server, enum evhttp_cmd_type method, co /*--------------------------------------------------------------------------*/ -char *get_config_path(void) -{ - int length; - char full_path[k_max_path_len]; - char *working_path; - char *last_slash; - - length = readlink("/proc/self/exe", full_path, sizeof(full_path)); - - /* Catch some errors: */ - if (length < 0) - { - fprintf(stderr, "Error resolving symlink /proc/self/exe.\n"); - return NULL; - } - if (length >= k_max_path_len) - { - fprintf(stderr, "Path too long. Truncated.\n"); - return NULL; - } - - /* I don't know why, but the string this readlink() function - * returns is appended with a '@'. - */ - full_path[length] = '\0'; /* Strip '@' off the end. */ - - working_path = dirname(full_path); - - return working_path; -} - -/*--------------------------------------------------------------------------*/ - TEST("Rest - create free\t") { restd_server_t *rest_server; @@ -611,15 +577,16 @@ TEST("Rest - Test Web server part.\t") { int ret; restd_server_t *rest_server; - char *root_path; + char *config_path; rest_server = restd_server_new(); ASSERT_NOT_NULL(rest_server); - root_path = get_config_path(); + config_path = get_config_path(); + ASSERT_NOT_NULL(config_path); restd_server_set_option(rest_server, "server.port", kserver_port); restd_server_set_option(rest_server, "server.thread", "1"); - restd_server_set_option(rest_server, "server.root_path", root_path); + restd_server_set_option(rest_server, "server.root_path", config_path); restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_rest_get_handler, NULL); restd_server_register_error_handler(rest_server, my_error_handler, NULL); @@ -639,4 +606,5 @@ TEST("Rest - Test Web server part.\t") ASSERT_EQUAL_INT(ret, 0); restd_server_free(rest_server); -} \ No newline at end of file + free(config_path); +}