[Unit Tests]:Added unit tests for oicgroup.c 59/236359/1
authorSourav Bhuwalka <s.bhuwalka@samsung.com>
Fri, 1 May 2020 00:40:53 +0000 (06:10 +0530)
committerSudipto <sudipto.bal@samsung.com>
Tue, 16 Jun 2020 10:53:15 +0000 (16:23 +0530)
Added the unit tests for oicgroup.c file

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/695
(cherry-picked from eb626ad01918d9fea35df2f322e5186b126c090d)

Change-Id: I87af29913e9d9277af9eb274378b9b1350e04d99
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
resource/csdk/stack/test/SConscript
resource/csdk/stack/test/oicgroup_test.cpp [new file with mode: 0644]
tools/generate_report.sh

index 309dfee..7468294 100644 (file)
@@ -69,6 +69,7 @@ else:
 stacktests = stacktest_env.Program('stacktests', ['stacktests.cpp'])
 cbortests = stacktest_env.Program('cbortests', ['cbortests.cpp'])
 keepalivetests = stacktest_env.Program('keepalivetests', ['oickeepalive_test.cpp'])
+oicgrouptests = stacktest_env.Program('oicgrouptests', ['oicgroup_test.cpp'])
 
 Alias("test", [stacktests, cbortests, keepalivetests])
 
@@ -85,3 +86,6 @@ if stacktest_env.get('TEST') == '1':
                 run_test(stacktest_env,
                          'resource_csdk_stack_keepalivetests.memcheck',
                          'resource/csdk/stack/test/keepalivetests')
+                run_test(stacktest_env,
+                         'resource_csdk_stack_oicgrouptests.memcheck',
+                         'resource/csdk/stack/test/oicgrouptests')
diff --git a/resource/csdk/stack/test/oicgroup_test.cpp b/resource/csdk/stack/test/oicgroup_test.cpp
new file mode 100644 (file)
index 0000000..e62f57e
--- /dev/null
@@ -0,0 +1,189 @@
+//******************************************************************
+//
+// Copyright 2020 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#define _POSIX_C_SOURCE 200112L
+
+#include "iotivity_config.h"
+
+#include <string.h>
+#include <gtest/gtest.h>
+
+#include "oicgroup.h"
+#include "ocpayload.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+#include "occollection.h"
+#include "ocserverrequest.h"
+#include <stdio.h>
+
+TEST(InitializeScheduleResourceListTest, InitializeScheduleResourceList)
+{
+    EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER));
+}
+TEST(TerminateScheduleResourceListTest, TerminateScheduleResourceList)
+{
+    EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER));
+    EXPECT_EQ(OC_STACK_OK, OCStop());
+}
+static OCActionSet *createAction(char *actionName)
+{
+    OCActionSet *actionSet;
+    actionSet = (OCActionSet*)OICMalloc(sizeof(OCActionSet));
+    actionSet->timesteps = 5;
+    actionSet->type = 1;
+    actionSet->actionsetName = (char*)OICMalloc(strlen(actionName)+1);
+    strncpy(actionSet->actionsetName, actionName, strlen(actionName)+1);
+    OCAction *action = (OCAction*)OICMalloc(sizeof(OCAction));
+    action->resourceUri = (char*)OICMalloc(20);
+    strncpy(action->resourceUri , "/a/light" , 9);
+    action->next = NULL;
+    OCCapability *cap=(OCCapability*)OICMalloc(sizeof(OCCapability));
+    cap->capability = (char*)OICMalloc(7);
+    strncpy(cap->capability, "switch", 7);
+    cap->status = (char*)OICMalloc(3);
+    strncpy(cap->status, "on", 3);
+    cap->next = NULL;
+    action->head = cap;
+    actionSet->head = action;
+    actionSet->next = NULL;
+    return actionSet;
+}
+TEST(BuildActionSetFromStringTest, BuildActionSetFromString)
+{
+    char temp[1024] = {0};
+    strncat(temp, "house*",6);
+    strncat(temp, "5 1", 3);
+    strncat(temp, "*uri=/a/light|switch=on*uri=/a/fan|switch=off", 45);
+    char *val = OICStrdup(temp);
+    OCActionSet *set = NULL;
+    EXPECT_EQ(OC_STACK_OK, BuildActionSetFromString(&set, val));
+}
+TEST(BuildStringFromActionSetTest, BuildStringFromActionSet)
+{
+    OCActionSet *actionSet = createAction("house");
+    char *s;
+    EXPECT_EQ(OC_STACK_OK, BuildStringFromActionSet(actionSet, &s));
+}
+TEST(FindAndDeleteActionSetTest, FindAndDeleteActionSet)
+{
+    OCResource *res;
+    res = (OCResource*)OICMalloc(sizeof(OCResource));
+    res->next = NULL;
+    OCActionSet *action = createAction("house");
+    res->actionsetHead = action;
+    char *actionsetFindName = (char*)OICMalloc(6);
+    strncpy(actionsetFindName, "house", 6);
+    EXPECT_EQ(OC_STACK_OK, FindAndDeleteActionSet(&res, actionsetFindName));
+}
+TEST(FindAndDeleteActionSetTestWithMultipleActions, FindAndDeleteActionSet)
+{
+    OCResource *res;
+    res = (OCResource*)OICMalloc(sizeof(OCResource));
+    res->next = NULL;
+    OCActionSet *action1 = createAction("house");
+    OCActionSet *action2 = createAction("kitchen");
+    action1->next = action2;
+    res->actionsetHead = action1;
+    char *actionsetFindName = (char*)OICMalloc(8);
+    strncpy(actionsetFindName, "kitchen", 8);
+    EXPECT_EQ(OC_STACK_OK, FindAndDeleteActionSet(&res, actionsetFindName));
+}
+TEST(AddActionSetTest, AddActionSet)
+{
+    OCActionSet *head = createAction("house");
+    OCActionSet *action = createAction("kitchen");
+    OCServerRequest *request;
+    EXPECT_EQ(OC_STACK_OK, AddActionSet(&head, action));
+}
+TEST(BuildCollectionGroupActionCBORResponsePutWithSameActionTest, BuildCollectionGroupActionCBORResponse)
+{
+    OCResource *res = (OCResource*)OICMalloc(sizeof(OCResource));
+    OCEntityHandlerRequest *request = (OCEntityHandlerRequest*)OICMalloc(sizeof(OCEntityHandlerRequest));
+    OCRepPayload *RepPayload = OCRepPayloadCreate();
+    char temp[1024] = {0};
+    strncat(temp, "house*",6);
+    strncat(temp, "5 1", 3);
+    strncat(temp, "*uri=/a/light|switch=on*uri=/a/fan|switch=off", 45);
+    char *val = OICStrdup(temp);
+    OCRepPayloadSetPropString(RepPayload, "ActionSet", temp);
+    request->payload = (OCPayload*)RepPayload;
+    res->actionsetHead = createAction("house");
+    EXPECT_EQ(OC_STACK_ERROR, BuildCollectionGroupActionCBORResponse(OC_REST_PUT, res, request));
+}
+TEST(BuildCollectionGroupActionCBORResponseDeleteTest, BuildCollectionGroupActionCBORResponse)
+{
+    OCResource *res = (OCResource*)OICMalloc(sizeof(OCResource));
+    OCEntityHandlerRequest *request = (OCEntityHandlerRequest*)OICMalloc(sizeof(OCEntityHandlerRequest));
+    OCRepPayload *RepPayload = OCRepPayloadCreate();
+    char temp[1024] = {0};
+    strncat(temp, "house", 5);
+    char *val = OICStrdup(temp);
+    OCRepPayloadSetPropString(RepPayload, "DelActionSet", temp);
+    request->payload = (OCPayload*)RepPayload;
+    res->actionsetHead = createAction("house");
+    EXPECT_EQ(OC_STACK_ERROR, BuildCollectionGroupActionCBORResponse(OC_REST_PUT, res, request));
+}
+TEST(BuildCollectionGroupActionCBORResponsePOSTTest, BuildCollectionGroupActionCBORResponse)
+{
+    OCResource *res = (OCResource*)OICMalloc(sizeof(OCResource));
+    OCEntityHandlerRequest *request = (OCEntityHandlerRequest*)OICMalloc(sizeof(OCEntityHandlerRequest));
+    OCRepPayload *RepPayload = OCRepPayloadCreate();
+    char temp[1024] = {0};
+    strncat(temp, "house*1", 7);
+    char *val = OICStrdup(temp);
+    OCRepPayloadSetPropString(RepPayload, "DoScheduledAction", temp);
+    request->payload = (OCPayload*)RepPayload;
+    res->actionsetHead = createAction("house");
+    res->uri = (char*)OICMalloc(9);
+    strncpy(res->uri , "/a/light" , 9);
+    EXPECT_EQ(OC_STACK_ERROR, BuildCollectionGroupActionCBORResponse(OC_REST_POST, res, request));
+}
+TEST(BuildCollectionGroupActionCBORResponseCancelActionTest, BuildCollectionGroupActionCBORResponse)
+{
+    OCResource *res = (OCResource*)OICMalloc(sizeof(OCResource));
+    OCEntityHandlerRequest *request = (OCEntityHandlerRequest*)OICMalloc(sizeof(OCEntityHandlerRequest));
+    OCRepPayload *RepPayload = OCRepPayloadCreate();
+    char temp[1024] = {0};
+    strncat(temp, "house", 5);
+    char *val = OICStrdup(temp);
+    OCRepPayloadSetPropString(RepPayload, "CancelAction", temp);
+    request->payload = (OCPayload*)RepPayload;
+    res->actionsetHead = createAction("house");
+    res->uri = (char*)OICMalloc(9);
+    strncpy(res->uri , "/a/light" , 9);
+    EXPECT_EQ(OC_STACK_ERROR, BuildCollectionGroupActionCBORResponse(OC_REST_POST, res, request));
+}
+TEST(BuildCollectionGroupActionCBORResponseGetActionTest, BuildCollectionGroupActionCBORResponse)
+{
+    OCResource *res = (OCResource*)OICMalloc(sizeof(OCResource));
+    OCEntityHandlerRequest *request = (OCEntityHandlerRequest*)OICMalloc(sizeof(OCEntityHandlerRequest));
+    OCRepPayload *RepPayload = OCRepPayloadCreate();
+    char temp[1024] = {0};
+    strncat(temp, "house", 5);
+    char *val = OICStrdup(temp);
+    OCRepPayloadSetPropString(RepPayload, "GetActionSet", temp);
+    request->payload = (OCPayload*)RepPayload;
+    res->actionsetHead = createAction("house");
+    res->uri = (char*)OICMalloc(9);
+    strncpy(res->uri , "/a/light" , 9);
+    EXPECT_EQ(OC_STACK_ERROR, BuildCollectionGroupActionCBORResponse(OC_REST_POST, res, request));
+}
+
+
index fcdc92a..587f328 100644 (file)
@@ -202,6 +202,7 @@ generate_report_CA()
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/csdk/connectivity/test/catests"
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/csdk/stack/test/cbortests"
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/csdk/stack/test/keepalivetests"
+                "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/csdk/stack/test/oicgrouptests"
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/unittests/unittests"
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/c_common/ocrandom/test/randomtests"
                 "${IOTIVITY_BASE}/out/${IOTIVITY_TARGET_OS}/${IOTIVITY_TARGET_ARCH}/debug/resource/c_common/oic_malloc/test/malloctests"