/*! * 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 * */ /*--------------------------------------------------------------------------*/ char *load_capabilities(devices_manager_t *dm) { int s; FILE *f; char buf[1024]; char *capabilities; f = fopen(dm->capabilities_path, "rb"); if (f == NULL) return NULL; s = fread(buf, 1, sizeof(buf), f); fclose(f); buf[s] = '\0'; capabilities = strdup(buf); return capabilities; } /*--------------------------------------------------------------------------*/ restd_server_t *setup_server(devices_manager_t *dm) { int ret; struct event_base *ev_base; char *config_path, *capabilities_path; restd_server_t *rest_server = NULL; 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); capabilities_path = qstrdupf("%s/capabilities.json", config_path); devices_manager_set_capabilities_path(dm, capabilities_path); free(capabilities_path); ret = setup_rest_server(rest_server, kserver_port, config_path, dm); ASSERT_EQUAL_INT(ret, 0); restd_server_set_option(rest_server, "server.thread", "1"); free(config_path); ret = restd_server_start(rest_server); ASSERT_EQUAL_INT(ret, 0); return rest_server; } /*--------------------------------------------------------------------------*/ 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 /api/v1/capabilities - correct access\t") { restd_server_t *rest_server; devices_manager_t *dm; int ret; char *capabilities; dm = devices_manager_new(); ASSERT_NOT_NULL(dm); rest_server = setup_server(dm); ASSERT_NOT_NULL(rest_server); sleep(1); // Capabilites capabilities = load_capabilities(dm); ASSERT_NOT_NULL(capabilities); ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/capabilities", 200, "", capabilities, false); ASSERT_EQUAL_INT(ret, 0); free(capabilities); restd_server_free(rest_server); devices_manager_free(dm); } /*--------------------------------------------------------------------------*/ TEST("Domo - Test API /api/v1/outlets - correct access\t") { restd_server_t *rest_server; devices_manager_t *dm; int ret; dm = devices_manager_new(); ASSERT_NOT_NULL(dm); rest_server = setup_server(dm); ASSERT_NOT_NULL(rest_server); sleep(1); // Get All Devices. Should be empty. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/outlets", 200, "", k_outlet_list_empty, false); ASSERT_EQUAL_INT(ret, 0); // Create An Outlet. Should be empty. ret = exec_request(kpost_method, "http://localhost:" kserver_port "/api/v1/outlets", 204, k_create_outlet_1, "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain One Device. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/outlets", 200, "", k_outlet_list_one_elem, false); ASSERT_EQUAL_INT(ret, 0); // Create A second Outlet. Should Contain two Device. ret = exec_request(kpost_method, "http://localhost:" kserver_port "/api/v1/outlets", 204, k_create_outlet_2, "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain two Devices. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/outlets", 200, "", k_outlet_list_two_elem, false); ASSERT_EQUAL_INT(ret, 0); // Update the second Outlet. Device should contain. ret = exec_request(kput_method, "http://localhost:" kserver_port "/api/v1/outlets/2", 204, k_update_outlet_2, "", false); ASSERT_EQUAL_INT(ret, 0); // Get Device 2 only. Should Contain Device two updated. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/outlets/2", 200, "", k_outlet_list_elem_2, false); ASSERT_EQUAL_INT(ret, 0); // Delete Device 2. ret = exec_request(kdelete_method, "http://localhost:" kserver_port "/api/v1/outlets/2", 204, "", "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain One Device. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/outlets", 200, "", k_outlet_list_one_elem, false); ASSERT_EQUAL_INT(ret, 0); restd_server_free(rest_server); devices_manager_free(dm); } /*--------------------------------------------------------------------------*/ TEST("Domo - Test API /api/v1/sprinklers - correct access\t") { restd_server_t *rest_server; devices_manager_t *dm; int ret; dm = devices_manager_new(); ASSERT_NOT_NULL(dm); rest_server = setup_server(dm); ASSERT_NOT_NULL(rest_server); sleep(1); // Get All Devices. Should be empty. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 200, "", k_sprinkler_list_empty, false); ASSERT_EQUAL_INT(ret, 0); // Create An Outlet. Should be empty. ret = exec_request(kpost_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 204, k_create_sprinkler_1, "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain One Device. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 200, "", k_sprinkler_list_one_elem, false); ASSERT_EQUAL_INT(ret, 0); // Create A second Outlet. Should Contain two Device. ret = exec_request(kpost_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 204, k_create_sprinkler_2, "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain two Devices. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 200, "", k_sprinkler_list_two_elem, false); ASSERT_EQUAL_INT(ret, 0); // Update the second Outlet. Device should contain. ret = exec_request(kput_method, "http://localhost:" kserver_port "/api/v1/sprinklers/2", 204, k_update_outlet_2, "", false); ASSERT_EQUAL_INT(ret, 0); // Get Device 2 only. Should Contain Device two updated. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/sprinklers/2", 200, "", k_sprinkler_list_elem_2, false); ASSERT_EQUAL_INT(ret, 0); // Delete Device 2. ret = exec_request(kdelete_method, "http://localhost:" kserver_port "/api/v1/sprinklers/2", 204, "", "", false); ASSERT_EQUAL_INT(ret, 0); // Get All Devices. Should Contain One Device. ret = exec_request(kget_method, "http://localhost:" kserver_port "/api/v1/sprinklers", 200, "", k_sprinkler_list_one_elem, false); ASSERT_EQUAL_INT(ret, 0); restd_server_free(rest_server); devices_manager_free(dm); }