aboutsummaryrefslogtreecommitdiff
path: root/src/jsonapi/test_jsonapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/jsonapi/test_jsonapi.c')
-rw-r--r--src/jsonapi/test_jsonapi.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/jsonapi/test_jsonapi.c b/src/jsonapi/test_jsonapi.c
index 8b0b13566..b5f4d4cba 100644
--- a/src/jsonapi/test_jsonapi.c
+++ b/src/jsonapi/test_jsonapi.c
@@ -24,11 +24,47 @@
24#include "gnunet_jsonapi_lib.h" 24#include "gnunet_jsonapi_lib.h"
25#include "gnunet_json_lib.h" 25#include "gnunet_json_lib.h"
26 26
27#define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
28
29static int
30test_document ()
31{
32 struct GNUNET_JSONAPI_Document *obj;
33 struct GNUNET_JSONAPI_Resource *res;
34 json_t *doc_json;
35 json_t *data_js;
36 json_error_t err;
37
38 obj = GNUNET_JSONAPI_document_new ();
39 res = GNUNET_JSONAPI_resource_new ("bar",
40 "1");
41
42 GNUNET_assert (GNUNET_OK ==
43 GNUNET_JSONAPI_resource_add_attr (res,
44 "foo",
45 json_string ("bar")));
46
47 GNUNET_JSONAPI_document_resource_add (obj,
48 res);
49
50 GNUNET_assert (GNUNET_OK ==
51 GNUNET_JSONAPI_document_to_json (obj,
52 &doc_json));
53 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
54 JSON_DECODE_ANY,
55 &err);
56 GNUNET_assert (NULL != data_js);
57 GNUNET_assert (0 != json_equal (data_js, doc_json));
58 GNUNET_JSONAPI_document_delete (obj);
59 json_decref (data_js);
60 json_decref (doc_json);
61 return 0;
62}
63
27static int 64static int
28test_serialize () 65test_serialize ()
29{ 66{
30 struct GNUNET_JSONAPI_Document *obj; 67 struct GNUNET_JSONAPI_Document *obj;
31 char* data = "{\"data\":{\"id\":\"1\",\"type\":\"bar\", \"attributes\":{\"foo\":\"bar\"}}}";
32 char* tmp_data; 68 char* tmp_data;
33 json_t* data_js; 69 json_t* data_js;
34 json_t* tmp_data_js; 70 json_t* tmp_data_js;
@@ -37,7 +73,9 @@ test_serialize ()
37 GNUNET_JSON_spec_jsonapi_document (&obj), 73 GNUNET_JSON_spec_jsonapi_document (&obj),
38 GNUNET_JSON_spec_end() 74 GNUNET_JSON_spec_end()
39 }; 75 };
40 data_js = json_loads (data, JSON_DECODE_ANY, &err); 76 data_js = json_loads (TEST_JSONAPI_DOCUMENT,
77 JSON_DECODE_ANY,
78 &err);
41 GNUNET_assert (NULL != data_js); 79 GNUNET_assert (NULL != data_js);
42 GNUNET_assert (GNUNET_OK == 80 GNUNET_assert (GNUNET_OK ==
43 GNUNET_JSON_parse (data_js, jsonapispec, 81 GNUNET_JSON_parse (data_js, jsonapispec,
@@ -98,6 +136,8 @@ main(int argc,
98 return 1; 136 return 1;
99 if (0 != test_serialize ()) 137 if (0 != test_serialize ())
100 return 1; 138 return 1;
139 if (0 != test_document ())
140 return 1;
101 return 0; 141 return 0;
102} 142}
103 143