TEST(PDMSetDeviceStaleTest, StaleDeviceNotinLinkedDevice)
{
    EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_11, sizeof(uid1.id));

    OicUuid_t uid2 = {{0,}};
    memcpy(&uid2.id, ID_12, sizeof(uid2.id));

    OicUuid_t uid3 = {{0,}};
    memcpy(&uid3.id, ID_13, sizeof(uid3.id));

    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid3));

    EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
    EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid2, &uid3));
    EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid3));

    EXPECT_EQ(OC_STACK_OK,PDMSetDeviceStale(&uid1));

    OCUuidList_t *list1 = NULL;
    size_t noOfDevices1 = 0;
    EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMGetLinkedDevices(&uid1, &list1, &noOfDevices1));

    OCUuidList_t *list2 = NULL;
    size_t noOfDevices2 = 0;
    EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid2, &list2, &noOfDevices2));
    OCUuidList_t *ptr = list2;
    while(ptr)
    {
        EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
        ptr = ptr->next;
    }
    ptr = list2;
    while(ptr)
    {
        EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid3.id,sizeof(uid3.id)));
        ptr = ptr->next;
    }

    OCUuidList_t *list3 = NULL;
    size_t noOfDevices3 = 0;
    EXPECT_EQ(OC_STACK_OK, PDMGetLinkedDevices(&uid3, &list3, &noOfDevices3));
    ptr = list3;
    while(ptr)
    {
        EXPECT_FALSE(0 == memcmp(ptr->dev.id, uid1.id,sizeof(uid1.id)));
        ptr = ptr->next;
    }

    ptr = list3;
    while(ptr)
    {
        EXPECT_TRUE(0 == memcmp(ptr->dev.id, uid2.id,sizeof(uid2.id)));
        ptr = ptr->next;
    }
}
TEST(PDMLinkDevicesTest, ValidCase)
{
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_4, sizeof(uid1.id));
    PDMAddDevice(&uid1);
    OicUuid_t uid2 = {{0,}};
    memcpy(&uid2.id, ID_5, sizeof(uid2.id));
    PDMAddDevice(&uid2);
    EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));
}
TEST(PDMIsLinkExistsTest, DuplicateID)
{
    EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_7, sizeof(uid1.id));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
    OicUuid_t uid2 = {{0,}};
    memcpy(&uid2.id, ID_8, sizeof(uid2.id));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid2));

    bool linkExisits = true;
    OCStackResult res = PDMIsLinkExists(&uid1, &uid2, &linkExisits);
    EXPECT_EQ(OC_STACK_OK, res);
    EXPECT_FALSE(linkExisits);
}
/**
 * Callback handler of SRPFinalizeProvisioning.
 *
 * @param[in] ctx             ctx value passed to callback from calling function.
 * @param[in] UNUSED          handle to an invocation
 * @param[in] clientResponse  Response from queries to remote servers.
 * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
 *          and OC_STACK_KEEP_TRANSACTION to keep it.
 */
static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle UNUSED,
                                                       OCClientResponse *clientResponse)
{
    OC_LOG_V(INFO, TAG, "IN FinalizeProvisioningCB.");

    VERIFY_NON_NULL(TAG, clientResponse, ERROR);
    VERIFY_NON_NULL(TAG, ctx, ERROR);

    OTMContext_t* otmCtx = (OTMContext_t*)ctx;
    (void)UNUSED;
    if(OC_STACK_OK == clientResponse->result)
    {
        OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);

         if (OC_STACK_OK == res)
         {
                OC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
                SetResult(otmCtx, OC_STACK_OK);
                return OC_STACK_DELETE_TRANSACTION;
         }
         else
         {
              OC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
         }
    }
exit:
    return OC_STACK_DELETE_TRANSACTION;
}
TEST(PDMIsDuplicateDeviceTest, ValidUUID)
{
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_1, sizeof(uid1.id));
    PDMAddDevice(&uid1);
    OicUuid_t uid2 = {{0,}};
    memcpy(&uid2.id, ID_2, sizeof(uid2.id));
    PDMAddDevice(&uid2);
    OicUuid_t uid3 = {{0,}};
    memcpy(&uid3.id, ID_3, sizeof(uid3.id));
    bool isDuplicate = true;
    EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid1,&isDuplicate));
    EXPECT_TRUE(isDuplicate);

    EXPECT_EQ(OC_STACK_OK, PDMIsDuplicateDevice(&uid3, &isDuplicate));
    EXPECT_FALSE(isDuplicate);
}
TEST(PDMSetDeviceStaleTest, VALIDUUID)
{
    EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_9, sizeof(uid1.id));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
    EXPECT_EQ(OC_STACK_OK,PDMSetDeviceStale(&uid1));
}
TEST(CallPDMAPIbeforeInit, BeforeInit)
{
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMAddDevice(NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsDuplicateDevice(NULL,NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMLinkDevices(NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMUnlinkDevices(NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMDeleteDevice(NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetOwnedDevices(NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetLinkedDevices(NULL, NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMSetLinkStale(NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMGetToBeUnlinkedDevices(NULL, NULL));
    EXPECT_EQ(OC_STACK_PDM_IS_NOT_INITIALIZED, PDMIsLinkExists(NULL, NULL, NULL));
}
TEST(PDMSetLinkStale, ValidCase)
{
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_6, sizeof(uid1.id));
    OicUuid_t uid2 = {{0,}};
    memcpy(&uid2.id, ID_1, sizeof(uid2.id));

    EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMSetLinkStale(&uid1, &uid2));

    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));

    EXPECT_EQ(OC_STACK_OK, PDMLinkDevices(&uid1, &uid2));

    EXPECT_EQ(OC_STACK_OK, PDMSetLinkStale(&uid1, &uid2));
}
TEST(PDMAddDeviceTest, ValidUUID)
{
    OicUuid_t uid = {{0,}};

    uint8_t id[UUID_LENGTH] = {0,};
    for (size_t i = 0 ; i < sizeof(id) ; i++)
    {
        int tem = rand() % 25;

        id[i] = tem + 65;
    }

    memcpy(&uid.id, &id, UUID_LENGTH);

    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid));
    PDMDeleteDevice(&uid);
}
TEST(PDMSetDeviceStaleTest, StaleDeviceNotinDeviceList)
{
    EXPECT_EQ(OC_STACK_OK, PDMInit(NULL));
    OicUuid_t uid1 = {{0,}};
    memcpy(&uid1.id, ID_10, sizeof(uid1.id));
    EXPECT_EQ(OC_STACK_OK, PDMAddDevice(&uid1));
    EXPECT_EQ(OC_STACK_OK,PDMSetDeviceStale(&uid1));

    OCUuidList_t *list = NULL;
    size_t noOfDevcies = 0;
    EXPECT_EQ(OC_STACK_OK, PDMGetOwnedDevices(&list, &noOfDevcies));

    while (list)
    {
        EXPECT_FALSE(0 == memcmp(list->dev.id, uid1.id,sizeof(uid1.id)));
        list = list->next;
    }
}
TEST(PDMAddDeviceTest, NullUUID)
{
    EXPECT_EQ(OC_STACK_INVALID_PARAM, PDMAddDevice(NULL));
}