Merge branch 'master' of ssh://gnunet.org/gnunet
[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 #define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
30
31 static int
32 test_document_error ()
33 {
34   struct GNUNET_JSONAPI_Document *obj;
35   struct GNUNET_JSONAPI_Error *error;
36   json_t *doc_json;
37   json_t *data_js;
38   json_error_t err;
39
40   obj = GNUNET_JSONAPI_document_new ();
41   error = GNUNET_JSONAPI_error_new ("1",
42                                     "403",
43                                     "23",
44                                     "Error",
45                                     "Error details",
46                                     NULL,
47                                     NULL,
48                                     NULL);
49
50
51   GNUNET_JSONAPI_document_error_add (obj,
52                                      error);
53
54   GNUNET_assert (GNUNET_OK == 
55                  GNUNET_JSONAPI_document_to_json (obj,
56                                                   &doc_json));
57   data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
58                         JSON_DECODE_ANY,
59                         &err);
60   GNUNET_assert (NULL != data_js);
61   GNUNET_assert (0 != json_equal (data_js, doc_json));
62   GNUNET_JSONAPI_document_delete (obj);
63   json_decref (data_js);
64   json_decref (doc_json);
65   return 0;
66 }
67
68
69 static int
70 test_document ()
71 {
72   struct GNUNET_JSONAPI_Document *obj;
73   struct GNUNET_JSONAPI_Resource *res;
74   json_t *doc_json;
75   json_t *data_js;
76   json_error_t err;
77
78   obj = GNUNET_JSONAPI_document_new ();
79   res = GNUNET_JSONAPI_resource_new ("bar",
80                                      "1");
81
82   GNUNET_assert (GNUNET_OK == 
83                  GNUNET_JSONAPI_resource_add_attr (res,
84                                                    "foo",
85                                                    json_string ("bar")));
86
87   GNUNET_JSONAPI_document_resource_add (obj,
88                                         res);
89
90   GNUNET_assert (GNUNET_OK == 
91                  GNUNET_JSONAPI_document_to_json (obj,
92                                                   &doc_json));
93   data_js = json_loads (TEST_JSONAPI_DOCUMENT,
94                         JSON_DECODE_ANY,
95                         &err);
96   GNUNET_assert (NULL != data_js);
97   GNUNET_assert (0 != json_equal (data_js, doc_json));
98   GNUNET_JSONAPI_document_delete (obj);
99   json_decref (data_js);
100   json_decref (doc_json);
101   return 0;
102 }
103
104 static int
105 test_serialize ()
106 {
107   struct GNUNET_JSONAPI_Document *obj;
108   char* tmp_data;
109   json_t* data_js;
110   json_t* tmp_data_js;
111   json_error_t err;
112   struct GNUNET_JSON_Specification jsonapispec[] = {
113     GNUNET_JSON_spec_jsonapi_document (&obj),
114     GNUNET_JSON_spec_end()
115   };
116   data_js = json_loads (TEST_JSONAPI_DOCUMENT,
117                         JSON_DECODE_ANY,
118                         &err);
119   GNUNET_assert (NULL != data_js);
120   GNUNET_assert (GNUNET_OK ==
121                  GNUNET_JSON_parse (data_js, jsonapispec,
122                                     NULL, NULL));
123   GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
124                                                                  &tmp_data));
125   GNUNET_JSON_parse_free (jsonapispec);
126   tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
127   GNUNET_assert (NULL != tmp_data_js);
128   GNUNET_assert (0 != json_equal (tmp_data_js, data_js));
129   json_decref (data_js);
130   json_decref (tmp_data_js);
131   GNUNET_free (tmp_data);
132   return 0;
133 }
134
135 /**
136  * Test rsa conversions from/to JSON.
137  *
138  * @return 0 on success
139  */
140 static int
141 test_spec_jsonapi ()
142 {
143   struct GNUNET_JSONAPI_Document *obj;
144   struct GNUNET_JSONAPI_Resource *res;
145   const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
146   json_t* data_js;
147   json_error_t err;
148
149   struct GNUNET_JSON_Specification jsonapispec[] = {
150     GNUNET_JSON_spec_jsonapi_document (&obj),
151     GNUNET_JSON_spec_end()
152   };
153   data_js = json_loads (data, JSON_DECODE_ANY, &err);
154   GNUNET_assert (NULL != data_js);
155   GNUNET_assert (GNUNET_OK ==
156                  GNUNET_JSON_parse (data_js, jsonapispec,
157                                     NULL, NULL));
158   json_decref (data_js);
159   res = GNUNET_JSONAPI_document_get_resource (obj, 0);
160   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
161   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
162   GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
163   GNUNET_JSON_parse_free (jsonapispec);
164   return 0;
165 }
166
167
168 int
169 main(int argc,
170      const char *const argv[])
171 {
172   GNUNET_log_setup ("test-jsonapi",
173                     "WARNING",
174                     NULL);
175   if (0 != test_spec_jsonapi ())
176     return 1;
177   if (0 != test_serialize ())
178     return 1;
179   if (0 != test_document ())
180     return 1;
181   if (0 != test_document_error ())
182     return 1;
183   return 0;
184 }
185
186 /* end of test_json.c */