-rps: open channel when inserting peer in view
[oweals/gnunet.git] / src / jsonapi / test_jsonapi.c
index e9c85eaf6f8c6d564bfedf386774130ed7ca9128..379dab9af52ebbebf714b938ddb61ef005c575ab 100644 (file)
 #include "gnunet_jsonapi_lib.h"
 #include "gnunet_json_lib.h"
 
+#define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}"
+
+#define TEST_JSONAPI_DOCUMENT_ERR "{\"errors\":[{\"id\":\"1\",\"status\":\"403\",\"code\":\"23\", \"title\":\"Error\", \"detail\":\"Error details\"}]}"
+
+static int
+test_document_error ()
+{
+  struct GNUNET_JSONAPI_Document *obj;
+  struct GNUNET_JSONAPI_Error *error;
+  json_t *doc_json;
+  json_t *data_js;
+  json_error_t err;
+
+  obj = GNUNET_JSONAPI_document_new ();
+  error = GNUNET_JSONAPI_error_new ("1",
+                                    "403",
+                                    "23",
+                                    "Error",
+                                    "Error details",
+                                    NULL,
+                                    NULL,
+                                    NULL);
+
+
+  GNUNET_JSONAPI_document_error_add (obj,
+                                     error);
+
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_JSONAPI_document_to_json (obj,
+                                                  &doc_json));
+  data_js = json_loads (TEST_JSONAPI_DOCUMENT_ERR,
+                        JSON_DECODE_ANY,
+                        &err);
+  GNUNET_assert (NULL != data_js);
+  GNUNET_assert (0 != json_equal (data_js, doc_json));
+  GNUNET_JSONAPI_document_delete (obj);
+  json_decref (data_js);
+  json_decref (doc_json);
+  return 0;
+}
+
+
+static int
+test_document ()
+{
+  struct GNUNET_JSONAPI_Document *obj;
+  struct GNUNET_JSONAPI_Resource *res;
+  json_t *doc_json;
+  json_t *data_js;
+  json_error_t err;
+
+  obj = GNUNET_JSONAPI_document_new ();
+  res = GNUNET_JSONAPI_resource_new ("bar",
+                                     "1");
+
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_JSONAPI_resource_add_attr (res,
+                                                   "foo",
+                                                   json_string ("bar")));
+
+  GNUNET_JSONAPI_document_resource_add (obj,
+                                        res);
+
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_JSONAPI_document_to_json (obj,
+                                                  &doc_json));
+  data_js = json_loads (TEST_JSONAPI_DOCUMENT,
+                        JSON_DECODE_ANY,
+                        &err);
+  GNUNET_assert (NULL != data_js);
+  GNUNET_assert (0 != json_equal (data_js, doc_json));
+  GNUNET_JSONAPI_document_delete (obj);
+  json_decref (data_js);
+  json_decref (doc_json);
+  return 0;
+}
+
 static int
 test_serialize ()
 {
-  struct GNUNET_JSONAPI_Object *obj;
-  char* data = "{\"data\":[{\"id\":\"1\", \"type\":\"test\"}]}";
+  struct GNUNET_JSONAPI_Document *obj;
   char* tmp_data;
   json_t* data_js;
   json_t* tmp_data_js;
   json_error_t err;
   struct GNUNET_JSON_Specification jsonapispec[] = {
-    GNUNET_JSON_spec_jsonapi (&obj),
+    GNUNET_JSON_spec_jsonapi_document (&obj),
     GNUNET_JSON_spec_end()
   };
-  data_js = json_loads (data, JSON_DECODE_ANY, &err);
+  data_js = json_loads (TEST_JSONAPI_DOCUMENT,
+                        JSON_DECODE_ANY,
+                        &err);
   GNUNET_assert (NULL != data_js);
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_JSON_parse (data_js, jsonapispec,
                                     NULL, NULL));
-  GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_data_serialize (obj,
-                                                             &tmp_data));
+  GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_serialize (obj,
+                                                                 &tmp_data));
   GNUNET_JSON_parse_free (jsonapispec);
   tmp_data_js = json_loads (tmp_data, JSON_DECODE_ANY, &err);
   GNUNET_assert (NULL != tmp_data_js);
@@ -62,14 +140,14 @@ test_serialize ()
 static int
 test_spec_jsonapi ()
 {
-  struct GNUNET_JSONAPI_Object *obj;
+  struct GNUNET_JSONAPI_Document *obj;
   struct GNUNET_JSONAPI_Resource *res;
   const char* data = "{\"data\":{\"id\":\"1\", \"type\":\"test\"}}";
   json_t* data_js;
   json_error_t err;
 
   struct GNUNET_JSON_Specification jsonapispec[] = {
-    GNUNET_JSON_spec_jsonapi (&obj),
+    GNUNET_JSON_spec_jsonapi_document (&obj),
     GNUNET_JSON_spec_end()
   };
   data_js = json_loads (data, JSON_DECODE_ANY, &err);
@@ -78,10 +156,10 @@ test_spec_jsonapi ()
                  GNUNET_JSON_parse (data_js, jsonapispec,
                                     NULL, NULL));
   json_decref (data_js);
-  res = GNUNET_JSONAPI_object_get_resource (obj, 0);
+  res = GNUNET_JSONAPI_document_get_resource (obj, 0);
   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_id (res, "1"));
   GNUNET_assert (GNUNET_YES == GNUNET_JSONAPI_resource_check_type (res, "test"));
-  GNUNET_assert (1 == GNUNET_JSONAPI_object_resource_count (obj));
+  GNUNET_assert (1 == GNUNET_JSONAPI_document_resource_count (obj));
   GNUNET_JSON_parse_free (jsonapispec);
   return 0;
 }
@@ -98,6 +176,10 @@ main(int argc,
     return 1;
   if (0 != test_serialize ())
     return 1;
+  if (0 != test_document ())
+    return 1;
+  if (0 != test_document_error ())
+    return 1;
   return 0;
 }