- refactor jsonpi utils, add test
[oweals/gnunet.git] / src / jsonapi / test_jsonapi.c
1 /*
2   This file is part of GNUnet
3   (C) 2015, 2016 GNUnet e.V.
4
5   GNUnet is free software; you can redistribute it and/or modify it under the
6   terms of the GNU General Public License as published by the Free Software
7   Foundation; either version 3, or (at your option) any later version.
8
9   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License along with
14   GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16
17 /**
18  * @file json/test_jsonapi.c
19  * @brief Tests for jsonapi conversion functions
20  * @author Martin Schanzenbach
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_jsonapi_lib.h"
25 #include "gnunet_json_lib.h"
26
27 #define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
28
29 static int
30 test_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
64 static int
65 test_serialize ()
66 {
67   struct GNUNET_JSONAPI_Document *obj;
68   char* tmp_data;
69   json_t* data_js;
70   json_t* tmp_data_js;
71   json_error_t err;
72   struct GNUNET_JSON_Specification jsonapispec[] = {
73     GNUNET_JSON_spec_jsonapi_document (&obj),
74     GNUNET_JSON_spec_end()
75   };
76   data_js = json_loads (TEST_JSONAPI_DOCUMENT,
77                         JSON_DECODE_ANY,
78                         &err);
79   GNUNET_assert (NULL != data_js);
80   GNUNET_assert (GNUNET_OK ==
81                  GNUNET_JSON_parse (data_js, jsonapispec,
82                                     NULL, NULL));
83   GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
84                                                                  &tmp_data));
85   GNUNET_JSON_parse_free (jsonapispec);
86   tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
87   GNUNET_assert (NULL != tmp_data_js);
88   GNUNET_assert (0 != json_equal (tmp_data_js, data_js));
89   json_decref (data_js);
90   json_decref (tmp_data_js);
91   GNUNET_free (tmp_data);
92   return 0;
93 }
94
95 /**
96  * Test rsa conversions from/to JSON.
97  *
98  * @return 0 on success
99  */
100 static int
101 test_spec_jsonapi ()
102 {
103   struct GNUNET_JSONAPI_Document *obj;
104   struct GNUNET_JSONAPI_Resource *res;
105   const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
106   json_t* data_js;
107   json_error_t err;
108
109   struct GNUNET_JSON_Specification jsonapispec[] = {
110     GNUNET_JSON_spec_jsonapi_document (&obj),
111     GNUNET_JSON_spec_end()
112   };
113   data_js = json_loads (data, JSON_DECODE_ANY, &err);
114   GNUNET_assert (NULL != data_js);
115   GNUNET_assert (GNUNET_OK ==
116                  GNUNET_JSON_parse (data_js, jsonapispec,
117                                     NULL, NULL));
118   json_decref (data_js);
119   res = GNUNET_JSONAPI_document_get_resource (obj, 0);
120   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
121   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
122   GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
123   GNUNET_JSON_parse_free (jsonapispec);
124   return 0;
125 }
126
127
128 int
129 main(int argc,
130      const char *const argv[])
131 {
132   GNUNET_log_setup ("test-jsonapi",
133                     "WARNING",
134                     NULL);
135   if (0 != test_spec_jsonapi ())
136     return 1;
137   if (0 != test_serialize ())
138     return 1;
139   if (0 != test_document ())
140     return 1;
141   return 0;
142 }
143
144 /* end of test_json.c */