1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H
16 #define OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H
17 
18 #include <cJSON.h>
19 #include <gmock/gmock.h>
20 
21 namespace OHOS::FileManagement::Backup {
22 class CJson {
23 public:
24     CJson() = default;
25     virtual ~CJson() = default;
26 
27 public:
28     virtual cJSON *cJSON_CreateArray() = 0;
29     virtual cJSON *cJSON_CreateObject() = 0;
30     virtual char *cJSON_Print(const cJSON *item) = 0;
31     virtual cJSON *cJSON_Parse(const char *value) = 0;
32     virtual cJSON *cJSON_GetObjectItem(const cJSON *const object, const char *const string) = 0;
33     virtual void cJSON_Delete(cJSON *item) = 0;
34     virtual cJSON_bool cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) = 0;
35     virtual int cJSON_GetArraySize(const cJSON *array) = 0;
36     virtual cJSON* cJSON_GetArrayItem(const cJSON* array, int index) = 0;
37     virtual void cJSON_free(void* object) = 0;
38     virtual cJSON_bool cJSON_AddItemToArray(cJSON *array, cJSON *item) = 0;
39     virtual cJSON *cJSON_AddStringToObject(cJSON *const object, const char *const name, const char *const string) = 0;
40     virtual cJSON_bool cJSON_IsArray(const cJSON * const item) = 0;
41 
42 public:
43     static inline std::shared_ptr<CJson> cJsonPtr = nullptr;
44 };
45 
46 class CJsonMock : public CJson {
47 public:
48     MOCK_METHOD3(cJSON_AddItemToObject, cJSON_bool(cJSON *object, const char *string, cJSON *item));
49     MOCK_METHOD1(cJSON_Delete, void(cJSON *item));
50     MOCK_METHOD0(cJSON_CreateObject, cJSON *());
51     MOCK_METHOD0(cJSON_CreateArray, cJSON *());
52     MOCK_METHOD1(cJSON_Print, char *(const cJSON *item));
53     MOCK_METHOD1(cJSON_Parse, cJSON *(const char *value));
54     MOCK_METHOD2(cJSON_GetObjectItem, cJSON *(const cJSON *const object, const char *const string));
55     MOCK_METHOD1(cJSON_GetArraySize, int(const cJSON *array));
56     MOCK_METHOD2(cJSON_GetArrayItem, cJSON *(const cJSON* array, int index));
57     MOCK_METHOD2(cJSON_AddItemToArray, cJSON_bool(cJSON *array, cJSON *item));
58     MOCK_METHOD3(cJSON_AddStringToObject,
59                  cJSON *(cJSON *const object, const char *const name, const char *const string));
60     MOCK_METHOD1(cJSON_free, void(void* object));
61     MOCK_METHOD1(cJSON_IsArray, cJSON_bool(const cJSON * const item));
62 };
63 } // namespace OHOS::FileManagement::Backup
64 #endif