1 /* 2 * Copyright (c) 2025 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 16 #ifndef SOFTBUS_JSON_UTILS_MOCK_H 17 #define SOFTBUS_JSON_UTILS_MOCK_H 18 19 #include "softbus_json_utils.h" 20 21 #include <gmock/gmock.h> 22 #include <mutex> 23 24 namespace OHOS { 25 class JsonUtilsInterface { 26 public: JsonUtilsInterface()27 JsonUtilsInterface() {}; ~JsonUtilsInterface()28 virtual ~JsonUtilsInterface() {}; 29 30 virtual int32_t GetStringItemByJsonObject( 31 const cJSON *json, const char * const string, char *target, uint32_t targetLen) = 0; 32 virtual bool GetJsonObjectStringItem( 33 const cJSON *json, const char * const string, char *target, uint32_t targetLen) = 0; 34 virtual bool GetJsonObjectNumber16Item(const cJSON *json, const char * const string, uint16_t *target) = 0; 35 virtual bool GetJsonObjectNumberItem(const cJSON *json, const char * const string, int32_t *target) = 0; 36 virtual bool GetJsonObjectSignedNumberItem(const cJSON *json, const char * const string, int32_t *target) = 0; 37 virtual bool GetJsonObjectNumber64Item(const cJSON *json, const char * const string, int64_t *target) = 0; 38 virtual bool GetJsonObjectSignedNumber64Item(const cJSON *json, const char * const string, int64_t *target) = 0; 39 virtual bool GetJsonObjectDoubleItem(const cJSON *json, const char * const string, double *target) = 0; 40 virtual bool GetJsonObjectBoolItem(const cJSON *json, const char * const string, bool *target) = 0; 41 virtual bool AddStringToJsonObject(cJSON *json, const char * const string, const char *value) = 0; 42 virtual bool AddStringArrayToJsonObject( 43 cJSON *json, const char * const string, const char * const *strings, int32_t count) = 0; 44 virtual bool AddNumber16ToJsonObject(cJSON *json, const char * const string, uint16_t num) = 0; 45 virtual bool AddNumberToJsonObject(cJSON *json, const char * const string, int32_t num) = 0; 46 virtual bool AddNumber64ToJsonObject(cJSON *json, const char * const string, int64_t num) = 0; 47 virtual bool AddBoolToJsonObject(cJSON *json, const char * const string, bool value) = 0; 48 virtual bool GetJsonObjectInt32Item(const cJSON *json, const char * const string, int32_t *target) = 0; 49 virtual char *GetDynamicStringItemByJsonObject( 50 const cJSON * const json, const char * const string, uint32_t limit) = 0; 51 virtual bool AddIntArrayToJsonObject(cJSON *json, const char *string, const int32_t *array, int32_t arrayLen) = 0; 52 virtual bool GetJsonObjectIntArrayItem(const cJSON *json, const char *string, int32_t *array, int32_t arrayLen) = 0; 53 }; 54 class JsonUtilsInterfaceMock : public JsonUtilsInterface { 55 public: 56 JsonUtilsInterfaceMock(); 57 ~JsonUtilsInterfaceMock() override; 58 59 MOCK_METHOD4(GetStringItemByJsonObject, 60 int32_t(const cJSON *json, const char * const string, char *target, uint32_t targetLen)); 61 MOCK_METHOD4( 62 GetJsonObjectStringItem, bool(const cJSON *json, const char * const string, char *target, uint32_t targetLen)); 63 MOCK_METHOD3(GetJsonObjectNumber16Item, bool(const cJSON *json, const char * const string, uint16_t *target)); 64 MOCK_METHOD3(GetJsonObjectNumberItem, bool(const cJSON *json, const char * const string, int32_t *target)); 65 MOCK_METHOD3(GetJsonObjectSignedNumberItem, bool(const cJSON *json, const char * const string, int32_t *target)); 66 MOCK_METHOD3(GetJsonObjectNumber64Item, bool(const cJSON *json, const char * const string, int64_t *target)); 67 MOCK_METHOD3(GetJsonObjectSignedNumber64Item, bool(const cJSON *json, const char * const string, int64_t *target)); 68 MOCK_METHOD3(GetJsonObjectDoubleItem, bool(const cJSON *json, const char * const string, double *target)); 69 MOCK_METHOD3(GetJsonObjectBoolItem, bool(const cJSON *json, const char * const string, bool *target)); 70 MOCK_METHOD3(AddStringToJsonObject, bool(cJSON *json, const char * const string, const char *value)); 71 MOCK_METHOD4(AddStringArrayToJsonObject, 72 bool(cJSON *json, const char * const string, const char * const *strings, int32_t count)); 73 MOCK_METHOD3(AddNumber16ToJsonObject, bool(cJSON *json, const char * const string, uint16_t num)); 74 MOCK_METHOD3(AddNumberToJsonObject, bool(cJSON *json, const char * const string, int32_t num)); 75 MOCK_METHOD3(AddNumber64ToJsonObject, bool(cJSON *json, const char * const string, int64_t num)); 76 MOCK_METHOD3(AddBoolToJsonObject, bool(cJSON *json, const char * const string, bool value)); 77 MOCK_METHOD3(GetJsonObjectInt32Item, bool(const cJSON *json, const char * const string, int32_t *target)); 78 MOCK_METHOD3( 79 GetDynamicStringItemByJsonObject, char *(const cJSON * const json, const char * const string, uint32_t limit)); 80 MOCK_METHOD4( 81 AddIntArrayToJsonObject, bool(cJSON *json, const char *string, const int32_t *array, int32_t arrayLen)); 82 MOCK_METHOD4( 83 GetJsonObjectIntArrayItem, bool(const cJSON *json, const char *string, int32_t *array, int32_t arrayLen)); 84 }; 85 } // namespace OHOS 86 #endif // SOFTBUS_JSON_UTILS_MOCK_H