Skip to content

Extracting property container functionality #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2020
4 changes: 3 additions & 1 deletion extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project(testArduinoIoTCloud)

include_directories(include)
include_directories(../../src/cbor)
include_directories(../../src/property)
include_directories(../../src/utility/ota)
include_directories(external/catch/v2.12.1/include)
include_directories(external/fakeit/v2.0.5/include)
Expand Down Expand Up @@ -51,8 +52,9 @@ set(TEST_DUT_SRCS
../../src/utility/ota/crc.cpp
../../src/utility/ota/OTALogic.cpp

../../src/property/Property.cpp
../../src/property/PropertyContainer.cpp
../../src/cbor/ArduinoCloudThing.cpp
../../src/cbor/ArduinoCloudProperty.cpp
../../src/cbor/lib/tinycbor/src/cborencoder.c
../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c
../../src/cbor/lib/tinycbor/src/cborerrorstrings.c
Expand Down
35 changes: 18 additions & 17 deletions extras/test/src/test_addPropertyReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@

#include <catch.hpp>

#include <ArduinoCloudThing.h>
#include <PropertyContainer.h>

#include <types/CloudInt.h>
#include <types/CloudBool.h>
#include <types/CloudFloat.h>
#include <types/CloudString.h>

/**************************************************************************************
TEST CODE
**************************************************************************************/

SCENARIO("The same arduino cloud properties are added multiple times", "[ArduinoCloudThing::addPropertyReal]") {
WHEN("The same bool property is added multiple times") {
ArduinoCloudThing thing;
thing.begin();
PropertyContainer property_container;

CloudBool bool_property = false;

ArduinoCloudProperty * bool_property_ptr_1 = &thing.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
ArduinoCloudProperty * bool_property_ptr_2 = &thing.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
Property * bool_property_ptr_1 = &property_container.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
Property * bool_property_ptr_2 = &property_container.addPropertyReal(bool_property, "bool_property", Permission::ReadWrite);
THEN("No new property is added and the first added property is returned instead of a new one") {
REQUIRE(bool_property_ptr_1 == bool_property_ptr_2);
}
Expand All @@ -31,13 +35,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
/**************************************************************************************/

WHEN("the same int property is added multiple times") {
ArduinoCloudThing thing;
thing.begin();
PropertyContainer property_container;

CloudInt int_property = 1;

ArduinoCloudProperty * int_property_ptr_1 = &thing.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
ArduinoCloudProperty * int_property_ptr_2 = &thing.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
Property * int_property_ptr_1 = &property_container.addPropertyReal(int_property, "int_property", Permission::ReadWrite);
Property * int_property_ptr_2 = &property_container.addPropertyReal(int_property, "int_property", Permission::ReadWrite);

THEN("No new property is added and the first added property is returned instead of a new one") {
REQUIRE(int_property_ptr_1 == int_property_ptr_2);
Expand All @@ -47,13 +50,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
/**************************************************************************************/

WHEN("the same float property is added multiple times") {
ArduinoCloudThing thing;
thing.begin();
PropertyContainer property_container;

CloudFloat float_property = 1.0f;

ArduinoCloudProperty * float_property_ptr_1 = &thing.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
ArduinoCloudProperty * float_property_ptr_2 = &thing.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
Property * float_property_ptr_1 = &property_container.addPropertyReal(float_property, "float_property", Permission::ReadWrite);
Property * float_property_ptr_2 = &property_container.addPropertyReal(float_property, "float_property", Permission::ReadWrite);

THEN("No new property is added and the first added property is returned instead of a new one") {
REQUIRE(float_property_ptr_1 == float_property_ptr_2);
Expand All @@ -63,13 +65,12 @@ SCENARIO("The same arduino cloud properties are added multiple times", "[Arduino
/**************************************************************************************/

WHEN("the same String property is added multiple times") {
ArduinoCloudThing thing;
thing.begin();
PropertyContainer property_container;

CloudString str_property;

ArduinoCloudProperty * str_property_ptr_1 = &thing.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
ArduinoCloudProperty * str_property_ptr_2 = &thing.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
Property * str_property_ptr_1 = &property_container.addPropertyReal(str_property, "str_property", Permission::ReadWrite);
Property * str_property_ptr_2 = &property_container.addPropertyReal(str_property, "str_property", Permission::ReadWrite);

THEN("No new property is added and the first added property is returned instead of a new one") {
REQUIRE(str_property_ptr_1 == str_property_ptr_2);
Expand Down
70 changes: 41 additions & 29 deletions extras/test/src/test_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <util/CBORTestUtil.h>
#include <ArduinoCloudThing.h>
#include <PropertyContainer.h>
#include "types/CloudWrapperBool.h"

/**************************************************************************************
Expand Down Expand Up @@ -39,11 +40,12 @@ SCENARIO("A callback is registered via 'onUpdate' to be called on property chang
/************************************************************************************/

GIVEN("CloudProtocol::V2") {
PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

CloudInt test = 10;
thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(externalCallbackV2);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(externalCallbackV2);

/* [{0: "test", 2: 7}] = 81 A2 00 64 74 65 73 74 02 07 */
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x02, 0x07};
Expand All @@ -68,11 +70,12 @@ void switch_callback() {

SCENARIO("A (boolean) property is manipulated in the callback to its origin state", "[ArduinoCloudThing::decode]") {
GIVEN("CloudProtocol::V2") {
PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);
cbor::encode(thing);

thing.addPropertyReal(switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);
property_container.addPropertyReal(switch_turned_on, "switch_turned_on", Permission::ReadWrite).onUpdate(switch_callback);

/* [{0: "switch_turned_on", 4: true}] = 81 A2 00 70 73 77 69 74 63 68 5F 74 75 72 6E 65 64 5F 6F 6E 04 F5 */
uint8_t const payload[] = {0x81, 0xA2, 0x00, 0x70, 0x73, 0x77, 0x69, 0x74, 0x63, 0x68, 0x5F, 0x74, 0x75, 0x72, 0x6E, 0x65, 0x64, 0x5F, 0x6F, 0x6E, 0x04, 0xF5};
Expand All @@ -98,7 +101,7 @@ SCENARIO("A (boolean) property is manipulated in the callback to its origin stat
static bool sync_callback_called = false;
static bool change_callback_called = false;

void auto_sync_callback(ArduinoCloudProperty& property) {
void auto_sync_callback(Property& property) {
MOST_RECENT_WINS(property);
sync_callback_called = true;
}
Expand All @@ -113,10 +116,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);

test.setLastLocalChangeTimestamp(1550138809);

Expand All @@ -140,10 +144,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
test = false;
test.setLastLocalChangeTimestamp(1550138811);

Expand All @@ -162,16 +167,17 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
SCENARIO("Primitive property: After a connection/reconnection an incoming cbor payload is processed and the synchronization callback is executed. The sync callback applies the AUTO_SYNC policy (the most recent value between the local one and the cloud one is finally assigned to the property). The onUpdate function is called if the cloud value is the most recent one. In this scenario the most updated value is the cloud one.") {
GIVEN("CloudProtocol::V2") {
bool test = true;
std::unique_ptr<ArduinoCloudProperty> p(new CloudWrapperBool(test));
std::unique_ptr<Property> p(new CloudWrapperBool(test));
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
test = false;
thing.updateTimestampOnLocallyChangedProperties();
property_container.updateTimestampOnLocallyChangedProperties();
//There is no RTC on test execution environment so we force the local timestamp
p->setLastLocalChangeTimestamp(1550138809);

Expand All @@ -192,16 +198,17 @@ SCENARIO("Primitive property: After a connection/reconnection an incoming cbor p
SCENARIO("Primitive property: After a connection/reconnection an incoming cbor payload is processed and the synchronization callback is executed. The sync callback apply the AUTO_SYNC policy (the most recent value between the local one and the cloud one is finally assigned to the property). The onUpdate function is called if the cloud value is the most recent one. In this scenario the most updated value is the local one.") {
GIVEN("CloudProtocol::V2") {
bool test = true;
std::unique_ptr<ArduinoCloudProperty> p(new CloudWrapperBool(test));
std::unique_ptr<Property> p(new CloudWrapperBool(test));
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(*p, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
test = false;
thing.updateTimestampOnLocallyChangedProperties();
property_container.updateTimestampOnLocallyChangedProperties();
//There is no RTC on test execution environment so we force the local timestamp
p->setLastLocalChangeTimestamp(1550138811);

Expand All @@ -223,10 +230,11 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
location_test.setLastLocalChangeTimestamp(1550138809);

/* [{-3: 1550138810.00, 0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 82 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 68 74 65 73 74 3A 6C 61 74 02 02 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 03*/
Expand Down Expand Up @@ -254,10 +262,11 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
property_container.addPropertyReal(location_test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(auto_sync_callback);
location_test.setLastLocalChangeTimestamp(1550138811);

/* [{-3: 1550138810.00, 0: "test:lat", 3: 2},{0: "test:lon", 3: 3}] = 82 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 68 74 65 73 74 3A 6C 61 74 02 02 A2 00 68 74 65 73 74 3A 6C 6F 6E 02 03*/
Expand All @@ -279,7 +288,7 @@ SCENARIO("Object property: After a connection/reconnection an incoming cbor payl

/**************************************************************************************/

void force_device_sync_callback(ArduinoCloudProperty& property) {
void force_device_sync_callback(Property& property) {
DEVICE_WINS(property);
sync_callback_called = true;
}
Expand All @@ -291,10 +300,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_device_sync_callback);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_device_sync_callback);

/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
Expand All @@ -311,7 +321,7 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed

/**************************************************************************************/

void force_cloud_sync_callback(ArduinoCloudProperty& property) {
void force_cloud_sync_callback(Property& property) {
CLOUD_WINS(property);
sync_callback_called = true;
}
Expand All @@ -323,10 +333,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_cloud_sync_callback);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback).onSync(force_cloud_sync_callback);

/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
Expand All @@ -349,10 +360,11 @@ SCENARIO("After a connection/reconnection an incoming cbor payload is processed.
sync_callback_called = false;
change_callback_called = false;

PropertyContainer property_container;
ArduinoCloudThing thing;
thing.begin();
thing.begin(&property_container);

thing.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback);
property_container.addPropertyReal(test, "test", Permission::ReadWrite).onUpdate(change_callback);

/* [{-3: 1550138810.00, 0: "test", 4: true}] = 81 A3 22 FB 41 D7 19 4F 6E 80 00 00 00 64 74 65 73 74 04 F5 */
uint8_t const payload[] = {0x81, 0xA3, 0x22, 0xFB, 0x41, 0xD7, 0x19, 0x4F, 0x6E, 0x80, 0x00, 0x00, 0x00, 0x64, 0x74, 0x65, 0x73, 0x74, 0x04, 0xF5};
Expand Down
Loading