Files
domo-iot/src/tests/test_devices.c
NADAL Jean-Baptiste 8e7e94568f
All checks were successful
continuous-integration/drone/push Build is passing
Test outlet is now functional
2020-02-24 16:39:30 +01:00

201 lines
6.1 KiB
C

/*!
* test_devices.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: 26/12/2019
*
*/
/*--------------------------------------------------------------------------*/
void device_create_set_state_free(const char *capability, const char *test_name_true, const char *test_name_false)
{
struct json_object *root_node;
devices_manager_t *dm;
int ret;
char *device_serialized;
dm = devices_manager_new();
ASSERT_NOT_NULL(dm);
// Create a New Entry.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_name, json_object_new_string("test_name"));
ret = devices_manager_create(dm, capability, root_node);
/* Clean the json object. */
json_object_put(root_node);
ASSERT_EQUAL_INT(ret, 0);
// Check Objets.
device_serialized = devices_manager_get_by_id(dm, capability, 1);
// printf ("device: %s\n", device_serialized);
ASSERT_EQUAL_STR(device_serialized, test_name_false);
free(device_serialized);
// Change the State.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_state, json_object_new_boolean(true));
json_object_object_add(root_node, k_entry_id, json_object_new_int(1));
ret = devices_manager_update(dm, capability, root_node);
ASSERT_EQUAL_INT(ret, 0);
/* Clean the json object. */
json_object_put(root_node);
// Check Objets.
device_serialized = devices_manager_get_by_id(dm, capability, 1);
ASSERT_EQUAL_STR(device_serialized, test_name_true);
free(device_serialized);
// Free the Object.
devices_manager_free(dm);
}
/*--------------------------------------------------------------------------*/
void device_create_three_devices_remove_second(const char *capability, const char *test_array_full, const char *test_array_removed)
{
struct json_object *root_node;
devices_manager_t *dm;
int ret;
char *device_serialized;
dm = devices_manager_new();
ASSERT_NOT_NULL(dm);
// Create a New Entry 1.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_name, json_object_new_string("test1"));
ret = devices_manager_create(dm, capability, root_node);
/* Clean the json object. */
json_object_put(root_node);
ASSERT_EQUAL_INT(ret, 0);
// Create a New Entry 2.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_name, json_object_new_string("test2"));
ret = devices_manager_create(dm, capability, root_node);
/* Clean the json object. */
json_object_put(root_node);
ASSERT_EQUAL_INT(ret, 0);
// Create a New Entry 3.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_name, json_object_new_string("test3"));
ret = devices_manager_create(dm, capability, root_node);
/* Clean the json object. */
json_object_put(root_node);
ASSERT_EQUAL_INT(ret, 0);
device_serialized = devices_manager_get(dm, capability);
// printf ("device: %s\n", device_serialized);
ASSERT_EQUAL_STR(device_serialized, test_array_full);
free(device_serialized);
// Remove ID 2.
ret = devices_manager_delete(dm, capability, 2);
ASSERT_EQUAL_INT(ret, 0);
device_serialized = devices_manager_get(dm, capability);
// printf ("device: %s\n", device_serialized);
ASSERT_EQUAL_STR(device_serialized, test_array_removed);
free(device_serialized);
// Free the Object.
devices_manager_free(dm);
}
/*--------------------------------------------------------------------------*/
void device_create_error_case(const char *capability)
{
struct json_object *root_node;
devices_manager_t *dm;
int ret;
dm = devices_manager_new();
ASSERT_NOT_NULL(dm);
// Create a New Entry.
root_node = json_object_new_object();
// dm is NULL
ret = devices_manager_create(NULL, capability, root_node);
ASSERT_EQUAL_INT(ret, -1);
// unknown capability
ret = devices_manager_create(dm, "", root_node);
ASSERT_EQUAL_INT(ret, -1);
// json node is null
ret = devices_manager_create(dm, capability, NULL);
ASSERT_EQUAL_INT(ret, -1);
// parameter is missing.
ret = devices_manager_create(dm, capability, root_node);
ASSERT_EQUAL_INT(ret, -1);
json_object_put(root_node);
// Free the Object.
devices_manager_free(dm);
}
/*--------------------------------------------------------------------------*/
void device_get_by_id_error_case(const char *capability)
{
struct json_object *root_node;
devices_manager_t *dm;
int ret;
char *device_serialized;
dm = devices_manager_new();
ASSERT_NOT_NULL(dm);
// Create a New Entry.
root_node = json_object_new_object();
json_object_object_add(root_node, k_entry_name, json_object_new_string("test_name"));
ret = devices_manager_create(dm, capability, root_node);
ASSERT_EQUAL_INT(ret, 0);
// dm is NULL
device_serialized = devices_manager_get_by_id(NULL, capability, 1);
ASSERT_NULL(device_serialized);
// unknown capability
device_serialized = devices_manager_get_by_id(dm, "", 1);
ASSERT_NULL(device_serialized);
// unknown id
device_serialized = devices_manager_get_by_id(dm, capability, 7);
ASSERT_NULL(device_serialized);
json_object_put(root_node);
// Free the Object.
devices_manager_free(dm);
}