domo update rest server to new API
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-21 11:37:32 +01:00
parent db0c855300
commit a5521f05f1
8 changed files with 209 additions and 119 deletions

5
src/tests/devices.json Normal file
View File

@@ -0,0 +1,5 @@
{
"outlets": [],
"shutters": [],
"sprinklers": []
}

75
src/tests/test_domo.c Normal file
View File

@@ -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);
}

View File

@@ -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 <libgen.h>
#include <unistd.h>
#include <event2/event.h>
#include <qlibc/qlibc.h>
#include <qlibc/extensions/qhttpclient.h>
#include <json.h>
#include <curl/curl.h>
@@ -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();

View File

@@ -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);
}
free(config_path);
}