LONGBOW_TEST_CASE(JSON, parcJSON_AddValue) { PARCJSON *json = parcJSON_Create(); char *expectedName = "value"; PARCJSONValue *value = parcJSONValue_CreateFromCString("Some Pig"); parcJSON_AddValue(json, expectedName, value); const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(parcJSONValue_IsString(actualValue), "Expect value to be type PARCJSONArray"); assertTrue(parcBuffer_Equals(parcJSONValue_GetString(actualValue), parcJSONValue_GetString(value)), "Expected %s actual %s", parcJSONValue_ToString(value), parcJSONValue_ToString(actualValue)); parcJSONValue_Release(&value); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(JSON, parcJSON_GetPairByName) { TestData *data = longBowTestCase_GetClipBoardData(testCase); int expected = 31415; const PARCJSONPair *pair = parcJSON_GetPairByName(data->json, "integer"); PARCBuffer *name = parcJSONPair_GetName(pair); PARCJSONValue *value = parcJSONPair_GetValue(pair); int64_t actual = parcJSONValue_GetInteger(value); PARCBuffer *expectedName = parcBuffer_WrapCString("integer"); assertTrue(parcBuffer_Equals(expectedName, name), "Expected 'integer', actual '%s'", (char *) parcBuffer_ToString(name)); assertTrue(expected == actual, "Expected %d, actual %" PRIi64 "", expected, actual); parcBuffer_Release(&expectedName); }
LONGBOW_TEST_CASE(Object, parcRandomAccessFile_ToJSON) { PARCFile *file = parcFile_Create("/tmp/tmpfile"); PARCRandomAccessFile *instance = parcRandomAccessFile_Open(file); parcFile_Release(&file); PARCJSON *json = parcRandomAccessFile_ToJSON(instance); PARCJSONPair *pair = parcJSON_GetPairByName(json, "fname"); PARCJSONValue *value = parcJSONPair_GetValue(pair); PARCBuffer *buffer = parcJSONValue_GetString(value); char *string = parcBuffer_ToString(buffer); assertTrue(strcmp("/tmp/tmpfile", string) == 0, "The file was stored correctly"); parcMemory_Deallocate(&string); parcJSON_Release(&json); parcRandomAccessFile_Release(&instance); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddBoolean) { PARCJSON *json = parcJSON_Create(); char *expectedName = "boolean"; bool expectedValue = true; parcJSON_AddBoolean(json, expectedName, expectedValue); const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(expectedValue == parcJSONValue_GetBoolean(actualValue), "Expected %d actual %d", expectedValue, parcJSONValue_GetBoolean(actualValue)); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddInteger) { PARCJSON *json = parcJSON_Create(); char *expectedName = "integer"; uint64_t expectedValue = 12345; parcJSON_AddInteger(json, expectedName, expectedValue); const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(expectedValue == parcJSONValue_GetInteger(actualValue), "Expected %" PRIi64 "d actual %" PRIi64 "d", expectedValue, parcJSONValue_GetInteger(actualValue)); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddObject) { PARCJSON *json = parcJSON_Create(); PARCJSON *expectedValue = parcJSON_ParseString("{ \"string\" : \"xyzzy\" }"); parcJSON_AddObject(json, "object", expectedValue); char *expectedName = "object"; const PARCJSONPair *pair = parcJSON_GetPairByName(json, expectedName); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, (char *) parcBuffer_ToString(actualName)); assertTrue(parcJSON_Equals(expectedValue, parcJSONValue_GetJSON(actualValue)), "Expected value did not match the actual value."); parcJSON_Release(&expectedValue); parcJSON_Release(&json); }
LONGBOW_TEST_CASE(JSON, parcJSON_AddString) { PARCJSON *json = parcJSON_Create(); char *expectedName = "string"; char *expectedValue = "value"; parcJSON_AddString(json, expectedName, expectedValue); const PARCJSONPair *pair = parcJSON_GetPairByName(json, "string"); PARCBuffer *actualName = parcJSONPair_GetName(pair); PARCJSONValue *actualValue = parcJSONPair_GetValue(pair); assertTrue(strcmp(expectedName, parcBuffer_Overlay(actualName, 0)) == 0, "Expected name %s, actual %s", expectedName, parcBuffer_ToString(actualName)); assertTrue(strcmp(expectedValue, parcBuffer_Overlay(parcJSONValue_GetString(actualValue), 0)) == 0, "Expected value %s, actual %s", expectedValue, parcJSONValue_ToString(actualValue)); parcJSON_Release(&json); }
CCNxManifestHashGroup * ccnxManifestHashGroup_CreateFromJson(const PARCJSON *json) { CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create(); PARCJSONValue *ptrListValue = parcJSON_GetValueByName(json, "HashGroup"); PARCJSONArray *ptrList = parcJSONValue_GetArray(ptrListValue); size_t numberOfPointers = parcJSONArray_GetLength(ptrList); for (size_t i = 0; i < numberOfPointers; i++) { PARCJSONValue *pointerValue = parcJSONArray_GetValue(ptrList, i); PARCJSON *typeJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *typeValue = parcJSON_GetValueByName(typeJson, "type"); CCNxManifestHashGroupPointerType type; if (parcJSONValue_GetInteger(typeValue) == 0) { type = CCNxManifestHashGroupPointerType_Data; } else { type = CCNxManifestHashGroupPointerType_Manifest; } PARCJSON *digestJson = parcJSONValue_GetJSON(pointerValue); PARCJSONValue *digestValue = parcJSON_GetValueByName(digestJson, "digest"); PARCBuffer *digestHex = parcJSONValue_GetString(digestValue); char *hexString = parcBuffer_ToString(digestHex); PARCBuffer *digest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); ccnxManifestHashGroup_AppendPointer(group, type, digest); parcBuffer_Release(&digest); } if (parcJSON_GetPairByName(json, "overallDataDigest") != NULL) { PARCJSONValue *overallDataDigestValue = parcJSON_GetValueByName(json, "overallDataDigest"); PARCBuffer *digestHex = parcJSONValue_GetString(overallDataDigestValue); char *hexString = parcBuffer_ToString(digestHex); group->overallDataDigest = parcBuffer_Flip(parcBuffer_ParseHexString(hexString)); parcMemory_Deallocate(&hexString); } if (parcJSON_GetPairByName(json, "locator") != NULL) { PARCJSONValue *locatorValue = parcJSON_GetValueByName(json, "locator"); PARCBuffer *buffer = parcJSONValue_GetString(locatorValue); char *locator = parcBuffer_ToString(buffer); group->locator = ccnxName_CreateFromCString(locator); parcMemory_Deallocate(&locator); } if (parcJSON_GetPairByName(json, "entrySize") != NULL) { PARCJSONValue *childBlockNodeSizeValue = parcJSON_GetValueByName(json, "entrySize"); group->entrySize = parcJSONValue_GetInteger(childBlockNodeSizeValue); } if (parcJSON_GetPairByName(json, "dataSize") != NULL) { PARCJSONValue *totalSizeValue = parcJSON_GetValueByName(json, "dataSize"); group->dataSize = parcJSONValue_GetInteger(totalSizeValue); } if (parcJSON_GetPairByName(json, "blockSize") != NULL) { PARCJSONValue *blockSizeValue = parcJSON_GetValueByName(json, "blockSize"); group->blockSize = parcJSONValue_GetInteger(blockSizeValue); } if (parcJSON_GetPairByName(json, "treeHeight") != NULL) { PARCJSONValue *treeHeightValue = parcJSON_GetValueByName(json, "treeHeight"); group->treeHeight = parcJSONValue_GetInteger(treeHeightValue); } return group; }