Add test for shutter objects.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -84,7 +84,7 @@ void devices_manager_free(devices_manager_t *devices_manager)
|
||||
i = 0;
|
||||
while (qlist_getnext(devices_manager->outlets, &obj, true))
|
||||
{
|
||||
sprinkler_free(obj.data);
|
||||
outlet_dio_free(obj.data);
|
||||
i++;
|
||||
}
|
||||
qlist_free(devices_manager->outlets);
|
||||
@@ -94,7 +94,7 @@ void devices_manager_free(devices_manager_t *devices_manager)
|
||||
i = 0;
|
||||
while (qlist_getnext(devices_manager->shutters, &obj, true))
|
||||
{
|
||||
sprinkler_free(obj.data);
|
||||
shutter_free(obj.data);
|
||||
i++;
|
||||
}
|
||||
qlist_free(devices_manager->shutters);
|
||||
@@ -206,10 +206,31 @@ char *devices_manager_get_by_id(devices_manager_t *dm, const char *capability, u
|
||||
|
||||
if (capability == kOutletEntry)
|
||||
{
|
||||
|
||||
outlet_dio_t *outlet_dio;
|
||||
outlet_dio = get_outlet_by_id(dm, id);
|
||||
if (outlet_dio != NULL)
|
||||
{
|
||||
json_object *output_node = outlet_dio_to_json_object(outlet_dio);
|
||||
if (output_node != NULL)
|
||||
{
|
||||
output = strdup(json_object_to_json_string(output_node));
|
||||
json_object_put(output_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (capability == kShutterEntry)
|
||||
{
|
||||
shutter_t *shutter;
|
||||
shutter = get_shutter_by_id(dm, id);
|
||||
if (shutter != NULL)
|
||||
{
|
||||
json_object *output_node = shutter_to_json_object(shutter);
|
||||
if (output_node != NULL)
|
||||
{
|
||||
output = strdup(json_object_to_json_string(output_node));
|
||||
json_object_put(output_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (capability == kSprinklerEntry)
|
||||
{
|
||||
@@ -221,7 +242,6 @@ char *devices_manager_get_by_id(devices_manager_t *dm, const char *capability, u
|
||||
if (output_node != NULL)
|
||||
{
|
||||
output = strdup(json_object_to_json_string(output_node));
|
||||
|
||||
json_object_put(output_node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +107,7 @@ int create_outlet(devices_manager_t *dm, struct json_object *node)
|
||||
|
||||
device_set_id(outlet->device, ++dm->outlets_max_id);
|
||||
qlist_addlast(dm->outlets, outlet, sizeof(outlet_dio_t));
|
||||
free(outlet); // duplicated by qlist
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -217,6 +218,7 @@ int create_shutter(devices_manager_t *dm, struct json_object *node)
|
||||
|
||||
device_set_id(shutter->outlet_dio->device, ++dm->shutters_max_id);
|
||||
qlist_addlast(dm->shutters, shutter, sizeof(shutter_t));
|
||||
free(shutter); // duplicated by qlist
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -225,16 +227,45 @@ int create_shutter(devices_manager_t *dm, struct json_object *node)
|
||||
|
||||
int remove_shutter(devices_manager_t *dm, uint16_t id)
|
||||
{
|
||||
#warning "TODO"
|
||||
return 0;
|
||||
qlist_obj_t obj;
|
||||
shutter_t *shutter;
|
||||
int position = 0;
|
||||
|
||||
memset((void *)&obj, 0, sizeof(obj));
|
||||
|
||||
while (qlist_getnext(dm->shutters, &obj, true))
|
||||
{
|
||||
shutter = obj.data;
|
||||
if (shutter->outlet_dio->device->id == id)
|
||||
{
|
||||
qlist_removeat(dm->shutters, position);
|
||||
return 0;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int update_shutter(devices_manager_t *dm, uint16_t id, struct json_object *node)
|
||||
{
|
||||
#warning "TODO"
|
||||
return 0;
|
||||
qlist_obj_t obj;
|
||||
shutter_t *shutter;
|
||||
|
||||
memset((void *)&obj, 0, sizeof(obj));
|
||||
|
||||
while (qlist_getnext(dm->shutters, &obj, false))
|
||||
{
|
||||
shutter = obj.data;
|
||||
if (shutter->outlet_dio->device->id == id)
|
||||
{
|
||||
return shutter_from_json(shutter, node);
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* test_sprinkler.c
|
||||
* test_devices.c
|
||||
*
|
||||
* Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved.
|
||||
*
|
||||
@@ -26,17 +26,14 @@
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
#define kDeviceTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false }"
|
||||
#define kDeviceTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true }"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
void device_create_set_state_remove(const char *capability)
|
||||
void device_create_set_state_remove(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 *sprinkler_serialized;
|
||||
char *device_serialized;
|
||||
dm = devices_manager_new();
|
||||
ASSERT_NOT_NULL(dm);
|
||||
|
||||
@@ -50,9 +47,9 @@ void device_create_set_state_remove(const char *capability)
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
// Check Objets.
|
||||
sprinkler_serialized = devices_manager_get_by_id(dm, capability, 1);
|
||||
ASSERT_EQUAL_STR(sprinkler_serialized, kDeviceTestNameFalse);
|
||||
free(sprinkler_serialized);
|
||||
device_serialized = devices_manager_get_by_id(dm, capability, 1);
|
||||
ASSERT_EQUAL_STR(device_serialized, test_name_false);
|
||||
free(device_serialized);
|
||||
|
||||
// Change the State.
|
||||
root_node = json_object_new_object();
|
||||
@@ -65,9 +62,9 @@ void device_create_set_state_remove(const char *capability)
|
||||
json_object_put(root_node);
|
||||
|
||||
// Check Objets.
|
||||
sprinkler_serialized = devices_manager_get_by_id(dm, capability, 1);
|
||||
ASSERT_EQUAL_STR(sprinkler_serialized, kDeviceTestNameTrue);
|
||||
free(sprinkler_serialized);
|
||||
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);
|
||||
@@ -115,7 +112,7 @@ void device_get_by_id_error_case(const char *capability)
|
||||
struct json_object *root_node;
|
||||
devices_manager_t *dm;
|
||||
int ret;
|
||||
char *sprinkler_serialized;
|
||||
char *device_serialized;
|
||||
dm = devices_manager_new();
|
||||
ASSERT_NOT_NULL(dm);
|
||||
|
||||
@@ -127,16 +124,16 @@ void device_get_by_id_error_case(const char *capability)
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
// dm is NULL
|
||||
sprinkler_serialized = devices_manager_get_by_id(NULL, capability, 1);
|
||||
ASSERT_NULL(sprinkler_serialized);
|
||||
device_serialized = devices_manager_get_by_id(NULL, capability, 1);
|
||||
ASSERT_NULL(device_serialized);
|
||||
|
||||
// unknown capability
|
||||
sprinkler_serialized = devices_manager_get_by_id(dm, "", 1);
|
||||
ASSERT_NULL(sprinkler_serialized);
|
||||
device_serialized = devices_manager_get_by_id(dm, "", 1);
|
||||
ASSERT_NULL(device_serialized);
|
||||
|
||||
// unknown id
|
||||
sprinkler_serialized = devices_manager_get_by_id(dm, capability, 7);
|
||||
ASSERT_NULL(sprinkler_serialized);
|
||||
device_serialized = devices_manager_get_by_id(dm, capability, 7);
|
||||
ASSERT_NULL(device_serialized);
|
||||
|
||||
json_object_put(root_node);
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ TEST("devices_manager create and free")
|
||||
|
||||
#include "test_devices.c"
|
||||
#include "test_sprinkler.c"
|
||||
#include "test_shutter.c"
|
||||
//#include "test_outlet.c"
|
||||
|
||||
#if 0
|
||||
devices_manager_t
|
||||
|
||||
55
src/tests/test_outlet.c
Normal file
55
src/tests/test_outlet.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*!
|
||||
* test_outlet.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: 08/01/2020
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Outlet - create set_state remove.")
|
||||
{
|
||||
device_create_set_state_remove(kOutletEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Outlet - create - Error case")
|
||||
{
|
||||
device_create_error_case(kOutletEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Outlet - get_by_id - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kOutletEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Outlet - update - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kOutletEntry);
|
||||
}
|
||||
59
src/tests/test_shutter.c
Normal file
59
src/tests/test_shutter.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/*!
|
||||
* test_shutter.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: 08/01/2020
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
|
||||
#define kShutterTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }"
|
||||
#define kShutterTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false, \"zone\": \"\", \"sender\": 0, \"switch\": 0, \"speed_up\": 0, \"speed_down\": 0 }"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Shutter - create set_state remove.")
|
||||
{
|
||||
device_create_set_state_remove(kShutterEntry, kShutterTestNameTrue, kShutterTestNameFalse);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Shutter - create - Error case")
|
||||
{
|
||||
device_create_error_case(kShutterEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Shutter - get_by_id - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kShutterEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Shutter - update - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kShutterEntry);
|
||||
}
|
||||
@@ -19,37 +19,41 @@
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* @Author: NADAL Jean-Baptiste
|
||||
* @Date: 26/12/2019
|
||||
* @Date: 07/01/2020
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
|
||||
|
||||
#define kSprinklerTestNameTrue "{ \"id\": 1, \"name\": \"test_name\", \"state\": true }"
|
||||
#define kSprinklerTestNameFalse "{ \"id\": 1, \"name\": \"test_name\", \"state\": false }"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("sprinkler - create set_state remove.")
|
||||
TEST("Sprinkler - create set_state remove.")
|
||||
{
|
||||
device_create_set_state_remove(kSprinklerEntry);
|
||||
device_create_set_state_remove(kSprinklerEntry, kSprinklerTestNameTrue, kSprinklerTestNameFalse);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("sprinkler - create - Error case")
|
||||
TEST("Sprinkler - create - Error case")
|
||||
{
|
||||
device_create_error_case(kSprinklerEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("sprinkler - get_by_id - Error case")
|
||||
TEST("Sprinkler - get_by_id - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kSprinklerEntry);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("sprinkler - update - Error case")
|
||||
TEST("Sprinkler - update - Error case")
|
||||
{
|
||||
device_get_by_id_error_case(kSprinklerEntry);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user