From 9dbfa2d62925122f9b12fff1867e4f55343874f7 Mon Sep 17 00:00:00 2001 From: Sakthivel Samidurai Date: Thu, 26 Mar 2015 15:43:41 -0700 Subject: [PATCH] Update C++ Unit test for OCResource Add unit test for missing OCResource API. Change-Id: I8434a5410a774a9b95863ab3f6a19844a4a4007b Signed-off-by: Sakthivel Samidurai Reviewed-on: https://gerrit.iotivity.org/gerrit/585 Tested-by: jenkins-iotivity Reviewed-by: Doug Hudson Reviewed-by: Erich Keane --- resource/unittests/OCPlatformTest.cpp | 8 +- resource/unittests/OCResourceTest.cpp | 460 +++++++++++++++++++++++++++++++--- 2 files changed, 428 insertions(+), 40 deletions(-) diff --git a/resource/unittests/OCPlatformTest.cpp b/resource/unittests/OCPlatformTest.cpp index d85fed5..4d50427 100644 --- a/resource/unittests/OCPlatformTest.cpp +++ b/resource/unittests/OCPlatformTest.cpp @@ -106,6 +106,7 @@ namespace OCPlatformTest //Configure // Enable it when the stack throw an exception + // https://jira.iotivity.org/browse/IOT-428 TEST(ConfigureTest, DISABLED_ConfigureInvalidModeType) { PlatformConfig cfg { @@ -115,11 +116,12 @@ namespace OCPlatformTest 0, OC::QualityOfService::LowQos }; - + OCPlatform::Configure(cfg); EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL)); } // Enable it when the stack throw an exception + // https://jira.iotivity.org/browse/IOT-428 TEST(ConfigureTest, DISABLED_ConfigureInvalidServiceType) { PlatformConfig cfg { @@ -129,11 +131,12 @@ namespace OCPlatformTest 0, OC::QualityOfService::LowQos }; - + OCPlatform::Configure(cfg); EXPECT_ANY_THROW(OCPlatform::setDefaultDeviceEntityHandler(NULL)); } // Enable it when the stack throw an exception + // https://jira.iotivity.org/browse/IOT-428 TEST(ConfigureTest, DISABLED_ConfigureClientOutProc) { PlatformConfig cfg { @@ -147,7 +150,6 @@ namespace OCPlatformTest std::string type = "core.light"; uint8_t gResourceProperty = 0; OCPlatform::Configure(cfg); - EXPECT_ANY_THROW(OCPlatform::registerResource( resourceHandle, uri, type, gResourceInterface, entityHandler, gResourceProperty)); diff --git a/resource/unittests/OCResourceTest.cpp b/resource/unittests/OCResourceTest.cpp index be804f4..c025df0 100644 --- a/resource/unittests/OCResourceTest.cpp +++ b/resource/unittests/OCResourceTest.cpp @@ -22,24 +22,28 @@ #include #include #include +#include +#include namespace OCResourceTest { using namespace OC; // Callbacks - - void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, - const int& eCode, const int& sequenceNumber) + void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&) { } - void onGetPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) + void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode) { + EXPECT_EQ(eCode, OC_STACK_OK); } - void foundResource(std::shared_ptr resource) + void foundResource(std::shared_ptr ) { + } + void deleteResponse(const HeaderOptions&, const int) + { } //Helper method @@ -53,7 +57,7 @@ namespace OCResourceTest connectivityType, false, types, ifaces); } - //ConstructResourceTest + //Get Test TEST(ConstructResourceTest, ConstructResourceObject) { EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string(""))); @@ -62,58 +66,440 @@ namespace OCResourceTest TEST(ResourceGetTest, ResourceGetForValidUri) { OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); - if(resource) - { - QueryParamsMap test; - EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut)); - } + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut)); + } TEST(ResourceGetTest, ResourceGetForBadUri) { OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000"); - if(resource) - { - QueryParamsMap test; - EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException); - } + EXPECT_TRUE(resource != NULL); + EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException); } - TEST(ResourcePutTest, ResourcePutForValid) + // Enable it when the CON is supported in the stack + TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos)); + } + + TEST(ResourceGetTest, ResourceGetWithLowQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos)); + } + + TEST(ResourceGetTest, ResourceGetWithMidQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos)); + } + + TEST(ResourceGetTest, ResourceGetWithNaQos) { OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); - if(resource) - { - QueryParamsMap test; - OCRepresentation rep; - EXPECT_EQ(OC_STACK_OK, resource->put(rep, test, &onGetPut)); - } + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos)); } + TEST(ResourceGetTest, ResourceGetWithRTIFNaQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut, + QualityOfService::NaQos)); + } + + TEST(ResourceGetTest, ResourceGetWithNullResourceType) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_ANY_THROW( + resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut, + QualityOfService::NaQos)); + } + + + TEST(ResourceGetTest, ResourceGetWithNullResourceInterface) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_ANY_THROW( + resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos)); + } + + TEST(ResourceGetTest, ResourceGetWithTypeAndInterface) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut)); + } + + //Post Test TEST(ResourcePostTest, ResourcePostValidConfiguration) { - PlatformConfig cfg; - OCPlatform::Configure(cfg); + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut)); + } + + TEST(ResourcePostTest, ResourcePostWithNaQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos)); + } + + TEST(ResourcePostTest, ResourcePostWithMidQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos)); + } + + TEST(ResourcePostTest, ResourcePostWithLowQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos)); + } + + // Enable it when the CON is supported in the stack + TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos)); + } + + //Put Test + TEST(ResourcePutTest, ResourcePutForValid) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut)); + } + TEST(ResourcePutTest, ResourcePutWithNaQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); - if(resource) - { - OCRepresentation rep; - QueryParamsMap test; - EXPECT_EQ(OC_STACK_OK, resource->post(rep, test, &onGetPut)); - } + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos)); } + TEST(ResourcePutTest, ResourcePutWithLowQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos)); + } + + TEST(ResourcePutTest, ResourcePutWithMidQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos)); + } + + // Enable it when the CON is supported in the stack + TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos) + { + OCRepresentation rep; + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos)); + } + + TEST(ResourcePutTest, ResourcePutWithTypeAndInterface) + { + OCRepresentation rep; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut)); + } + + TEST(ResourcePutTest, ResourcePutWithNullType) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + OCRepresentation rep; + EXPECT_ANY_THROW( + resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut)); + HeaderOptions headerOptions = {}; + onGetPut(headerOptions, rep, OC_STACK_OK); + } + + TEST(ResourcePutTest, ResourcePutWithNullInterface) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + OCRepresentation rep; + EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut)); + } + + // Observer Test TEST(ResourceObserveTest, ResourceObserveValidUri) { + OCResource::Ptr resource = + ConstructResourceObject("coap://192.168.1.2:5000", "/Observe"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve)); + } + + TEST(ResourceObserveTest, ResourceObserveLoQos) + { + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::LowQos)); + } + + TEST(ResourceObserveTest, ResourceObserveNaQos) + { + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::NaQos)); + } + + TEST(ResourceObserveTest, ResourceObserveMidQos) + { + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::MidQos)); + } + + //Enable it when CON is supported in the stack. + TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos) + { + QueryParamsMap query = {}; + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::HighQos)); + } + + TEST(ResourceObserveTest, ResourceCancelObserveValidUri) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve)); + EXPECT_EQ(OC_STACK_OK, resource->cancelObserve()); + } + + TEST(ResourceObserveTest, ResourceCancelObserveWithNaQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::NaQos)); + EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos)); + } + + TEST(ResourceObserveTest, ResourceCancelObserveWithLowQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::LowQos)); + EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos)); + } + + TEST(ResourceObserveTest, ResourceCancelObserveWithMidQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::MidQos)); + EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos)); + } + + // We enable it when the CON is supported in the stack. + TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + QueryParamsMap query = {}; + EXPECT_EQ(OC_STACK_OK, + resource->observe(ObserveType::ObserveAll, query, &onObserve, + QualityOfService::HighQos)); + EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos)); + } + + //DeleteResource + TEST(DeleteResourceTest, DeleteResourceValidUri) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse)); + } + + TEST(DeleteResourceTest, DeleteResourceWithLowQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->deleteResource(&deleteResponse, QualityOfService::LowQos)); + } + + TEST(DeleteResourceTest, DeleteResourceWithMidQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->deleteResource(&deleteResponse, QualityOfService::MidQos)); + } + + TEST(DeleteResourceTest, DeleteResourceWithHighQos) + { OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); - if(resource) - { - QueryParamsMap test; - OCRepresentation rep; - EXPECT_EQ(OC_STACK_OK,resource->observe(ObserveType::ObserveAll, test, &onObserve)); - } + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, + resource->deleteResource(&deleteResponse, QualityOfService::HighQos)); + } + TEST(DeleteResourceTest, DeleteResourceWithNaQos) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos)); + } + + //GetResourceInterfaces Test + TEST(GetResourceInterfacesTest, GetResourceInterfaces) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE); + } + + //GetResourceTypes Test + TEST(GetResourceTypesTest, GetResourceTypes) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost"); } + // Host Test + TEST(HostTest, Host) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000"); + } + + //Uri Test + TEST(UriTest, Uri) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->uri() == "/resource"); + } + + //ConnectivityType Test + TEST(ConnectivityTypeTest, ConnectivityType) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->connectivityType() == OC_WIFI); + } + + //IsObservable Test + TEST(IsObservableTest, isObservable) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_FALSE(resource->isObservable()); + } + + //SID Test + TEST(SidTest, sid) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_TRUE(resource->sid() == "0"); + } + + //UniqueIdentifier Test + TEST(UniqueIdentifierTest, uniqueIdentifier) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + std::ostringstream ss; + ss << resource->uniqueIdentifier(); + EXPECT_TRUE(ss.str() == "0/resource"); + } + + // HeaderOptions Test + TEST(HeaderOptionsTest, SetHeaderOptionsValidInput) + { + const uint16_t API_VERSION = 2048; + const uint16_t TOKEN = 3000; + HeaderOptions headerOptions; + HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0"); + HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf"); + headerOptions.push_back(apiVersion); + headerOptions.push_back(clientToken); + + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions)); + EXPECT_NO_THROW(resource->unsetHeaderOptions()); + } + + TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput) + { + OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource"); + EXPECT_TRUE(resource != NULL); + HeaderOptions headerOptions = {}; + EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions)); + EXPECT_NO_THROW(resource->unsetHeaderOptions()); + } } -- 2.7.4